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