Merge tag 'v3.10.13' into lsk/v3.10/topic/kvm
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / mvm / mac80211.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 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
81 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
82         {
83                 .max = 1,
84                 .types = BIT(NL80211_IFTYPE_STATION) |
85                         BIT(NL80211_IFTYPE_AP),
86         },
87         {
88                 .max = 1,
89                 .types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
90                         BIT(NL80211_IFTYPE_P2P_GO),
91         },
92         {
93                 .max = 1,
94                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
95         },
96 };
97
98 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
99         {
100                 .num_different_channels = 1,
101                 .max_interfaces = 3,
102                 .limits = iwl_mvm_limits,
103                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
104         },
105 };
106
107 #ifdef CONFIG_PM_SLEEP
108 static const struct nl80211_wowlan_tcp_data_token_feature
109 iwl_mvm_wowlan_tcp_token_feature = {
110         .min_len = 0,
111         .max_len = 255,
112         .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
113 };
114
115 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
116         .tok = &iwl_mvm_wowlan_tcp_token_feature,
117         .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
118                             sizeof(struct ethhdr) -
119                             sizeof(struct iphdr) -
120                             sizeof(struct tcphdr),
121         .data_interval_max = 65535, /* __le16 in API */
122         .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
123                             sizeof(struct ethhdr) -
124                             sizeof(struct iphdr) -
125                             sizeof(struct tcphdr),
126         .seq = true,
127 };
128 #endif
129
130 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
131 {
132         struct ieee80211_hw *hw = mvm->hw;
133         int num_mac, ret, i;
134
135         /* Tell mac80211 our characteristics */
136         hw->flags = IEEE80211_HW_SIGNAL_DBM |
137                     IEEE80211_HW_SPECTRUM_MGMT |
138                     IEEE80211_HW_REPORTS_TX_ACK_STATUS |
139                     IEEE80211_HW_QUEUE_CONTROL |
140                     IEEE80211_HW_WANT_MONITOR_VIF |
141                     IEEE80211_HW_SUPPORTS_PS |
142                     IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
143                     IEEE80211_HW_AMPDU_AGGREGATION |
144                     IEEE80211_HW_TIMING_BEACON_ONLY;
145
146         hw->queues = IWL_MVM_FIRST_AGG_QUEUE;
147         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
148         hw->rate_control_algorithm = "iwl-mvm-rs";
149
150         /*
151          * Enable 11w if advertised by firmware and software crypto
152          * is not enabled (as the firmware will interpret some mgmt
153          * packets, so enabling it with software crypto isn't safe)
154          */
155         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
156             !iwlwifi_mod_params.sw_crypto)
157                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
158
159         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
160         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
161         hw->chanctx_data_size = sizeof(struct iwl_mvm_phy_ctxt);
162
163         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
164                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
165                 BIT(NL80211_IFTYPE_AP) |
166                 BIT(NL80211_IFTYPE_P2P_GO) |
167                 BIT(NL80211_IFTYPE_P2P_DEVICE);
168
169         hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
170                             WIPHY_FLAG_DISABLE_BEACON_HINTS |
171                             WIPHY_FLAG_IBSS_RSN;
172
173         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
174         hw->wiphy->n_iface_combinations =
175                 ARRAY_SIZE(iwl_mvm_iface_combinations);
176
177         hw->wiphy->max_remain_on_channel_duration = 10000;
178         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
179
180         /* Extract MAC address */
181         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
182         hw->wiphy->addresses = mvm->addresses;
183         hw->wiphy->n_addresses = 1;
184
185         /* Extract additional MAC addresses if available */
186         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
187                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
188
189         for (i = 1; i < num_mac; i++) {
190                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
191                        ETH_ALEN);
192                 mvm->addresses[i].addr[5]++;
193                 hw->wiphy->n_addresses++;
194         }
195
196         /* we create the 802.11 header and a max-length SSID element */
197         hw->wiphy->max_scan_ie_len =
198                 mvm->fw->ucode_capa.max_probe_length - 24 - 34;
199         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
200
201         if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
202                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
203                         &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
204         if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
205                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
206                         &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
207
208         hw->wiphy->hw_version = mvm->trans->hw_id;
209
210         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
211                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
212         else
213                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
214
215         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
216                                NL80211_FEATURE_P2P_GO_OPPPS;
217
218         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
219
220 #ifdef CONFIG_PM_SLEEP
221         if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
222             mvm->trans->ops->d3_suspend &&
223             mvm->trans->ops->d3_resume &&
224             device_can_wakeup(mvm->trans->dev)) {
225                 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
226                                           WIPHY_WOWLAN_DISCONNECT |
227                                           WIPHY_WOWLAN_EAP_IDENTITY_REQ |
228                                           WIPHY_WOWLAN_RFKILL_RELEASE;
229                 if (!iwlwifi_mod_params.sw_crypto)
230                         hw->wiphy->wowlan.flags |=
231                                 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
232                                 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
233                                 WIPHY_WOWLAN_4WAY_HANDSHAKE;
234
235                 hw->wiphy->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
236                 hw->wiphy->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
237                 hw->wiphy->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
238                 hw->wiphy->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
239         }
240 #endif
241
242         ret = iwl_mvm_leds_init(mvm);
243         if (ret)
244                 return ret;
245
246         ret = ieee80211_register_hw(mvm->hw);
247         if (ret)
248                 iwl_mvm_leds_exit(mvm);
249
250         return ret;
251 }
252
253 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
254                            struct ieee80211_tx_control *control,
255                            struct sk_buff *skb)
256 {
257         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
258
259         if (test_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status)) {
260                 IWL_DEBUG_DROP(mvm, "Dropping - RF KILL\n");
261                 goto drop;
262         }
263
264         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
265             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
266                 goto drop;
267
268         if (control->sta) {
269                 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
270                         goto drop;
271                 return;
272         }
273
274         if (iwl_mvm_tx_skb_non_sta(mvm, skb))
275                 goto drop;
276         return;
277  drop:
278         ieee80211_free_txskb(hw, skb);
279 }
280
281 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
282                                     struct ieee80211_vif *vif,
283                                     enum ieee80211_ampdu_mlme_action action,
284                                     struct ieee80211_sta *sta, u16 tid,
285                                     u16 *ssn, u8 buf_size)
286 {
287         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
288         int ret;
289
290         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
291                      sta->addr, tid, action);
292
293         if (!(mvm->nvm_data->sku_cap_11n_enable))
294                 return -EACCES;
295
296         mutex_lock(&mvm->mutex);
297
298         switch (action) {
299         case IEEE80211_AMPDU_RX_START:
300                 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
301                         ret = -EINVAL;
302                         break;
303                 }
304                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
305                 break;
306         case IEEE80211_AMPDU_RX_STOP:
307                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
308                 break;
309         case IEEE80211_AMPDU_TX_START:
310                 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
311                         ret = -EINVAL;
312                         break;
313                 }
314                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
315                 break;
316         case IEEE80211_AMPDU_TX_STOP_CONT:
317                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
318                 break;
319         case IEEE80211_AMPDU_TX_STOP_FLUSH:
320         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
321                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
322                 break;
323         case IEEE80211_AMPDU_TX_OPERATIONAL:
324                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
325                 break;
326         default:
327                 WARN_ON_ONCE(1);
328                 ret = -EINVAL;
329                 break;
330         }
331         mutex_unlock(&mvm->mutex);
332
333         return ret;
334 }
335
336 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
337                                      struct ieee80211_vif *vif)
338 {
339         struct iwl_mvm *mvm = data;
340         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
341
342         mvmvif->uploaded = false;
343         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
344
345         /* does this make sense at all? */
346         mvmvif->color++;
347
348         spin_lock_bh(&mvm->time_event_lock);
349         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
350         spin_unlock_bh(&mvm->time_event_lock);
351
352         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
353                 mvmvif->phy_ctxt = NULL;
354 }
355
356 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
357 {
358         iwl_trans_stop_device(mvm->trans);
359         iwl_trans_stop_hw(mvm->trans, false);
360
361         mvm->scan_status = IWL_MVM_SCAN_NONE;
362
363         /* just in case one was running */
364         ieee80211_remain_on_channel_expired(mvm->hw);
365
366         ieee80211_iterate_active_interfaces_atomic(
367                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
368                 iwl_mvm_cleanup_iterator, mvm);
369
370         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
371         memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
372
373         ieee80211_wake_queues(mvm->hw);
374
375         mvm->vif_count = 0;
376 }
377
378 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
379 {
380         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
381         int ret;
382
383         mutex_lock(&mvm->mutex);
384
385         /* Clean up some internal and mac80211 state on restart */
386         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
387                 iwl_mvm_restart_cleanup(mvm);
388
389         ret = iwl_mvm_up(mvm);
390         mutex_unlock(&mvm->mutex);
391
392         return ret;
393 }
394
395 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
396 {
397         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
398         int ret;
399
400         mutex_lock(&mvm->mutex);
401
402         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
403         ret = iwl_mvm_update_quotas(mvm, NULL);
404         if (ret)
405                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
406                         ret);
407
408         mutex_unlock(&mvm->mutex);
409 }
410
411 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
412 {
413         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
414
415         flush_work(&mvm->async_handlers_wk);
416
417         mutex_lock(&mvm->mutex);
418         /* async_handlers_wk is now blocked */
419
420         /*
421          * The work item could be running or queued if the
422          * ROC time event stops just as we get here.
423          */
424         cancel_work_sync(&mvm->roc_done_wk);
425
426         iwl_trans_stop_device(mvm->trans);
427         iwl_trans_stop_hw(mvm->trans, false);
428
429         iwl_mvm_async_handlers_purge(mvm);
430         /* async_handlers_list is empty and will stay empty: HW is stopped */
431
432         /* the fw is stopped, the aux sta is dead: clean up driver state */
433         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
434
435         mutex_unlock(&mvm->mutex);
436
437         /*
438          * The worker might have been waiting for the mutex, let it run and
439          * discover that its list is now empty.
440          */
441         cancel_work_sync(&mvm->async_handlers_wk);
442 }
443
444 static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
445                                         struct ieee80211_vif *vif)
446 {
447         struct iwl_mvm *mvm = data;
448         int ret;
449
450         ret = iwl_mvm_power_disable(mvm, vif);
451         if (ret)
452                 IWL_ERR(mvm, "failed to disable power management\n");
453 }
454
455 static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
456                                           struct ieee80211_vif *vif)
457 {
458         struct iwl_mvm *mvm = data;
459
460         iwl_mvm_power_update_mode(mvm, vif);
461 }
462
463 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
464                                      struct ieee80211_vif *vif)
465 {
466         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
467         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
468         int ret;
469
470         /*
471          * Not much to do here. The stack will not allow interface
472          * types or combinations that we didn't advertise, so we
473          * don't really have to check the types.
474          */
475
476         mutex_lock(&mvm->mutex);
477
478         /* Allocate resources for the MAC context, and add it the the fw  */
479         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
480         if (ret)
481                 goto out_unlock;
482
483         /*
484          * The AP binding flow can be done only after the beacon
485          * template is configured (which happens only in the mac80211
486          * start_ap() flow), and adding the broadcast station can happen
487          * only after the binding.
488          * In addition, since modifying the MAC before adding a bcast
489          * station is not allowed by the FW, delay the adding of MAC context to
490          * the point where we can also add the bcast station.
491          * In short: there's not much we can do at this point, other than
492          * allocating resources :)
493          */
494         if (vif->type == NL80211_IFTYPE_AP) {
495                 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
496                 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
497                                                qmask);
498                 if (ret) {
499                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
500                         goto out_release;
501                 }
502
503                 goto out_unlock;
504         }
505
506         /*
507          * TODO: remove this temporary code.
508          * Currently MVM FW supports power management only on single MAC.
509          * If new interface added, disable PM on existing interface.
510          * P2P device is a special case, since it is handled by FW similary to
511          * scan. If P2P deviced is added, PM remains enabled on existing
512          * interface.
513          * Note: the method below does not count the new interface being added
514          * at this moment.
515          */
516         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
517                 mvm->vif_count++;
518         if (mvm->vif_count > 1) {
519                 IWL_DEBUG_MAC80211(mvm,
520                                    "Disable power on existing interfaces\n");
521                 ieee80211_iterate_active_interfaces_atomic(
522                                             mvm->hw,
523                                             IEEE80211_IFACE_ITER_NORMAL,
524                                             iwl_mvm_pm_disable_iterator, mvm);
525         }
526
527         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
528         if (ret)
529                 goto out_release;
530
531         /*
532          * Update power state on the new interface. Admittedly, based on
533          * mac80211 logics this power update will disable power management
534          */
535         iwl_mvm_power_update_mode(mvm, vif);
536
537         /*
538          * P2P_DEVICE interface does not have a channel context assigned to it,
539          * so a dedicated PHY context is allocated to it and the corresponding
540          * MAC context is bound to it at this stage.
541          */
542         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
543                 struct ieee80211_channel *chan;
544                 struct cfg80211_chan_def chandef;
545
546                 mvmvif->phy_ctxt = &mvm->phy_ctxt_roc;
547
548                 /*
549                  * The channel used here isn't relevant as it's
550                  * going to be overwritten as part of the ROC flow.
551                  * For now use the first channel we have.
552                  */
553                 chan = &mvm->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels[0];
554                 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
555                 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt,
556                                            &chandef, 1, 1);
557                 if (ret)
558                         goto out_remove_mac;
559
560                 ret = iwl_mvm_binding_add_vif(mvm, vif);
561                 if (ret)
562                         goto out_remove_phy;
563
564                 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
565                 if (ret)
566                         goto out_unbind;
567
568                 /* Save a pointer to p2p device vif, so it can later be used to
569                  * update the p2p device MAC when a GO is started/stopped */
570                 mvm->p2p_device_vif = vif;
571         }
572
573         iwl_mvm_vif_dbgfs_register(mvm, vif);
574         goto out_unlock;
575
576  out_unbind:
577         iwl_mvm_binding_remove_vif(mvm, vif);
578  out_remove_phy:
579         iwl_mvm_phy_ctxt_remove(mvm, mvmvif->phy_ctxt);
580  out_remove_mac:
581         mvmvif->phy_ctxt = NULL;
582         iwl_mvm_mac_ctxt_remove(mvm, vif);
583  out_release:
584         /*
585          * TODO: remove this temporary code.
586          * Currently MVM FW supports power management only on single MAC.
587          * Check if only one additional interface remains after releasing
588          * current one. Update power mode on the remaining interface.
589          */
590         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
591                 mvm->vif_count--;
592         IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
593                            mvm->vif_count);
594         if (mvm->vif_count == 1) {
595                 ieee80211_iterate_active_interfaces(
596                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
597                                         iwl_mvm_power_update_iterator, mvm);
598         }
599         iwl_mvm_mac_ctxt_release(mvm, vif);
600  out_unlock:
601         mutex_unlock(&mvm->mutex);
602
603         return ret;
604 }
605
606 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
607                                         struct ieee80211_vif *vif)
608 {
609         u32 tfd_msk = 0, ac;
610
611         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
612                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
613                         tfd_msk |= BIT(vif->hw_queue[ac]);
614
615         if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
616                 tfd_msk |= BIT(vif->cab_queue);
617
618         if (tfd_msk) {
619                 mutex_lock(&mvm->mutex);
620                 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
621                 mutex_unlock(&mvm->mutex);
622         }
623
624         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
625                 /*
626                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
627                  * We assume here that all the packets sent to the OFFCHANNEL
628                  * queue are sent in ROC session.
629                  */
630                 flush_work(&mvm->roc_done_wk);
631         } else {
632                 /*
633                  * By now, all the AC queues are empty. The AGG queues are
634                  * empty too. We already got all the Tx responses for all the
635                  * packets in the queues. The drain work can have been
636                  * triggered. Flush it. This work item takes the mutex, so kill
637                  * it before we take it.
638                  */
639                 flush_work(&mvm->sta_drained_wk);
640         }
641 }
642
643 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
644                                          struct ieee80211_vif *vif)
645 {
646         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
647         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
648
649         iwl_mvm_prepare_mac_removal(mvm, vif);
650
651         mutex_lock(&mvm->mutex);
652
653         iwl_mvm_vif_dbgfs_clean(mvm, vif);
654
655         /*
656          * For AP/GO interface, the tear down of the resources allocated to the
657          * interface is be handled as part of the stop_ap flow.
658          */
659         if (vif->type == NL80211_IFTYPE_AP) {
660                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
661                 goto out_release;
662         }
663
664         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
665                 mvm->p2p_device_vif = NULL;
666                 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
667                 iwl_mvm_binding_remove_vif(mvm, vif);
668                 iwl_mvm_phy_ctxt_remove(mvm, mvmvif->phy_ctxt);
669                 mvmvif->phy_ctxt = NULL;
670         }
671
672         /*
673          * TODO: remove this temporary code.
674          * Currently MVM FW supports power management only on single MAC.
675          * Check if only one additional interface remains after removing
676          * current one. Update power mode on the remaining interface.
677          */
678         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
679                 mvm->vif_count--;
680         IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
681                            mvm->vif_count);
682         if (mvm->vif_count == 1) {
683                 ieee80211_iterate_active_interfaces(
684                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
685                                         iwl_mvm_power_update_iterator, mvm);
686         }
687
688         iwl_mvm_mac_ctxt_remove(mvm, vif);
689
690 out_release:
691         iwl_mvm_mac_ctxt_release(mvm, vif);
692         mutex_unlock(&mvm->mutex);
693 }
694
695 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
696 {
697         return 0;
698 }
699
700 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
701                                      unsigned int changed_flags,
702                                      unsigned int *total_flags,
703                                      u64 multicast)
704 {
705         *total_flags = 0;
706 }
707
708 static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
709                                           struct ieee80211_vif *vif)
710 {
711         struct iwl_mcast_filter_cmd mcast_filter_cmd = {
712                 .pass_all = 1,
713         };
714
715         memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
716
717         return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
718                                     sizeof(mcast_filter_cmd),
719                                     &mcast_filter_cmd);
720 }
721
722 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
723                                              struct ieee80211_vif *vif,
724                                              struct ieee80211_bss_conf *bss_conf,
725                                              u32 changes)
726 {
727         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
728         int ret;
729
730         ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
731         if (ret)
732                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
733
734         if (changes & BSS_CHANGED_ASSOC) {
735                 if (bss_conf->assoc) {
736                         /* add quota for this interface */
737                         ret = iwl_mvm_update_quotas(mvm, vif);
738                         if (ret) {
739                                 IWL_ERR(mvm, "failed to update quotas\n");
740                                 return;
741                         }
742                         iwl_mvm_bt_coex_vif_assoc(mvm, vif);
743                         iwl_mvm_configure_mcast_filter(mvm, vif);
744                 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
745                         /* remove AP station now that the MAC is unassoc */
746                         ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
747                         if (ret)
748                                 IWL_ERR(mvm, "failed to remove AP station\n");
749                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
750                         /* remove quota for this interface */
751                         ret = iwl_mvm_update_quotas(mvm, NULL);
752                         if (ret)
753                                 IWL_ERR(mvm, "failed to update quotas\n");
754                 }
755         } else if (changes & BSS_CHANGED_DTIM_PERIOD) {
756                 /*
757                  * We received a beacon _after_ association so
758                  * remove the session protection.
759                  */
760                 iwl_mvm_remove_time_event(mvm, mvmvif,
761                                           &mvmvif->time_event_data);
762         } else if (changes & BSS_CHANGED_PS) {
763                 /*
764                  * TODO: remove this temporary code.
765                  * Currently MVM FW supports power management only on single
766                  * MAC. Avoid power mode update if more than one interface
767                  * is active.
768                  */
769                 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
770                                    mvm->vif_count);
771                 if (mvm->vif_count == 1) {
772                         ret = iwl_mvm_power_update_mode(mvm, vif);
773                         if (ret)
774                                 IWL_ERR(mvm, "failed to update power mode\n");
775                 }
776         }
777 }
778
779 static int iwl_mvm_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
780 {
781         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
782         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
783         int ret;
784
785         mutex_lock(&mvm->mutex);
786
787         /* Send the beacon template */
788         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
789         if (ret)
790                 goto out_unlock;
791
792         /* Add the mac context */
793         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
794         if (ret)
795                 goto out_unlock;
796
797         /* Perform the binding */
798         ret = iwl_mvm_binding_add_vif(mvm, vif);
799         if (ret)
800                 goto out_remove;
801
802         mvmvif->ap_active = true;
803
804         /* Send the bcast station. At this stage the TBTT and DTIM time events
805          * are added and applied to the scheduler */
806         ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
807         if (ret)
808                 goto out_unbind;
809
810         ret = iwl_mvm_update_quotas(mvm, vif);
811         if (ret)
812                 goto out_rm_bcast;
813
814         /* Need to update the P2P Device MAC */
815         if (vif->p2p && mvm->p2p_device_vif)
816                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
817
818         mutex_unlock(&mvm->mutex);
819         return 0;
820
821 out_rm_bcast:
822         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
823 out_unbind:
824         iwl_mvm_binding_remove_vif(mvm, vif);
825 out_remove:
826         iwl_mvm_mac_ctxt_remove(mvm, vif);
827 out_unlock:
828         mutex_unlock(&mvm->mutex);
829         return ret;
830 }
831
832 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
833 {
834         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
835         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
836
837         iwl_mvm_prepare_mac_removal(mvm, vif);
838
839         mutex_lock(&mvm->mutex);
840
841         mvmvif->ap_active = false;
842
843         /* Need to update the P2P Device MAC */
844         if (vif->p2p && mvm->p2p_device_vif)
845                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
846
847         iwl_mvm_update_quotas(mvm, NULL);
848         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
849         iwl_mvm_binding_remove_vif(mvm, vif);
850         iwl_mvm_mac_ctxt_remove(mvm, vif);
851
852         mutex_unlock(&mvm->mutex);
853 }
854
855 static void iwl_mvm_bss_info_changed_ap(struct iwl_mvm *mvm,
856                                         struct ieee80211_vif *vif,
857                                         struct ieee80211_bss_conf *bss_conf,
858                                         u32 changes)
859 {
860         /* Need to send a new beacon template to the FW */
861         if (changes & BSS_CHANGED_BEACON) {
862                 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
863                         IWL_WARN(mvm, "Failed updating beacon data\n");
864         }
865 }
866
867 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
868                                      struct ieee80211_vif *vif,
869                                      struct ieee80211_bss_conf *bss_conf,
870                                      u32 changes)
871 {
872         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
873
874         mutex_lock(&mvm->mutex);
875
876         switch (vif->type) {
877         case NL80211_IFTYPE_STATION:
878                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
879                 break;
880         case NL80211_IFTYPE_AP:
881                 iwl_mvm_bss_info_changed_ap(mvm, vif, bss_conf, changes);
882                 break;
883         default:
884                 /* shouldn't happen */
885                 WARN_ON_ONCE(1);
886         }
887
888         mutex_unlock(&mvm->mutex);
889 }
890
891 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
892                                struct ieee80211_vif *vif,
893                                struct cfg80211_scan_request *req)
894 {
895         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
896         int ret;
897
898         if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
899                 return -EINVAL;
900
901         mutex_lock(&mvm->mutex);
902
903         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
904                 ret = iwl_mvm_scan_request(mvm, vif, req);
905         else
906                 ret = -EBUSY;
907
908         mutex_unlock(&mvm->mutex);
909
910         return ret;
911 }
912
913 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
914                                        struct ieee80211_vif *vif)
915 {
916         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
917
918         mutex_lock(&mvm->mutex);
919
920         iwl_mvm_cancel_scan(mvm);
921
922         mutex_unlock(&mvm->mutex);
923 }
924
925 static void
926 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
927                                   struct ieee80211_sta *sta, u16 tid,
928                                   int num_frames,
929                                   enum ieee80211_frame_release_type reason,
930                                   bool more_data)
931 {
932         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
933
934         /* TODO: how do we tell the fw to send frames for a specific TID */
935
936         /*
937          * The fw will send EOSP notification when the last frame will be
938          * transmitted.
939          */
940         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
941 }
942
943 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
944                                    struct ieee80211_vif *vif,
945                                    enum sta_notify_cmd cmd,
946                                    struct ieee80211_sta *sta)
947 {
948         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
949         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
950
951         switch (cmd) {
952         case STA_NOTIFY_SLEEP:
953                 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
954                         ieee80211_sta_block_awake(hw, sta, true);
955                 /*
956                  * The fw updates the STA to be asleep. Tx packets on the Tx
957                  * queues to this station will not be transmitted. The fw will
958                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
959                  */
960                 break;
961         case STA_NOTIFY_AWAKE:
962                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
963                         break;
964                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
965                 break;
966         default:
967                 break;
968         }
969 }
970
971 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
972                                  struct ieee80211_vif *vif,
973                                  struct ieee80211_sta *sta,
974                                  enum ieee80211_sta_state old_state,
975                                  enum ieee80211_sta_state new_state)
976 {
977         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
978         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
979         int ret;
980
981         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
982                            sta->addr, old_state, new_state);
983
984         /* this would be a mac80211 bug ... but don't crash */
985         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
986                 return -EINVAL;
987
988         /* if a STA is being removed, reuse its ID */
989         flush_work(&mvm->sta_drained_wk);
990
991         mutex_lock(&mvm->mutex);
992         if (old_state == IEEE80211_STA_NOTEXIST &&
993             new_state == IEEE80211_STA_NONE) {
994                 /*
995                  * Firmware bug - it'll crash if the beacon interval is less
996                  * than 16. We can't avoid connecting at all, so refuse the
997                  * station state change, this will cause mac80211 to abandon
998                  * attempts to connect to this AP, and eventually wpa_s will
999                  * blacklist the AP...
1000                  */
1001                 if (vif->type == NL80211_IFTYPE_STATION &&
1002                     vif->bss_conf.beacon_int < 16) {
1003                         IWL_ERR(mvm,
1004                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1005                                 sta->addr, vif->bss_conf.beacon_int);
1006                         ret = -EINVAL;
1007                         goto out_unlock;
1008                 }
1009                 ret = iwl_mvm_add_sta(mvm, vif, sta);
1010         } else if (old_state == IEEE80211_STA_NONE &&
1011                    new_state == IEEE80211_STA_AUTH) {
1012                 ret = 0;
1013         } else if (old_state == IEEE80211_STA_AUTH &&
1014                    new_state == IEEE80211_STA_ASSOC) {
1015                 ret = iwl_mvm_update_sta(mvm, vif, sta);
1016                 if (ret == 0)
1017                         iwl_mvm_rs_rate_init(mvm, sta,
1018                                              mvmvif->phy_ctxt->channel->band);
1019         } else if (old_state == IEEE80211_STA_ASSOC &&
1020                    new_state == IEEE80211_STA_AUTHORIZED) {
1021                 ret = 0;
1022         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1023                    new_state == IEEE80211_STA_ASSOC) {
1024                 ret = 0;
1025         } else if (old_state == IEEE80211_STA_ASSOC &&
1026                    new_state == IEEE80211_STA_AUTH) {
1027                 ret = 0;
1028         } else if (old_state == IEEE80211_STA_AUTH &&
1029                    new_state == IEEE80211_STA_NONE) {
1030                 ret = 0;
1031         } else if (old_state == IEEE80211_STA_NONE &&
1032                    new_state == IEEE80211_STA_NOTEXIST) {
1033                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1034         } else {
1035                 ret = -EIO;
1036         }
1037  out_unlock:
1038         mutex_unlock(&mvm->mutex);
1039
1040         return ret;
1041 }
1042
1043 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1044 {
1045         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1046
1047         mvm->rts_threshold = value;
1048
1049         return 0;
1050 }
1051
1052 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1053                                struct ieee80211_vif *vif, u16 ac,
1054                                const struct ieee80211_tx_queue_params *params)
1055 {
1056         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1057         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1058
1059         mvmvif->queue_params[ac] = *params;
1060
1061         /*
1062          * No need to update right away, we'll get BSS_CHANGED_QOS
1063          * The exception is P2P_DEVICE interface which needs immediate update.
1064          */
1065         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1066                 int ret;
1067
1068                 mutex_lock(&mvm->mutex);
1069                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1070                 mutex_unlock(&mvm->mutex);
1071                 return ret;
1072         }
1073         return 0;
1074 }
1075
1076 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1077                                       struct ieee80211_vif *vif)
1078 {
1079         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1080         u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1081                            200 + vif->bss_conf.beacon_int);
1082         u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1083                                100 + vif->bss_conf.beacon_int);
1084
1085         if (WARN_ON_ONCE(vif->bss_conf.assoc))
1086                 return;
1087
1088         mutex_lock(&mvm->mutex);
1089         /* Try really hard to protect the session and hear a beacon */
1090         iwl_mvm_protect_session(mvm, vif, duration, min_duration);
1091         mutex_unlock(&mvm->mutex);
1092 }
1093
1094 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1095                                enum set_key_cmd cmd,
1096                                struct ieee80211_vif *vif,
1097                                struct ieee80211_sta *sta,
1098                                struct ieee80211_key_conf *key)
1099 {
1100         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1101         int ret;
1102
1103         if (iwlwifi_mod_params.sw_crypto) {
1104                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1105                 return -EOPNOTSUPP;
1106         }
1107
1108         switch (key->cipher) {
1109         case WLAN_CIPHER_SUITE_TKIP:
1110                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1111                 /* fall-through */
1112         case WLAN_CIPHER_SUITE_CCMP:
1113                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1114                 break;
1115         case WLAN_CIPHER_SUITE_AES_CMAC:
1116                 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1117                 break;
1118         case WLAN_CIPHER_SUITE_WEP40:
1119         case WLAN_CIPHER_SUITE_WEP104:
1120                 /*
1121                  * Support for TX only, at least for now, so accept
1122                  * the key and do nothing else. Then mac80211 will
1123                  * pass it for TX but we don't have to use it for RX.
1124                  */
1125                 return 0;
1126         default:
1127                 return -EOPNOTSUPP;
1128         }
1129
1130         mutex_lock(&mvm->mutex);
1131
1132         switch (cmd) {
1133         case SET_KEY:
1134                 if (vif->type == NL80211_IFTYPE_AP && !sta) {
1135                         /* GTK on AP interface is a TX-only key, return 0 */
1136                         ret = 0;
1137                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1138                         break;
1139                 }
1140
1141                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1142                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1143                 if (ret) {
1144                         IWL_WARN(mvm, "set key failed\n");
1145                         /*
1146                          * can't add key for RX, but we don't need it
1147                          * in the device for TX so still return 0
1148                          */
1149                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1150                         ret = 0;
1151                 }
1152
1153                 break;
1154         case DISABLE_KEY:
1155                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1156                         ret = 0;
1157                         break;
1158                 }
1159
1160                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1161                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1162                 break;
1163         default:
1164                 ret = -EINVAL;
1165         }
1166
1167         mutex_unlock(&mvm->mutex);
1168         return ret;
1169 }
1170
1171 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1172                                         struct ieee80211_vif *vif,
1173                                         struct ieee80211_key_conf *keyconf,
1174                                         struct ieee80211_sta *sta,
1175                                         u32 iv32, u16 *phase1key)
1176 {
1177         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1178
1179         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1180 }
1181
1182
1183 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1184                        struct ieee80211_vif *vif,
1185                        struct ieee80211_channel *channel,
1186                        int duration,
1187                        enum ieee80211_roc_type type)
1188 {
1189         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1190         struct cfg80211_chan_def chandef;
1191         int ret;
1192
1193         if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1194                 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1195                 return -EINVAL;
1196         }
1197
1198         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1199                            duration, type);
1200
1201         mutex_lock(&mvm->mutex);
1202
1203         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1204         ret = iwl_mvm_phy_ctxt_changed(mvm, &mvm->phy_ctxt_roc,
1205                                        &chandef, 1, 1);
1206
1207         /* Schedule the time events */
1208         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
1209
1210         mutex_unlock(&mvm->mutex);
1211         IWL_DEBUG_MAC80211(mvm, "leave\n");
1212
1213         return ret;
1214 }
1215
1216 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1217 {
1218         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1219
1220         IWL_DEBUG_MAC80211(mvm, "enter\n");
1221
1222         mutex_lock(&mvm->mutex);
1223         iwl_mvm_stop_p2p_roc(mvm);
1224         mutex_unlock(&mvm->mutex);
1225
1226         IWL_DEBUG_MAC80211(mvm, "leave\n");
1227         return 0;
1228 }
1229
1230 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1231                                struct ieee80211_chanctx_conf *ctx)
1232 {
1233         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1234         struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
1235         int ret;
1236
1237         mutex_lock(&mvm->mutex);
1238
1239         IWL_DEBUG_MAC80211(mvm, "Add PHY context\n");
1240         ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, &ctx->def,
1241                                    ctx->rx_chains_static,
1242                                    ctx->rx_chains_dynamic);
1243         mutex_unlock(&mvm->mutex);
1244         return ret;
1245 }
1246
1247 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1248                                    struct ieee80211_chanctx_conf *ctx)
1249 {
1250         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1251         struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
1252
1253         mutex_lock(&mvm->mutex);
1254         iwl_mvm_phy_ctxt_remove(mvm, phy_ctxt);
1255         mutex_unlock(&mvm->mutex);
1256 }
1257
1258 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1259                                    struct ieee80211_chanctx_conf *ctx,
1260                                    u32 changed)
1261 {
1262         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1263         struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
1264
1265         mutex_lock(&mvm->mutex);
1266         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1267                                  ctx->rx_chains_static,
1268                                  ctx->rx_chains_dynamic);
1269         mutex_unlock(&mvm->mutex);
1270 }
1271
1272 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1273                                       struct ieee80211_vif *vif,
1274                                       struct ieee80211_chanctx_conf *ctx)
1275 {
1276         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1277         struct iwl_mvm_phy_ctxt *phyctx = (void *)ctx->drv_priv;
1278         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1279         int ret;
1280
1281         mutex_lock(&mvm->mutex);
1282
1283         mvmvif->phy_ctxt = phyctx;
1284
1285         switch (vif->type) {
1286         case NL80211_IFTYPE_AP:
1287                 /*
1288                  * The AP binding flow is handled as part of the start_ap flow
1289                  * (in bss_info_changed).
1290                  */
1291                 ret = 0;
1292                 goto out_unlock;
1293         case NL80211_IFTYPE_STATION:
1294         case NL80211_IFTYPE_ADHOC:
1295         case NL80211_IFTYPE_MONITOR:
1296                 break;
1297         default:
1298                 ret = -EINVAL;
1299                 goto out_unlock;
1300         }
1301
1302         ret = iwl_mvm_binding_add_vif(mvm, vif);
1303         if (ret)
1304                 goto out_unlock;
1305
1306         /*
1307          * Setting the quota at this stage is only required for monitor
1308          * interfaces. For the other types, the bss_info changed flow
1309          * will handle quota settings.
1310          */
1311         if (vif->type == NL80211_IFTYPE_MONITOR) {
1312                 mvmvif->monitor_active = true;
1313                 ret = iwl_mvm_update_quotas(mvm, vif);
1314                 if (ret)
1315                         goto out_remove_binding;
1316         }
1317
1318         goto out_unlock;
1319
1320  out_remove_binding:
1321         iwl_mvm_binding_remove_vif(mvm, vif);
1322  out_unlock:
1323         mutex_unlock(&mvm->mutex);
1324         if (ret)
1325                 mvmvif->phy_ctxt = NULL;
1326         return ret;
1327 }
1328
1329 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1330                                          struct ieee80211_vif *vif,
1331                                          struct ieee80211_chanctx_conf *ctx)
1332 {
1333         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1334         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1335
1336         mutex_lock(&mvm->mutex);
1337
1338         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1339
1340         if (vif->type == NL80211_IFTYPE_AP)
1341                 goto out_unlock;
1342
1343         switch (vif->type) {
1344         case NL80211_IFTYPE_MONITOR:
1345                 mvmvif->monitor_active = false;
1346                 iwl_mvm_update_quotas(mvm, NULL);
1347                 break;
1348         default:
1349                 break;
1350         }
1351
1352         iwl_mvm_binding_remove_vif(mvm, vif);
1353 out_unlock:
1354         mvmvif->phy_ctxt = NULL;
1355         mutex_unlock(&mvm->mutex);
1356 }
1357
1358 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1359                            struct ieee80211_sta *sta,
1360                            bool set)
1361 {
1362         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1363         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1364
1365         if (!mvm_sta || !mvm_sta->vif) {
1366                 IWL_ERR(mvm, "Station is not associated to a vif\n");
1367                 return -EINVAL;
1368         }
1369
1370         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1371 }
1372
1373 static void iwl_mvm_mac_rssi_callback(struct ieee80211_hw *hw,
1374                                       struct ieee80211_vif *vif,
1375                                       enum ieee80211_rssi_event rssi_event)
1376 {
1377         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1378
1379         iwl_mvm_bt_rssi_event(mvm, vif, rssi_event);
1380 }
1381
1382 struct ieee80211_ops iwl_mvm_hw_ops = {
1383         .tx = iwl_mvm_mac_tx,
1384         .ampdu_action = iwl_mvm_mac_ampdu_action,
1385         .start = iwl_mvm_mac_start,
1386         .restart_complete = iwl_mvm_mac_restart_complete,
1387         .stop = iwl_mvm_mac_stop,
1388         .add_interface = iwl_mvm_mac_add_interface,
1389         .remove_interface = iwl_mvm_mac_remove_interface,
1390         .config = iwl_mvm_mac_config,
1391         .configure_filter = iwl_mvm_configure_filter,
1392         .bss_info_changed = iwl_mvm_bss_info_changed,
1393         .hw_scan = iwl_mvm_mac_hw_scan,
1394         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1395         .sta_state = iwl_mvm_mac_sta_state,
1396         .sta_notify = iwl_mvm_mac_sta_notify,
1397         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1398         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1399         .conf_tx = iwl_mvm_mac_conf_tx,
1400         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1401         .set_key = iwl_mvm_mac_set_key,
1402         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1403         .remain_on_channel = iwl_mvm_roc,
1404         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
1405         .rssi_callback = iwl_mvm_mac_rssi_callback,
1406
1407         .add_chanctx = iwl_mvm_add_chanctx,
1408         .remove_chanctx = iwl_mvm_remove_chanctx,
1409         .change_chanctx = iwl_mvm_change_chanctx,
1410         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1411         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1412
1413         .start_ap = iwl_mvm_start_ap,
1414         .stop_ap = iwl_mvm_stop_ap,
1415
1416         .set_tim = iwl_mvm_set_tim,
1417
1418 #ifdef CONFIG_PM_SLEEP
1419         /* look at d3.c */
1420         .suspend = iwl_mvm_suspend,
1421         .resume = iwl_mvm_resume,
1422         .set_wakeup = iwl_mvm_set_wakeup,
1423         .set_rekey_data = iwl_mvm_set_rekey_data,
1424 #if IS_ENABLED(CONFIG_IPV6)
1425         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1426 #endif
1427         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1428 #endif
1429 };