Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / net / wireless / iwlwifi / mvm / mac80211.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63 #include <linux/kernel.h>
64 #include <linux/slab.h>
65 #include <linux/skbuff.h>
66 #include <linux/netdevice.h>
67 #include <linux/etherdevice.h>
68 #include <linux/ip.h>
69 #include <net/mac80211.h>
70 #include <net/tcp.h>
71
72 #include "iwl-op-mode.h"
73 #include "iwl-io.h"
74 #include "mvm.h"
75 #include "sta.h"
76 #include "time-event.h"
77 #include "iwl-eeprom-parse.h"
78 #include "fw-api-scan.h"
79 #include "iwl-phy-db.h"
80
81 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
82         {
83                 .max = 1,
84                 .types = BIT(NL80211_IFTYPE_STATION),
85         },
86         {
87                 .max = 1,
88                 .types = BIT(NL80211_IFTYPE_AP) |
89                         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 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
131 {
132         int i;
133
134         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
135         for (i = 0; i < NUM_PHY_CTX; i++) {
136                 mvm->phy_ctxts[i].id = i;
137                 mvm->phy_ctxts[i].ref = 0;
138         }
139 }
140
141 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
142 {
143         struct ieee80211_hw *hw = mvm->hw;
144         int num_mac, ret, i;
145
146         /* Tell mac80211 our characteristics */
147         hw->flags = IEEE80211_HW_SIGNAL_DBM |
148                     IEEE80211_HW_SPECTRUM_MGMT |
149                     IEEE80211_HW_REPORTS_TX_ACK_STATUS |
150                     IEEE80211_HW_QUEUE_CONTROL |
151                     IEEE80211_HW_WANT_MONITOR_VIF |
152                     IEEE80211_HW_SUPPORTS_PS |
153                     IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
154                     IEEE80211_HW_AMPDU_AGGREGATION |
155                     IEEE80211_HW_TIMING_BEACON_ONLY |
156                     IEEE80211_HW_CONNECTION_MONITOR |
157                     IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
158                     IEEE80211_HW_SUPPORTS_STATIC_SMPS;
159
160         hw->queues = IWL_MVM_FIRST_AGG_QUEUE;
161         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
162         hw->rate_control_algorithm = "iwl-mvm-rs";
163
164         /*
165          * Enable 11w if advertised by firmware and software crypto
166          * is not enabled (as the firmware will interpret some mgmt
167          * packets, so enabling it with software crypto isn't safe)
168          */
169         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
170             !iwlwifi_mod_params.sw_crypto)
171                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
172
173         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
174         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
175         hw->chanctx_data_size = sizeof(u16);
176
177         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
178                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
179                 BIT(NL80211_IFTYPE_AP) |
180                 BIT(NL80211_IFTYPE_P2P_GO) |
181                 BIT(NL80211_IFTYPE_P2P_DEVICE);
182
183         hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
184                             WIPHY_FLAG_DISABLE_BEACON_HINTS |
185                             WIPHY_FLAG_IBSS_RSN;
186
187         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
188         hw->wiphy->n_iface_combinations =
189                 ARRAY_SIZE(iwl_mvm_iface_combinations);
190
191         hw->wiphy->max_remain_on_channel_duration = 10000;
192         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
193
194         /* Extract MAC address */
195         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
196         hw->wiphy->addresses = mvm->addresses;
197         hw->wiphy->n_addresses = 1;
198
199         /* Extract additional MAC addresses if available */
200         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
201                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
202
203         for (i = 1; i < num_mac; i++) {
204                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
205                        ETH_ALEN);
206                 mvm->addresses[i].addr[5]++;
207                 hw->wiphy->n_addresses++;
208         }
209
210         iwl_mvm_reset_phy_ctxts(mvm);
211
212         /* we create the 802.11 header and a max-length SSID element */
213         hw->wiphy->max_scan_ie_len =
214                 mvm->fw->ucode_capa.max_probe_length - 24 - 34;
215         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
216
217         if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
218                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
219                         &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
220         if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
221                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
222                         &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
223
224         hw->wiphy->hw_version = mvm->trans->hw_id;
225
226         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
227                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
228         else
229                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
230
231         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
232                                NL80211_FEATURE_P2P_GO_OPPPS;
233
234         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
235
236 #ifdef CONFIG_PM_SLEEP
237         if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
238             mvm->trans->ops->d3_suspend &&
239             mvm->trans->ops->d3_resume &&
240             device_can_wakeup(mvm->trans->dev)) {
241                 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
242                                     WIPHY_WOWLAN_DISCONNECT |
243                                     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
244                                     WIPHY_WOWLAN_RFKILL_RELEASE;
245                 if (!iwlwifi_mod_params.sw_crypto)
246                         mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
247                                              WIPHY_WOWLAN_GTK_REKEY_FAILURE |
248                                              WIPHY_WOWLAN_4WAY_HANDSHAKE;
249
250                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
251                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
252                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
253                 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
254                 hw->wiphy->wowlan = &mvm->wowlan;
255         }
256 #endif
257
258         ret = iwl_mvm_leds_init(mvm);
259         if (ret)
260                 return ret;
261
262         ret = ieee80211_register_hw(mvm->hw);
263         if (ret)
264                 iwl_mvm_leds_exit(mvm);
265
266         return ret;
267 }
268
269 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
270                            struct ieee80211_tx_control *control,
271                            struct sk_buff *skb)
272 {
273         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
274
275         if (iwl_mvm_is_radio_killed(mvm)) {
276                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
277                 goto drop;
278         }
279
280         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
281             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
282                 goto drop;
283
284         if (control->sta) {
285                 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
286                         goto drop;
287                 return;
288         }
289
290         if (iwl_mvm_tx_skb_non_sta(mvm, skb))
291                 goto drop;
292         return;
293  drop:
294         ieee80211_free_txskb(hw, skb);
295 }
296
297 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
298                                     struct ieee80211_vif *vif,
299                                     enum ieee80211_ampdu_mlme_action action,
300                                     struct ieee80211_sta *sta, u16 tid,
301                                     u16 *ssn, u8 buf_size)
302 {
303         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
304         int ret;
305
306         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
307                      sta->addr, tid, action);
308
309         if (!(mvm->nvm_data->sku_cap_11n_enable))
310                 return -EACCES;
311
312         mutex_lock(&mvm->mutex);
313
314         switch (action) {
315         case IEEE80211_AMPDU_RX_START:
316                 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
317                         ret = -EINVAL;
318                         break;
319                 }
320                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
321                 break;
322         case IEEE80211_AMPDU_RX_STOP:
323                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
324                 break;
325         case IEEE80211_AMPDU_TX_START:
326                 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
327                         ret = -EINVAL;
328                         break;
329                 }
330                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
331                 break;
332         case IEEE80211_AMPDU_TX_STOP_CONT:
333                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
334                 break;
335         case IEEE80211_AMPDU_TX_STOP_FLUSH:
336         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
337                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
338                 break;
339         case IEEE80211_AMPDU_TX_OPERATIONAL:
340                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
341                 break;
342         default:
343                 WARN_ON_ONCE(1);
344                 ret = -EINVAL;
345                 break;
346         }
347         mutex_unlock(&mvm->mutex);
348
349         return ret;
350 }
351
352 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
353                                      struct ieee80211_vif *vif)
354 {
355         struct iwl_mvm *mvm = data;
356         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
357
358         mvmvif->uploaded = false;
359         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
360
361         /* does this make sense at all? */
362         mvmvif->color++;
363
364         spin_lock_bh(&mvm->time_event_lock);
365         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
366         spin_unlock_bh(&mvm->time_event_lock);
367
368         mvmvif->phy_ctxt = NULL;
369 }
370
371 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
372 {
373         iwl_trans_stop_device(mvm->trans);
374         iwl_trans_stop_hw(mvm->trans, false);
375
376         mvm->scan_status = IWL_MVM_SCAN_NONE;
377
378         /* just in case one was running */
379         ieee80211_remain_on_channel_expired(mvm->hw);
380
381         ieee80211_iterate_active_interfaces_atomic(
382                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
383                 iwl_mvm_cleanup_iterator, mvm);
384
385         mvm->p2p_device_vif = NULL;
386
387         iwl_mvm_reset_phy_ctxts(mvm);
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         mvm->rx_ba_sessions = 0;
395 }
396
397 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
398 {
399         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
400         int ret;
401
402         mutex_lock(&mvm->mutex);
403
404         /* Clean up some internal and mac80211 state on restart */
405         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
406                 iwl_mvm_restart_cleanup(mvm);
407
408         ret = iwl_mvm_up(mvm);
409         mutex_unlock(&mvm->mutex);
410
411         return ret;
412 }
413
414 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
415 {
416         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
417         int ret;
418
419         mutex_lock(&mvm->mutex);
420
421         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
422         ret = iwl_mvm_update_quotas(mvm, NULL);
423         if (ret)
424                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
425                         ret);
426
427         mutex_unlock(&mvm->mutex);
428 }
429
430 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
431 {
432         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
433
434         flush_work(&mvm->async_handlers_wk);
435
436         mutex_lock(&mvm->mutex);
437         /* async_handlers_wk is now blocked */
438
439         /*
440          * The work item could be running or queued if the
441          * ROC time event stops just as we get here.
442          */
443         cancel_work_sync(&mvm->roc_done_wk);
444
445         iwl_trans_stop_device(mvm->trans);
446         iwl_trans_stop_hw(mvm->trans, false);
447
448         iwl_mvm_async_handlers_purge(mvm);
449         /* async_handlers_list is empty and will stay empty: HW is stopped */
450
451         /* the fw is stopped, the aux sta is dead: clean up driver state */
452         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
453
454         mutex_unlock(&mvm->mutex);
455
456         /*
457          * The worker might have been waiting for the mutex, let it run and
458          * discover that its list is now empty.
459          */
460         cancel_work_sync(&mvm->async_handlers_wk);
461 }
462
463 static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
464                                         struct ieee80211_vif *vif)
465 {
466         struct iwl_mvm *mvm = data;
467         int ret;
468
469         ret = iwl_mvm_power_disable(mvm, vif);
470         if (ret)
471                 IWL_ERR(mvm, "failed to disable power management\n");
472 }
473
474 static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
475                                           struct ieee80211_vif *vif)
476 {
477         struct iwl_mvm *mvm = data;
478
479         iwl_mvm_power_update_mode(mvm, vif);
480 }
481
482 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
483 {
484         u16 i;
485
486         lockdep_assert_held(&mvm->mutex);
487
488         for (i = 0; i < NUM_PHY_CTX; i++)
489                 if (!mvm->phy_ctxts[i].ref)
490                         return &mvm->phy_ctxts[i];
491
492         IWL_ERR(mvm, "No available PHY context\n");
493         return NULL;
494 }
495
496 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
497                                      struct ieee80211_vif *vif)
498 {
499         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
500         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
501         int ret;
502
503         /*
504          * Not much to do here. The stack will not allow interface
505          * types or combinations that we didn't advertise, so we
506          * don't really have to check the types.
507          */
508
509         mutex_lock(&mvm->mutex);
510
511         /* Allocate resources for the MAC context, and add it the the fw  */
512         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
513         if (ret)
514                 goto out_unlock;
515
516         /*
517          * TODO: remove this temporary code.
518          * Currently MVM FW supports power management only on single MAC.
519          * If new interface added, disable PM on existing interface.
520          * P2P device is a special case, since it is handled by FW similary to
521          * scan. If P2P deviced is added, PM remains enabled on existing
522          * interface.
523          * Note: the method below does not count the new interface being added
524          * at this moment.
525          */
526         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
527                 mvm->vif_count++;
528         if (mvm->vif_count > 1) {
529                 IWL_DEBUG_MAC80211(mvm,
530                                    "Disable power on existing interfaces\n");
531                 ieee80211_iterate_active_interfaces_atomic(
532                                             mvm->hw,
533                                             IEEE80211_IFACE_ITER_NORMAL,
534                                             iwl_mvm_pm_disable_iterator, mvm);
535         }
536
537         /*
538          * The AP binding flow can be done only after the beacon
539          * template is configured (which happens only in the mac80211
540          * start_ap() flow), and adding the broadcast station can happen
541          * only after the binding.
542          * In addition, since modifying the MAC before adding a bcast
543          * station is not allowed by the FW, delay the adding of MAC context to
544          * the point where we can also add the bcast station.
545          * In short: there's not much we can do at this point, other than
546          * allocating resources :)
547          */
548         if (vif->type == NL80211_IFTYPE_AP) {
549                 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
550                 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
551                                                qmask);
552                 if (ret) {
553                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
554                         goto out_release;
555                 }
556
557                 iwl_mvm_vif_dbgfs_register(mvm, vif);
558                 goto out_unlock;
559         }
560
561         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
562         if (ret)
563                 goto out_release;
564
565         /*
566          * Update power state on the new interface. Admittedly, based on
567          * mac80211 logics this power update will disable power management
568          */
569         iwl_mvm_power_update_mode(mvm, vif);
570
571         /* beacon filtering */
572         if (!mvm->bf_allowed_vif &&
573             vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
574             mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
575                 mvm->bf_allowed_vif = mvmvif;
576                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
577         }
578
579         ret = iwl_mvm_disable_beacon_filter(mvm, vif);
580         if (ret)
581                 goto out_release;
582
583         /*
584          * P2P_DEVICE interface does not have a channel context assigned to it,
585          * so a dedicated PHY context is allocated to it and the corresponding
586          * MAC context is bound to it at this stage.
587          */
588         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
589
590                 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
591                 if (!mvmvif->phy_ctxt) {
592                         ret = -ENOSPC;
593                         goto out_remove_mac;
594                 }
595
596                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
597                 ret = iwl_mvm_binding_add_vif(mvm, vif);
598                 if (ret)
599                         goto out_unref_phy;
600
601                 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
602                 if (ret)
603                         goto out_unbind;
604
605                 /* Save a pointer to p2p device vif, so it can later be used to
606                  * update the p2p device MAC when a GO is started/stopped */
607                 mvm->p2p_device_vif = vif;
608         }
609
610         iwl_mvm_vif_dbgfs_register(mvm, vif);
611         goto out_unlock;
612
613  out_unbind:
614         iwl_mvm_binding_remove_vif(mvm, vif);
615  out_unref_phy:
616         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
617  out_remove_mac:
618         mvmvif->phy_ctxt = NULL;
619         iwl_mvm_mac_ctxt_remove(mvm, vif);
620  out_release:
621         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
622                 mvm->vif_count--;
623         ieee80211_iterate_active_interfaces(
624                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
625                 iwl_mvm_power_update_iterator, mvm);
626         iwl_mvm_mac_ctxt_release(mvm, vif);
627  out_unlock:
628         mutex_unlock(&mvm->mutex);
629
630         return ret;
631 }
632
633 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
634                                         struct ieee80211_vif *vif)
635 {
636         u32 tfd_msk = 0, ac;
637
638         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
639                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
640                         tfd_msk |= BIT(vif->hw_queue[ac]);
641
642         if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
643                 tfd_msk |= BIT(vif->cab_queue);
644
645         if (tfd_msk) {
646                 mutex_lock(&mvm->mutex);
647                 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
648                 mutex_unlock(&mvm->mutex);
649         }
650
651         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
652                 /*
653                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
654                  * We assume here that all the packets sent to the OFFCHANNEL
655                  * queue are sent in ROC session.
656                  */
657                 flush_work(&mvm->roc_done_wk);
658         } else {
659                 /*
660                  * By now, all the AC queues are empty. The AGG queues are
661                  * empty too. We already got all the Tx responses for all the
662                  * packets in the queues. The drain work can have been
663                  * triggered. Flush it.
664                  */
665                 flush_work(&mvm->sta_drained_wk);
666         }
667 }
668
669 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
670                                          struct ieee80211_vif *vif)
671 {
672         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
673         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
674
675         iwl_mvm_prepare_mac_removal(mvm, vif);
676
677         mutex_lock(&mvm->mutex);
678
679         if (mvm->bf_allowed_vif == mvmvif) {
680                 mvm->bf_allowed_vif = NULL;
681                 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
682         }
683
684         iwl_mvm_vif_dbgfs_clean(mvm, vif);
685
686         /*
687          * For AP/GO interface, the tear down of the resources allocated to the
688          * interface is be handled as part of the stop_ap flow.
689          */
690         if (vif->type == NL80211_IFTYPE_AP) {
691                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
692                 goto out_release;
693         }
694
695         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
696                 mvm->p2p_device_vif = NULL;
697                 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
698                 iwl_mvm_binding_remove_vif(mvm, vif);
699                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
700                 mvmvif->phy_ctxt = NULL;
701         }
702
703         /*
704          * TODO: remove this temporary code.
705          * Currently MVM FW supports power management only on single MAC.
706          * Check if only one additional interface remains after removing
707          * current one. Update power mode on the remaining interface.
708          */
709         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
710                 mvm->vif_count--;
711         IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
712                            mvm->vif_count);
713         if (mvm->vif_count == 1) {
714                 ieee80211_iterate_active_interfaces(
715                                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
716                                         iwl_mvm_power_update_iterator, mvm);
717         }
718
719         iwl_mvm_mac_ctxt_remove(mvm, vif);
720
721 out_release:
722         iwl_mvm_mac_ctxt_release(mvm, vif);
723         mutex_unlock(&mvm->mutex);
724 }
725
726 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
727                                 s8 tx_power)
728 {
729         /* FW is in charge of regulatory enforcement */
730         struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
731                 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
732                 .pwr_restriction = cpu_to_le16(tx_power),
733         };
734
735         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
736                                     sizeof(reduce_txpwr_cmd),
737                                     &reduce_txpwr_cmd);
738 }
739
740 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
741 {
742         return 0;
743 }
744
745 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
746                                      unsigned int changed_flags,
747                                      unsigned int *total_flags,
748                                      u64 multicast)
749 {
750         *total_flags = 0;
751 }
752
753 static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
754                                           struct ieee80211_vif *vif)
755 {
756         struct iwl_mcast_filter_cmd mcast_filter_cmd = {
757                 .pass_all = 1,
758         };
759
760         memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
761
762         return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
763                                     sizeof(mcast_filter_cmd),
764                                     &mcast_filter_cmd);
765 }
766
767 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
768                                              struct ieee80211_vif *vif,
769                                              struct ieee80211_bss_conf *bss_conf,
770                                              u32 changes)
771 {
772         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
773         int ret;
774
775         ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
776         if (ret)
777                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
778
779         if (changes & BSS_CHANGED_ASSOC) {
780                 if (bss_conf->assoc) {
781                         /* add quota for this interface */
782                         ret = iwl_mvm_update_quotas(mvm, vif);
783                         if (ret) {
784                                 IWL_ERR(mvm, "failed to update quotas\n");
785                                 return;
786                         }
787                         iwl_mvm_configure_mcast_filter(mvm, vif);
788                 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
789                         /* remove AP station now that the MAC is unassoc */
790                         ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
791                         if (ret)
792                                 IWL_ERR(mvm, "failed to remove AP station\n");
793                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
794                         /* remove quota for this interface */
795                         ret = iwl_mvm_update_quotas(mvm, NULL);
796                         if (ret)
797                                 IWL_ERR(mvm, "failed to update quotas\n");
798                 }
799                 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD)) {
800                         /* Workaround for FW bug, otherwise FW disables device
801                          * power save upon disassociation
802                          */
803                         ret = iwl_mvm_power_update_mode(mvm, vif);
804                         if (ret)
805                                 IWL_ERR(mvm, "failed to update power mode\n");
806                 }
807                 iwl_mvm_bt_coex_vif_assoc(mvm, vif);
808         } else if (changes & BSS_CHANGED_BEACON_INFO) {
809                 /*
810                  * We received a beacon _after_ association so
811                  * remove the session protection.
812                  */
813                 iwl_mvm_remove_time_event(mvm, mvmvif,
814                                           &mvmvif->time_event_data);
815         } else if (changes & BSS_CHANGED_PS) {
816                 ret = iwl_mvm_power_update_mode(mvm, vif);
817                 if (ret)
818                         IWL_ERR(mvm, "failed to update power mode\n");
819         }
820         if (changes & BSS_CHANGED_TXPOWER) {
821                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
822                                 bss_conf->txpower);
823                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
824         }
825 }
826
827 static int iwl_mvm_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
828 {
829         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
830         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
831         int ret;
832
833         mutex_lock(&mvm->mutex);
834
835         /* Send the beacon template */
836         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
837         if (ret)
838                 goto out_unlock;
839
840         /* Add the mac context */
841         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
842         if (ret)
843                 goto out_unlock;
844
845         /* Perform the binding */
846         ret = iwl_mvm_binding_add_vif(mvm, vif);
847         if (ret)
848                 goto out_remove;
849
850         mvmvif->ap_active = true;
851
852         /* Send the bcast station. At this stage the TBTT and DTIM time events
853          * are added and applied to the scheduler */
854         ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
855         if (ret)
856                 goto out_unbind;
857
858         ret = iwl_mvm_update_quotas(mvm, vif);
859         if (ret)
860                 goto out_rm_bcast;
861
862         /* Need to update the P2P Device MAC */
863         if (vif->p2p && mvm->p2p_device_vif)
864                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
865
866         mutex_unlock(&mvm->mutex);
867         return 0;
868
869 out_rm_bcast:
870         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
871 out_unbind:
872         iwl_mvm_binding_remove_vif(mvm, vif);
873 out_remove:
874         iwl_mvm_mac_ctxt_remove(mvm, vif);
875 out_unlock:
876         mutex_unlock(&mvm->mutex);
877         return ret;
878 }
879
880 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
881 {
882         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
883         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
884
885         iwl_mvm_prepare_mac_removal(mvm, vif);
886
887         mutex_lock(&mvm->mutex);
888
889         mvmvif->ap_active = false;
890
891         /* Need to update the P2P Device MAC */
892         if (vif->p2p && mvm->p2p_device_vif)
893                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
894
895         iwl_mvm_update_quotas(mvm, NULL);
896         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
897         iwl_mvm_binding_remove_vif(mvm, vif);
898         iwl_mvm_mac_ctxt_remove(mvm, vif);
899
900         mutex_unlock(&mvm->mutex);
901 }
902
903 static void iwl_mvm_bss_info_changed_ap(struct iwl_mvm *mvm,
904                                         struct ieee80211_vif *vif,
905                                         struct ieee80211_bss_conf *bss_conf,
906                                         u32 changes)
907 {
908         /* Need to send a new beacon template to the FW */
909         if (changes & BSS_CHANGED_BEACON) {
910                 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
911                         IWL_WARN(mvm, "Failed updating beacon data\n");
912         }
913 }
914
915 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
916                                      struct ieee80211_vif *vif,
917                                      struct ieee80211_bss_conf *bss_conf,
918                                      u32 changes)
919 {
920         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
921
922         mutex_lock(&mvm->mutex);
923
924         switch (vif->type) {
925         case NL80211_IFTYPE_STATION:
926                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
927                 break;
928         case NL80211_IFTYPE_AP:
929                 iwl_mvm_bss_info_changed_ap(mvm, vif, bss_conf, changes);
930                 break;
931         default:
932                 /* shouldn't happen */
933                 WARN_ON_ONCE(1);
934         }
935
936         mutex_unlock(&mvm->mutex);
937 }
938
939 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
940                                struct ieee80211_vif *vif,
941                                struct cfg80211_scan_request *req)
942 {
943         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
944         int ret;
945
946         if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
947                 return -EINVAL;
948
949         mutex_lock(&mvm->mutex);
950
951         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
952                 ret = iwl_mvm_scan_request(mvm, vif, req);
953         else
954                 ret = -EBUSY;
955
956         mutex_unlock(&mvm->mutex);
957
958         return ret;
959 }
960
961 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
962                                        struct ieee80211_vif *vif)
963 {
964         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
965
966         mutex_lock(&mvm->mutex);
967
968         iwl_mvm_cancel_scan(mvm);
969
970         mutex_unlock(&mvm->mutex);
971 }
972
973 static void
974 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
975                                   struct ieee80211_sta *sta, u16 tid,
976                                   int num_frames,
977                                   enum ieee80211_frame_release_type reason,
978                                   bool more_data)
979 {
980         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
981
982         /* TODO: how do we tell the fw to send frames for a specific TID */
983
984         /*
985          * The fw will send EOSP notification when the last frame will be
986          * transmitted.
987          */
988         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
989 }
990
991 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
992                                    struct ieee80211_vif *vif,
993                                    enum sta_notify_cmd cmd,
994                                    struct ieee80211_sta *sta)
995 {
996         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
997         struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
998
999         switch (cmd) {
1000         case STA_NOTIFY_SLEEP:
1001                 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1002                         ieee80211_sta_block_awake(hw, sta, true);
1003                 /*
1004                  * The fw updates the STA to be asleep. Tx packets on the Tx
1005                  * queues to this station will not be transmitted. The fw will
1006                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1007                  */
1008                 break;
1009         case STA_NOTIFY_AWAKE:
1010                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1011                         break;
1012                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1013                 break;
1014         default:
1015                 break;
1016         }
1017 }
1018
1019 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1020                                  struct ieee80211_vif *vif,
1021                                  struct ieee80211_sta *sta,
1022                                  enum ieee80211_sta_state old_state,
1023                                  enum ieee80211_sta_state new_state)
1024 {
1025         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1026         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1027         int ret;
1028
1029         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1030                            sta->addr, old_state, new_state);
1031
1032         /* this would be a mac80211 bug ... but don't crash */
1033         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1034                 return -EINVAL;
1035
1036         /* if a STA is being removed, reuse its ID */
1037         flush_work(&mvm->sta_drained_wk);
1038
1039         mutex_lock(&mvm->mutex);
1040         if (old_state == IEEE80211_STA_NOTEXIST &&
1041             new_state == IEEE80211_STA_NONE) {
1042                 /*
1043                  * Firmware bug - it'll crash if the beacon interval is less
1044                  * than 16. We can't avoid connecting at all, so refuse the
1045                  * station state change, this will cause mac80211 to abandon
1046                  * attempts to connect to this AP, and eventually wpa_s will
1047                  * blacklist the AP...
1048                  */
1049                 if (vif->type == NL80211_IFTYPE_STATION &&
1050                     vif->bss_conf.beacon_int < 16) {
1051                         IWL_ERR(mvm,
1052                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1053                                 sta->addr, vif->bss_conf.beacon_int);
1054                         ret = -EINVAL;
1055                         goto out_unlock;
1056                 }
1057                 ret = iwl_mvm_add_sta(mvm, vif, sta);
1058         } else if (old_state == IEEE80211_STA_NONE &&
1059                    new_state == IEEE80211_STA_AUTH) {
1060                 ret = 0;
1061         } else if (old_state == IEEE80211_STA_AUTH &&
1062                    new_state == IEEE80211_STA_ASSOC) {
1063                 ret = iwl_mvm_update_sta(mvm, vif, sta);
1064                 if (ret == 0)
1065                         iwl_mvm_rs_rate_init(mvm, sta,
1066                                              mvmvif->phy_ctxt->channel->band);
1067         } else if (old_state == IEEE80211_STA_ASSOC &&
1068                    new_state == IEEE80211_STA_AUTHORIZED) {
1069                 /* enable beacon filtering */
1070                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
1071                 ret = 0;
1072         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1073                    new_state == IEEE80211_STA_ASSOC) {
1074                 /* disable beacon filtering */
1075                 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
1076                 ret = 0;
1077         } else if (old_state == IEEE80211_STA_ASSOC &&
1078                    new_state == IEEE80211_STA_AUTH) {
1079                 ret = 0;
1080         } else if (old_state == IEEE80211_STA_AUTH &&
1081                    new_state == IEEE80211_STA_NONE) {
1082                 ret = 0;
1083         } else if (old_state == IEEE80211_STA_NONE &&
1084                    new_state == IEEE80211_STA_NOTEXIST) {
1085                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1086         } else {
1087                 ret = -EIO;
1088         }
1089  out_unlock:
1090         mutex_unlock(&mvm->mutex);
1091
1092         return ret;
1093 }
1094
1095 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1096 {
1097         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1098
1099         mvm->rts_threshold = value;
1100
1101         return 0;
1102 }
1103
1104 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1105                                struct ieee80211_vif *vif, u16 ac,
1106                                const struct ieee80211_tx_queue_params *params)
1107 {
1108         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1109         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1110
1111         mvmvif->queue_params[ac] = *params;
1112
1113         /*
1114          * No need to update right away, we'll get BSS_CHANGED_QOS
1115          * The exception is P2P_DEVICE interface which needs immediate update.
1116          */
1117         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1118                 int ret;
1119
1120                 mutex_lock(&mvm->mutex);
1121                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1122                 mutex_unlock(&mvm->mutex);
1123                 return ret;
1124         }
1125         return 0;
1126 }
1127
1128 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1129                                       struct ieee80211_vif *vif)
1130 {
1131         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1132         u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1133                            200 + vif->bss_conf.beacon_int);
1134         u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1135                                100 + vif->bss_conf.beacon_int);
1136
1137         if (WARN_ON_ONCE(vif->bss_conf.assoc))
1138                 return;
1139
1140         mutex_lock(&mvm->mutex);
1141         /* Try really hard to protect the session and hear a beacon */
1142         iwl_mvm_protect_session(mvm, vif, duration, min_duration);
1143         mutex_unlock(&mvm->mutex);
1144 }
1145
1146 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1147                                enum set_key_cmd cmd,
1148                                struct ieee80211_vif *vif,
1149                                struct ieee80211_sta *sta,
1150                                struct ieee80211_key_conf *key)
1151 {
1152         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1153         int ret;
1154
1155         if (iwlwifi_mod_params.sw_crypto) {
1156                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1157                 return -EOPNOTSUPP;
1158         }
1159
1160         switch (key->cipher) {
1161         case WLAN_CIPHER_SUITE_TKIP:
1162                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1163                 /* fall-through */
1164         case WLAN_CIPHER_SUITE_CCMP:
1165                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1166                 break;
1167         case WLAN_CIPHER_SUITE_AES_CMAC:
1168                 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1169                 break;
1170         case WLAN_CIPHER_SUITE_WEP40:
1171         case WLAN_CIPHER_SUITE_WEP104:
1172                 /*
1173                  * Support for TX only, at least for now, so accept
1174                  * the key and do nothing else. Then mac80211 will
1175                  * pass it for TX but we don't have to use it for RX.
1176                  */
1177                 return 0;
1178         default:
1179                 return -EOPNOTSUPP;
1180         }
1181
1182         mutex_lock(&mvm->mutex);
1183
1184         switch (cmd) {
1185         case SET_KEY:
1186                 if (vif->type == NL80211_IFTYPE_AP && !sta) {
1187                         /* GTK on AP interface is a TX-only key, return 0 */
1188                         ret = 0;
1189                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1190                         break;
1191                 }
1192
1193                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1194                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1195                 if (ret) {
1196                         IWL_WARN(mvm, "set key failed\n");
1197                         /*
1198                          * can't add key for RX, but we don't need it
1199                          * in the device for TX so still return 0
1200                          */
1201                         key->hw_key_idx = STA_KEY_IDX_INVALID;
1202                         ret = 0;
1203                 }
1204
1205                 break;
1206         case DISABLE_KEY:
1207                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1208                         ret = 0;
1209                         break;
1210                 }
1211
1212                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1213                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1214                 break;
1215         default:
1216                 ret = -EINVAL;
1217         }
1218
1219         mutex_unlock(&mvm->mutex);
1220         return ret;
1221 }
1222
1223 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1224                                         struct ieee80211_vif *vif,
1225                                         struct ieee80211_key_conf *keyconf,
1226                                         struct ieee80211_sta *sta,
1227                                         u32 iv32, u16 *phase1key)
1228 {
1229         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1230
1231         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1232 }
1233
1234
1235 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1236                        struct ieee80211_vif *vif,
1237                        struct ieee80211_channel *channel,
1238                        int duration,
1239                        enum ieee80211_roc_type type)
1240 {
1241         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1242         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1243         struct cfg80211_chan_def chandef;
1244         struct iwl_mvm_phy_ctxt *phy_ctxt;
1245         int ret, i;
1246
1247         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1248                            duration, type);
1249
1250         if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1251                 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1252                 return -EINVAL;
1253         }
1254
1255         mutex_lock(&mvm->mutex);
1256
1257         for (i = 0; i < NUM_PHY_CTX; i++) {
1258                 phy_ctxt = &mvm->phy_ctxts[i];
1259                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1260                         continue;
1261
1262                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
1263                         /*
1264                          * Unbind the P2P_DEVICE from the current PHY context,
1265                          * and if the PHY context is not used remove it.
1266                          */
1267                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
1268                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1269                                 goto out_unlock;
1270
1271                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1272
1273                         /* Bind the P2P_DEVICE to the current PHY Context */
1274                         mvmvif->phy_ctxt = phy_ctxt;
1275
1276                         ret = iwl_mvm_binding_add_vif(mvm, vif);
1277                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1278                                 goto out_unlock;
1279
1280                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1281                         goto schedule_time_event;
1282                 }
1283         }
1284
1285         /* Need to update the PHY context only if the ROC channel changed */
1286         if (channel == mvmvif->phy_ctxt->channel)
1287                 goto schedule_time_event;
1288
1289         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1290
1291         /*
1292          * Change the PHY context configuration as it is currently referenced
1293          * only by the P2P Device MAC
1294          */
1295         if (mvmvif->phy_ctxt->ref == 1) {
1296                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
1297                                                &chandef, 1, 1);
1298                 if (ret)
1299                         goto out_unlock;
1300         } else {
1301                 /*
1302                  * The PHY context is shared with other MACs. Need to remove the
1303                  * P2P Device from the binding, allocate an new PHY context and
1304                  * create a new binding
1305                  */
1306                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1307                 if (!phy_ctxt) {
1308                         ret = -ENOSPC;
1309                         goto out_unlock;
1310                 }
1311
1312                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
1313                                                1, 1);
1314                 if (ret) {
1315                         IWL_ERR(mvm, "Failed to change PHY context\n");
1316                         goto out_unlock;
1317                 }
1318
1319                 /* Unbind the P2P_DEVICE from the current PHY context */
1320                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1321                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1322                         goto out_unlock;
1323
1324                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1325
1326                 /* Bind the P2P_DEVICE to the new allocated PHY context */
1327                 mvmvif->phy_ctxt = phy_ctxt;
1328
1329                 ret = iwl_mvm_binding_add_vif(mvm, vif);
1330                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1331                         goto out_unlock;
1332
1333                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1334         }
1335
1336 schedule_time_event:
1337         /* Schedule the time events */
1338         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
1339
1340 out_unlock:
1341         mutex_unlock(&mvm->mutex);
1342         IWL_DEBUG_MAC80211(mvm, "leave\n");
1343         return ret;
1344 }
1345
1346 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1347 {
1348         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1349
1350         IWL_DEBUG_MAC80211(mvm, "enter\n");
1351
1352         mutex_lock(&mvm->mutex);
1353         iwl_mvm_stop_p2p_roc(mvm);
1354         mutex_unlock(&mvm->mutex);
1355
1356         IWL_DEBUG_MAC80211(mvm, "leave\n");
1357         return 0;
1358 }
1359
1360 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1361                                struct ieee80211_chanctx_conf *ctx)
1362 {
1363         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1364         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1365         struct iwl_mvm_phy_ctxt *phy_ctxt;
1366         int ret;
1367
1368         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
1369
1370         mutex_lock(&mvm->mutex);
1371         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1372         if (!phy_ctxt) {
1373                 ret = -ENOSPC;
1374                 goto out;
1375         }
1376
1377         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1378                                        ctx->rx_chains_static,
1379                                        ctx->rx_chains_dynamic);
1380         if (ret) {
1381                 IWL_ERR(mvm, "Failed to add PHY context\n");
1382                 goto out;
1383         }
1384
1385         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
1386         *phy_ctxt_id = phy_ctxt->id;
1387 out:
1388         mutex_unlock(&mvm->mutex);
1389         return ret;
1390 }
1391
1392 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1393                                    struct ieee80211_chanctx_conf *ctx)
1394 {
1395         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1396         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1397         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1398
1399         mutex_lock(&mvm->mutex);
1400         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
1401         mutex_unlock(&mvm->mutex);
1402 }
1403
1404 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1405                                    struct ieee80211_chanctx_conf *ctx,
1406                                    u32 changed)
1407 {
1408         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1409         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1410         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1411
1412         if (WARN_ONCE((phy_ctxt->ref > 1) &&
1413                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
1414                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1415                                    IEEE80211_CHANCTX_CHANGE_RADAR)),
1416                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
1417                       phy_ctxt->ref, changed))
1418                 return;
1419
1420         mutex_lock(&mvm->mutex);
1421         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1422                                  ctx->rx_chains_static,
1423                                  ctx->rx_chains_dynamic);
1424         mutex_unlock(&mvm->mutex);
1425 }
1426
1427 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1428                                       struct ieee80211_vif *vif,
1429                                       struct ieee80211_chanctx_conf *ctx)
1430 {
1431         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1432         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1433         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1434         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1435         int ret;
1436
1437         mutex_lock(&mvm->mutex);
1438
1439         mvmvif->phy_ctxt = phy_ctxt;
1440
1441         switch (vif->type) {
1442         case NL80211_IFTYPE_AP:
1443                 /*
1444                  * The AP binding flow is handled as part of the start_ap flow
1445                  * (in bss_info_changed).
1446                  */
1447                 ret = 0;
1448                 goto out_unlock;
1449         case NL80211_IFTYPE_STATION:
1450         case NL80211_IFTYPE_ADHOC:
1451         case NL80211_IFTYPE_MONITOR:
1452                 break;
1453         default:
1454                 ret = -EINVAL;
1455                 goto out_unlock;
1456         }
1457
1458         ret = iwl_mvm_binding_add_vif(mvm, vif);
1459         if (ret)
1460                 goto out_unlock;
1461
1462         /*
1463          * Setting the quota at this stage is only required for monitor
1464          * interfaces. For the other types, the bss_info changed flow
1465          * will handle quota settings.
1466          */
1467         if (vif->type == NL80211_IFTYPE_MONITOR) {
1468                 mvmvif->monitor_active = true;
1469                 ret = iwl_mvm_update_quotas(mvm, vif);
1470                 if (ret)
1471                         goto out_remove_binding;
1472         }
1473
1474         goto out_unlock;
1475
1476  out_remove_binding:
1477         iwl_mvm_binding_remove_vif(mvm, vif);
1478  out_unlock:
1479         mutex_unlock(&mvm->mutex);
1480         if (ret)
1481                 mvmvif->phy_ctxt = NULL;
1482         return ret;
1483 }
1484
1485 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1486                                          struct ieee80211_vif *vif,
1487                                          struct ieee80211_chanctx_conf *ctx)
1488 {
1489         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1490         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1491
1492         mutex_lock(&mvm->mutex);
1493
1494         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1495
1496         if (vif->type == NL80211_IFTYPE_AP)
1497                 goto out_unlock;
1498
1499         switch (vif->type) {
1500         case NL80211_IFTYPE_MONITOR:
1501                 mvmvif->monitor_active = false;
1502                 iwl_mvm_update_quotas(mvm, NULL);
1503                 break;
1504         default:
1505                 break;
1506         }
1507
1508         iwl_mvm_binding_remove_vif(mvm, vif);
1509 out_unlock:
1510         mvmvif->phy_ctxt = NULL;
1511         mutex_unlock(&mvm->mutex);
1512 }
1513
1514 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1515                            struct ieee80211_sta *sta,
1516                            bool set)
1517 {
1518         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1519         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1520
1521         if (!mvm_sta || !mvm_sta->vif) {
1522                 IWL_ERR(mvm, "Station is not associated to a vif\n");
1523                 return -EINVAL;
1524         }
1525
1526         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1527 }
1528
1529 static void iwl_mvm_mac_rssi_callback(struct ieee80211_hw *hw,
1530                                       struct ieee80211_vif *vif,
1531                                       enum ieee80211_rssi_event rssi_event)
1532 {
1533         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1534
1535         iwl_mvm_bt_rssi_event(mvm, vif, rssi_event);
1536 }
1537
1538 struct ieee80211_ops iwl_mvm_hw_ops = {
1539         .tx = iwl_mvm_mac_tx,
1540         .ampdu_action = iwl_mvm_mac_ampdu_action,
1541         .start = iwl_mvm_mac_start,
1542         .restart_complete = iwl_mvm_mac_restart_complete,
1543         .stop = iwl_mvm_mac_stop,
1544         .add_interface = iwl_mvm_mac_add_interface,
1545         .remove_interface = iwl_mvm_mac_remove_interface,
1546         .config = iwl_mvm_mac_config,
1547         .configure_filter = iwl_mvm_configure_filter,
1548         .bss_info_changed = iwl_mvm_bss_info_changed,
1549         .hw_scan = iwl_mvm_mac_hw_scan,
1550         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1551         .sta_state = iwl_mvm_mac_sta_state,
1552         .sta_notify = iwl_mvm_mac_sta_notify,
1553         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1554         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1555         .conf_tx = iwl_mvm_mac_conf_tx,
1556         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1557         .set_key = iwl_mvm_mac_set_key,
1558         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1559         .remain_on_channel = iwl_mvm_roc,
1560         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
1561         .rssi_callback = iwl_mvm_mac_rssi_callback,
1562
1563         .add_chanctx = iwl_mvm_add_chanctx,
1564         .remove_chanctx = iwl_mvm_remove_chanctx,
1565         .change_chanctx = iwl_mvm_change_chanctx,
1566         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1567         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1568
1569         .start_ap = iwl_mvm_start_ap,
1570         .stop_ap = iwl_mvm_stop_ap,
1571
1572         .set_tim = iwl_mvm_set_tim,
1573
1574 #ifdef CONFIG_PM_SLEEP
1575         /* look at d3.c */
1576         .suspend = iwl_mvm_suspend,
1577         .resume = iwl_mvm_resume,
1578         .set_wakeup = iwl_mvm_set_wakeup,
1579         .set_rekey_data = iwl_mvm_set_rekey_data,
1580 #if IS_ENABLED(CONFIG_IPV6)
1581         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1582 #endif
1583         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1584 #endif
1585 };