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