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