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