f0f8b2c7ebef20ac070631b2c5e7cd9d2286f9b0
[platform/adaptation/renesas_rcar/renesas_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 - 2013 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 - 2013 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 <net/mac80211.h>
70 #include <net/tcp.h>
71
72 #include "iwl-op-mode.h"
73 #include "iwl-io.h"
74 #include "mvm.h"
75 #include "sta.h"
76 #include "time-event.h"
77 #include "iwl-eeprom-parse.h"
78 #include "fw-api-scan.h"
79 #include "iwl-phy-db.h"
80 #include "testmode.h"
81
82 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
83         {
84                 .max = 1,
85                 .types = BIT(NL80211_IFTYPE_STATION),
86         },
87         {
88                 .max = 1,
89                 .types = BIT(NL80211_IFTYPE_AP) |
90                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
91                         BIT(NL80211_IFTYPE_P2P_GO),
92         },
93         {
94                 .max = 1,
95                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
96         },
97 };
98
99 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
100         {
101                 .num_different_channels = 1,
102                 .max_interfaces = 3,
103                 .limits = iwl_mvm_limits,
104                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
105         },
106 };
107
108 #ifdef CONFIG_PM_SLEEP
109 static const struct nl80211_wowlan_tcp_data_token_feature
110 iwl_mvm_wowlan_tcp_token_feature = {
111         .min_len = 0,
112         .max_len = 255,
113         .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
114 };
115
116 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
117         .tok = &iwl_mvm_wowlan_tcp_token_feature,
118         .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
119                             sizeof(struct ethhdr) -
120                             sizeof(struct iphdr) -
121                             sizeof(struct tcphdr),
122         .data_interval_max = 65535, /* __le16 in API */
123         .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
124                             sizeof(struct ethhdr) -
125                             sizeof(struct iphdr) -
126                             sizeof(struct tcphdr),
127         .seq = true,
128 };
129 #endif
130
131 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
132 {
133         int i;
134
135         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
136         for (i = 0; i < NUM_PHY_CTX; i++) {
137                 mvm->phy_ctxts[i].id = i;
138                 mvm->phy_ctxts[i].ref = 0;
139         }
140 }
141
142 static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
143 {
144         /* we create the 802.11 header and SSID element */
145         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
146                 return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
147         return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
148 }
149
150 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
151 {
152         struct ieee80211_hw *hw = mvm->hw;
153         int num_mac, ret, i;
154
155         /* Tell mac80211 our characteristics */
156         hw->flags = IEEE80211_HW_SIGNAL_DBM |
157                     IEEE80211_HW_SPECTRUM_MGMT |
158                     IEEE80211_HW_REPORTS_TX_ACK_STATUS |
159                     IEEE80211_HW_QUEUE_CONTROL |
160                     IEEE80211_HW_WANT_MONITOR_VIF |
161                     IEEE80211_HW_SUPPORTS_PS |
162                     IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
163                     IEEE80211_HW_AMPDU_AGGREGATION |
164                     IEEE80211_HW_TIMING_BEACON_ONLY |
165                     IEEE80211_HW_CONNECTION_MONITOR |
166                     IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
167                     IEEE80211_HW_SUPPORTS_STATIC_SMPS;
168
169         hw->queues = mvm->first_agg_queue;
170         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
171         hw->rate_control_algorithm = "iwl-mvm-rs";
172
173         /*
174          * Enable 11w if advertised by firmware and software crypto
175          * is not enabled (as the firmware will interpret some mgmt
176          * packets, so enabling it with software crypto isn't safe)
177          */
178         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
179             !iwlwifi_mod_params.sw_crypto)
180                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
181
182         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT) {
183                 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
184                 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
185                 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
186         }
187
188         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
189         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
190         hw->chanctx_data_size = sizeof(u16);
191
192         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
193                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
194                 BIT(NL80211_IFTYPE_AP) |
195                 BIT(NL80211_IFTYPE_P2P_GO) |
196                 BIT(NL80211_IFTYPE_P2P_DEVICE);
197
198         /* IBSS has bugs in older versions */
199         if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 8)
200                 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
201
202         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
203         hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
204                                        REGULATORY_DISABLE_BEACON_HINTS;
205
206         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
207         hw->wiphy->n_iface_combinations =
208                 ARRAY_SIZE(iwl_mvm_iface_combinations);
209
210         hw->wiphy->max_remain_on_channel_duration = 10000;
211         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
212
213         /* Extract MAC address */
214         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
215         hw->wiphy->addresses = mvm->addresses;
216         hw->wiphy->n_addresses = 1;
217
218         /* Extract additional MAC addresses if available */
219         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
220                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
221
222         for (i = 1; i < num_mac; i++) {
223                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
224                        ETH_ALEN);
225                 mvm->addresses[i].addr[5]++;
226                 hw->wiphy->n_addresses++;
227         }
228
229         iwl_mvm_reset_phy_ctxts(mvm);
230
231         hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
232
233         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
234
235         if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
236                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
237                         &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
238         if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
239                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
240                         &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
241
242         hw->wiphy->hw_version = mvm->trans->hw_id;
243
244         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
245                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
246         else
247                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
248
249         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SCHED_SCAN) {
250                 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
251                 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
252                 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
253                 /* we create the 802.11 header and zero length SSID IE. */
254                 hw->wiphy->max_sched_scan_ie_len =
255                                         SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
256         }
257
258         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
259                                NL80211_FEATURE_P2P_GO_OPPPS |
260                                NL80211_FEATURE_LOW_PRIORITY_SCAN;
261
262         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
263
264 #ifdef CONFIG_PM_SLEEP
265         if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
266             mvm->trans->ops->d3_suspend &&
267             mvm->trans->ops->d3_resume &&
268             device_can_wakeup(mvm->trans->dev)) {
269                 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
270                                     WIPHY_WOWLAN_DISCONNECT |
271                                     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
272                                     WIPHY_WOWLAN_RFKILL_RELEASE;
273                 if (!iwlwifi_mod_params.sw_crypto)
274                         mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
275                                              WIPHY_WOWLAN_GTK_REKEY_FAILURE |
276                                              WIPHY_WOWLAN_4WAY_HANDSHAKE;
277
278                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
279                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
280                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
281                 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
282                 hw->wiphy->wowlan = &mvm->wowlan;
283         }
284 #endif
285
286         ret = iwl_mvm_leds_init(mvm);
287         if (ret)
288                 return ret;
289
290         ret = ieee80211_register_hw(mvm->hw);
291         if (ret)
292                 iwl_mvm_leds_exit(mvm);
293
294         return ret;
295 }
296
297 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
298                            struct ieee80211_tx_control *control,
299                            struct sk_buff *skb)
300 {
301         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
302
303         if (iwl_mvm_is_radio_killed(mvm)) {
304                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
305                 goto drop;
306         }
307
308         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
309             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
310                 goto drop;
311
312         if (control->sta) {
313                 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
314                         goto drop;
315                 return;
316         }
317
318         if (iwl_mvm_tx_skb_non_sta(mvm, skb))
319                 goto drop;
320         return;
321  drop:
322         ieee80211_free_txskb(hw, skb);
323 }
324
325 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
326                                     struct ieee80211_vif *vif,
327                                     enum ieee80211_ampdu_mlme_action action,
328                                     struct ieee80211_sta *sta, u16 tid,
329                                     u16 *ssn, u8 buf_size)
330 {
331         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
332         int ret;
333
334         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
335                      sta->addr, tid, action);
336
337         if (!(mvm->nvm_data->sku_cap_11n_enable))
338                 return -EACCES;
339
340         mutex_lock(&mvm->mutex);
341
342         switch (action) {
343         case IEEE80211_AMPDU_RX_START:
344                 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
345                         ret = -EINVAL;
346                         break;
347                 }
348                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
349                 break;
350         case IEEE80211_AMPDU_RX_STOP:
351                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
352                 break;
353         case IEEE80211_AMPDU_TX_START:
354                 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
355                         ret = -EINVAL;
356                         break;
357                 }
358                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
359                 break;
360         case IEEE80211_AMPDU_TX_STOP_CONT:
361                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
362                 break;
363         case IEEE80211_AMPDU_TX_STOP_FLUSH:
364         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
365                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
366                 break;
367         case IEEE80211_AMPDU_TX_OPERATIONAL:
368                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
369                 break;
370         default:
371                 WARN_ON_ONCE(1);
372                 ret = -EINVAL;
373                 break;
374         }
375         mutex_unlock(&mvm->mutex);
376
377         return ret;
378 }
379
380 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
381                                      struct ieee80211_vif *vif)
382 {
383         struct iwl_mvm *mvm = data;
384         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
385
386         mvmvif->uploaded = false;
387         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
388
389         /* does this make sense at all? */
390         mvmvif->color++;
391
392         spin_lock_bh(&mvm->time_event_lock);
393         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
394         spin_unlock_bh(&mvm->time_event_lock);
395
396         mvmvif->phy_ctxt = NULL;
397 }
398
399 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
400 {
401         iwl_trans_stop_device(mvm->trans);
402         iwl_trans_stop_hw(mvm->trans, false);
403
404         mvm->scan_status = IWL_MVM_SCAN_NONE;
405
406         /* just in case one was running */
407         ieee80211_remain_on_channel_expired(mvm->hw);
408
409         ieee80211_iterate_active_interfaces_atomic(
410                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
411                 iwl_mvm_cleanup_iterator, mvm);
412
413         mvm->p2p_device_vif = NULL;
414
415         iwl_mvm_reset_phy_ctxts(mvm);
416         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
417         memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
418
419         ieee80211_wake_queues(mvm->hw);
420
421         mvm->vif_count = 0;
422         mvm->rx_ba_sessions = 0;
423 }
424
425 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
426 {
427         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
428         int ret;
429
430         mutex_lock(&mvm->mutex);
431
432         /* Clean up some internal and mac80211 state on restart */
433         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
434                 iwl_mvm_restart_cleanup(mvm);
435
436         ret = iwl_mvm_up(mvm);
437         mutex_unlock(&mvm->mutex);
438
439         return ret;
440 }
441
442 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
443 {
444         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
445         int ret;
446
447         mutex_lock(&mvm->mutex);
448
449         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
450         ret = iwl_mvm_update_quotas(mvm, NULL);
451         if (ret)
452                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
453                         ret);
454
455         mutex_unlock(&mvm->mutex);
456 }
457
458 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
459 {
460         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
461
462         flush_work(&mvm->async_handlers_wk);
463
464         mutex_lock(&mvm->mutex);
465         /* async_handlers_wk is now blocked */
466
467         /*
468          * The work item could be running or queued if the
469          * ROC time event stops just as we get here.
470          */
471         cancel_work_sync(&mvm->roc_done_wk);
472
473         iwl_trans_stop_device(mvm->trans);
474         iwl_trans_stop_hw(mvm->trans, false);
475
476         iwl_mvm_async_handlers_purge(mvm);
477         /* async_handlers_list is empty and will stay empty: HW is stopped */
478
479         /* the fw is stopped, the aux sta is dead: clean up driver state */
480         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
481
482         mutex_unlock(&mvm->mutex);
483
484         /*
485          * The worker might have been waiting for the mutex, let it run and
486          * discover that its list is now empty.
487          */
488         cancel_work_sync(&mvm->async_handlers_wk);
489 }
490
491 static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
492                                         struct ieee80211_vif *vif)
493 {
494         struct iwl_mvm *mvm = data;
495         int ret;
496
497         ret = iwl_mvm_power_disable(mvm, vif);
498         if (ret)
499                 IWL_ERR(mvm, "failed to disable power management\n");
500 }
501
502 static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
503                                           struct ieee80211_vif *vif)
504 {
505         struct iwl_mvm *mvm = data;
506
507         iwl_mvm_power_update_mode(mvm, vif);
508 }
509
510 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
511 {
512         u16 i;
513
514         lockdep_assert_held(&mvm->mutex);
515
516         for (i = 0; i < NUM_PHY_CTX; i++)
517                 if (!mvm->phy_ctxts[i].ref)
518                         return &mvm->phy_ctxts[i];
519
520         IWL_ERR(mvm, "No available PHY context\n");
521         return NULL;
522 }
523
524 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
525                                      struct ieee80211_vif *vif)
526 {
527         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
528         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
529         int ret;
530
531         /*
532          * Not much to do here. The stack will not allow interface
533          * types or combinations that we didn't advertise, so we
534          * don't really have to check the types.
535          */
536
537         mutex_lock(&mvm->mutex);
538
539         /* Allocate resources for the MAC context, and add it to the fw  */
540         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
541         if (ret)
542                 goto out_unlock;
543
544         /*
545          * TODO: remove this temporary code.
546          * Currently MVM FW supports power management only on single MAC.
547          * If new interface added, disable PM on existing interface.
548          * P2P device is a special case, since it is handled by FW similary to
549          * scan. If P2P deviced is added, PM remains enabled on existing
550          * interface.
551          * Note: the method below does not count the new interface being added
552          * at this moment.
553          */
554         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
555                 mvm->vif_count++;
556         if (mvm->vif_count > 1) {
557                 IWL_DEBUG_MAC80211(mvm,
558                                    "Disable power on existing interfaces\n");
559                 ieee80211_iterate_active_interfaces_atomic(
560                                             mvm->hw,
561                                             IEEE80211_IFACE_ITER_NORMAL,
562                                             iwl_mvm_pm_disable_iterator, mvm);
563         }
564
565         /*
566          * The AP binding flow can be done only after the beacon
567          * template is configured (which happens only in the mac80211
568          * start_ap() flow), and adding the broadcast station can happen
569          * only after the binding.
570          * In addition, since modifying the MAC before adding a bcast
571          * station is not allowed by the FW, delay the adding of MAC context to
572          * the point where we can also add the bcast station.
573          * In short: there's not much we can do at this point, other than
574          * allocating resources :)
575          */
576         if (vif->type == NL80211_IFTYPE_AP ||
577             vif->type == NL80211_IFTYPE_ADHOC) {
578                 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
579                 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
580                                                qmask);
581                 if (ret) {
582                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
583                         goto out_release;
584                 }
585
586                 iwl_mvm_vif_dbgfs_register(mvm, vif);
587                 goto out_unlock;
588         }
589
590         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
591         if (ret)
592                 goto out_release;
593
594         /*
595          * Update power state on the new interface. Admittedly, based on
596          * mac80211 logics this power update will disable power management
597          */
598         iwl_mvm_power_update_mode(mvm, vif);
599
600         /* beacon filtering */
601         ret = iwl_mvm_disable_beacon_filter(mvm, vif);
602         if (ret)
603                 goto out_remove_mac;
604
605         if (!mvm->bf_allowed_vif &&
606             vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
607             mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
608                 mvm->bf_allowed_vif = mvmvif;
609                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
610                                      IEEE80211_VIF_SUPPORTS_CQM_RSSI;
611         }
612
613         /*
614          * P2P_DEVICE interface does not have a channel context assigned to it,
615          * so a dedicated PHY context is allocated to it and the corresponding
616          * MAC context is bound to it at this stage.
617          */
618         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
619
620                 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
621                 if (!mvmvif->phy_ctxt) {
622                         ret = -ENOSPC;
623                         goto out_free_bf;
624                 }
625
626                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
627                 ret = iwl_mvm_binding_add_vif(mvm, vif);
628                 if (ret)
629                         goto out_unref_phy;
630
631                 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
632                 if (ret)
633                         goto out_unbind;
634
635                 /* Save a pointer to p2p device vif, so it can later be used to
636                  * update the p2p device MAC when a GO is started/stopped */
637                 mvm->p2p_device_vif = vif;
638         }
639
640         iwl_mvm_vif_dbgfs_register(mvm, vif);
641         goto out_unlock;
642
643  out_unbind:
644         iwl_mvm_binding_remove_vif(mvm, vif);
645  out_unref_phy:
646         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
647  out_free_bf:
648         if (mvm->bf_allowed_vif == mvmvif) {
649                 mvm->bf_allowed_vif = NULL;
650                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
651                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
652         }
653  out_remove_mac:
654         mvmvif->phy_ctxt = NULL;
655         iwl_mvm_mac_ctxt_remove(mvm, vif);
656  out_release:
657         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
658                 mvm->vif_count--;
659         ieee80211_iterate_active_interfaces(
660                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
661                 iwl_mvm_power_update_iterator, mvm);
662         iwl_mvm_mac_ctxt_release(mvm, vif);
663  out_unlock:
664         mutex_unlock(&mvm->mutex);
665
666         return ret;
667 }
668
669 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
670                                         struct ieee80211_vif *vif)
671 {
672         u32 tfd_msk = 0, ac;
673
674         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
675                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
676                         tfd_msk |= BIT(vif->hw_queue[ac]);
677
678         if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
679                 tfd_msk |= BIT(vif->cab_queue);
680
681         if (tfd_msk) {
682                 mutex_lock(&mvm->mutex);
683                 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
684                 mutex_unlock(&mvm->mutex);
685         }
686
687         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
688                 /*
689                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
690                  * We assume here that all the packets sent to the OFFCHANNEL
691                  * queue are sent in ROC session.
692                  */
693                 flush_work(&mvm->roc_done_wk);
694         } else {
695                 /*
696                  * By now, all the AC queues are empty. The AGG queues are
697                  * empty too. We already got all the Tx responses for all the
698                  * packets in the queues. The drain work can have been
699                  * triggered. Flush it.
700                  */
701                 flush_work(&mvm->sta_drained_wk);
702         }
703 }
704
705 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
706                                          struct ieee80211_vif *vif)
707 {
708         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
709         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
710
711         iwl_mvm_prepare_mac_removal(mvm, vif);
712
713         mutex_lock(&mvm->mutex);
714
715         if (mvm->bf_allowed_vif == mvmvif) {
716                 mvm->bf_allowed_vif = NULL;
717                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
718                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
719         }
720
721         iwl_mvm_vif_dbgfs_clean(mvm, vif);
722
723         /*
724          * For AP/GO interface, the tear down of the resources allocated to the
725          * interface is be handled as part of the stop_ap flow.
726          */
727         if (vif->type == NL80211_IFTYPE_AP ||
728             vif->type == NL80211_IFTYPE_ADHOC) {
729 #ifdef CONFIG_NL80211_TESTMODE
730                 if (vif == mvm->noa_vif) {
731                         mvm->noa_vif = NULL;
732                         mvm->noa_duration = 0;
733                 }
734 #endif
735                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
736                 goto out_release;
737         }
738
739         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
740                 mvm->p2p_device_vif = NULL;
741                 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
742                 iwl_mvm_binding_remove_vif(mvm, vif);
743                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
744                 mvmvif->phy_ctxt = NULL;
745         }
746
747         /*
748          * TODO: remove this temporary code.
749          * Currently MVM FW supports power management only on single MAC.
750          * Check if only one additional interface remains after removing
751          * current one. Update power mode on the remaining interface.
752          */
753         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
754                 mvm->vif_count--;
755         IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
756                            mvm->vif_count);
757         if (mvm->vif_count == 1) {
758                 ieee80211_iterate_active_interfaces(
759                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
760                                         iwl_mvm_power_update_iterator, mvm);
761         }
762
763         iwl_mvm_mac_ctxt_remove(mvm, vif);
764
765 out_release:
766         iwl_mvm_mac_ctxt_release(mvm, vif);
767         mutex_unlock(&mvm->mutex);
768 }
769
770 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
771                                 s8 tx_power)
772 {
773         /* FW is in charge of regulatory enforcement */
774         struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
775                 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
776                 .pwr_restriction = cpu_to_le16(tx_power),
777         };
778
779         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
780                                     sizeof(reduce_txpwr_cmd),
781                                     &reduce_txpwr_cmd);
782 }
783
784 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
785 {
786         return 0;
787 }
788
789 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
790                                      unsigned int changed_flags,
791                                      unsigned int *total_flags,
792                                      u64 multicast)
793 {
794         *total_flags = 0;
795 }
796
797 static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
798                                           struct ieee80211_vif *vif)
799 {
800         struct iwl_mcast_filter_cmd mcast_filter_cmd = {
801                 .pass_all = 1,
802         };
803
804         memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
805
806         return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
807                                     sizeof(mcast_filter_cmd),
808                                     &mcast_filter_cmd);
809 }
810
811 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
812                                              struct ieee80211_vif *vif,
813                                              struct ieee80211_bss_conf *bss_conf,
814                                              u32 changes)
815 {
816         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
817         int ret;
818
819         ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
820         if (ret)
821                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
822
823         if (changes & BSS_CHANGED_ASSOC) {
824                 if (bss_conf->assoc) {
825                         /* add quota for this interface */
826                         ret = iwl_mvm_update_quotas(mvm, vif);
827                         if (ret) {
828                                 IWL_ERR(mvm, "failed to update quotas\n");
829                                 return;
830                         }
831                         iwl_mvm_configure_mcast_filter(mvm, vif);
832
833                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
834                                      &mvm->status)) {
835                                 /*
836                                  * If we're restarting then the firmware will
837                                  * obviously have lost synchronisation with
838                                  * the AP. It will attempt to synchronise by
839                                  * itself, but we can make it more reliable by
840                                  * scheduling a session protection time event.
841                                  *
842                                  * The firmware needs to receive a beacon to
843                                  * catch up with synchronisation, use 110% of
844                                  * the beacon interval.
845                                  *
846                                  * Set a large maximum delay to allow for more
847                                  * than a single interface.
848                                  */
849                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
850                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
851                                                         5 * dur);
852                         }
853
854                         iwl_mvm_sf_update(mvm, vif, false);
855                 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
856                         /*
857                          * If update fails - SF might be running in associated
858                          * mode while disassociated - which is forbidden.
859                          */
860                         WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
861                                   "Failed to update SF upon disassociation\n");
862
863                         /* remove AP station now that the MAC is unassoc */
864                         ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
865                         if (ret)
866                                 IWL_ERR(mvm, "failed to remove AP station\n");
867                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
868                         /* remove quota for this interface */
869                         ret = iwl_mvm_update_quotas(mvm, NULL);
870                         if (ret)
871                                 IWL_ERR(mvm, "failed to update quotas\n");
872                 }
873
874                 /* reset rssi values */
875                 mvmvif->bf_data.ave_beacon_signal = 0;
876
877                 if (!(mvm->fw->ucode_capa.flags &
878                                         IWL_UCODE_TLV_FLAGS_PM_CMD_SUPPORT)) {
879                         /* Workaround for FW bug, otherwise FW disables device
880                          * power save upon disassociation
881                          */
882                         ret = iwl_mvm_power_update_mode(mvm, vif);
883                         if (ret)
884                                 IWL_ERR(mvm, "failed to update power mode\n");
885                 }
886                 iwl_mvm_bt_coex_vif_change(mvm);
887         } else if (changes & BSS_CHANGED_BEACON_INFO) {
888                 /*
889                  * We received a beacon _after_ association so
890                  * remove the session protection.
891                  */
892                 iwl_mvm_remove_time_event(mvm, mvmvif,
893                                           &mvmvif->time_event_data);
894         } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_QOS)) {
895                 ret = iwl_mvm_power_update_mode(mvm, vif);
896                 if (ret)
897                         IWL_ERR(mvm, "failed to update power mode\n");
898         }
899         if (changes & BSS_CHANGED_TXPOWER) {
900                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
901                                 bss_conf->txpower);
902                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
903         }
904
905         if (changes & BSS_CHANGED_CQM) {
906                 IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
907                 /* reset cqm events tracking */
908                 mvmvif->bf_data.last_cqm_event = 0;
909                 ret = iwl_mvm_update_beacon_filter(mvm, vif);
910                 if (ret)
911                         IWL_ERR(mvm, "failed to update CQM thresholds\n");
912         }
913 }
914
915 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
916                                  struct ieee80211_vif *vif)
917 {
918         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
919         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
920         int ret;
921
922         mutex_lock(&mvm->mutex);
923
924         /* Send the beacon template */
925         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
926         if (ret)
927                 goto out_unlock;
928
929         /* Add the mac context */
930         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
931         if (ret)
932                 goto out_unlock;
933
934         /* Perform the binding */
935         ret = iwl_mvm_binding_add_vif(mvm, vif);
936         if (ret)
937                 goto out_remove;
938
939         mvmvif->ap_ibss_active = true;
940
941         /* Send the bcast station. At this stage the TBTT and DTIM time events
942          * are added and applied to the scheduler */
943         ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
944         if (ret)
945                 goto out_unbind;
946
947         ret = iwl_mvm_update_quotas(mvm, vif);
948         if (ret)
949                 goto out_rm_bcast;
950
951         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
952         if (vif->p2p && mvm->p2p_device_vif)
953                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
954
955         iwl_mvm_bt_coex_vif_change(mvm);
956
957         mutex_unlock(&mvm->mutex);
958         return 0;
959
960 out_rm_bcast:
961         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
962 out_unbind:
963         iwl_mvm_binding_remove_vif(mvm, vif);
964 out_remove:
965         iwl_mvm_mac_ctxt_remove(mvm, vif);
966 out_unlock:
967         mutex_unlock(&mvm->mutex);
968         return ret;
969 }
970
971 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
972                                  struct ieee80211_vif *vif)
973 {
974         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
975         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
976
977         iwl_mvm_prepare_mac_removal(mvm, vif);
978
979         mutex_lock(&mvm->mutex);
980
981         mvmvif->ap_ibss_active = false;
982
983         iwl_mvm_bt_coex_vif_change(mvm);
984
985         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
986         if (vif->p2p && mvm->p2p_device_vif)
987                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
988
989         iwl_mvm_update_quotas(mvm, NULL);
990         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
991         iwl_mvm_binding_remove_vif(mvm, vif);
992         iwl_mvm_mac_ctxt_remove(mvm, vif);
993
994         mutex_unlock(&mvm->mutex);
995 }
996
997 static void
998 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
999                                  struct ieee80211_vif *vif,
1000                                  struct ieee80211_bss_conf *bss_conf,
1001                                  u32 changes)
1002 {
1003         enum ieee80211_bss_change ht_change = BSS_CHANGED_ERP_CTS_PROT |
1004                                               BSS_CHANGED_HT |
1005                                               BSS_CHANGED_BANDWIDTH;
1006         int ret;
1007
1008         if (changes & ht_change) {
1009                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1010                 if (ret)
1011                         IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1012         }
1013
1014         /* Need to send a new beacon template to the FW */
1015         if (changes & BSS_CHANGED_BEACON) {
1016                 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1017                         IWL_WARN(mvm, "Failed updating beacon data\n");
1018         }
1019 }
1020
1021 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1022                                      struct ieee80211_vif *vif,
1023                                      struct ieee80211_bss_conf *bss_conf,
1024                                      u32 changes)
1025 {
1026         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1027
1028         mutex_lock(&mvm->mutex);
1029
1030         switch (vif->type) {
1031         case NL80211_IFTYPE_STATION:
1032                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1033                 break;
1034         case NL80211_IFTYPE_AP:
1035         case NL80211_IFTYPE_ADHOC:
1036                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1037                 break;
1038         default:
1039                 /* shouldn't happen */
1040                 WARN_ON_ONCE(1);
1041         }
1042
1043         mutex_unlock(&mvm->mutex);
1044 }
1045
1046 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1047                                struct ieee80211_vif *vif,
1048                                struct cfg80211_scan_request *req)
1049 {
1050         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1051         int ret;
1052
1053         if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
1054                 return -EINVAL;
1055
1056         mutex_lock(&mvm->mutex);
1057
1058         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1059                 ret = iwl_mvm_scan_request(mvm, vif, req);
1060         else
1061                 ret = -EBUSY;
1062
1063         mutex_unlock(&mvm->mutex);
1064
1065         return ret;
1066 }
1067
1068 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1069                                        struct ieee80211_vif *vif)
1070 {
1071         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1072
1073         mutex_lock(&mvm->mutex);
1074
1075         iwl_mvm_cancel_scan(mvm);
1076
1077         mutex_unlock(&mvm->mutex);
1078 }
1079
1080 static void
1081 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1082                                   struct ieee80211_sta *sta, u16 tid,
1083                                   int num_frames,
1084                                   enum ieee80211_frame_release_type reason,
1085                                   bool more_data)
1086 {
1087         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1088
1089         /* TODO: how do we tell the fw to send frames for a specific TID */
1090
1091         /*
1092          * The fw will send EOSP notification when the last frame will be
1093          * transmitted.
1094          */
1095         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
1096 }
1097
1098 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1099                                    struct ieee80211_vif *vif,
1100                                    enum sta_notify_cmd cmd,
1101                                    struct ieee80211_sta *sta)
1102 {
1103         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1104         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1105
1106         switch (cmd) {
1107         case STA_NOTIFY_SLEEP:
1108                 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1109                         ieee80211_sta_block_awake(hw, sta, true);
1110                 /*
1111                  * The fw updates the STA to be asleep. Tx packets on the Tx
1112                  * queues to this station will not be transmitted. The fw will
1113                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1114                  */
1115                 break;
1116         case STA_NOTIFY_AWAKE:
1117                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1118                         break;
1119                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1120                 break;
1121         default:
1122                 break;
1123         }
1124 }
1125
1126 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1127                                  struct ieee80211_vif *vif,
1128                                  struct ieee80211_sta *sta,
1129                                  enum ieee80211_sta_state old_state,
1130                                  enum ieee80211_sta_state new_state)
1131 {
1132         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1133         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1134         int ret;
1135
1136         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1137                            sta->addr, old_state, new_state);
1138
1139         /* this would be a mac80211 bug ... but don't crash */
1140         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1141                 return -EINVAL;
1142
1143         /* if a STA is being removed, reuse its ID */
1144         flush_work(&mvm->sta_drained_wk);
1145
1146         mutex_lock(&mvm->mutex);
1147         if (old_state == IEEE80211_STA_NOTEXIST &&
1148             new_state == IEEE80211_STA_NONE) {
1149                 /*
1150                  * Firmware bug - it'll crash if the beacon interval is less
1151                  * than 16. We can't avoid connecting at all, so refuse the
1152                  * station state change, this will cause mac80211 to abandon
1153                  * attempts to connect to this AP, and eventually wpa_s will
1154                  * blacklist the AP...
1155                  */
1156                 if (vif->type == NL80211_IFTYPE_STATION &&
1157                     vif->bss_conf.beacon_int < 16) {
1158                         IWL_ERR(mvm,
1159                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1160                                 sta->addr, vif->bss_conf.beacon_int);
1161                         ret = -EINVAL;
1162                         goto out_unlock;
1163                 }
1164                 ret = iwl_mvm_add_sta(mvm, vif, sta);
1165         } else if (old_state == IEEE80211_STA_NONE &&
1166                    new_state == IEEE80211_STA_AUTH) {
1167                 ret = 0;
1168         } else if (old_state == IEEE80211_STA_AUTH &&
1169                    new_state == IEEE80211_STA_ASSOC) {
1170                 ret = iwl_mvm_update_sta(mvm, vif, sta);
1171                 if (ret == 0)
1172                         iwl_mvm_rs_rate_init(mvm, sta,
1173                                              mvmvif->phy_ctxt->channel->band,
1174                                              true);
1175         } else if (old_state == IEEE80211_STA_ASSOC &&
1176                    new_state == IEEE80211_STA_AUTHORIZED) {
1177                 /* enable beacon filtering */
1178                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
1179                 ret = 0;
1180         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1181                    new_state == IEEE80211_STA_ASSOC) {
1182                 /* disable beacon filtering */
1183                 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
1184                 ret = 0;
1185         } else if (old_state == IEEE80211_STA_ASSOC &&
1186                    new_state == IEEE80211_STA_AUTH) {
1187                 ret = 0;
1188         } else if (old_state == IEEE80211_STA_AUTH &&
1189                    new_state == IEEE80211_STA_NONE) {
1190                 ret = 0;
1191         } else if (old_state == IEEE80211_STA_NONE &&
1192                    new_state == IEEE80211_STA_NOTEXIST) {
1193                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1194         } else {
1195                 ret = -EIO;
1196         }
1197  out_unlock:
1198         mutex_unlock(&mvm->mutex);
1199
1200         return ret;
1201 }
1202
1203 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1204 {
1205         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1206
1207         mvm->rts_threshold = value;
1208
1209         return 0;
1210 }
1211
1212 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
1213                                   struct ieee80211_vif *vif,
1214                                   struct ieee80211_sta *sta, u32 changed)
1215 {
1216         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1217
1218         if (vif->type == NL80211_IFTYPE_STATION &&
1219             changed & IEEE80211_RC_NSS_CHANGED)
1220                 iwl_mvm_sf_update(mvm, vif, false);
1221 }
1222
1223 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1224                                struct ieee80211_vif *vif, u16 ac,
1225                                const struct ieee80211_tx_queue_params *params)
1226 {
1227         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1228         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1229
1230         mvmvif->queue_params[ac] = *params;
1231
1232         /*
1233          * No need to update right away, we'll get BSS_CHANGED_QOS
1234          * The exception is P2P_DEVICE interface which needs immediate update.
1235          */
1236         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1237                 int ret;
1238
1239                 mutex_lock(&mvm->mutex);
1240                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1241                 mutex_unlock(&mvm->mutex);
1242                 return ret;
1243         }
1244         return 0;
1245 }
1246
1247 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1248                                       struct ieee80211_vif *vif)
1249 {
1250         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1251         u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1252                            200 + vif->bss_conf.beacon_int);
1253         u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1254                                100 + vif->bss_conf.beacon_int);
1255
1256         if (WARN_ON_ONCE(vif->bss_conf.assoc))
1257                 return;
1258
1259         mutex_lock(&mvm->mutex);
1260         /* Try really hard to protect the session and hear a beacon */
1261         iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
1262         mutex_unlock(&mvm->mutex);
1263 }
1264
1265 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
1266                                         struct ieee80211_vif *vif,
1267                                         struct cfg80211_sched_scan_request *req,
1268                                         struct ieee80211_sched_scan_ies *ies)
1269 {
1270         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1271         int ret;
1272
1273         mutex_lock(&mvm->mutex);
1274
1275         if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
1276                 IWL_DEBUG_SCAN(mvm,
1277                                "SCHED SCAN request during internal scan - abort\n");
1278                 ret = -EBUSY;
1279                 goto out;
1280         }
1281
1282         mvm->scan_status = IWL_MVM_SCAN_SCHED;
1283
1284         ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1285         if (ret)
1286                 goto err;
1287
1288         ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1289         if (ret)
1290                 goto err;
1291
1292         ret = iwl_mvm_sched_scan_start(mvm, req);
1293         if (!ret)
1294                 goto out;
1295 err:
1296         mvm->scan_status = IWL_MVM_SCAN_NONE;
1297 out:
1298         mutex_unlock(&mvm->mutex);
1299         return ret;
1300 }
1301
1302 static void iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
1303                                         struct ieee80211_vif *vif)
1304 {
1305         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1306
1307         mutex_lock(&mvm->mutex);
1308         iwl_mvm_sched_scan_stop(mvm);
1309         mutex_unlock(&mvm->mutex);
1310 }
1311
1312 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1313                                enum set_key_cmd cmd,
1314                                struct ieee80211_vif *vif,
1315                                struct ieee80211_sta *sta,
1316                                struct ieee80211_key_conf *key)
1317 {
1318         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1319         int ret;
1320
1321         if (iwlwifi_mod_params.sw_crypto) {
1322                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1323                 return -EOPNOTSUPP;
1324         }
1325
1326         switch (key->cipher) {
1327         case WLAN_CIPHER_SUITE_TKIP:
1328                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1329                 /* fall-through */
1330         case WLAN_CIPHER_SUITE_CCMP:
1331                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1332                 break;
1333         case WLAN_CIPHER_SUITE_AES_CMAC:
1334                 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1335                 break;
1336         case WLAN_CIPHER_SUITE_WEP40:
1337         case WLAN_CIPHER_SUITE_WEP104:
1338                 /*
1339                  * Support for TX only, at least for now, so accept
1340                  * the key and do nothing else. Then mac80211 will
1341                  * pass it for TX but we don't have to use it for RX.
1342                  */
1343                 return 0;
1344         default:
1345                 return -EOPNOTSUPP;
1346         }
1347
1348         mutex_lock(&mvm->mutex);
1349
1350         switch (cmd) {
1351         case SET_KEY:
1352                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1353                      vif->type == NL80211_IFTYPE_AP) && !sta) {
1354                         /*
1355                          * GTK on AP interface is a TX-only key, return 0;
1356                          * on IBSS they're per-station and because we're lazy
1357                          * we don't support them for RX, so do the same.
1358                          */
1359                         ret = 0;
1360                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1361                         break;
1362                 }
1363
1364                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1365                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1366                 if (ret) {
1367                         IWL_WARN(mvm, "set key failed\n");
1368                         /*
1369                          * can't add key for RX, but we don't need it
1370                          * in the device for TX so still return 0
1371                          */
1372                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1373                         ret = 0;
1374                 }
1375
1376                 break;
1377         case DISABLE_KEY:
1378                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1379                         ret = 0;
1380                         break;
1381                 }
1382
1383                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1384                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1385                 break;
1386         default:
1387                 ret = -EINVAL;
1388         }
1389
1390         mutex_unlock(&mvm->mutex);
1391         return ret;
1392 }
1393
1394 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1395                                         struct ieee80211_vif *vif,
1396                                         struct ieee80211_key_conf *keyconf,
1397                                         struct ieee80211_sta *sta,
1398                                         u32 iv32, u16 *phase1key)
1399 {
1400         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1401
1402         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1403                 return;
1404
1405         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1406 }
1407
1408
1409 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1410                        struct ieee80211_vif *vif,
1411                        struct ieee80211_channel *channel,
1412                        int duration,
1413                        enum ieee80211_roc_type type)
1414 {
1415         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1416         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1417         struct cfg80211_chan_def chandef;
1418         struct iwl_mvm_phy_ctxt *phy_ctxt;
1419         int ret, i;
1420
1421         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1422                            duration, type);
1423
1424         if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1425                 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1426                 return -EINVAL;
1427         }
1428
1429         mutex_lock(&mvm->mutex);
1430
1431         for (i = 0; i < NUM_PHY_CTX; i++) {
1432                 phy_ctxt = &mvm->phy_ctxts[i];
1433                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1434                         continue;
1435
1436                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
1437                         /*
1438                          * Unbind the P2P_DEVICE from the current PHY context,
1439                          * and if the PHY context is not used remove it.
1440                          */
1441                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
1442                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1443                                 goto out_unlock;
1444
1445                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1446
1447                         /* Bind the P2P_DEVICE to the current PHY Context */
1448                         mvmvif->phy_ctxt = phy_ctxt;
1449
1450                         ret = iwl_mvm_binding_add_vif(mvm, vif);
1451                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1452                                 goto out_unlock;
1453
1454                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1455                         goto schedule_time_event;
1456                 }
1457         }
1458
1459         /* Need to update the PHY context only if the ROC channel changed */
1460         if (channel == mvmvif->phy_ctxt->channel)
1461                 goto schedule_time_event;
1462
1463         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1464
1465         /*
1466          * Change the PHY context configuration as it is currently referenced
1467          * only by the P2P Device MAC
1468          */
1469         if (mvmvif->phy_ctxt->ref == 1) {
1470                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
1471                                                &chandef, 1, 1);
1472                 if (ret)
1473                         goto out_unlock;
1474         } else {
1475                 /*
1476                  * The PHY context is shared with other MACs. Need to remove the
1477                  * P2P Device from the binding, allocate an new PHY context and
1478                  * create a new binding
1479                  */
1480                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1481                 if (!phy_ctxt) {
1482                         ret = -ENOSPC;
1483                         goto out_unlock;
1484                 }
1485
1486                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
1487                                                1, 1);
1488                 if (ret) {
1489                         IWL_ERR(mvm, "Failed to change PHY context\n");
1490                         goto out_unlock;
1491                 }
1492
1493                 /* Unbind the P2P_DEVICE from the current PHY context */
1494                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1495                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1496                         goto out_unlock;
1497
1498                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1499
1500                 /* Bind the P2P_DEVICE to the new allocated PHY context */
1501                 mvmvif->phy_ctxt = phy_ctxt;
1502
1503                 ret = iwl_mvm_binding_add_vif(mvm, vif);
1504                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1505                         goto out_unlock;
1506
1507                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1508         }
1509
1510 schedule_time_event:
1511         /* Schedule the time events */
1512         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
1513
1514 out_unlock:
1515         mutex_unlock(&mvm->mutex);
1516         IWL_DEBUG_MAC80211(mvm, "leave\n");
1517         return ret;
1518 }
1519
1520 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1521 {
1522         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1523
1524         IWL_DEBUG_MAC80211(mvm, "enter\n");
1525
1526         mutex_lock(&mvm->mutex);
1527         iwl_mvm_stop_p2p_roc(mvm);
1528         mutex_unlock(&mvm->mutex);
1529
1530         IWL_DEBUG_MAC80211(mvm, "leave\n");
1531         return 0;
1532 }
1533
1534 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1535                                struct ieee80211_chanctx_conf *ctx)
1536 {
1537         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1538         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1539         struct iwl_mvm_phy_ctxt *phy_ctxt;
1540         int ret;
1541
1542         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
1543
1544         mutex_lock(&mvm->mutex);
1545         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1546         if (!phy_ctxt) {
1547                 ret = -ENOSPC;
1548                 goto out;
1549         }
1550
1551         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1552                                        ctx->rx_chains_static,
1553                                        ctx->rx_chains_dynamic);
1554         if (ret) {
1555                 IWL_ERR(mvm, "Failed to add PHY context\n");
1556                 goto out;
1557         }
1558
1559         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
1560         *phy_ctxt_id = phy_ctxt->id;
1561 out:
1562         mutex_unlock(&mvm->mutex);
1563         return ret;
1564 }
1565
1566 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1567                                    struct ieee80211_chanctx_conf *ctx)
1568 {
1569         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1570         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1571         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1572
1573         mutex_lock(&mvm->mutex);
1574         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
1575         mutex_unlock(&mvm->mutex);
1576 }
1577
1578 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1579                                    struct ieee80211_chanctx_conf *ctx,
1580                                    u32 changed)
1581 {
1582         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1583         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1584         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1585
1586         if (WARN_ONCE((phy_ctxt->ref > 1) &&
1587                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
1588                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1589                                    IEEE80211_CHANCTX_CHANGE_RADAR)),
1590                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
1591                       phy_ctxt->ref, changed))
1592                 return;
1593
1594         mutex_lock(&mvm->mutex);
1595         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1596                                  ctx->rx_chains_static,
1597                                  ctx->rx_chains_dynamic);
1598         iwl_mvm_bt_coex_vif_change(mvm);
1599         mutex_unlock(&mvm->mutex);
1600 }
1601
1602 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1603                                       struct ieee80211_vif *vif,
1604                                       struct ieee80211_chanctx_conf *ctx)
1605 {
1606         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1607         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1608         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1609         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1610         int ret;
1611
1612         mutex_lock(&mvm->mutex);
1613
1614         mvmvif->phy_ctxt = phy_ctxt;
1615
1616         switch (vif->type) {
1617         case NL80211_IFTYPE_AP:
1618         case NL80211_IFTYPE_ADHOC:
1619                 /*
1620                  * The AP binding flow is handled as part of the start_ap flow
1621                  * (in bss_info_changed), similarly for IBSS.
1622                  */
1623                 ret = 0;
1624                 goto out_unlock;
1625         case NL80211_IFTYPE_STATION:
1626         case NL80211_IFTYPE_MONITOR:
1627                 break;
1628         default:
1629                 ret = -EINVAL;
1630                 goto out_unlock;
1631         }
1632
1633         ret = iwl_mvm_binding_add_vif(mvm, vif);
1634         if (ret)
1635                 goto out_unlock;
1636
1637         /*
1638          * Setting the quota at this stage is only required for monitor
1639          * interfaces. For the other types, the bss_info changed flow
1640          * will handle quota settings.
1641          */
1642         if (vif->type == NL80211_IFTYPE_MONITOR) {
1643                 mvmvif->monitor_active = true;
1644                 ret = iwl_mvm_update_quotas(mvm, vif);
1645                 if (ret)
1646                         goto out_remove_binding;
1647         }
1648
1649         goto out_unlock;
1650
1651  out_remove_binding:
1652         iwl_mvm_binding_remove_vif(mvm, vif);
1653  out_unlock:
1654         mutex_unlock(&mvm->mutex);
1655         if (ret)
1656                 mvmvif->phy_ctxt = NULL;
1657         return ret;
1658 }
1659
1660 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1661                                          struct ieee80211_vif *vif,
1662                                          struct ieee80211_chanctx_conf *ctx)
1663 {
1664         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1665         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1666
1667         mutex_lock(&mvm->mutex);
1668
1669         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1670
1671         switch (vif->type) {
1672         case NL80211_IFTYPE_AP:
1673         case NL80211_IFTYPE_ADHOC:
1674                 goto out_unlock;
1675         case NL80211_IFTYPE_MONITOR:
1676                 mvmvif->monitor_active = false;
1677                 iwl_mvm_update_quotas(mvm, NULL);
1678                 break;
1679         default:
1680                 break;
1681         }
1682
1683         iwl_mvm_binding_remove_vif(mvm, vif);
1684 out_unlock:
1685         mvmvif->phy_ctxt = NULL;
1686         mutex_unlock(&mvm->mutex);
1687 }
1688
1689 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1690                            struct ieee80211_sta *sta,
1691                            bool set)
1692 {
1693         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1694         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1695
1696         if (!mvm_sta || !mvm_sta->vif) {
1697                 IWL_ERR(mvm, "Station is not associated to a vif\n");
1698                 return -EINVAL;
1699         }
1700
1701         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1702 }
1703
1704 #ifdef CONFIG_NL80211_TESTMODE
1705 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
1706         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
1707         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
1708         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
1709 };
1710
1711 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
1712                                       struct ieee80211_vif *vif,
1713                                       void *data, int len)
1714 {
1715         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
1716         int err;
1717         u32 noa_duration;
1718
1719         err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
1720         if (err)
1721                 return err;
1722
1723         if (!tb[IWL_MVM_TM_ATTR_CMD])
1724                 return -EINVAL;
1725
1726         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
1727         case IWL_MVM_TM_CMD_SET_NOA:
1728                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
1729                     !vif->bss_conf.enable_beacon ||
1730                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
1731                         return -EINVAL;
1732
1733                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
1734                 if (noa_duration >= vif->bss_conf.beacon_int)
1735                         return -EINVAL;
1736
1737                 mvm->noa_duration = noa_duration;
1738                 mvm->noa_vif = vif;
1739
1740                 return iwl_mvm_update_quotas(mvm, NULL);
1741         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
1742                 /* must be associated client vif - ignore authorized */
1743                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
1744                     !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
1745                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
1746                         return -EINVAL;
1747
1748                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
1749                         return iwl_mvm_enable_beacon_filter(mvm, vif);
1750                 return iwl_mvm_disable_beacon_filter(mvm, vif);
1751         }
1752
1753         return -EOPNOTSUPP;
1754 }
1755
1756 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
1757                                     struct ieee80211_vif *vif,
1758                                     void *data, int len)
1759 {
1760         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1761         int err;
1762
1763         mutex_lock(&mvm->mutex);
1764         err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
1765         mutex_unlock(&mvm->mutex);
1766
1767         return err;
1768 }
1769 #endif
1770
1771 struct ieee80211_ops iwl_mvm_hw_ops = {
1772         .tx = iwl_mvm_mac_tx,
1773         .ampdu_action = iwl_mvm_mac_ampdu_action,
1774         .start = iwl_mvm_mac_start,
1775         .restart_complete = iwl_mvm_mac_restart_complete,
1776         .stop = iwl_mvm_mac_stop,
1777         .add_interface = iwl_mvm_mac_add_interface,
1778         .remove_interface = iwl_mvm_mac_remove_interface,
1779         .config = iwl_mvm_mac_config,
1780         .configure_filter = iwl_mvm_configure_filter,
1781         .bss_info_changed = iwl_mvm_bss_info_changed,
1782         .hw_scan = iwl_mvm_mac_hw_scan,
1783         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1784         .sta_state = iwl_mvm_mac_sta_state,
1785         .sta_notify = iwl_mvm_mac_sta_notify,
1786         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1787         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1788         .sta_rc_update = iwl_mvm_sta_rc_update,
1789         .conf_tx = iwl_mvm_mac_conf_tx,
1790         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1791         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
1792         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
1793         .set_key = iwl_mvm_mac_set_key,
1794         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1795         .remain_on_channel = iwl_mvm_roc,
1796         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
1797         .add_chanctx = iwl_mvm_add_chanctx,
1798         .remove_chanctx = iwl_mvm_remove_chanctx,
1799         .change_chanctx = iwl_mvm_change_chanctx,
1800         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1801         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1802
1803         .start_ap = iwl_mvm_start_ap_ibss,
1804         .stop_ap = iwl_mvm_stop_ap_ibss,
1805         .join_ibss = iwl_mvm_start_ap_ibss,
1806         .leave_ibss = iwl_mvm_stop_ap_ibss,
1807
1808         .set_tim = iwl_mvm_set_tim,
1809
1810         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
1811
1812 #ifdef CONFIG_PM_SLEEP
1813         /* look at d3.c */
1814         .suspend = iwl_mvm_suspend,
1815         .resume = iwl_mvm_resume,
1816         .set_wakeup = iwl_mvm_set_wakeup,
1817         .set_rekey_data = iwl_mvm_set_rekey_data,
1818 #if IS_ENABLED(CONFIG_IPV6)
1819         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1820 #endif
1821         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1822 #endif
1823 };