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