Merge remote-tracking branch 'wireless-next/master' into iwlwifi-next
[firefly-linux-kernel-4.4.55.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
84 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
85         {
86                 .max = 1,
87                 .types = BIT(NL80211_IFTYPE_STATION),
88         },
89         {
90                 .max = 1,
91                 .types = BIT(NL80211_IFTYPE_AP) |
92                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
93                         BIT(NL80211_IFTYPE_P2P_GO),
94         },
95         {
96                 .max = 1,
97                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
98         },
99 };
100
101 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
102         {
103                 .num_different_channels = 1,
104                 .max_interfaces = 3,
105                 .limits = iwl_mvm_limits,
106                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
107         },
108 };
109
110 #ifdef CONFIG_PM_SLEEP
111 static const struct nl80211_wowlan_tcp_data_token_feature
112 iwl_mvm_wowlan_tcp_token_feature = {
113         .min_len = 0,
114         .max_len = 255,
115         .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
116 };
117
118 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
119         .tok = &iwl_mvm_wowlan_tcp_token_feature,
120         .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
121                             sizeof(struct ethhdr) -
122                             sizeof(struct iphdr) -
123                             sizeof(struct tcphdr),
124         .data_interval_max = 65535, /* __le16 in API */
125         .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
126                             sizeof(struct ethhdr) -
127                             sizeof(struct iphdr) -
128                             sizeof(struct tcphdr),
129         .seq = true,
130 };
131 #endif
132
133 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
134 /*
135  * Use the reserved field to indicate magic values.
136  * these values will only be used internally by the driver,
137  * and won't make it to the fw (reserved will be 0).
138  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
139  *      be the vif's ip address. in case there is not a single
140  *      ip address (0, or more than 1), this attribute will
141  *      be skipped.
142  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
143  *      the LSB bytes of the vif's mac address
144  */
145 enum {
146         BC_FILTER_MAGIC_NONE = 0,
147         BC_FILTER_MAGIC_IP,
148         BC_FILTER_MAGIC_MAC,
149 };
150
151 static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
152         {
153                 /* arp */
154                 .discard = 0,
155                 .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
156                 .attrs = {
157                         {
158                                 /* frame type - arp, hw type - ethernet */
159                                 .offset_type =
160                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
161                                 .offset = sizeof(rfc1042_header),
162                                 .val = cpu_to_be32(0x08060001),
163                                 .mask = cpu_to_be32(0xffffffff),
164                         },
165                         {
166                                 /* arp dest ip */
167                                 .offset_type =
168                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
169                                 .offset = sizeof(rfc1042_header) + 2 +
170                                           sizeof(struct arphdr) +
171                                           ETH_ALEN + sizeof(__be32) +
172                                           ETH_ALEN,
173                                 .mask = cpu_to_be32(0xffffffff),
174                                 /* mark it as special field */
175                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
176                         },
177                 },
178         },
179         {
180                 /* dhcp offer bcast */
181                 .discard = 0,
182                 .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
183                 .attrs = {
184                         {
185                                 /* udp dest port - 68 (bootp client)*/
186                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
187                                 .offset = offsetof(struct udphdr, dest),
188                                 .val = cpu_to_be32(0x00440000),
189                                 .mask = cpu_to_be32(0xffff0000),
190                         },
191                         {
192                                 /* dhcp - lsb bytes of client hw address */
193                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
194                                 .offset = 38,
195                                 .mask = cpu_to_be32(0xffffffff),
196                                 /* mark it as special field */
197                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
198                         },
199                 },
200         },
201         /* last filter must be empty */
202         {},
203 };
204 #endif
205
206 void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
207 {
208         if (!iwl_mvm_is_d0i3_supported(mvm))
209                 return;
210
211         IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
212         WARN_ON(test_and_set_bit(ref_type, mvm->ref_bitmap));
213         iwl_trans_ref(mvm->trans);
214 }
215
216 void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
217 {
218         if (!iwl_mvm_is_d0i3_supported(mvm))
219                 return;
220
221         IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
222         WARN_ON(!test_and_clear_bit(ref_type, mvm->ref_bitmap));
223         iwl_trans_unref(mvm->trans);
224 }
225
226 static void
227 iwl_mvm_unref_all_except(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref)
228 {
229         int i;
230
231         if (!iwl_mvm_is_d0i3_supported(mvm))
232                 return;
233
234         for_each_set_bit(i, mvm->ref_bitmap, IWL_MVM_REF_COUNT) {
235                 if (ref == i)
236                         continue;
237
238                 IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d\n", i);
239                 clear_bit(i, mvm->ref_bitmap);
240                 iwl_trans_unref(mvm->trans);
241         }
242 }
243
244 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
245 {
246         int i;
247
248         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
249         for (i = 0; i < NUM_PHY_CTX; i++) {
250                 mvm->phy_ctxts[i].id = i;
251                 mvm->phy_ctxts[i].ref = 0;
252         }
253 }
254
255 static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
256 {
257         /* we create the 802.11 header and SSID element */
258         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
259                 return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
260         return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
261 }
262
263 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
264 {
265         struct ieee80211_hw *hw = mvm->hw;
266         int num_mac, ret, i;
267
268         /* Tell mac80211 our characteristics */
269         hw->flags = IEEE80211_HW_SIGNAL_DBM |
270                     IEEE80211_HW_SPECTRUM_MGMT |
271                     IEEE80211_HW_REPORTS_TX_ACK_STATUS |
272                     IEEE80211_HW_QUEUE_CONTROL |
273                     IEEE80211_HW_WANT_MONITOR_VIF |
274                     IEEE80211_HW_SUPPORTS_PS |
275                     IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
276                     IEEE80211_HW_AMPDU_AGGREGATION |
277                     IEEE80211_HW_TIMING_BEACON_ONLY |
278                     IEEE80211_HW_CONNECTION_MONITOR |
279                     IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
280                     IEEE80211_HW_SUPPORTS_STATIC_SMPS;
281
282         hw->queues = mvm->first_agg_queue;
283         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
284         hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
285                                     IEEE80211_RADIOTAP_MCS_HAVE_STBC;
286         hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
287         hw->rate_control_algorithm = "iwl-mvm-rs";
288
289         /*
290          * Enable 11w if advertised by firmware and software crypto
291          * is not enabled (as the firmware will interpret some mgmt
292          * packets, so enabling it with software crypto isn't safe)
293          */
294         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
295             !iwlwifi_mod_params.sw_crypto)
296                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
297
298         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT &&
299             IWL_UCODE_API(mvm->fw->ucode_ver) >= 9 &&
300             !iwlwifi_mod_params.uapsd_disable) {
301                 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
302                 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
303                 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
304         }
305
306         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT &&
307             !iwlwifi_mod_params.uapsd_disable) {
308                 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
309                 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
310                 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
311         }
312
313         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
314         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
315         hw->chanctx_data_size = sizeof(u16);
316
317         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
318                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
319                 BIT(NL80211_IFTYPE_AP) |
320                 BIT(NL80211_IFTYPE_P2P_GO) |
321                 BIT(NL80211_IFTYPE_P2P_DEVICE) |
322                 BIT(NL80211_IFTYPE_ADHOC);
323
324         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
325         hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
326                                        REGULATORY_DISABLE_BEACON_HINTS;
327
328         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
329                 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
330
331         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_CSA_FLOW)
332                 hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
333
334         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
335         hw->wiphy->n_iface_combinations =
336                 ARRAY_SIZE(iwl_mvm_iface_combinations);
337
338         hw->wiphy->max_remain_on_channel_duration = 10000;
339         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
340
341         /* Extract MAC address */
342         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
343         hw->wiphy->addresses = mvm->addresses;
344         hw->wiphy->n_addresses = 1;
345
346         /* Extract additional MAC addresses if available */
347         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
348                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
349
350         for (i = 1; i < num_mac; i++) {
351                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
352                        ETH_ALEN);
353                 mvm->addresses[i].addr[5]++;
354                 hw->wiphy->n_addresses++;
355         }
356
357         iwl_mvm_reset_phy_ctxts(mvm);
358
359         hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
360
361         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
362
363         if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
364                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
365                         &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
366         if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
367                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
368                         &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
369
370         hw->wiphy->hw_version = mvm->trans->hw_id;
371
372         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
373                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
374         else
375                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
376
377         hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
378         hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
379         hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
380         /* we create the 802.11 header and zero length SSID IE. */
381         hw->wiphy->max_sched_scan_ie_len = SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
382
383         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
384                                NL80211_FEATURE_LOW_PRIORITY_SCAN |
385                                NL80211_FEATURE_P2P_GO_OPPPS;
386
387         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
388
389         /* currently FW API supports only one optional cipher scheme */
390         if (mvm->fw->cs[0].cipher) {
391                 mvm->hw->n_cipher_schemes = 1;
392                 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
393         }
394
395 #ifdef CONFIG_PM_SLEEP
396         if (iwl_mvm_is_d0i3_supported(mvm) &&
397             device_can_wakeup(mvm->trans->dev)) {
398                 mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
399                 hw->wiphy->wowlan = &mvm->wowlan;
400         } else if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
401             mvm->trans->ops->d3_suspend &&
402             mvm->trans->ops->d3_resume &&
403             device_can_wakeup(mvm->trans->dev)) {
404                 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
405                                     WIPHY_WOWLAN_DISCONNECT |
406                                     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
407                                     WIPHY_WOWLAN_RFKILL_RELEASE;
408                 if (!iwlwifi_mod_params.sw_crypto)
409                         mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
410                                              WIPHY_WOWLAN_GTK_REKEY_FAILURE |
411                                              WIPHY_WOWLAN_4WAY_HANDSHAKE;
412
413                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
414                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
415                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
416                 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
417                 hw->wiphy->wowlan = &mvm->wowlan;
418         }
419 #endif
420
421 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
422         /* assign default bcast filtering configuration */
423         mvm->bcast_filters = iwl_mvm_default_bcast_filters;
424 #endif
425
426         ret = iwl_mvm_leds_init(mvm);
427         if (ret)
428                 return ret;
429
430         ret = ieee80211_register_hw(mvm->hw);
431         if (ret)
432                 iwl_mvm_leds_exit(mvm);
433
434         return ret;
435 }
436
437 static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
438                              struct ieee80211_sta *sta,
439                              struct sk_buff *skb)
440 {
441         struct iwl_mvm_sta *mvmsta;
442         bool defer = false;
443
444         /*
445          * double check the IN_D0I3 flag both before and after
446          * taking the spinlock, in order to prevent taking
447          * the spinlock when not needed.
448          */
449         if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
450                 return false;
451
452         spin_lock(&mvm->d0i3_tx_lock);
453         /*
454          * testing the flag again ensures the skb dequeue
455          * loop (on d0i3 exit) hasn't run yet.
456          */
457         if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
458                 goto out;
459
460         mvmsta = iwl_mvm_sta_from_mac80211(sta);
461         if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
462             mvmsta->sta_id != mvm->d0i3_ap_sta_id)
463                 goto out;
464
465         __skb_queue_tail(&mvm->d0i3_tx, skb);
466         ieee80211_stop_queues(mvm->hw);
467
468         /* trigger wakeup */
469         iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
470         iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
471
472         defer = true;
473 out:
474         spin_unlock(&mvm->d0i3_tx_lock);
475         return defer;
476 }
477
478 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
479                            struct ieee80211_tx_control *control,
480                            struct sk_buff *skb)
481 {
482         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
483         struct ieee80211_sta *sta = control->sta;
484         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
485         struct ieee80211_hdr *hdr = (void *)skb->data;
486
487         if (iwl_mvm_is_radio_killed(mvm)) {
488                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
489                 goto drop;
490         }
491
492         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
493             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
494                 goto drop;
495
496         /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
497         if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
498                      ieee80211_is_mgmt(hdr->frame_control) &&
499                      !ieee80211_is_deauth(hdr->frame_control) &&
500                      !ieee80211_is_disassoc(hdr->frame_control) &&
501                      !ieee80211_is_action(hdr->frame_control)))
502                 sta = NULL;
503
504         if (sta) {
505                 if (iwl_mvm_defer_tx(mvm, sta, skb))
506                         return;
507                 if (iwl_mvm_tx_skb(mvm, skb, sta))
508                         goto drop;
509                 return;
510         }
511
512         if (iwl_mvm_tx_skb_non_sta(mvm, skb))
513                 goto drop;
514         return;
515  drop:
516         ieee80211_free_txskb(hw, skb);
517 }
518
519 static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
520 {
521         if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
522                 return false;
523         return true;
524 }
525
526 static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
527 {
528         if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
529                 return false;
530         if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
531                 return true;
532
533         /* enabled by default */
534         return true;
535 }
536
537 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
538                                     struct ieee80211_vif *vif,
539                                     enum ieee80211_ampdu_mlme_action action,
540                                     struct ieee80211_sta *sta, u16 tid,
541                                     u16 *ssn, u8 buf_size)
542 {
543         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
544         int ret;
545         bool tx_agg_ref = false;
546
547         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
548                      sta->addr, tid, action);
549
550         if (!(mvm->nvm_data->sku_cap_11n_enable))
551                 return -EACCES;
552
553         /* return from D0i3 before starting a new Tx aggregation */
554         switch (action) {
555         case IEEE80211_AMPDU_TX_START:
556         case IEEE80211_AMPDU_TX_STOP_CONT:
557         case IEEE80211_AMPDU_TX_STOP_FLUSH:
558         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
559         case IEEE80211_AMPDU_TX_OPERATIONAL:
560                 iwl_mvm_ref(mvm, IWL_MVM_REF_TX_AGG);
561                 tx_agg_ref = true;
562
563                 /*
564                  * for tx start, wait synchronously until D0i3 exit to
565                  * get the correct sequence number for the tid.
566                  * additionally, some other ampdu actions use direct
567                  * target access, which is not handled automatically
568                  * by the trans layer (unlike commands), so wait for
569                  * d0i3 exit in these cases as well.
570                  */
571                 if (!wait_event_timeout(mvm->d0i3_exit_waitq,
572                           !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status), HZ)) {
573                         WARN_ON_ONCE(1);
574                         iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
575                         return -EIO;
576                 }
577                 break;
578         default:
579                 break;
580         }
581
582         mutex_lock(&mvm->mutex);
583
584         switch (action) {
585         case IEEE80211_AMPDU_RX_START:
586                 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
587                         ret = -EINVAL;
588                         break;
589                 }
590                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
591                 break;
592         case IEEE80211_AMPDU_RX_STOP:
593                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
594                 break;
595         case IEEE80211_AMPDU_TX_START:
596                 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
597                         ret = -EINVAL;
598                         break;
599                 }
600                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
601                 break;
602         case IEEE80211_AMPDU_TX_STOP_CONT:
603                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
604                 break;
605         case IEEE80211_AMPDU_TX_STOP_FLUSH:
606         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
607                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
608                 break;
609         case IEEE80211_AMPDU_TX_OPERATIONAL:
610                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
611                 break;
612         default:
613                 WARN_ON_ONCE(1);
614                 ret = -EINVAL;
615                 break;
616         }
617         mutex_unlock(&mvm->mutex);
618
619         /*
620          * If the tid is marked as started, we won't use it for offloaded
621          * traffic on the next D0i3 entry. It's safe to unref.
622          */
623         if (tx_agg_ref)
624                 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
625
626         return ret;
627 }
628
629 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
630                                      struct ieee80211_vif *vif)
631 {
632         struct iwl_mvm *mvm = data;
633         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
634
635         mvmvif->uploaded = false;
636         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
637
638         /* does this make sense at all? */
639         mvmvif->color++;
640
641         spin_lock_bh(&mvm->time_event_lock);
642         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
643         spin_unlock_bh(&mvm->time_event_lock);
644
645         mvmvif->phy_ctxt = NULL;
646 }
647
648 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
649 {
650 #ifdef CONFIG_IWLWIFI_DEBUGFS
651         static char *env[] = { "DRIVER=iwlwifi", "EVENT=error_dump", NULL };
652
653         iwl_mvm_fw_error_dump(mvm);
654
655         /* notify the userspace about the error we had */
656         kobject_uevent_env(&mvm->hw->wiphy->dev.kobj, KOBJ_CHANGE, env);
657 #endif
658
659         iwl_trans_stop_device(mvm->trans);
660
661         mvm->scan_status = IWL_MVM_SCAN_NONE;
662
663         /* just in case one was running */
664         ieee80211_remain_on_channel_expired(mvm->hw);
665
666         ieee80211_iterate_active_interfaces_atomic(
667                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
668                 iwl_mvm_cleanup_iterator, mvm);
669
670         mvm->p2p_device_vif = NULL;
671         mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
672
673         iwl_mvm_reset_phy_ctxts(mvm);
674         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
675         memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
676
677         ieee80211_wake_queues(mvm->hw);
678
679         /* cleanup all stale references (scan, roc), but keep the
680          * ucode_down ref until reconfig is complete */
681         iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
682
683         mvm->vif_count = 0;
684         mvm->rx_ba_sessions = 0;
685 }
686
687 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
688 {
689         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
690         int ret;
691
692         mutex_lock(&mvm->mutex);
693
694         /* Clean up some internal and mac80211 state on restart */
695         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
696                 iwl_mvm_restart_cleanup(mvm);
697
698         ret = iwl_mvm_up(mvm);
699
700         if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
701                 /* Something went wrong - we need to finish some cleanup
702                  * that normally iwl_mvm_mac_restart_complete() below
703                  * would do.
704                  */
705                 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
706                 iwl_mvm_d0i3_enable_tx(mvm, NULL);
707         }
708
709         mutex_unlock(&mvm->mutex);
710
711         return ret;
712 }
713
714 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
715 {
716         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
717         int ret;
718
719         mutex_lock(&mvm->mutex);
720
721         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
722         iwl_mvm_d0i3_enable_tx(mvm, NULL);
723         ret = iwl_mvm_update_quotas(mvm, NULL);
724         if (ret)
725                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
726                         ret);
727
728         /* allow transport/FW low power modes */
729         iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
730
731         mutex_unlock(&mvm->mutex);
732 }
733
734 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
735 {
736         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
737
738         flush_work(&mvm->d0i3_exit_work);
739         flush_work(&mvm->async_handlers_wk);
740
741         mutex_lock(&mvm->mutex);
742
743         /* disallow low power states when the FW is down */
744         iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
745
746         /* async_handlers_wk is now blocked */
747
748         /*
749          * The work item could be running or queued if the
750          * ROC time event stops just as we get here.
751          */
752         cancel_work_sync(&mvm->roc_done_wk);
753
754         iwl_trans_stop_device(mvm->trans);
755
756         iwl_mvm_async_handlers_purge(mvm);
757         /* async_handlers_list is empty and will stay empty: HW is stopped */
758
759         /* the fw is stopped, the aux sta is dead: clean up driver state */
760         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
761
762         mutex_unlock(&mvm->mutex);
763
764         /*
765          * The worker might have been waiting for the mutex, let it run and
766          * discover that its list is now empty.
767          */
768         cancel_work_sync(&mvm->async_handlers_wk);
769 }
770
771 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
772 {
773         u16 i;
774
775         lockdep_assert_held(&mvm->mutex);
776
777         for (i = 0; i < NUM_PHY_CTX; i++)
778                 if (!mvm->phy_ctxts[i].ref)
779                         return &mvm->phy_ctxts[i];
780
781         IWL_ERR(mvm, "No available PHY context\n");
782         return NULL;
783 }
784
785 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
786                                 s8 tx_power)
787 {
788         /* FW is in charge of regulatory enforcement */
789         struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
790                 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
791                 .pwr_restriction = cpu_to_le16(tx_power),
792         };
793
794         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0,
795                                     sizeof(reduce_txpwr_cmd),
796                                     &reduce_txpwr_cmd);
797 }
798
799 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
800                                      struct ieee80211_vif *vif)
801 {
802         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
803         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
804         int ret;
805
806         /*
807          * Not much to do here. The stack will not allow interface
808          * types or combinations that we didn't advertise, so we
809          * don't really have to check the types.
810          */
811
812         mutex_lock(&mvm->mutex);
813
814         /* Allocate resources for the MAC context, and add it to the fw  */
815         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
816         if (ret)
817                 goto out_unlock;
818
819         /* Counting number of interfaces is needed for legacy PM */
820         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
821                 mvm->vif_count++;
822
823         /*
824          * The AP binding flow can be done only after the beacon
825          * template is configured (which happens only in the mac80211
826          * start_ap() flow), and adding the broadcast station can happen
827          * only after the binding.
828          * In addition, since modifying the MAC before adding a bcast
829          * station is not allowed by the FW, delay the adding of MAC context to
830          * the point where we can also add the bcast station.
831          * In short: there's not much we can do at this point, other than
832          * allocating resources :)
833          */
834         if (vif->type == NL80211_IFTYPE_AP ||
835             vif->type == NL80211_IFTYPE_ADHOC) {
836                 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
837                 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
838                                                qmask,
839                                                ieee80211_vif_type_p2p(vif));
840                 if (ret) {
841                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
842                         goto out_release;
843                 }
844
845                 iwl_mvm_vif_dbgfs_register(mvm, vif);
846                 goto out_unlock;
847         }
848
849         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
850         if (ret)
851                 goto out_release;
852
853         ret = iwl_mvm_power_update_mac(mvm);
854         if (ret)
855                 goto out_release;
856
857         /* beacon filtering */
858         ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
859         if (ret)
860                 goto out_remove_mac;
861
862         if (!mvm->bf_allowed_vif &&
863             vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
864                 mvm->bf_allowed_vif = mvmvif;
865                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
866                                      IEEE80211_VIF_SUPPORTS_CQM_RSSI;
867         }
868
869         /*
870          * P2P_DEVICE interface does not have a channel context assigned to it,
871          * so a dedicated PHY context is allocated to it and the corresponding
872          * MAC context is bound to it at this stage.
873          */
874         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
875
876                 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
877                 if (!mvmvif->phy_ctxt) {
878                         ret = -ENOSPC;
879                         goto out_free_bf;
880                 }
881
882                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
883                 ret = iwl_mvm_binding_add_vif(mvm, vif);
884                 if (ret)
885                         goto out_unref_phy;
886
887                 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
888                 if (ret)
889                         goto out_unbind;
890
891                 /* Save a pointer to p2p device vif, so it can later be used to
892                  * update the p2p device MAC when a GO is started/stopped */
893                 mvm->p2p_device_vif = vif;
894         }
895
896         iwl_mvm_vif_dbgfs_register(mvm, vif);
897         goto out_unlock;
898
899  out_unbind:
900         iwl_mvm_binding_remove_vif(mvm, vif);
901  out_unref_phy:
902         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
903  out_free_bf:
904         if (mvm->bf_allowed_vif == mvmvif) {
905                 mvm->bf_allowed_vif = NULL;
906                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
907                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
908         }
909  out_remove_mac:
910         mvmvif->phy_ctxt = NULL;
911         iwl_mvm_mac_ctxt_remove(mvm, vif);
912  out_release:
913         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
914                 mvm->vif_count--;
915
916         iwl_mvm_mac_ctxt_release(mvm, vif);
917  out_unlock:
918         mutex_unlock(&mvm->mutex);
919
920         return ret;
921 }
922
923 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
924                                         struct ieee80211_vif *vif)
925 {
926         u32 tfd_msk = 0, ac;
927
928         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
929                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
930                         tfd_msk |= BIT(vif->hw_queue[ac]);
931
932         if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
933                 tfd_msk |= BIT(vif->cab_queue);
934
935         if (tfd_msk) {
936                 mutex_lock(&mvm->mutex);
937                 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
938                 mutex_unlock(&mvm->mutex);
939         }
940
941         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
942                 /*
943                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
944                  * We assume here that all the packets sent to the OFFCHANNEL
945                  * queue are sent in ROC session.
946                  */
947                 flush_work(&mvm->roc_done_wk);
948         } else {
949                 /*
950                  * By now, all the AC queues are empty. The AGG queues are
951                  * empty too. We already got all the Tx responses for all the
952                  * packets in the queues. The drain work can have been
953                  * triggered. Flush it.
954                  */
955                 flush_work(&mvm->sta_drained_wk);
956         }
957 }
958
959 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
960                                          struct ieee80211_vif *vif)
961 {
962         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
963         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
964
965         iwl_mvm_prepare_mac_removal(mvm, vif);
966
967         mutex_lock(&mvm->mutex);
968
969         if (mvm->bf_allowed_vif == mvmvif) {
970                 mvm->bf_allowed_vif = NULL;
971                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
972                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
973         }
974
975         iwl_mvm_vif_dbgfs_clean(mvm, vif);
976
977         /*
978          * For AP/GO interface, the tear down of the resources allocated to the
979          * interface is be handled as part of the stop_ap flow.
980          */
981         if (vif->type == NL80211_IFTYPE_AP ||
982             vif->type == NL80211_IFTYPE_ADHOC) {
983 #ifdef CONFIG_NL80211_TESTMODE
984                 if (vif == mvm->noa_vif) {
985                         mvm->noa_vif = NULL;
986                         mvm->noa_duration = 0;
987                 }
988 #endif
989                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
990                 goto out_release;
991         }
992
993         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
994                 mvm->p2p_device_vif = NULL;
995                 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
996                 iwl_mvm_binding_remove_vif(mvm, vif);
997                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
998                 mvmvif->phy_ctxt = NULL;
999         }
1000
1001         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1002                 mvm->vif_count--;
1003
1004         iwl_mvm_power_update_mac(mvm);
1005         iwl_mvm_mac_ctxt_remove(mvm, vif);
1006
1007 out_release:
1008         iwl_mvm_mac_ctxt_release(mvm, vif);
1009         mutex_unlock(&mvm->mutex);
1010 }
1011
1012 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1013 {
1014         return 0;
1015 }
1016
1017 struct iwl_mvm_mc_iter_data {
1018         struct iwl_mvm *mvm;
1019         int port_id;
1020 };
1021
1022 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1023                                       struct ieee80211_vif *vif)
1024 {
1025         struct iwl_mvm_mc_iter_data *data = _data;
1026         struct iwl_mvm *mvm = data->mvm;
1027         struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1028         int ret, len;
1029
1030         /* if we don't have free ports, mcast frames will be dropped */
1031         if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1032                 return;
1033
1034         if (vif->type != NL80211_IFTYPE_STATION ||
1035             !vif->bss_conf.assoc)
1036                 return;
1037
1038         cmd->port_id = data->port_id++;
1039         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1040         len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1041
1042         ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
1043         if (ret)
1044                 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1045 }
1046
1047 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1048 {
1049         struct iwl_mvm_mc_iter_data iter_data = {
1050                 .mvm = mvm,
1051         };
1052
1053         lockdep_assert_held(&mvm->mutex);
1054
1055         if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1056                 return;
1057
1058         ieee80211_iterate_active_interfaces_atomic(
1059                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1060                 iwl_mvm_mc_iface_iterator, &iter_data);
1061 }
1062
1063 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1064                                      struct netdev_hw_addr_list *mc_list)
1065 {
1066         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1067         struct iwl_mcast_filter_cmd *cmd;
1068         struct netdev_hw_addr *addr;
1069         int addr_count = netdev_hw_addr_list_count(mc_list);
1070         bool pass_all = false;
1071         int len;
1072
1073         if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
1074                 pass_all = true;
1075                 addr_count = 0;
1076         }
1077
1078         len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1079         cmd = kzalloc(len, GFP_ATOMIC);
1080         if (!cmd)
1081                 return 0;
1082
1083         if (pass_all) {
1084                 cmd->pass_all = 1;
1085                 return (u64)(unsigned long)cmd;
1086         }
1087
1088         netdev_hw_addr_list_for_each(addr, mc_list) {
1089                 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1090                                    cmd->count, addr->addr);
1091                 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1092                        addr->addr, ETH_ALEN);
1093                 cmd->count++;
1094         }
1095
1096         return (u64)(unsigned long)cmd;
1097 }
1098
1099 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1100                                      unsigned int changed_flags,
1101                                      unsigned int *total_flags,
1102                                      u64 multicast)
1103 {
1104         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1105         struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1106
1107         mutex_lock(&mvm->mutex);
1108
1109         /* replace previous configuration */
1110         kfree(mvm->mcast_filter_cmd);
1111         mvm->mcast_filter_cmd = cmd;
1112
1113         if (!cmd)
1114                 goto out;
1115
1116         iwl_mvm_recalc_multicast(mvm);
1117 out:
1118         mutex_unlock(&mvm->mutex);
1119         *total_flags = 0;
1120 }
1121
1122 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1123 struct iwl_bcast_iter_data {
1124         struct iwl_mvm *mvm;
1125         struct iwl_bcast_filter_cmd *cmd;
1126         u8 current_filter;
1127 };
1128
1129 static void
1130 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1131                          const struct iwl_fw_bcast_filter *in_filter,
1132                          struct iwl_fw_bcast_filter *out_filter)
1133 {
1134         struct iwl_fw_bcast_filter_attr *attr;
1135         int i;
1136
1137         memcpy(out_filter, in_filter, sizeof(*out_filter));
1138
1139         for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1140                 attr = &out_filter->attrs[i];
1141
1142                 if (!attr->mask)
1143                         break;
1144
1145                 switch (attr->reserved1) {
1146                 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1147                         if (vif->bss_conf.arp_addr_cnt != 1) {
1148                                 attr->mask = 0;
1149                                 continue;
1150                         }
1151
1152                         attr->val = vif->bss_conf.arp_addr_list[0];
1153                         break;
1154                 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1155                         attr->val = *(__be32 *)&vif->addr[2];
1156                         break;
1157                 default:
1158                         break;
1159                 }
1160                 attr->reserved1 = 0;
1161                 out_filter->num_attrs++;
1162         }
1163 }
1164
1165 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1166                                           struct ieee80211_vif *vif)
1167 {
1168         struct iwl_bcast_iter_data *data = _data;
1169         struct iwl_mvm *mvm = data->mvm;
1170         struct iwl_bcast_filter_cmd *cmd = data->cmd;
1171         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1172         struct iwl_fw_bcast_mac *bcast_mac;
1173         int i;
1174
1175         if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1176                 return;
1177
1178         bcast_mac = &cmd->macs[mvmvif->id];
1179
1180         /*
1181          * enable filtering only for associated stations, but not for P2P
1182          * Clients
1183          */
1184         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1185             !vif->bss_conf.assoc)
1186                 return;
1187
1188         bcast_mac->default_discard = 1;
1189
1190         /* copy all configured filters */
1191         for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1192                 /*
1193                  * Make sure we don't exceed our filters limit.
1194                  * if there is still a valid filter to be configured,
1195                  * be on the safe side and just allow bcast for this mac.
1196                  */
1197                 if (WARN_ON_ONCE(data->current_filter >=
1198                                  ARRAY_SIZE(cmd->filters))) {
1199                         bcast_mac->default_discard = 0;
1200                         bcast_mac->attached_filters = 0;
1201                         break;
1202                 }
1203
1204                 iwl_mvm_set_bcast_filter(vif,
1205                                          &mvm->bcast_filters[i],
1206                                          &cmd->filters[data->current_filter]);
1207
1208                 /* skip current filter if it contains no attributes */
1209                 if (!cmd->filters[data->current_filter].num_attrs)
1210                         continue;
1211
1212                 /* attach the filter to current mac */
1213                 bcast_mac->attached_filters |=
1214                                 cpu_to_le16(BIT(data->current_filter));
1215
1216                 data->current_filter++;
1217         }
1218 }
1219
1220 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1221                                     struct iwl_bcast_filter_cmd *cmd)
1222 {
1223         struct iwl_bcast_iter_data iter_data = {
1224                 .mvm = mvm,
1225                 .cmd = cmd,
1226         };
1227
1228         memset(cmd, 0, sizeof(*cmd));
1229         cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1230         cmd->max_macs = ARRAY_SIZE(cmd->macs);
1231
1232 #ifdef CONFIG_IWLWIFI_DEBUGFS
1233         /* use debugfs filters/macs if override is configured */
1234         if (mvm->dbgfs_bcast_filtering.override) {
1235                 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1236                        sizeof(cmd->filters));
1237                 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1238                        sizeof(cmd->macs));
1239                 return true;
1240         }
1241 #endif
1242
1243         /* if no filters are configured, do nothing */
1244         if (!mvm->bcast_filters)
1245                 return false;
1246
1247         /* configure and attach these filters for each associated sta vif */
1248         ieee80211_iterate_active_interfaces(
1249                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1250                 iwl_mvm_bcast_filter_iterator, &iter_data);
1251
1252         return true;
1253 }
1254 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1255                                           struct ieee80211_vif *vif)
1256 {
1257         struct iwl_bcast_filter_cmd cmd;
1258
1259         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1260                 return 0;
1261
1262         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1263                 return 0;
1264
1265         return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1266                                     sizeof(cmd), &cmd);
1267 }
1268 #else
1269 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1270                                                  struct ieee80211_vif *vif)
1271 {
1272         return 0;
1273 }
1274 #endif
1275
1276 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1277                                              struct ieee80211_vif *vif,
1278                                              struct ieee80211_bss_conf *bss_conf,
1279                                              u32 changes)
1280 {
1281         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1282         int ret;
1283
1284         /*
1285          * Re-calculate the tsf id, as the master-slave relations depend on the
1286          * beacon interval, which was not known when the station interface was
1287          * added.
1288          */
1289         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1290                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1291
1292         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
1293         if (ret)
1294                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1295
1296         if (changes & BSS_CHANGED_ASSOC) {
1297                 if (bss_conf->assoc) {
1298                         /* add quota for this interface */
1299                         ret = iwl_mvm_update_quotas(mvm, vif);
1300                         if (ret) {
1301                                 IWL_ERR(mvm, "failed to update quotas\n");
1302                                 return;
1303                         }
1304
1305                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1306                                      &mvm->status)) {
1307                                 /*
1308                                  * If we're restarting then the firmware will
1309                                  * obviously have lost synchronisation with
1310                                  * the AP. It will attempt to synchronise by
1311                                  * itself, but we can make it more reliable by
1312                                  * scheduling a session protection time event.
1313                                  *
1314                                  * The firmware needs to receive a beacon to
1315                                  * catch up with synchronisation, use 110% of
1316                                  * the beacon interval.
1317                                  *
1318                                  * Set a large maximum delay to allow for more
1319                                  * than a single interface.
1320                                  */
1321                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1322                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
1323                                                         5 * dur);
1324                         }
1325
1326                         iwl_mvm_sf_update(mvm, vif, false);
1327                         iwl_mvm_power_vif_assoc(mvm, vif);
1328                         if (vif->p2p)
1329                                 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1330                 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1331                         /*
1332                          * If update fails - SF might be running in associated
1333                          * mode while disassociated - which is forbidden.
1334                          */
1335                         WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1336                                   "Failed to update SF upon disassociation\n");
1337
1338                         /* remove AP station now that the MAC is unassoc */
1339                         ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1340                         if (ret)
1341                                 IWL_ERR(mvm, "failed to remove AP station\n");
1342
1343                         if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1344                                 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1345                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1346                         /* remove quota for this interface */
1347                         ret = iwl_mvm_update_quotas(mvm, NULL);
1348                         if (ret)
1349                                 IWL_ERR(mvm, "failed to update quotas\n");
1350
1351                         if (vif->p2p)
1352                                 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1353                 }
1354
1355                 iwl_mvm_recalc_multicast(mvm);
1356                 iwl_mvm_configure_bcast_filter(mvm, vif);
1357
1358                 /* reset rssi values */
1359                 mvmvif->bf_data.ave_beacon_signal = 0;
1360
1361                 iwl_mvm_bt_coex_vif_change(mvm);
1362                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1363                                     IEEE80211_SMPS_AUTOMATIC);
1364         } else if (changes & BSS_CHANGED_BEACON_INFO) {
1365                 /*
1366                  * We received a beacon _after_ association so
1367                  * remove the session protection.
1368                  */
1369                 iwl_mvm_remove_time_event(mvm, mvmvif,
1370                                           &mvmvif->time_event_data);
1371                 iwl_mvm_sf_update(mvm, vif, false);
1372                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1373         } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS |
1374                               BSS_CHANGED_QOS)) {
1375                 ret = iwl_mvm_power_update_mac(mvm);
1376                 if (ret)
1377                         IWL_ERR(mvm, "failed to update power mode\n");
1378         }
1379         if (changes & BSS_CHANGED_TXPOWER) {
1380                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1381                                 bss_conf->txpower);
1382                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1383         }
1384
1385         if (changes & BSS_CHANGED_CQM) {
1386                 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
1387                 /* reset cqm events tracking */
1388                 mvmvif->bf_data.last_cqm_event = 0;
1389                 if (mvmvif->bf_data.bf_enabled) {
1390                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1391                         if (ret)
1392                                 IWL_ERR(mvm,
1393                                         "failed to update CQM thresholds\n");
1394                 }
1395         }
1396
1397         if (changes & BSS_CHANGED_ARP_FILTER) {
1398                 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
1399                 iwl_mvm_configure_bcast_filter(mvm, vif);
1400         }
1401 }
1402
1403 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1404                                  struct ieee80211_vif *vif)
1405 {
1406         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1407         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1408         int ret;
1409
1410         mutex_lock(&mvm->mutex);
1411
1412         /* Send the beacon template */
1413         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1414         if (ret)
1415                 goto out_unlock;
1416
1417         /*
1418          * Re-calculate the tsf id, as the master-slave relations depend on the
1419          * beacon interval, which was not known when the AP interface was added.
1420          */
1421         if (vif->type == NL80211_IFTYPE_AP)
1422                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1423
1424         /* Add the mac context */
1425         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1426         if (ret)
1427                 goto out_unlock;
1428
1429         /* Perform the binding */
1430         ret = iwl_mvm_binding_add_vif(mvm, vif);
1431         if (ret)
1432                 goto out_remove;
1433
1434         /* Send the bcast station. At this stage the TBTT and DTIM time events
1435          * are added and applied to the scheduler */
1436         ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
1437         if (ret)
1438                 goto out_unbind;
1439
1440         /* must be set before quota calculations */
1441         mvmvif->ap_ibss_active = true;
1442
1443         /* power updated needs to be done before quotas */
1444         iwl_mvm_power_update_mac(mvm);
1445
1446         ret = iwl_mvm_update_quotas(mvm, vif);
1447         if (ret)
1448                 goto out_quota_failed;
1449
1450         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1451         if (vif->p2p && mvm->p2p_device_vif)
1452                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
1453
1454         iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1455
1456         iwl_mvm_bt_coex_vif_change(mvm);
1457
1458         mutex_unlock(&mvm->mutex);
1459         return 0;
1460
1461 out_quota_failed:
1462         iwl_mvm_power_update_mac(mvm);
1463         mvmvif->ap_ibss_active = false;
1464         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1465 out_unbind:
1466         iwl_mvm_binding_remove_vif(mvm, vif);
1467 out_remove:
1468         iwl_mvm_mac_ctxt_remove(mvm, vif);
1469 out_unlock:
1470         mutex_unlock(&mvm->mutex);
1471         return ret;
1472 }
1473
1474 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1475                                  struct ieee80211_vif *vif)
1476 {
1477         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1478         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1479
1480         iwl_mvm_prepare_mac_removal(mvm, vif);
1481
1482         mutex_lock(&mvm->mutex);
1483
1484         mvmvif->ap_ibss_active = false;
1485         mvm->ap_last_beacon_gp2 = 0;
1486
1487         iwl_mvm_bt_coex_vif_change(mvm);
1488
1489         iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
1490
1491         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1492         if (vif->p2p && mvm->p2p_device_vif)
1493                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
1494
1495         iwl_mvm_update_quotas(mvm, NULL);
1496         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1497         iwl_mvm_binding_remove_vif(mvm, vif);
1498
1499         iwl_mvm_power_update_mac(mvm);
1500
1501         iwl_mvm_mac_ctxt_remove(mvm, vif);
1502
1503         mutex_unlock(&mvm->mutex);
1504 }
1505
1506 static void
1507 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1508                                  struct ieee80211_vif *vif,
1509                                  struct ieee80211_bss_conf *bss_conf,
1510                                  u32 changes)
1511 {
1512         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1513
1514         /* Changes will be applied when the AP/IBSS is started */
1515         if (!mvmvif->ap_ibss_active)
1516                 return;
1517
1518         if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
1519                        BSS_CHANGED_BANDWIDTH) &&
1520             iwl_mvm_mac_ctxt_changed(mvm, vif, false))
1521                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1522
1523         /* Need to send a new beacon template to the FW */
1524         if (changes & BSS_CHANGED_BEACON &&
1525             iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1526                 IWL_WARN(mvm, "Failed updating beacon data\n");
1527 }
1528
1529 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1530                                      struct ieee80211_vif *vif,
1531                                      struct ieee80211_bss_conf *bss_conf,
1532                                      u32 changes)
1533 {
1534         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1535
1536         mutex_lock(&mvm->mutex);
1537
1538         if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
1539                 iwl_mvm_sched_scan_stop(mvm, true);
1540
1541         switch (vif->type) {
1542         case NL80211_IFTYPE_STATION:
1543                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1544                 break;
1545         case NL80211_IFTYPE_AP:
1546         case NL80211_IFTYPE_ADHOC:
1547                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1548                 break;
1549         default:
1550                 /* shouldn't happen */
1551                 WARN_ON_ONCE(1);
1552         }
1553
1554         mutex_unlock(&mvm->mutex);
1555 }
1556
1557 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1558                                struct ieee80211_vif *vif,
1559                                struct ieee80211_scan_request *hw_req)
1560 {
1561         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1562         struct cfg80211_scan_request *req = &hw_req->req;
1563         int ret;
1564
1565         if (req->n_channels == 0 ||
1566             req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1567                 return -EINVAL;
1568
1569         mutex_lock(&mvm->mutex);
1570
1571         switch (mvm->scan_status) {
1572         case IWL_MVM_SCAN_SCHED:
1573                 ret = iwl_mvm_sched_scan_stop(mvm, true);
1574                 if (ret) {
1575                         ret = -EBUSY;
1576                         goto out;
1577                 }
1578                 break;
1579         case IWL_MVM_SCAN_NONE:
1580                 break;
1581         default:
1582                 ret = -EBUSY;
1583                 goto out;
1584         }
1585
1586         iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1587
1588         ret = iwl_mvm_scan_request(mvm, vif, req);
1589         if (ret)
1590                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1591 out:
1592         mutex_unlock(&mvm->mutex);
1593         /* make sure to flush the Rx handler before the next scan arrives */
1594         iwl_mvm_wait_for_async_handlers(mvm);
1595         return ret;
1596 }
1597
1598 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1599                                        struct ieee80211_vif *vif)
1600 {
1601         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1602
1603         mutex_lock(&mvm->mutex);
1604
1605         iwl_mvm_cancel_scan(mvm);
1606
1607         mutex_unlock(&mvm->mutex);
1608 }
1609
1610 static void
1611 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1612                                   struct ieee80211_sta *sta, u16 tids,
1613                                   int num_frames,
1614                                   enum ieee80211_frame_release_type reason,
1615                                   bool more_data)
1616 {
1617         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1618
1619         /* Called when we need to transmit (a) frame(s) from mac80211 */
1620
1621         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1622                                           tids, more_data, false);
1623 }
1624
1625 static void
1626 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
1627                                     struct ieee80211_sta *sta, u16 tids,
1628                                     int num_frames,
1629                                     enum ieee80211_frame_release_type reason,
1630                                     bool more_data)
1631 {
1632         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1633
1634         /* Called when we need to transmit (a) frame(s) from agg queue */
1635
1636         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1637                                           tids, more_data, true);
1638 }
1639
1640 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1641                                    struct ieee80211_vif *vif,
1642                                    enum sta_notify_cmd cmd,
1643                                    struct ieee80211_sta *sta)
1644 {
1645         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1646         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1647         int tid;
1648
1649         switch (cmd) {
1650         case STA_NOTIFY_SLEEP:
1651                 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1652                         ieee80211_sta_block_awake(hw, sta, true);
1653                 spin_lock_bh(&mvmsta->lock);
1654                 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1655                         struct iwl_mvm_tid_data *tid_data;
1656
1657                         tid_data = &mvmsta->tid_data[tid];
1658                         if (tid_data->state != IWL_AGG_ON &&
1659                             tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
1660                                 continue;
1661                         if (iwl_mvm_tid_queued(tid_data) == 0)
1662                                 continue;
1663                         ieee80211_sta_set_buffered(sta, tid, true);
1664                 }
1665                 spin_unlock_bh(&mvmsta->lock);
1666                 /*
1667                  * The fw updates the STA to be asleep. Tx packets on the Tx
1668                  * queues to this station will not be transmitted. The fw will
1669                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1670                  */
1671                 break;
1672         case STA_NOTIFY_AWAKE:
1673                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1674                         break;
1675                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1676                 break;
1677         default:
1678                 break;
1679         }
1680 }
1681
1682 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
1683                                        struct ieee80211_vif *vif,
1684                                        struct ieee80211_sta *sta)
1685 {
1686         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1687         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1688
1689         /*
1690          * This is called before mac80211 does RCU synchronisation,
1691          * so here we already invalidate our internal RCU-protected
1692          * station pointer. The rest of the code will thus no longer
1693          * be able to find the station this way, and we don't rely
1694          * on further RCU synchronisation after the sta_state()
1695          * callback deleted the station.
1696          */
1697         mutex_lock(&mvm->mutex);
1698         if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
1699                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1700                                    ERR_PTR(-ENOENT));
1701         mutex_unlock(&mvm->mutex);
1702 }
1703
1704 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1705                                  struct ieee80211_vif *vif,
1706                                  struct ieee80211_sta *sta,
1707                                  enum ieee80211_sta_state old_state,
1708                                  enum ieee80211_sta_state new_state)
1709 {
1710         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1711         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1712         int ret;
1713
1714         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1715                            sta->addr, old_state, new_state);
1716
1717         /* this would be a mac80211 bug ... but don't crash */
1718         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1719                 return -EINVAL;
1720
1721         /* if a STA is being removed, reuse its ID */
1722         flush_work(&mvm->sta_drained_wk);
1723
1724         mutex_lock(&mvm->mutex);
1725         if (old_state == IEEE80211_STA_NOTEXIST &&
1726             new_state == IEEE80211_STA_NONE) {
1727                 /*
1728                  * Firmware bug - it'll crash if the beacon interval is less
1729                  * than 16. We can't avoid connecting at all, so refuse the
1730                  * station state change, this will cause mac80211 to abandon
1731                  * attempts to connect to this AP, and eventually wpa_s will
1732                  * blacklist the AP...
1733                  */
1734                 if (vif->type == NL80211_IFTYPE_STATION &&
1735                     vif->bss_conf.beacon_int < 16) {
1736                         IWL_ERR(mvm,
1737                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1738                                 sta->addr, vif->bss_conf.beacon_int);
1739                         ret = -EINVAL;
1740                         goto out_unlock;
1741                 }
1742                 ret = iwl_mvm_add_sta(mvm, vif, sta);
1743         } else if (old_state == IEEE80211_STA_NONE &&
1744                    new_state == IEEE80211_STA_AUTH) {
1745                 /*
1746                  * EBS may be disabled due to previous failures reported by FW.
1747                  * Reset EBS status here assuming environment has been changed.
1748                  */
1749                 mvm->last_ebs_successful = true;
1750                 ret = 0;
1751         } else if (old_state == IEEE80211_STA_AUTH &&
1752                    new_state == IEEE80211_STA_ASSOC) {
1753                 ret = iwl_mvm_update_sta(mvm, vif, sta);
1754                 if (ret == 0)
1755                         iwl_mvm_rs_rate_init(mvm, sta,
1756                                              mvmvif->phy_ctxt->channel->band,
1757                                              true);
1758         } else if (old_state == IEEE80211_STA_ASSOC &&
1759                    new_state == IEEE80211_STA_AUTHORIZED) {
1760                 /* enable beacon filtering */
1761                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1762                 ret = 0;
1763         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1764                    new_state == IEEE80211_STA_ASSOC) {
1765                 /* disable beacon filtering */
1766                 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
1767                 ret = 0;
1768         } else if (old_state == IEEE80211_STA_ASSOC &&
1769                    new_state == IEEE80211_STA_AUTH) {
1770                 ret = 0;
1771         } else if (old_state == IEEE80211_STA_AUTH &&
1772                    new_state == IEEE80211_STA_NONE) {
1773                 ret = 0;
1774         } else if (old_state == IEEE80211_STA_NONE &&
1775                    new_state == IEEE80211_STA_NOTEXIST) {
1776                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1777         } else {
1778                 ret = -EIO;
1779         }
1780  out_unlock:
1781         mutex_unlock(&mvm->mutex);
1782
1783         return ret;
1784 }
1785
1786 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1787 {
1788         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1789
1790         mvm->rts_threshold = value;
1791
1792         return 0;
1793 }
1794
1795 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
1796                                   struct ieee80211_vif *vif,
1797                                   struct ieee80211_sta *sta, u32 changed)
1798 {
1799         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1800
1801         if (vif->type == NL80211_IFTYPE_STATION &&
1802             changed & IEEE80211_RC_NSS_CHANGED)
1803                 iwl_mvm_sf_update(mvm, vif, false);
1804 }
1805
1806 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1807                                struct ieee80211_vif *vif, u16 ac,
1808                                const struct ieee80211_tx_queue_params *params)
1809 {
1810         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1811         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1812
1813         mvmvif->queue_params[ac] = *params;
1814
1815         /*
1816          * No need to update right away, we'll get BSS_CHANGED_QOS
1817          * The exception is P2P_DEVICE interface which needs immediate update.
1818          */
1819         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1820                 int ret;
1821
1822                 mutex_lock(&mvm->mutex);
1823                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
1824                 mutex_unlock(&mvm->mutex);
1825                 return ret;
1826         }
1827         return 0;
1828 }
1829
1830 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1831                                       struct ieee80211_vif *vif)
1832 {
1833         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1834         u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1835                            200 + vif->bss_conf.beacon_int);
1836         u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1837                                100 + vif->bss_conf.beacon_int);
1838
1839         if (WARN_ON_ONCE(vif->bss_conf.assoc))
1840                 return;
1841
1842         mutex_lock(&mvm->mutex);
1843         /* Try really hard to protect the session and hear a beacon */
1844         iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
1845         mutex_unlock(&mvm->mutex);
1846 }
1847
1848 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
1849                                         struct ieee80211_vif *vif,
1850                                         struct cfg80211_sched_scan_request *req,
1851                                         struct ieee80211_scan_ies *ies)
1852 {
1853         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1854         int ret;
1855
1856         mutex_lock(&mvm->mutex);
1857
1858         if (!iwl_mvm_is_idle(mvm)) {
1859                 ret = -EBUSY;
1860                 goto out;
1861         }
1862
1863         switch (mvm->scan_status) {
1864         case IWL_MVM_SCAN_OS:
1865                 IWL_DEBUG_SCAN(mvm, "Stopping previous scan for sched_scan\n");
1866                 ret = iwl_mvm_cancel_scan(mvm);
1867                 if (ret) {
1868                         ret = -EBUSY;
1869                         goto out;
1870                 }
1871
1872                 /*
1873                  * iwl_mvm_rx_scan_complete() will be called soon but will
1874                  * not reset the scan status as it won't be IWL_MVM_SCAN_OS
1875                  * any more since we queue the next scan immediately (below).
1876                  * We make sure it is called before the next scan starts by
1877                  * flushing the async-handlers work.
1878                  */
1879                 break;
1880         case IWL_MVM_SCAN_NONE:
1881                 break;
1882         default:
1883                 ret = -EBUSY;
1884                 goto out;
1885         }
1886
1887         mvm->scan_status = IWL_MVM_SCAN_SCHED;
1888
1889         ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1890         if (ret)
1891                 goto err;
1892
1893         ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1894         if (ret)
1895                 goto err;
1896
1897         ret = iwl_mvm_sched_scan_start(mvm, req);
1898         if (!ret)
1899                 goto out;
1900 err:
1901         mvm->scan_status = IWL_MVM_SCAN_NONE;
1902 out:
1903         mutex_unlock(&mvm->mutex);
1904         /* make sure to flush the Rx handler before the next scan arrives */
1905         iwl_mvm_wait_for_async_handlers(mvm);
1906         return ret;
1907 }
1908
1909 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
1910                                        struct ieee80211_vif *vif)
1911 {
1912         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1913         int ret;
1914
1915         mutex_lock(&mvm->mutex);
1916         ret = iwl_mvm_sched_scan_stop(mvm, false);
1917         mutex_unlock(&mvm->mutex);
1918         iwl_mvm_wait_for_async_handlers(mvm);
1919
1920         return ret;
1921 }
1922
1923 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1924                                enum set_key_cmd cmd,
1925                                struct ieee80211_vif *vif,
1926                                struct ieee80211_sta *sta,
1927                                struct ieee80211_key_conf *key)
1928 {
1929         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1930         int ret;
1931
1932         if (iwlwifi_mod_params.sw_crypto) {
1933                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1934                 return -EOPNOTSUPP;
1935         }
1936
1937         switch (key->cipher) {
1938         case WLAN_CIPHER_SUITE_TKIP:
1939                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1940                 /* fall-through */
1941         case WLAN_CIPHER_SUITE_CCMP:
1942                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1943                 break;
1944         case WLAN_CIPHER_SUITE_AES_CMAC:
1945                 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1946                 break;
1947         case WLAN_CIPHER_SUITE_WEP40:
1948         case WLAN_CIPHER_SUITE_WEP104:
1949                 /*
1950                  * Support for TX only, at least for now, so accept
1951                  * the key and do nothing else. Then mac80211 will
1952                  * pass it for TX but we don't have to use it for RX.
1953                  */
1954                 return 0;
1955         default:
1956                 /* currently FW supports only one optional cipher scheme */
1957                 if (hw->n_cipher_schemes &&
1958                     hw->cipher_schemes->cipher == key->cipher)
1959                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
1960                 else
1961                         return -EOPNOTSUPP;
1962         }
1963
1964         mutex_lock(&mvm->mutex);
1965
1966         switch (cmd) {
1967         case SET_KEY:
1968                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1969                      vif->type == NL80211_IFTYPE_AP) && !sta) {
1970                         /*
1971                          * GTK on AP interface is a TX-only key, return 0;
1972                          * on IBSS they're per-station and because we're lazy
1973                          * we don't support them for RX, so do the same.
1974                          */
1975                         ret = 0;
1976                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1977                         break;
1978                 }
1979
1980                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1981                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1982                 if (ret) {
1983                         IWL_WARN(mvm, "set key failed\n");
1984                         /*
1985                          * can't add key for RX, but we don't need it
1986                          * in the device for TX so still return 0
1987                          */
1988                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1989                         ret = 0;
1990                 }
1991
1992                 break;
1993         case DISABLE_KEY:
1994                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1995                         ret = 0;
1996                         break;
1997                 }
1998
1999                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
2000                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
2001                 break;
2002         default:
2003                 ret = -EINVAL;
2004         }
2005
2006         mutex_unlock(&mvm->mutex);
2007         return ret;
2008 }
2009
2010 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
2011                                         struct ieee80211_vif *vif,
2012                                         struct ieee80211_key_conf *keyconf,
2013                                         struct ieee80211_sta *sta,
2014                                         u32 iv32, u16 *phase1key)
2015 {
2016         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2017
2018         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
2019                 return;
2020
2021         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
2022 }
2023
2024
2025 static int iwl_mvm_roc(struct ieee80211_hw *hw,
2026                        struct ieee80211_vif *vif,
2027                        struct ieee80211_channel *channel,
2028                        int duration,
2029                        enum ieee80211_roc_type type)
2030 {
2031         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2032         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2033         struct cfg80211_chan_def chandef;
2034         struct iwl_mvm_phy_ctxt *phy_ctxt;
2035         int ret, i;
2036
2037         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2038                            duration, type);
2039
2040         if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
2041                 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
2042                 return -EINVAL;
2043         }
2044
2045         mutex_lock(&mvm->mutex);
2046
2047         for (i = 0; i < NUM_PHY_CTX; i++) {
2048                 phy_ctxt = &mvm->phy_ctxts[i];
2049                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2050                         continue;
2051
2052                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2053                         /*
2054                          * Unbind the P2P_DEVICE from the current PHY context,
2055                          * and if the PHY context is not used remove it.
2056                          */
2057                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
2058                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2059                                 goto out_unlock;
2060
2061                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2062
2063                         /* Bind the P2P_DEVICE to the current PHY Context */
2064                         mvmvif->phy_ctxt = phy_ctxt;
2065
2066                         ret = iwl_mvm_binding_add_vif(mvm, vif);
2067                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2068                                 goto out_unlock;
2069
2070                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2071                         goto schedule_time_event;
2072                 }
2073         }
2074
2075         /* Need to update the PHY context only if the ROC channel changed */
2076         if (channel == mvmvif->phy_ctxt->channel)
2077                 goto schedule_time_event;
2078
2079         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2080
2081         /*
2082          * Change the PHY context configuration as it is currently referenced
2083          * only by the P2P Device MAC
2084          */
2085         if (mvmvif->phy_ctxt->ref == 1) {
2086                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2087                                                &chandef, 1, 1);
2088                 if (ret)
2089                         goto out_unlock;
2090         } else {
2091                 /*
2092                  * The PHY context is shared with other MACs. Need to remove the
2093                  * P2P Device from the binding, allocate an new PHY context and
2094                  * create a new binding
2095                  */
2096                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2097                 if (!phy_ctxt) {
2098                         ret = -ENOSPC;
2099                         goto out_unlock;
2100                 }
2101
2102                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2103                                                1, 1);
2104                 if (ret) {
2105                         IWL_ERR(mvm, "Failed to change PHY context\n");
2106                         goto out_unlock;
2107                 }
2108
2109                 /* Unbind the P2P_DEVICE from the current PHY context */
2110                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2111                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2112                         goto out_unlock;
2113
2114                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2115
2116                 /* Bind the P2P_DEVICE to the new allocated PHY context */
2117                 mvmvif->phy_ctxt = phy_ctxt;
2118
2119                 ret = iwl_mvm_binding_add_vif(mvm, vif);
2120                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2121                         goto out_unlock;
2122
2123                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2124         }
2125
2126 schedule_time_event:
2127         /* Schedule the time events */
2128         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2129
2130 out_unlock:
2131         mutex_unlock(&mvm->mutex);
2132         IWL_DEBUG_MAC80211(mvm, "leave\n");
2133         return ret;
2134 }
2135
2136 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2137 {
2138         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2139
2140         IWL_DEBUG_MAC80211(mvm, "enter\n");
2141
2142         mutex_lock(&mvm->mutex);
2143         iwl_mvm_stop_p2p_roc(mvm);
2144         mutex_unlock(&mvm->mutex);
2145
2146         IWL_DEBUG_MAC80211(mvm, "leave\n");
2147         return 0;
2148 }
2149
2150 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2151                                struct ieee80211_chanctx_conf *ctx)
2152 {
2153         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2154         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2155         struct iwl_mvm_phy_ctxt *phy_ctxt;
2156         int ret;
2157
2158         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2159
2160         mutex_lock(&mvm->mutex);
2161         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2162         if (!phy_ctxt) {
2163                 ret = -ENOSPC;
2164                 goto out;
2165         }
2166
2167         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2168                                        ctx->rx_chains_static,
2169                                        ctx->rx_chains_dynamic);
2170         if (ret) {
2171                 IWL_ERR(mvm, "Failed to add PHY context\n");
2172                 goto out;
2173         }
2174
2175         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
2176         *phy_ctxt_id = phy_ctxt->id;
2177 out:
2178         mutex_unlock(&mvm->mutex);
2179         return ret;
2180 }
2181
2182 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2183                                    struct ieee80211_chanctx_conf *ctx)
2184 {
2185         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2186         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2187         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2188
2189         mutex_lock(&mvm->mutex);
2190         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2191         mutex_unlock(&mvm->mutex);
2192 }
2193
2194 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
2195                                    struct ieee80211_chanctx_conf *ctx,
2196                                    u32 changed)
2197 {
2198         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2199         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2200         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2201
2202         if (WARN_ONCE((phy_ctxt->ref > 1) &&
2203                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
2204                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2205                                    IEEE80211_CHANCTX_CHANGE_RADAR |
2206                                    IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
2207                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
2208                       phy_ctxt->ref, changed))
2209                 return;
2210
2211         mutex_lock(&mvm->mutex);
2212         iwl_mvm_bt_coex_vif_change(mvm);
2213         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2214                                  ctx->rx_chains_static,
2215                                  ctx->rx_chains_dynamic);
2216         mutex_unlock(&mvm->mutex);
2217 }
2218
2219 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
2220                                       struct ieee80211_vif *vif,
2221                                       struct ieee80211_chanctx_conf *ctx)
2222 {
2223         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2224         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2225         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2226         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2227         int ret;
2228
2229         mutex_lock(&mvm->mutex);
2230
2231         mvmvif->phy_ctxt = phy_ctxt;
2232
2233         switch (vif->type) {
2234         case NL80211_IFTYPE_AP:
2235                 /* Unless it's a CSA flow we have nothing to do here */
2236                 if (vif->csa_active) {
2237                         mvmvif->ap_ibss_active = true;
2238                         break;
2239                 }
2240         case NL80211_IFTYPE_ADHOC:
2241                 /*
2242                  * The AP binding flow is handled as part of the start_ap flow
2243                  * (in bss_info_changed), similarly for IBSS.
2244                  */
2245                 ret = 0;
2246                 goto out_unlock;
2247         case NL80211_IFTYPE_STATION:
2248         case NL80211_IFTYPE_MONITOR:
2249                 break;
2250         default:
2251                 ret = -EINVAL;
2252                 goto out_unlock;
2253         }
2254
2255         ret = iwl_mvm_binding_add_vif(mvm, vif);
2256         if (ret)
2257                 goto out_unlock;
2258
2259         /*
2260          * Power state must be updated before quotas,
2261          * otherwise fw will complain.
2262          */
2263         iwl_mvm_power_update_mac(mvm);
2264
2265         /* Setting the quota at this stage is only required for monitor
2266          * interfaces. For the other types, the bss_info changed flow
2267          * will handle quota settings.
2268          */
2269         if (vif->type == NL80211_IFTYPE_MONITOR) {
2270                 mvmvif->monitor_active = true;
2271                 ret = iwl_mvm_update_quotas(mvm, vif);
2272                 if (ret)
2273                         goto out_remove_binding;
2274         }
2275
2276         /* Handle binding during CSA */
2277         if (vif->type == NL80211_IFTYPE_AP) {
2278                 iwl_mvm_update_quotas(mvm, vif);
2279                 iwl_mvm_mac_ctxt_changed(mvm, vif, false);
2280         }
2281
2282         goto out_unlock;
2283
2284  out_remove_binding:
2285         iwl_mvm_binding_remove_vif(mvm, vif);
2286         iwl_mvm_power_update_mac(mvm);
2287  out_unlock:
2288         mutex_unlock(&mvm->mutex);
2289         if (ret)
2290                 mvmvif->phy_ctxt = NULL;
2291         return ret;
2292 }
2293
2294 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
2295                                          struct ieee80211_vif *vif,
2296                                          struct ieee80211_chanctx_conf *ctx)
2297 {
2298         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2299         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2300
2301         mutex_lock(&mvm->mutex);
2302
2303         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
2304
2305         switch (vif->type) {
2306         case NL80211_IFTYPE_ADHOC:
2307                 goto out_unlock;
2308         case NL80211_IFTYPE_MONITOR:
2309                 mvmvif->monitor_active = false;
2310                 iwl_mvm_update_quotas(mvm, NULL);
2311                 break;
2312         case NL80211_IFTYPE_AP:
2313                 /* This part is triggered only during CSA */
2314                 if (!vif->csa_active || !mvmvif->ap_ibss_active)
2315                         goto out_unlock;
2316
2317                 mvmvif->ap_ibss_active = false;
2318                 iwl_mvm_update_quotas(mvm, NULL);
2319                 /*TODO: bt_coex notification here? */
2320         default:
2321                 break;
2322         }
2323
2324         iwl_mvm_binding_remove_vif(mvm, vif);
2325
2326 out_unlock:
2327         mvmvif->phy_ctxt = NULL;
2328         iwl_mvm_power_update_mac(mvm);
2329         mutex_unlock(&mvm->mutex);
2330 }
2331
2332 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
2333                            struct ieee80211_sta *sta,
2334                            bool set)
2335 {
2336         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2337         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
2338
2339         if (!mvm_sta || !mvm_sta->vif) {
2340                 IWL_ERR(mvm, "Station is not associated to a vif\n");
2341                 return -EINVAL;
2342         }
2343
2344         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
2345 }
2346
2347 #ifdef CONFIG_NL80211_TESTMODE
2348 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
2349         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
2350         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
2351         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
2352 };
2353
2354 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
2355                                       struct ieee80211_vif *vif,
2356                                       void *data, int len)
2357 {
2358         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
2359         int err;
2360         u32 noa_duration;
2361
2362         err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
2363         if (err)
2364                 return err;
2365
2366         if (!tb[IWL_MVM_TM_ATTR_CMD])
2367                 return -EINVAL;
2368
2369         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
2370         case IWL_MVM_TM_CMD_SET_NOA:
2371                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
2372                     !vif->bss_conf.enable_beacon ||
2373                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
2374                         return -EINVAL;
2375
2376                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
2377                 if (noa_duration >= vif->bss_conf.beacon_int)
2378                         return -EINVAL;
2379
2380                 mvm->noa_duration = noa_duration;
2381                 mvm->noa_vif = vif;
2382
2383                 return iwl_mvm_update_quotas(mvm, NULL);
2384         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
2385                 /* must be associated client vif - ignore authorized */
2386                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
2387                     !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
2388                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
2389                         return -EINVAL;
2390
2391                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
2392                         return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2393                 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
2394         }
2395
2396         return -EOPNOTSUPP;
2397 }
2398
2399 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
2400                                     struct ieee80211_vif *vif,
2401                                     void *data, int len)
2402 {
2403         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2404         int err;
2405
2406         mutex_lock(&mvm->mutex);
2407         err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
2408         mutex_unlock(&mvm->mutex);
2409
2410         return err;
2411 }
2412 #endif
2413
2414 static void iwl_mvm_channel_switch_beacon(struct ieee80211_hw *hw,
2415                                           struct ieee80211_vif *vif,
2416                                           struct cfg80211_chan_def *chandef)
2417 {
2418         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2419
2420         mutex_lock(&mvm->mutex);
2421         if (WARN(mvm->csa_vif && mvm->csa_vif->csa_active,
2422                  "Another CSA is already in progress"))
2423                 goto out_unlock;
2424
2425         IWL_DEBUG_MAC80211(mvm, "CSA started to freq %d\n",
2426                            chandef->center_freq1);
2427         mvm->csa_vif = vif;
2428
2429 out_unlock:
2430         mutex_unlock(&mvm->mutex);
2431 }
2432
2433 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
2434                               struct ieee80211_vif *vif, u32 queues, bool drop)
2435 {
2436         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2437         struct iwl_mvm_vif *mvmvif;
2438         struct iwl_mvm_sta *mvmsta;
2439
2440         if (!vif || vif->type != NL80211_IFTYPE_STATION)
2441                 return;
2442
2443         mutex_lock(&mvm->mutex);
2444         mvmvif = iwl_mvm_vif_from_mac80211(vif);
2445         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, mvmvif->ap_sta_id);
2446
2447         if (WARN_ON_ONCE(!mvmsta))
2448                 goto done;
2449
2450         if (drop) {
2451                 if (iwl_mvm_flush_tx_path(mvm, mvmsta->tfd_queue_msk, true))
2452                         IWL_ERR(mvm, "flush request fail\n");
2453         } else {
2454                 iwl_trans_wait_tx_queue_empty(mvm->trans,
2455                                               mvmsta->tfd_queue_msk);
2456         }
2457 done:
2458         mutex_unlock(&mvm->mutex);
2459 }
2460
2461 const struct ieee80211_ops iwl_mvm_hw_ops = {
2462         .tx = iwl_mvm_mac_tx,
2463         .ampdu_action = iwl_mvm_mac_ampdu_action,
2464         .start = iwl_mvm_mac_start,
2465         .restart_complete = iwl_mvm_mac_restart_complete,
2466         .stop = iwl_mvm_mac_stop,
2467         .add_interface = iwl_mvm_mac_add_interface,
2468         .remove_interface = iwl_mvm_mac_remove_interface,
2469         .config = iwl_mvm_mac_config,
2470         .prepare_multicast = iwl_mvm_prepare_multicast,
2471         .configure_filter = iwl_mvm_configure_filter,
2472         .bss_info_changed = iwl_mvm_bss_info_changed,
2473         .hw_scan = iwl_mvm_mac_hw_scan,
2474         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
2475         .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
2476         .sta_state = iwl_mvm_mac_sta_state,
2477         .sta_notify = iwl_mvm_mac_sta_notify,
2478         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
2479         .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
2480         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
2481         .sta_rc_update = iwl_mvm_sta_rc_update,
2482         .conf_tx = iwl_mvm_mac_conf_tx,
2483         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
2484         .flush = iwl_mvm_mac_flush,
2485         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
2486         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
2487         .set_key = iwl_mvm_mac_set_key,
2488         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
2489         .remain_on_channel = iwl_mvm_roc,
2490         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
2491         .add_chanctx = iwl_mvm_add_chanctx,
2492         .remove_chanctx = iwl_mvm_remove_chanctx,
2493         .change_chanctx = iwl_mvm_change_chanctx,
2494         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
2495         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
2496
2497         .start_ap = iwl_mvm_start_ap_ibss,
2498         .stop_ap = iwl_mvm_stop_ap_ibss,
2499         .join_ibss = iwl_mvm_start_ap_ibss,
2500         .leave_ibss = iwl_mvm_stop_ap_ibss,
2501
2502         .set_tim = iwl_mvm_set_tim,
2503
2504         .channel_switch_beacon = iwl_mvm_channel_switch_beacon,
2505
2506         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
2507
2508 #ifdef CONFIG_PM_SLEEP
2509         /* look at d3.c */
2510         .suspend = iwl_mvm_suspend,
2511         .resume = iwl_mvm_resume,
2512         .set_wakeup = iwl_mvm_set_wakeup,
2513         .set_rekey_data = iwl_mvm_set_rekey_data,
2514 #if IS_ENABLED(CONFIG_IPV6)
2515         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
2516 #endif
2517         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
2518 #endif
2519 };