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