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