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