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