iwlwifi: mvm: add WEP RX hardware offload support
[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 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23  * USA
24  *
25  * The full GNU General Public License is included in this distribution
26  * in the file called COPYING.
27  *
28  * Contact Information:
29  *  Intel Linux Wireless <ilw@linux.intel.com>
30  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31  *
32  * BSD LICENSE
33  *
34  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  *
42  *  * Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  *  * Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in
46  *    the documentation and/or other materials provided with the
47  *    distribution.
48  *  * Neither the name Intel Corporation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  *
64  *****************************************************************************/
65 #include <linux/kernel.h>
66 #include <linux/slab.h>
67 #include <linux/skbuff.h>
68 #include <linux/netdevice.h>
69 #include <linux/etherdevice.h>
70 #include <linux/ip.h>
71 #include <linux/if_arp.h>
72 #include <linux/devcoredump.h>
73 #include <net/mac80211.h>
74 #include <net/ieee80211_radiotap.h>
75 #include <net/tcp.h>
76
77 #include "iwl-op-mode.h"
78 #include "iwl-io.h"
79 #include "mvm.h"
80 #include "sta.h"
81 #include "time-event.h"
82 #include "iwl-eeprom-parse.h"
83 #include "fw-api-scan.h"
84 #include "iwl-phy-db.h"
85 #include "testmode.h"
86 #include "iwl-fw-error-dump.h"
87 #include "iwl-prph.h"
88
89 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
90         {
91                 .max = 1,
92                 .types = BIT(NL80211_IFTYPE_STATION),
93         },
94         {
95                 .max = 1,
96                 .types = BIT(NL80211_IFTYPE_AP) |
97                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
98                         BIT(NL80211_IFTYPE_P2P_GO),
99         },
100         {
101                 .max = 1,
102                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
103         },
104 };
105
106 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
107         {
108                 .num_different_channels = 1,
109                 .max_interfaces = 3,
110                 .limits = iwl_mvm_limits,
111                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
112         },
113 };
114
115 #ifdef CONFIG_PM_SLEEP
116 static const struct nl80211_wowlan_tcp_data_token_feature
117 iwl_mvm_wowlan_tcp_token_feature = {
118         .min_len = 0,
119         .max_len = 255,
120         .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
121 };
122
123 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
124         .tok = &iwl_mvm_wowlan_tcp_token_feature,
125         .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
126                             sizeof(struct ethhdr) -
127                             sizeof(struct iphdr) -
128                             sizeof(struct tcphdr),
129         .data_interval_max = 65535, /* __le16 in API */
130         .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
131                             sizeof(struct ethhdr) -
132                             sizeof(struct iphdr) -
133                             sizeof(struct tcphdr),
134         .seq = true,
135 };
136 #endif
137
138 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
139 /*
140  * Use the reserved field to indicate magic values.
141  * these values will only be used internally by the driver,
142  * and won't make it to the fw (reserved will be 0).
143  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
144  *      be the vif's ip address. in case there is not a single
145  *      ip address (0, or more than 1), this attribute will
146  *      be skipped.
147  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
148  *      the LSB bytes of the vif's mac address
149  */
150 enum {
151         BC_FILTER_MAGIC_NONE = 0,
152         BC_FILTER_MAGIC_IP,
153         BC_FILTER_MAGIC_MAC,
154 };
155
156 static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
157         {
158                 /* arp */
159                 .discard = 0,
160                 .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
161                 .attrs = {
162                         {
163                                 /* frame type - arp, hw type - ethernet */
164                                 .offset_type =
165                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
166                                 .offset = sizeof(rfc1042_header),
167                                 .val = cpu_to_be32(0x08060001),
168                                 .mask = cpu_to_be32(0xffffffff),
169                         },
170                         {
171                                 /* arp dest ip */
172                                 .offset_type =
173                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
174                                 .offset = sizeof(rfc1042_header) + 2 +
175                                           sizeof(struct arphdr) +
176                                           ETH_ALEN + sizeof(__be32) +
177                                           ETH_ALEN,
178                                 .mask = cpu_to_be32(0xffffffff),
179                                 /* mark it as special field */
180                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
181                         },
182                 },
183         },
184         {
185                 /* dhcp offer bcast */
186                 .discard = 0,
187                 .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
188                 .attrs = {
189                         {
190                                 /* udp dest port - 68 (bootp client)*/
191                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
192                                 .offset = offsetof(struct udphdr, dest),
193                                 .val = cpu_to_be32(0x00440000),
194                                 .mask = cpu_to_be32(0xffff0000),
195                         },
196                         {
197                                 /* dhcp - lsb bytes of client hw address */
198                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
199                                 .offset = 38,
200                                 .mask = cpu_to_be32(0xffffffff),
201                                 /* mark it as special field */
202                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
203                         },
204                 },
205         },
206         /* last filter must be empty */
207         {},
208 };
209 #endif
210
211 void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
212 {
213         if (!iwl_mvm_is_d0i3_supported(mvm))
214                 return;
215
216         IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
217         spin_lock_bh(&mvm->refs_lock);
218         mvm->refs[ref_type]++;
219         spin_unlock_bh(&mvm->refs_lock);
220         iwl_trans_ref(mvm->trans);
221 }
222
223 void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
224 {
225         if (!iwl_mvm_is_d0i3_supported(mvm))
226                 return;
227
228         IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
229         spin_lock_bh(&mvm->refs_lock);
230         WARN_ON(!mvm->refs[ref_type]--);
231         spin_unlock_bh(&mvm->refs_lock);
232         iwl_trans_unref(mvm->trans);
233 }
234
235 static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
236                                      enum iwl_mvm_ref_type except_ref)
237 {
238         int i, j;
239
240         if (!iwl_mvm_is_d0i3_supported(mvm))
241                 return;
242
243         spin_lock_bh(&mvm->refs_lock);
244         for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
245                 if (except_ref == i || !mvm->refs[i])
246                         continue;
247
248                 IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
249                               i, mvm->refs[i]);
250                 for (j = 0; j < mvm->refs[i]; j++)
251                         iwl_trans_unref(mvm->trans);
252                 mvm->refs[i] = 0;
253         }
254         spin_unlock_bh(&mvm->refs_lock);
255 }
256
257 bool iwl_mvm_ref_taken(struct iwl_mvm *mvm)
258 {
259         int i;
260         bool taken = false;
261
262         if (!iwl_mvm_is_d0i3_supported(mvm))
263                 return true;
264
265         spin_lock_bh(&mvm->refs_lock);
266         for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
267                 if (mvm->refs[i]) {
268                         taken = true;
269                         break;
270                 }
271         }
272         spin_unlock_bh(&mvm->refs_lock);
273
274         return taken;
275 }
276
277 int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
278 {
279         iwl_mvm_ref(mvm, ref_type);
280
281         if (!wait_event_timeout(mvm->d0i3_exit_waitq,
282                                 !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
283                                 HZ)) {
284                 WARN_ON_ONCE(1);
285                 iwl_mvm_unref(mvm, ref_type);
286                 return -EIO;
287         }
288
289         return 0;
290 }
291
292 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
293 {
294         int i;
295
296         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
297         for (i = 0; i < NUM_PHY_CTX; i++) {
298                 mvm->phy_ctxts[i].id = i;
299                 mvm->phy_ctxts[i].ref = 0;
300         }
301 }
302
303 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
304 {
305         struct ieee80211_hw *hw = mvm->hw;
306         int num_mac, ret, i;
307
308         /* Tell mac80211 our characteristics */
309         hw->flags = IEEE80211_HW_SIGNAL_DBM |
310                     IEEE80211_HW_SPECTRUM_MGMT |
311                     IEEE80211_HW_REPORTS_TX_ACK_STATUS |
312                     IEEE80211_HW_QUEUE_CONTROL |
313                     IEEE80211_HW_WANT_MONITOR_VIF |
314                     IEEE80211_HW_SUPPORTS_PS |
315                     IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
316                     IEEE80211_HW_AMPDU_AGGREGATION |
317                     IEEE80211_HW_TIMING_BEACON_ONLY |
318                     IEEE80211_HW_CONNECTION_MONITOR |
319                     IEEE80211_HW_CHANCTX_STA_CSA |
320                     IEEE80211_HW_SUPPORTS_CLONED_SKBS;
321
322         hw->queues = mvm->first_agg_queue;
323         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
324         hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
325                                     IEEE80211_RADIOTAP_MCS_HAVE_STBC;
326         hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
327         hw->rate_control_algorithm = "iwl-mvm-rs";
328
329         /*
330          * Enable 11w if advertised by firmware and software crypto
331          * is not enabled (as the firmware will interpret some mgmt
332          * packets, so enabling it with software crypto isn't safe)
333          */
334         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
335             !iwlwifi_mod_params.sw_crypto)
336                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
337
338         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT &&
339             IWL_UCODE_API(mvm->fw->ucode_ver) >= 9 &&
340             !iwlwifi_mod_params.uapsd_disable) {
341                 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
342                 hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
343                 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
344         }
345
346         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
347             mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
348                 hw->flags |= IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS;
349                 hw->wiphy->features |=
350                         NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
351                         NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
352         }
353
354         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
355         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
356         hw->chanctx_data_size = sizeof(u16);
357
358         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
359                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
360                 BIT(NL80211_IFTYPE_AP) |
361                 BIT(NL80211_IFTYPE_P2P_GO) |
362                 BIT(NL80211_IFTYPE_P2P_DEVICE) |
363                 BIT(NL80211_IFTYPE_ADHOC);
364
365         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
366         hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
367                                        REGULATORY_DISABLE_BEACON_HINTS;
368
369         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
370                 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
371
372         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_CSA_FLOW)
373                 hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
374
375         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
376         hw->wiphy->n_iface_combinations =
377                 ARRAY_SIZE(iwl_mvm_iface_combinations);
378
379         hw->wiphy->max_remain_on_channel_duration = 10000;
380         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
381
382         /* Extract MAC address */
383         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
384         hw->wiphy->addresses = mvm->addresses;
385         hw->wiphy->n_addresses = 1;
386
387         /* Extract additional MAC addresses if available */
388         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
389                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
390
391         for (i = 1; i < num_mac; i++) {
392                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
393                        ETH_ALEN);
394                 mvm->addresses[i].addr[5]++;
395                 hw->wiphy->n_addresses++;
396         }
397
398         iwl_mvm_reset_phy_ctxts(mvm);
399
400         hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm, false);
401
402         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
403
404         if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
405                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
406                         &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
407         if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
408                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
409                         &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
410
411         hw->wiphy->hw_version = mvm->trans->hw_id;
412
413         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
414                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
415         else
416                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
417
418         if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 10) {
419                 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
420                 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
421                 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
422                 /* we create the 802.11 header and zero length SSID IE. */
423                 hw->wiphy->max_sched_scan_ie_len =
424                         SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
425         }
426
427         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
428                                NL80211_FEATURE_LOW_PRIORITY_SCAN |
429                                NL80211_FEATURE_P2P_GO_OPPPS |
430                                NL80211_FEATURE_DYNAMIC_SMPS |
431                                NL80211_FEATURE_STATIC_SMPS |
432                                NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
433
434         if (mvm->fw->ucode_capa.capa[0] &
435             IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT)
436                 hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
437         if (mvm->fw->ucode_capa.capa[0] &
438             IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT)
439                 hw->wiphy->features |= NL80211_FEATURE_QUIET;
440
441         if (mvm->fw->ucode_capa.capa[0] &
442             IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT)
443                 hw->wiphy->features |=
444                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
445
446         if (mvm->fw->ucode_capa.capa[0] &
447             IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)
448                 hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
449
450         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
451
452         /* currently FW API supports only one optional cipher scheme */
453         if (mvm->fw->cs[0].cipher) {
454                 mvm->hw->n_cipher_schemes = 1;
455                 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
456         }
457
458 #ifdef CONFIG_PM_SLEEP
459         if (iwl_mvm_is_d0i3_supported(mvm) &&
460             device_can_wakeup(mvm->trans->dev)) {
461                 mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
462                 hw->wiphy->wowlan = &mvm->wowlan;
463         } else if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
464             mvm->trans->ops->d3_suspend &&
465             mvm->trans->ops->d3_resume &&
466             device_can_wakeup(mvm->trans->dev)) {
467                 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
468                                     WIPHY_WOWLAN_DISCONNECT |
469                                     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
470                                     WIPHY_WOWLAN_RFKILL_RELEASE;
471                 if (!iwlwifi_mod_params.sw_crypto)
472                         mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
473                                              WIPHY_WOWLAN_GTK_REKEY_FAILURE |
474                                              WIPHY_WOWLAN_4WAY_HANDSHAKE;
475
476                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
477                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
478                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
479                 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
480                 hw->wiphy->wowlan = &mvm->wowlan;
481         }
482 #endif
483
484 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
485         /* assign default bcast filtering configuration */
486         mvm->bcast_filters = iwl_mvm_default_bcast_filters;
487 #endif
488
489         ret = iwl_mvm_leds_init(mvm);
490         if (ret)
491                 return ret;
492
493         ret = ieee80211_register_hw(mvm->hw);
494         if (ret)
495                 iwl_mvm_leds_exit(mvm);
496
497         return ret;
498 }
499
500 static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
501                              struct ieee80211_sta *sta,
502                              struct sk_buff *skb)
503 {
504         struct iwl_mvm_sta *mvmsta;
505         bool defer = false;
506
507         /*
508          * double check the IN_D0I3 flag both before and after
509          * taking the spinlock, in order to prevent taking
510          * the spinlock when not needed.
511          */
512         if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
513                 return false;
514
515         spin_lock(&mvm->d0i3_tx_lock);
516         /*
517          * testing the flag again ensures the skb dequeue
518          * loop (on d0i3 exit) hasn't run yet.
519          */
520         if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
521                 goto out;
522
523         mvmsta = iwl_mvm_sta_from_mac80211(sta);
524         if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
525             mvmsta->sta_id != mvm->d0i3_ap_sta_id)
526                 goto out;
527
528         __skb_queue_tail(&mvm->d0i3_tx, skb);
529         ieee80211_stop_queues(mvm->hw);
530
531         /* trigger wakeup */
532         iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
533         iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
534
535         defer = true;
536 out:
537         spin_unlock(&mvm->d0i3_tx_lock);
538         return defer;
539 }
540
541 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
542                            struct ieee80211_tx_control *control,
543                            struct sk_buff *skb)
544 {
545         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
546         struct ieee80211_sta *sta = control->sta;
547         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
548         struct ieee80211_hdr *hdr = (void *)skb->data;
549
550         if (iwl_mvm_is_radio_killed(mvm)) {
551                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
552                 goto drop;
553         }
554
555         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
556             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
557             !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
558                 goto drop;
559
560         /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
561         if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
562                      ieee80211_is_mgmt(hdr->frame_control) &&
563                      !ieee80211_is_deauth(hdr->frame_control) &&
564                      !ieee80211_is_disassoc(hdr->frame_control) &&
565                      !ieee80211_is_action(hdr->frame_control)))
566                 sta = NULL;
567
568         if (sta) {
569                 if (iwl_mvm_defer_tx(mvm, sta, skb))
570                         return;
571                 if (iwl_mvm_tx_skb(mvm, skb, sta))
572                         goto drop;
573                 return;
574         }
575
576         if (iwl_mvm_tx_skb_non_sta(mvm, skb))
577                 goto drop;
578         return;
579  drop:
580         ieee80211_free_txskb(hw, skb);
581 }
582
583 static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
584 {
585         if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
586                 return false;
587         return true;
588 }
589
590 static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
591 {
592         if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
593                 return false;
594         if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
595                 return true;
596
597         /* enabled by default */
598         return true;
599 }
600
601 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
602                                     struct ieee80211_vif *vif,
603                                     enum ieee80211_ampdu_mlme_action action,
604                                     struct ieee80211_sta *sta, u16 tid,
605                                     u16 *ssn, u8 buf_size)
606 {
607         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
608         int ret;
609         bool tx_agg_ref = false;
610
611         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
612                      sta->addr, tid, action);
613
614         if (!(mvm->nvm_data->sku_cap_11n_enable))
615                 return -EACCES;
616
617         /* return from D0i3 before starting a new Tx aggregation */
618         switch (action) {
619         case IEEE80211_AMPDU_TX_START:
620         case IEEE80211_AMPDU_TX_STOP_CONT:
621         case IEEE80211_AMPDU_TX_STOP_FLUSH:
622         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
623         case IEEE80211_AMPDU_TX_OPERATIONAL:
624                 /*
625                  * for tx start, wait synchronously until D0i3 exit to
626                  * get the correct sequence number for the tid.
627                  * additionally, some other ampdu actions use direct
628                  * target access, which is not handled automatically
629                  * by the trans layer (unlike commands), so wait for
630                  * d0i3 exit in these cases as well.
631                  */
632                 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
633                 if (ret)
634                         return ret;
635
636                 tx_agg_ref = true;
637                 break;
638         default:
639                 break;
640         }
641
642         mutex_lock(&mvm->mutex);
643
644         switch (action) {
645         case IEEE80211_AMPDU_RX_START:
646                 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
647                         ret = -EINVAL;
648                         break;
649                 }
650                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
651                 break;
652         case IEEE80211_AMPDU_RX_STOP:
653                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
654                 break;
655         case IEEE80211_AMPDU_TX_START:
656                 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
657                         ret = -EINVAL;
658                         break;
659                 }
660                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
661                 break;
662         case IEEE80211_AMPDU_TX_STOP_CONT:
663                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
664                 break;
665         case IEEE80211_AMPDU_TX_STOP_FLUSH:
666         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
667                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
668                 break;
669         case IEEE80211_AMPDU_TX_OPERATIONAL:
670                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
671                 break;
672         default:
673                 WARN_ON_ONCE(1);
674                 ret = -EINVAL;
675                 break;
676         }
677         mutex_unlock(&mvm->mutex);
678
679         /*
680          * If the tid is marked as started, we won't use it for offloaded
681          * traffic on the next D0i3 entry. It's safe to unref.
682          */
683         if (tx_agg_ref)
684                 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
685
686         return ret;
687 }
688
689 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
690                                      struct ieee80211_vif *vif)
691 {
692         struct iwl_mvm *mvm = data;
693         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
694
695         mvmvif->uploaded = false;
696         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
697
698         /* does this make sense at all? */
699         mvmvif->color++;
700
701         spin_lock_bh(&mvm->time_event_lock);
702         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
703         spin_unlock_bh(&mvm->time_event_lock);
704
705         mvmvif->phy_ctxt = NULL;
706         memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
707 }
708
709 static ssize_t iwl_mvm_read_coredump(char *buffer, loff_t offset, size_t count,
710                                      const void *data, size_t datalen)
711 {
712         const struct iwl_mvm_dump_ptrs *dump_ptrs = data;
713         ssize_t bytes_read;
714         ssize_t bytes_read_trans;
715
716         if (offset < dump_ptrs->op_mode_len) {
717                 bytes_read = min_t(ssize_t, count,
718                                    dump_ptrs->op_mode_len - offset);
719                 memcpy(buffer, (u8 *)dump_ptrs->op_mode_ptr + offset,
720                        bytes_read);
721                 offset += bytes_read;
722                 count -= bytes_read;
723
724                 if (count == 0)
725                         return bytes_read;
726         } else {
727                 bytes_read = 0;
728         }
729
730         if (!dump_ptrs->trans_ptr)
731                 return bytes_read;
732
733         offset -= dump_ptrs->op_mode_len;
734         bytes_read_trans = min_t(ssize_t, count,
735                                  dump_ptrs->trans_ptr->len - offset);
736         memcpy(buffer + bytes_read,
737                (u8 *)dump_ptrs->trans_ptr->data + offset,
738                bytes_read_trans);
739
740         return bytes_read + bytes_read_trans;
741 }
742
743 static void iwl_mvm_free_coredump(const void *data)
744 {
745         const struct iwl_mvm_dump_ptrs *fw_error_dump = data;
746
747         vfree(fw_error_dump->op_mode_ptr);
748         vfree(fw_error_dump->trans_ptr);
749         kfree(fw_error_dump);
750 }
751
752 void iwl_mvm_fw_error_dump(struct iwl_mvm *mvm)
753 {
754         struct iwl_fw_error_dump_file *dump_file;
755         struct iwl_fw_error_dump_data *dump_data;
756         struct iwl_fw_error_dump_info *dump_info;
757         struct iwl_mvm_dump_ptrs *fw_error_dump;
758         const struct fw_img *img;
759         u32 sram_len, sram_ofs;
760         u32 file_len, rxf_len;
761         unsigned long flags;
762         int reg_val;
763
764         lockdep_assert_held(&mvm->mutex);
765
766         fw_error_dump = kzalloc(sizeof(*fw_error_dump), GFP_KERNEL);
767         if (!fw_error_dump)
768                 return;
769
770         img = &mvm->fw->img[mvm->cur_ucode];
771         sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
772         sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
773
774         /* reading buffer size */
775         reg_val = iwl_trans_read_prph(mvm->trans, RXF_SIZE_ADDR);
776         rxf_len = (reg_val & RXF_SIZE_BYTE_CNT_MSK) >> RXF_SIZE_BYTE_CND_POS;
777
778         /* the register holds the value divided by 128 */
779         rxf_len = rxf_len << 7;
780
781         file_len = sizeof(*dump_file) +
782                    sizeof(*dump_data) * 3 +
783                    sram_len +
784                    rxf_len +
785                    sizeof(*dump_info);
786
787         dump_file = vzalloc(file_len);
788         if (!dump_file) {
789                 kfree(fw_error_dump);
790                 return;
791         }
792
793         fw_error_dump->op_mode_ptr = dump_file;
794
795         dump_file->barker = cpu_to_le32(IWL_FW_ERROR_DUMP_BARKER);
796         dump_data = (void *)dump_file->data;
797
798         dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_DEV_FW_INFO);
799         dump_data->len = cpu_to_le32(sizeof(*dump_info));
800         dump_info = (void *) dump_data->data;
801         dump_info->device_family =
802                 mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000 ?
803                         cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_7) :
804                         cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_8);
805         memcpy(dump_info->fw_human_readable, mvm->fw->human_readable,
806                sizeof(dump_info->fw_human_readable));
807         strncpy(dump_info->dev_human_readable, mvm->cfg->name,
808                 sizeof(dump_info->dev_human_readable));
809         strncpy(dump_info->bus_human_readable, mvm->dev->bus->name,
810                 sizeof(dump_info->bus_human_readable));
811
812         dump_data = iwl_fw_error_next_data(dump_data);
813         dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RXF);
814         dump_data->len = cpu_to_le32(rxf_len);
815
816         if (iwl_trans_grab_nic_access(mvm->trans, false, &flags)) {
817                 u32 *rxf = (void *)dump_data->data;
818                 int i;
819
820                 for (i = 0; i < (rxf_len / sizeof(u32)); i++) {
821                         iwl_trans_write_prph(mvm->trans,
822                                              RXF_LD_FENCE_OFFSET_ADDR,
823                                              i * sizeof(u32));
824                         rxf[i] = iwl_trans_read_prph(mvm->trans,
825                                                      RXF_FIFO_RD_FENCE_ADDR);
826                 }
827                 iwl_trans_release_nic_access(mvm->trans, &flags);
828         }
829
830         dump_data = iwl_fw_error_next_data(dump_data);
831         dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_SRAM);
832         dump_data->len = cpu_to_le32(sram_len);
833         iwl_trans_read_mem_bytes(mvm->trans, sram_ofs, dump_data->data,
834                                  sram_len);
835
836         fw_error_dump->trans_ptr = iwl_trans_dump_data(mvm->trans);
837         fw_error_dump->op_mode_len = file_len;
838         if (fw_error_dump->trans_ptr)
839                 file_len += fw_error_dump->trans_ptr->len;
840         dump_file->file_len = cpu_to_le32(file_len);
841
842         dev_coredumpm(mvm->trans->dev, THIS_MODULE, fw_error_dump, 0,
843                       GFP_KERNEL, iwl_mvm_read_coredump, iwl_mvm_free_coredump);
844 }
845
846 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
847 {
848         /* clear the D3 reconfig, we only need it to avoid dumping a
849          * firmware coredump on reconfiguration, we shouldn't do that
850          * on D3->D0 transition
851          */
852         if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status))
853                 iwl_mvm_fw_error_dump(mvm);
854
855         iwl_trans_stop_device(mvm->trans);
856
857         mvm->scan_status = IWL_MVM_SCAN_NONE;
858         mvm->ps_disabled = false;
859         mvm->calibrating = false;
860
861         /* just in case one was running */
862         ieee80211_remain_on_channel_expired(mvm->hw);
863
864         ieee80211_iterate_active_interfaces_atomic(
865                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
866                 iwl_mvm_cleanup_iterator, mvm);
867
868         mvm->p2p_device_vif = NULL;
869         mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
870
871         iwl_mvm_reset_phy_ctxts(mvm);
872         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
873         memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
874         memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
875         memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
876         memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
877         memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
878         memset(&mvm->bt_ack_kill_msk, 0, sizeof(mvm->bt_ack_kill_msk));
879         memset(&mvm->bt_cts_kill_msk, 0, sizeof(mvm->bt_cts_kill_msk));
880
881         ieee80211_wake_queues(mvm->hw);
882
883         /* cleanup all stale references (scan, roc), but keep the
884          * ucode_down ref until reconfig is complete */
885         iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
886
887         /* clear any stale d0i3 state */
888         clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
889
890         mvm->vif_count = 0;
891         mvm->rx_ba_sessions = 0;
892 }
893
894 int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
895 {
896         int ret;
897
898         lockdep_assert_held(&mvm->mutex);
899
900         /* Clean up some internal and mac80211 state on restart */
901         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
902                 iwl_mvm_restart_cleanup(mvm);
903
904         ret = iwl_mvm_up(mvm);
905
906         if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
907                 /* Something went wrong - we need to finish some cleanup
908                  * that normally iwl_mvm_mac_restart_complete() below
909                  * would do.
910                  */
911                 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
912                 iwl_mvm_d0i3_enable_tx(mvm, NULL);
913         }
914
915         return ret;
916 }
917
918 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
919 {
920         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
921         int ret;
922
923         mutex_lock(&mvm->mutex);
924         ret = __iwl_mvm_mac_start(mvm);
925         mutex_unlock(&mvm->mutex);
926
927         return ret;
928 }
929
930 static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
931 {
932         int ret;
933
934         mutex_lock(&mvm->mutex);
935
936         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
937         iwl_mvm_d0i3_enable_tx(mvm, NULL);
938         ret = iwl_mvm_update_quotas(mvm, NULL);
939         if (ret)
940                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
941                         ret);
942
943         /* allow transport/FW low power modes */
944         iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
945
946         mutex_unlock(&mvm->mutex);
947 }
948
949 static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
950 {
951         bool exit_now;
952
953         if (!iwl_mvm_is_d0i3_supported(mvm))
954                 return;
955
956         mutex_lock(&mvm->d0i3_suspend_mutex);
957         __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
958         exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
959                                         &mvm->d0i3_suspend_flags);
960         mutex_unlock(&mvm->d0i3_suspend_mutex);
961
962         if (exit_now) {
963                 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
964                 _iwl_mvm_exit_d0i3(mvm);
965         }
966 }
967
968 static void
969 iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
970                               enum ieee80211_reconfig_type reconfig_type)
971 {
972         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
973
974         switch (reconfig_type) {
975         case IEEE80211_RECONFIG_TYPE_RESTART:
976                 iwl_mvm_restart_complete(mvm);
977                 break;
978         case IEEE80211_RECONFIG_TYPE_SUSPEND:
979                 iwl_mvm_resume_complete(mvm);
980                 break;
981         }
982 }
983
984 void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
985 {
986         lockdep_assert_held(&mvm->mutex);
987
988         /* disallow low power states when the FW is down */
989         iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
990
991         /* async_handlers_wk is now blocked */
992
993         /*
994          * The work item could be running or queued if the
995          * ROC time event stops just as we get here.
996          */
997         cancel_work_sync(&mvm->roc_done_wk);
998
999         iwl_trans_stop_device(mvm->trans);
1000
1001         iwl_mvm_async_handlers_purge(mvm);
1002         /* async_handlers_list is empty and will stay empty: HW is stopped */
1003
1004         /* the fw is stopped, the aux sta is dead: clean up driver state */
1005         iwl_mvm_del_aux_sta(mvm);
1006
1007         mvm->ucode_loaded = false;
1008 }
1009
1010 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1011 {
1012         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1013
1014         flush_work(&mvm->d0i3_exit_work);
1015         flush_work(&mvm->async_handlers_wk);
1016         flush_work(&mvm->fw_error_dump_wk);
1017
1018         mutex_lock(&mvm->mutex);
1019         __iwl_mvm_mac_stop(mvm);
1020         mutex_unlock(&mvm->mutex);
1021
1022         /*
1023          * The worker might have been waiting for the mutex, let it run and
1024          * discover that its list is now empty.
1025          */
1026         cancel_work_sync(&mvm->async_handlers_wk);
1027 }
1028
1029 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1030 {
1031         u16 i;
1032
1033         lockdep_assert_held(&mvm->mutex);
1034
1035         for (i = 0; i < NUM_PHY_CTX; i++)
1036                 if (!mvm->phy_ctxts[i].ref)
1037                         return &mvm->phy_ctxts[i];
1038
1039         IWL_ERR(mvm, "No available PHY context\n");
1040         return NULL;
1041 }
1042
1043 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1044                                 s8 tx_power)
1045 {
1046         /* FW is in charge of regulatory enforcement */
1047         struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
1048                 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
1049                 .pwr_restriction = cpu_to_le16(tx_power),
1050         };
1051
1052         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0,
1053                                     sizeof(reduce_txpwr_cmd),
1054                                     &reduce_txpwr_cmd);
1055 }
1056
1057 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1058                                      struct ieee80211_vif *vif)
1059 {
1060         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1061         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1062         int ret;
1063
1064         /*
1065          * make sure D0i3 exit is completed, otherwise a target access
1066          * during tx queue configuration could be done when still in
1067          * D0i3 state.
1068          */
1069         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1070         if (ret)
1071                 return ret;
1072
1073         /*
1074          * Not much to do here. The stack will not allow interface
1075          * types or combinations that we didn't advertise, so we
1076          * don't really have to check the types.
1077          */
1078
1079         mutex_lock(&mvm->mutex);
1080
1081         /* Allocate resources for the MAC context, and add it to the fw  */
1082         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1083         if (ret)
1084                 goto out_unlock;
1085
1086         /* Counting number of interfaces is needed for legacy PM */
1087         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1088                 mvm->vif_count++;
1089
1090         /*
1091          * The AP binding flow can be done only after the beacon
1092          * template is configured (which happens only in the mac80211
1093          * start_ap() flow), and adding the broadcast station can happen
1094          * only after the binding.
1095          * In addition, since modifying the MAC before adding a bcast
1096          * station is not allowed by the FW, delay the adding of MAC context to
1097          * the point where we can also add the bcast station.
1098          * In short: there's not much we can do at this point, other than
1099          * allocating resources :)
1100          */
1101         if (vif->type == NL80211_IFTYPE_AP ||
1102             vif->type == NL80211_IFTYPE_ADHOC) {
1103                 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1104                 if (ret) {
1105                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1106                         goto out_release;
1107                 }
1108
1109                 iwl_mvm_vif_dbgfs_register(mvm, vif);
1110                 goto out_unlock;
1111         }
1112
1113         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1114         if (ret)
1115                 goto out_release;
1116
1117         ret = iwl_mvm_power_update_mac(mvm);
1118         if (ret)
1119                 goto out_release;
1120
1121         /* beacon filtering */
1122         ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1123         if (ret)
1124                 goto out_remove_mac;
1125
1126         if (!mvm->bf_allowed_vif &&
1127             vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1128                 mvm->bf_allowed_vif = mvmvif;
1129                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1130                                      IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1131         }
1132
1133         /*
1134          * P2P_DEVICE interface does not have a channel context assigned to it,
1135          * so a dedicated PHY context is allocated to it and the corresponding
1136          * MAC context is bound to it at this stage.
1137          */
1138         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1139
1140                 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1141                 if (!mvmvif->phy_ctxt) {
1142                         ret = -ENOSPC;
1143                         goto out_free_bf;
1144                 }
1145
1146                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1147                 ret = iwl_mvm_binding_add_vif(mvm, vif);
1148                 if (ret)
1149                         goto out_unref_phy;
1150
1151                 ret = iwl_mvm_add_bcast_sta(mvm, vif);
1152                 if (ret)
1153                         goto out_unbind;
1154
1155                 /* Save a pointer to p2p device vif, so it can later be used to
1156                  * update the p2p device MAC when a GO is started/stopped */
1157                 mvm->p2p_device_vif = vif;
1158         }
1159
1160         iwl_mvm_vif_dbgfs_register(mvm, vif);
1161         goto out_unlock;
1162
1163  out_unbind:
1164         iwl_mvm_binding_remove_vif(mvm, vif);
1165  out_unref_phy:
1166         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1167  out_free_bf:
1168         if (mvm->bf_allowed_vif == mvmvif) {
1169                 mvm->bf_allowed_vif = NULL;
1170                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1171                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1172         }
1173  out_remove_mac:
1174         mvmvif->phy_ctxt = NULL;
1175         iwl_mvm_mac_ctxt_remove(mvm, vif);
1176  out_release:
1177         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1178                 mvm->vif_count--;
1179
1180         iwl_mvm_mac_ctxt_release(mvm, vif);
1181  out_unlock:
1182         mutex_unlock(&mvm->mutex);
1183
1184         iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1185
1186         return ret;
1187 }
1188
1189 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1190                                         struct ieee80211_vif *vif)
1191 {
1192         u32 tfd_msk = iwl_mvm_mac_get_queues_mask(vif);
1193
1194         if (tfd_msk) {
1195                 mutex_lock(&mvm->mutex);
1196                 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
1197                 mutex_unlock(&mvm->mutex);
1198         }
1199
1200         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1201                 /*
1202                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
1203                  * We assume here that all the packets sent to the OFFCHANNEL
1204                  * queue are sent in ROC session.
1205                  */
1206                 flush_work(&mvm->roc_done_wk);
1207         } else {
1208                 /*
1209                  * By now, all the AC queues are empty. The AGG queues are
1210                  * empty too. We already got all the Tx responses for all the
1211                  * packets in the queues. The drain work can have been
1212                  * triggered. Flush it.
1213                  */
1214                 flush_work(&mvm->sta_drained_wk);
1215         }
1216 }
1217
1218 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1219                                          struct ieee80211_vif *vif)
1220 {
1221         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1222         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1223
1224         iwl_mvm_prepare_mac_removal(mvm, vif);
1225
1226         mutex_lock(&mvm->mutex);
1227
1228         if (mvm->bf_allowed_vif == mvmvif) {
1229                 mvm->bf_allowed_vif = NULL;
1230                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1231                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1232         }
1233
1234         iwl_mvm_vif_dbgfs_clean(mvm, vif);
1235
1236         /*
1237          * For AP/GO interface, the tear down of the resources allocated to the
1238          * interface is be handled as part of the stop_ap flow.
1239          */
1240         if (vif->type == NL80211_IFTYPE_AP ||
1241             vif->type == NL80211_IFTYPE_ADHOC) {
1242 #ifdef CONFIG_NL80211_TESTMODE
1243                 if (vif == mvm->noa_vif) {
1244                         mvm->noa_vif = NULL;
1245                         mvm->noa_duration = 0;
1246                 }
1247 #endif
1248                 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1249                 goto out_release;
1250         }
1251
1252         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1253                 mvm->p2p_device_vif = NULL;
1254                 iwl_mvm_rm_bcast_sta(mvm, vif);
1255                 iwl_mvm_binding_remove_vif(mvm, vif);
1256                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1257                 mvmvif->phy_ctxt = NULL;
1258         }
1259
1260         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1261                 mvm->vif_count--;
1262
1263         iwl_mvm_power_update_mac(mvm);
1264         iwl_mvm_mac_ctxt_remove(mvm, vif);
1265
1266 out_release:
1267         iwl_mvm_mac_ctxt_release(mvm, vif);
1268         mutex_unlock(&mvm->mutex);
1269 }
1270
1271 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1272 {
1273         return 0;
1274 }
1275
1276 struct iwl_mvm_mc_iter_data {
1277         struct iwl_mvm *mvm;
1278         int port_id;
1279 };
1280
1281 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1282                                       struct ieee80211_vif *vif)
1283 {
1284         struct iwl_mvm_mc_iter_data *data = _data;
1285         struct iwl_mvm *mvm = data->mvm;
1286         struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1287         int ret, len;
1288
1289         /* if we don't have free ports, mcast frames will be dropped */
1290         if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1291                 return;
1292
1293         if (vif->type != NL80211_IFTYPE_STATION ||
1294             !vif->bss_conf.assoc)
1295                 return;
1296
1297         cmd->port_id = data->port_id++;
1298         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1299         len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1300
1301         ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
1302         if (ret)
1303                 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1304 }
1305
1306 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1307 {
1308         struct iwl_mvm_mc_iter_data iter_data = {
1309                 .mvm = mvm,
1310         };
1311
1312         lockdep_assert_held(&mvm->mutex);
1313
1314         if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1315                 return;
1316
1317         ieee80211_iterate_active_interfaces_atomic(
1318                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1319                 iwl_mvm_mc_iface_iterator, &iter_data);
1320 }
1321
1322 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1323                                      struct netdev_hw_addr_list *mc_list)
1324 {
1325         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1326         struct iwl_mcast_filter_cmd *cmd;
1327         struct netdev_hw_addr *addr;
1328         int addr_count;
1329         bool pass_all;
1330         int len;
1331
1332         addr_count = netdev_hw_addr_list_count(mc_list);
1333         pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1334                    IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1335         if (pass_all)
1336                 addr_count = 0;
1337
1338         len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1339         cmd = kzalloc(len, GFP_ATOMIC);
1340         if (!cmd)
1341                 return 0;
1342
1343         if (pass_all) {
1344                 cmd->pass_all = 1;
1345                 return (u64)(unsigned long)cmd;
1346         }
1347
1348         netdev_hw_addr_list_for_each(addr, mc_list) {
1349                 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1350                                    cmd->count, addr->addr);
1351                 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1352                        addr->addr, ETH_ALEN);
1353                 cmd->count++;
1354         }
1355
1356         return (u64)(unsigned long)cmd;
1357 }
1358
1359 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1360                                      unsigned int changed_flags,
1361                                      unsigned int *total_flags,
1362                                      u64 multicast)
1363 {
1364         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1365         struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1366
1367         mutex_lock(&mvm->mutex);
1368
1369         /* replace previous configuration */
1370         kfree(mvm->mcast_filter_cmd);
1371         mvm->mcast_filter_cmd = cmd;
1372
1373         if (!cmd)
1374                 goto out;
1375
1376         iwl_mvm_recalc_multicast(mvm);
1377 out:
1378         mutex_unlock(&mvm->mutex);
1379         *total_flags = 0;
1380 }
1381
1382 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1383 struct iwl_bcast_iter_data {
1384         struct iwl_mvm *mvm;
1385         struct iwl_bcast_filter_cmd *cmd;
1386         u8 current_filter;
1387 };
1388
1389 static void
1390 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1391                          const struct iwl_fw_bcast_filter *in_filter,
1392                          struct iwl_fw_bcast_filter *out_filter)
1393 {
1394         struct iwl_fw_bcast_filter_attr *attr;
1395         int i;
1396
1397         memcpy(out_filter, in_filter, sizeof(*out_filter));
1398
1399         for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1400                 attr = &out_filter->attrs[i];
1401
1402                 if (!attr->mask)
1403                         break;
1404
1405                 switch (attr->reserved1) {
1406                 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1407                         if (vif->bss_conf.arp_addr_cnt != 1) {
1408                                 attr->mask = 0;
1409                                 continue;
1410                         }
1411
1412                         attr->val = vif->bss_conf.arp_addr_list[0];
1413                         break;
1414                 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1415                         attr->val = *(__be32 *)&vif->addr[2];
1416                         break;
1417                 default:
1418                         break;
1419                 }
1420                 attr->reserved1 = 0;
1421                 out_filter->num_attrs++;
1422         }
1423 }
1424
1425 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1426                                           struct ieee80211_vif *vif)
1427 {
1428         struct iwl_bcast_iter_data *data = _data;
1429         struct iwl_mvm *mvm = data->mvm;
1430         struct iwl_bcast_filter_cmd *cmd = data->cmd;
1431         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1432         struct iwl_fw_bcast_mac *bcast_mac;
1433         int i;
1434
1435         if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1436                 return;
1437
1438         bcast_mac = &cmd->macs[mvmvif->id];
1439
1440         /*
1441          * enable filtering only for associated stations, but not for P2P
1442          * Clients
1443          */
1444         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1445             !vif->bss_conf.assoc)
1446                 return;
1447
1448         bcast_mac->default_discard = 1;
1449
1450         /* copy all configured filters */
1451         for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1452                 /*
1453                  * Make sure we don't exceed our filters limit.
1454                  * if there is still a valid filter to be configured,
1455                  * be on the safe side and just allow bcast for this mac.
1456                  */
1457                 if (WARN_ON_ONCE(data->current_filter >=
1458                                  ARRAY_SIZE(cmd->filters))) {
1459                         bcast_mac->default_discard = 0;
1460                         bcast_mac->attached_filters = 0;
1461                         break;
1462                 }
1463
1464                 iwl_mvm_set_bcast_filter(vif,
1465                                          &mvm->bcast_filters[i],
1466                                          &cmd->filters[data->current_filter]);
1467
1468                 /* skip current filter if it contains no attributes */
1469                 if (!cmd->filters[data->current_filter].num_attrs)
1470                         continue;
1471
1472                 /* attach the filter to current mac */
1473                 bcast_mac->attached_filters |=
1474                                 cpu_to_le16(BIT(data->current_filter));
1475
1476                 data->current_filter++;
1477         }
1478 }
1479
1480 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1481                                     struct iwl_bcast_filter_cmd *cmd)
1482 {
1483         struct iwl_bcast_iter_data iter_data = {
1484                 .mvm = mvm,
1485                 .cmd = cmd,
1486         };
1487
1488         if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1489                 return false;
1490
1491         memset(cmd, 0, sizeof(*cmd));
1492         cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1493         cmd->max_macs = ARRAY_SIZE(cmd->macs);
1494
1495 #ifdef CONFIG_IWLWIFI_DEBUGFS
1496         /* use debugfs filters/macs if override is configured */
1497         if (mvm->dbgfs_bcast_filtering.override) {
1498                 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1499                        sizeof(cmd->filters));
1500                 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1501                        sizeof(cmd->macs));
1502                 return true;
1503         }
1504 #endif
1505
1506         /* if no filters are configured, do nothing */
1507         if (!mvm->bcast_filters)
1508                 return false;
1509
1510         /* configure and attach these filters for each associated sta vif */
1511         ieee80211_iterate_active_interfaces(
1512                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1513                 iwl_mvm_bcast_filter_iterator, &iter_data);
1514
1515         return true;
1516 }
1517 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1518                                           struct ieee80211_vif *vif)
1519 {
1520         struct iwl_bcast_filter_cmd cmd;
1521
1522         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1523                 return 0;
1524
1525         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1526                 return 0;
1527
1528         return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1529                                     sizeof(cmd), &cmd);
1530 }
1531 #else
1532 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1533                                                  struct ieee80211_vif *vif)
1534 {
1535         return 0;
1536 }
1537 #endif
1538
1539 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1540                                              struct ieee80211_vif *vif,
1541                                              struct ieee80211_bss_conf *bss_conf,
1542                                              u32 changes)
1543 {
1544         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1545         int ret;
1546
1547         /*
1548          * Re-calculate the tsf id, as the master-slave relations depend on the
1549          * beacon interval, which was not known when the station interface was
1550          * added.
1551          */
1552         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1553                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1554
1555         /*
1556          * If we're not associated yet, take the (new) BSSID before associating
1557          * so the firmware knows. If we're already associated, then use the old
1558          * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
1559          * branch for disassociation below.
1560          */
1561         if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
1562                 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1563
1564         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
1565         if (ret)
1566                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1567
1568         /* after sending it once, adopt mac80211 data */
1569         memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1570         mvmvif->associated = bss_conf->assoc;
1571
1572         if (changes & BSS_CHANGED_ASSOC) {
1573                 if (bss_conf->assoc) {
1574                         /* add quota for this interface */
1575                         ret = iwl_mvm_update_quotas(mvm, NULL);
1576                         if (ret) {
1577                                 IWL_ERR(mvm, "failed to update quotas\n");
1578                                 return;
1579                         }
1580
1581                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1582                                      &mvm->status)) {
1583                                 /*
1584                                  * If we're restarting then the firmware will
1585                                  * obviously have lost synchronisation with
1586                                  * the AP. It will attempt to synchronise by
1587                                  * itself, but we can make it more reliable by
1588                                  * scheduling a session protection time event.
1589                                  *
1590                                  * The firmware needs to receive a beacon to
1591                                  * catch up with synchronisation, use 110% of
1592                                  * the beacon interval.
1593                                  *
1594                                  * Set a large maximum delay to allow for more
1595                                  * than a single interface.
1596                                  */
1597                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1598                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
1599                                                         5 * dur, false);
1600                         }
1601
1602                         iwl_mvm_sf_update(mvm, vif, false);
1603                         iwl_mvm_power_vif_assoc(mvm, vif);
1604                         if (vif->p2p) {
1605                                 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1606                                 iwl_mvm_update_smps(mvm, vif,
1607                                                     IWL_MVM_SMPS_REQ_PROT,
1608                                                     IEEE80211_SMPS_DYNAMIC);
1609                         }
1610                 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1611                         /*
1612                          * If update fails - SF might be running in associated
1613                          * mode while disassociated - which is forbidden.
1614                          */
1615                         WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1616                                   "Failed to update SF upon disassociation\n");
1617
1618                         /* remove AP station now that the MAC is unassoc */
1619                         ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1620                         if (ret)
1621                                 IWL_ERR(mvm, "failed to remove AP station\n");
1622
1623                         if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1624                                 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1625                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1626                         /* remove quota for this interface */
1627                         ret = iwl_mvm_update_quotas(mvm, NULL);
1628                         if (ret)
1629                                 IWL_ERR(mvm, "failed to update quotas\n");
1630
1631                         if (vif->p2p)
1632                                 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1633
1634                         /* this will take the cleared BSSID from bss_conf */
1635                         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1636                         if (ret)
1637                                 IWL_ERR(mvm,
1638                                         "failed to update MAC %pM (clear after unassoc)\n",
1639                                         vif->addr);
1640                 }
1641
1642                 iwl_mvm_recalc_multicast(mvm);
1643                 iwl_mvm_configure_bcast_filter(mvm, vif);
1644
1645                 /* reset rssi values */
1646                 mvmvif->bf_data.ave_beacon_signal = 0;
1647
1648                 iwl_mvm_bt_coex_vif_change(mvm);
1649                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1650                                     IEEE80211_SMPS_AUTOMATIC);
1651         } else if (changes & BSS_CHANGED_BEACON_INFO) {
1652                 /*
1653                  * We received a beacon _after_ association so
1654                  * remove the session protection.
1655                  */
1656                 iwl_mvm_remove_time_event(mvm, mvmvif,
1657                                           &mvmvif->time_event_data);
1658         }
1659
1660         if (changes & BSS_CHANGED_BEACON_INFO) {
1661                 iwl_mvm_sf_update(mvm, vif, false);
1662                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1663         }
1664
1665         if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS)) {
1666                 ret = iwl_mvm_power_update_mac(mvm);
1667                 if (ret)
1668                         IWL_ERR(mvm, "failed to update power mode\n");
1669         }
1670
1671         if (changes & BSS_CHANGED_TXPOWER) {
1672                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1673                                 bss_conf->txpower);
1674                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1675         }
1676
1677         if (changes & BSS_CHANGED_CQM) {
1678                 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
1679                 /* reset cqm events tracking */
1680                 mvmvif->bf_data.last_cqm_event = 0;
1681                 if (mvmvif->bf_data.bf_enabled) {
1682                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1683                         if (ret)
1684                                 IWL_ERR(mvm,
1685                                         "failed to update CQM thresholds\n");
1686                 }
1687         }
1688
1689         if (changes & BSS_CHANGED_ARP_FILTER) {
1690                 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
1691                 iwl_mvm_configure_bcast_filter(mvm, vif);
1692         }
1693 }
1694
1695 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1696                                  struct ieee80211_vif *vif)
1697 {
1698         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1699         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1700         int ret;
1701
1702         /*
1703          * iwl_mvm_mac_ctxt_add() might read directly from the device
1704          * (the system time), so make sure it is available.
1705          */
1706         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
1707         if (ret)
1708                 return ret;
1709
1710         mutex_lock(&mvm->mutex);
1711
1712         /* Send the beacon template */
1713         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1714         if (ret)
1715                 goto out_unlock;
1716
1717         /*
1718          * Re-calculate the tsf id, as the master-slave relations depend on the
1719          * beacon interval, which was not known when the AP interface was added.
1720          */
1721         if (vif->type == NL80211_IFTYPE_AP)
1722                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1723
1724         /* Add the mac context */
1725         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1726         if (ret)
1727                 goto out_unlock;
1728
1729         /* Perform the binding */
1730         ret = iwl_mvm_binding_add_vif(mvm, vif);
1731         if (ret)
1732                 goto out_remove;
1733
1734         /* Send the bcast station. At this stage the TBTT and DTIM time events
1735          * are added and applied to the scheduler */
1736         ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1737         if (ret)
1738                 goto out_unbind;
1739
1740         /* must be set before quota calculations */
1741         mvmvif->ap_ibss_active = true;
1742
1743         /* power updated needs to be done before quotas */
1744         iwl_mvm_power_update_mac(mvm);
1745
1746         ret = iwl_mvm_update_quotas(mvm, NULL);
1747         if (ret)
1748                 goto out_quota_failed;
1749
1750         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1751         if (vif->p2p && mvm->p2p_device_vif)
1752                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
1753
1754         iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1755
1756         iwl_mvm_bt_coex_vif_change(mvm);
1757
1758         /* we don't support TDLS during DCM */
1759         if (iwl_mvm_phy_ctx_count(mvm) > 1)
1760                 iwl_mvm_teardown_tdls_peers(mvm);
1761
1762         mutex_unlock(&mvm->mutex);
1763         return 0;
1764
1765 out_quota_failed:
1766         iwl_mvm_power_update_mac(mvm);
1767         mvmvif->ap_ibss_active = false;
1768         iwl_mvm_send_rm_bcast_sta(mvm, vif);
1769 out_unbind:
1770         iwl_mvm_binding_remove_vif(mvm, vif);
1771 out_remove:
1772         iwl_mvm_mac_ctxt_remove(mvm, vif);
1773 out_unlock:
1774         mutex_unlock(&mvm->mutex);
1775         iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
1776         return ret;
1777 }
1778
1779 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1780                                  struct ieee80211_vif *vif)
1781 {
1782         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1783         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1784
1785         iwl_mvm_prepare_mac_removal(mvm, vif);
1786
1787         mutex_lock(&mvm->mutex);
1788
1789         /* Handle AP stop while in CSA */
1790         if (rcu_access_pointer(mvm->csa_vif) == vif) {
1791                 iwl_mvm_remove_time_event(mvm, mvmvif,
1792                                           &mvmvif->time_event_data);
1793                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1794         }
1795
1796         if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
1797                 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1798                 mvm->csa_tx_block_bcn_timeout = 0;
1799         }
1800
1801         mvmvif->ap_ibss_active = false;
1802         mvm->ap_last_beacon_gp2 = 0;
1803
1804         iwl_mvm_bt_coex_vif_change(mvm);
1805
1806         iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
1807
1808         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1809         if (vif->p2p && mvm->p2p_device_vif)
1810                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
1811
1812         iwl_mvm_update_quotas(mvm, NULL);
1813         iwl_mvm_send_rm_bcast_sta(mvm, vif);
1814         iwl_mvm_binding_remove_vif(mvm, vif);
1815
1816         iwl_mvm_power_update_mac(mvm);
1817
1818         iwl_mvm_mac_ctxt_remove(mvm, vif);
1819
1820         mutex_unlock(&mvm->mutex);
1821 }
1822
1823 static void
1824 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1825                                  struct ieee80211_vif *vif,
1826                                  struct ieee80211_bss_conf *bss_conf,
1827                                  u32 changes)
1828 {
1829         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1830
1831         /* Changes will be applied when the AP/IBSS is started */
1832         if (!mvmvif->ap_ibss_active)
1833                 return;
1834
1835         if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
1836                        BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
1837             iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
1838                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1839
1840         /* Need to send a new beacon template to the FW */
1841         if (changes & BSS_CHANGED_BEACON &&
1842             iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1843                 IWL_WARN(mvm, "Failed updating beacon data\n");
1844
1845         if (changes & BSS_CHANGED_TXPOWER) {
1846                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1847                                 bss_conf->txpower);
1848                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1849         }
1850
1851 }
1852
1853 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1854                                      struct ieee80211_vif *vif,
1855                                      struct ieee80211_bss_conf *bss_conf,
1856                                      u32 changes)
1857 {
1858         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1859
1860         /*
1861          * iwl_mvm_bss_info_changed_station() might call
1862          * iwl_mvm_protect_session(), which reads directly from
1863          * the device (the system time), so make sure it is available.
1864          */
1865         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
1866                 return;
1867
1868         mutex_lock(&mvm->mutex);
1869
1870         if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
1871                 iwl_mvm_scan_offload_stop(mvm, true);
1872
1873         switch (vif->type) {
1874         case NL80211_IFTYPE_STATION:
1875                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1876                 break;
1877         case NL80211_IFTYPE_AP:
1878         case NL80211_IFTYPE_ADHOC:
1879                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1880                 break;
1881         default:
1882                 /* shouldn't happen */
1883                 WARN_ON_ONCE(1);
1884         }
1885
1886         mutex_unlock(&mvm->mutex);
1887         iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
1888 }
1889
1890 static int iwl_mvm_cancel_scan_wait_notif(struct iwl_mvm *mvm,
1891                                           enum iwl_scan_status scan_type)
1892 {
1893         int ret;
1894         bool wait_for_handlers = false;
1895
1896         mutex_lock(&mvm->mutex);
1897
1898         if (mvm->scan_status != scan_type) {
1899                 ret = 0;
1900                 /* make sure there are no pending notifications */
1901                 wait_for_handlers = true;
1902                 goto out;
1903         }
1904
1905         switch (scan_type) {
1906         case IWL_MVM_SCAN_SCHED:
1907                 ret = iwl_mvm_scan_offload_stop(mvm, true);
1908                 break;
1909         case IWL_MVM_SCAN_OS:
1910                 ret = iwl_mvm_cancel_scan(mvm);
1911                 break;
1912         case IWL_MVM_SCAN_NONE:
1913         default:
1914                 WARN_ON_ONCE(1);
1915                 ret = -EINVAL;
1916                 break;
1917         }
1918         if (ret)
1919                 goto out;
1920
1921         wait_for_handlers = true;
1922 out:
1923         mutex_unlock(&mvm->mutex);
1924
1925         /* make sure we consume the completion notification */
1926         if (wait_for_handlers)
1927                 iwl_mvm_wait_for_async_handlers(mvm);
1928
1929         return ret;
1930 }
1931 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1932                                struct ieee80211_vif *vif,
1933                                struct ieee80211_scan_request *hw_req)
1934 {
1935         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1936         struct cfg80211_scan_request *req = &hw_req->req;
1937         int ret;
1938
1939         if (req->n_channels == 0 ||
1940             req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1941                 return -EINVAL;
1942
1943         if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1944                 ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_SCHED);
1945                 if (ret)
1946                         return ret;
1947         }
1948
1949         mutex_lock(&mvm->mutex);
1950
1951         if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
1952                 ret = -EBUSY;
1953                 goto out;
1954         }
1955
1956         iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1957
1958         if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1959                 ret = iwl_mvm_scan_umac(mvm, vif, hw_req);
1960         else if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1961                 ret = iwl_mvm_unified_scan_lmac(mvm, vif, hw_req);
1962         else
1963                 ret = iwl_mvm_scan_request(mvm, vif, req);
1964
1965         if (ret)
1966                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1967 out:
1968         mutex_unlock(&mvm->mutex);
1969         return ret;
1970 }
1971
1972 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1973                                        struct ieee80211_vif *vif)
1974 {
1975         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1976
1977         mutex_lock(&mvm->mutex);
1978
1979         iwl_mvm_cancel_scan(mvm);
1980
1981         mutex_unlock(&mvm->mutex);
1982 }
1983
1984 static void
1985 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1986                                   struct ieee80211_sta *sta, u16 tids,
1987                                   int num_frames,
1988                                   enum ieee80211_frame_release_type reason,
1989                                   bool more_data)
1990 {
1991         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1992
1993         /* Called when we need to transmit (a) frame(s) from mac80211 */
1994
1995         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1996                                           tids, more_data, false);
1997 }
1998
1999 static void
2000 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2001                                     struct ieee80211_sta *sta, u16 tids,
2002                                     int num_frames,
2003                                     enum ieee80211_frame_release_type reason,
2004                                     bool more_data)
2005 {
2006         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2007
2008         /* Called when we need to transmit (a) frame(s) from agg queue */
2009
2010         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2011                                           tids, more_data, true);
2012 }
2013
2014 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2015                                    struct ieee80211_vif *vif,
2016                                    enum sta_notify_cmd cmd,
2017                                    struct ieee80211_sta *sta)
2018 {
2019         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2020         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2021         int tid;
2022
2023         switch (cmd) {
2024         case STA_NOTIFY_SLEEP:
2025                 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
2026                         ieee80211_sta_block_awake(hw, sta, true);
2027                 spin_lock_bh(&mvmsta->lock);
2028                 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2029                         struct iwl_mvm_tid_data *tid_data;
2030
2031                         tid_data = &mvmsta->tid_data[tid];
2032                         if (tid_data->state != IWL_AGG_ON &&
2033                             tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2034                                 continue;
2035                         if (iwl_mvm_tid_queued(tid_data) == 0)
2036                                 continue;
2037                         ieee80211_sta_set_buffered(sta, tid, true);
2038                 }
2039                 spin_unlock_bh(&mvmsta->lock);
2040                 /*
2041                  * The fw updates the STA to be asleep. Tx packets on the Tx
2042                  * queues to this station will not be transmitted. The fw will
2043                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2044                  */
2045                 break;
2046         case STA_NOTIFY_AWAKE:
2047                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
2048                         break;
2049                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
2050                 break;
2051         default:
2052                 break;
2053         }
2054 }
2055
2056 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2057                                        struct ieee80211_vif *vif,
2058                                        struct ieee80211_sta *sta)
2059 {
2060         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2061         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
2062
2063         /*
2064          * This is called before mac80211 does RCU synchronisation,
2065          * so here we already invalidate our internal RCU-protected
2066          * station pointer. The rest of the code will thus no longer
2067          * be able to find the station this way, and we don't rely
2068          * on further RCU synchronisation after the sta_state()
2069          * callback deleted the station.
2070          */
2071         mutex_lock(&mvm->mutex);
2072         if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2073                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2074                                    ERR_PTR(-ENOENT));
2075         mutex_unlock(&mvm->mutex);
2076 }
2077
2078 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2079                                  struct ieee80211_vif *vif,
2080                                  struct ieee80211_sta *sta,
2081                                  enum ieee80211_sta_state old_state,
2082                                  enum ieee80211_sta_state new_state)
2083 {
2084         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2085         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2086         int ret;
2087
2088         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2089                            sta->addr, old_state, new_state);
2090
2091         /* this would be a mac80211 bug ... but don't crash */
2092         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2093                 return -EINVAL;
2094
2095         /* if a STA is being removed, reuse its ID */
2096         flush_work(&mvm->sta_drained_wk);
2097
2098         mutex_lock(&mvm->mutex);
2099         if (old_state == IEEE80211_STA_NOTEXIST &&
2100             new_state == IEEE80211_STA_NONE) {
2101                 /*
2102                  * Firmware bug - it'll crash if the beacon interval is less
2103                  * than 16. We can't avoid connecting at all, so refuse the
2104                  * station state change, this will cause mac80211 to abandon
2105                  * attempts to connect to this AP, and eventually wpa_s will
2106                  * blacklist the AP...
2107                  */
2108                 if (vif->type == NL80211_IFTYPE_STATION &&
2109                     vif->bss_conf.beacon_int < 16) {
2110                         IWL_ERR(mvm,
2111                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2112                                 sta->addr, vif->bss_conf.beacon_int);
2113                         ret = -EINVAL;
2114                         goto out_unlock;
2115                 }
2116
2117                 if (sta->tdls &&
2118                     (vif->p2p ||
2119                      iwl_mvm_tdls_sta_count(mvm, NULL) ==
2120                                                 IWL_MVM_TDLS_STA_COUNT ||
2121                      iwl_mvm_phy_ctx_count(mvm) > 1)) {
2122                         IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2123                         ret = -EBUSY;
2124                         goto out_unlock;
2125                 }
2126
2127                 ret = iwl_mvm_add_sta(mvm, vif, sta);
2128                 if (sta->tdls && ret == 0)
2129                         iwl_mvm_recalc_tdls_state(mvm, vif, true);
2130         } else if (old_state == IEEE80211_STA_NONE &&
2131                    new_state == IEEE80211_STA_AUTH) {
2132                 /*
2133                  * EBS may be disabled due to previous failures reported by FW.
2134                  * Reset EBS status here assuming environment has been changed.
2135                  */
2136                 mvm->last_ebs_successful = true;
2137                 ret = 0;
2138         } else if (old_state == IEEE80211_STA_AUTH &&
2139                    new_state == IEEE80211_STA_ASSOC) {
2140                 ret = iwl_mvm_update_sta(mvm, vif, sta);
2141                 if (ret == 0)
2142                         iwl_mvm_rs_rate_init(mvm, sta,
2143                                              mvmvif->phy_ctxt->channel->band,
2144                                              true);
2145         } else if (old_state == IEEE80211_STA_ASSOC &&
2146                    new_state == IEEE80211_STA_AUTHORIZED) {
2147
2148                 /* we don't support TDLS during DCM */
2149                 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2150                         iwl_mvm_teardown_tdls_peers(mvm);
2151
2152                 /* enable beacon filtering */
2153                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2154                 ret = 0;
2155         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
2156                    new_state == IEEE80211_STA_ASSOC) {
2157                 /* disable beacon filtering */
2158                 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
2159                 ret = 0;
2160         } else if (old_state == IEEE80211_STA_ASSOC &&
2161                    new_state == IEEE80211_STA_AUTH) {
2162                 ret = 0;
2163         } else if (old_state == IEEE80211_STA_AUTH &&
2164                    new_state == IEEE80211_STA_NONE) {
2165                 ret = 0;
2166         } else if (old_state == IEEE80211_STA_NONE &&
2167                    new_state == IEEE80211_STA_NOTEXIST) {
2168                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
2169                 if (sta->tdls)
2170                         iwl_mvm_recalc_tdls_state(mvm, vif, false);
2171         } else {
2172                 ret = -EIO;
2173         }
2174  out_unlock:
2175         mutex_unlock(&mvm->mutex);
2176
2177         return ret;
2178 }
2179
2180 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2181 {
2182         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2183
2184         mvm->rts_threshold = value;
2185
2186         return 0;
2187 }
2188
2189 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2190                                   struct ieee80211_vif *vif,
2191                                   struct ieee80211_sta *sta, u32 changed)
2192 {
2193         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2194
2195         if (vif->type == NL80211_IFTYPE_STATION &&
2196             changed & IEEE80211_RC_NSS_CHANGED)
2197                 iwl_mvm_sf_update(mvm, vif, false);
2198 }
2199
2200 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2201                                struct ieee80211_vif *vif, u16 ac,
2202                                const struct ieee80211_tx_queue_params *params)
2203 {
2204         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2205         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2206
2207         mvmvif->queue_params[ac] = *params;
2208
2209         /*
2210          * No need to update right away, we'll get BSS_CHANGED_QOS
2211          * The exception is P2P_DEVICE interface which needs immediate update.
2212          */
2213         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2214                 int ret;
2215
2216                 mutex_lock(&mvm->mutex);
2217                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2218                 mutex_unlock(&mvm->mutex);
2219                 return ret;
2220         }
2221         return 0;
2222 }
2223
2224 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2225                                       struct ieee80211_vif *vif)
2226 {
2227         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2228         u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
2229                            200 + vif->bss_conf.beacon_int);
2230         u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
2231                                100 + vif->bss_conf.beacon_int);
2232
2233         if (WARN_ON_ONCE(vif->bss_conf.assoc))
2234                 return;
2235
2236         /*
2237          * iwl_mvm_protect_session() reads directly from the device
2238          * (the system time), so make sure it is available.
2239          */
2240         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2241                 return;
2242
2243         mutex_lock(&mvm->mutex);
2244         /* Try really hard to protect the session and hear a beacon */
2245         iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
2246         mutex_unlock(&mvm->mutex);
2247
2248         iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
2249 }
2250
2251 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2252                                         struct ieee80211_vif *vif,
2253                                         struct cfg80211_sched_scan_request *req,
2254                                         struct ieee80211_scan_ies *ies)
2255 {
2256         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2257         int ret;
2258
2259         if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2260                 ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_OS);
2261                 if (ret)
2262                         return ret;
2263         }
2264
2265         mutex_lock(&mvm->mutex);
2266
2267         /* Newest FW fixes sched scan while connected on another interface */
2268         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
2269                 if (!vif->bss_conf.idle) {
2270                         ret = -EBUSY;
2271                         goto out;
2272                 }
2273         } else if (!iwl_mvm_is_idle(mvm)) {
2274                 ret = -EBUSY;
2275                 goto out;
2276         }
2277
2278         if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
2279                 ret = -EBUSY;
2280                 goto out;
2281         }
2282
2283         ret = iwl_mvm_scan_offload_start(mvm, vif, req, ies);
2284         if (ret)
2285                 mvm->scan_status = IWL_MVM_SCAN_NONE;
2286
2287 out:
2288         mutex_unlock(&mvm->mutex);
2289         return ret;
2290 }
2291
2292 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2293                                        struct ieee80211_vif *vif)
2294 {
2295         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2296         int ret;
2297
2298         mutex_lock(&mvm->mutex);
2299         ret = iwl_mvm_scan_offload_stop(mvm, false);
2300         mutex_unlock(&mvm->mutex);
2301         iwl_mvm_wait_for_async_handlers(mvm);
2302
2303         return ret;
2304
2305 }
2306
2307 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2308                                enum set_key_cmd cmd,
2309                                struct ieee80211_vif *vif,
2310                                struct ieee80211_sta *sta,
2311                                struct ieee80211_key_conf *key)
2312 {
2313         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2314         int ret;
2315
2316         if (iwlwifi_mod_params.sw_crypto) {
2317                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2318                 return -EOPNOTSUPP;
2319         }
2320
2321         switch (key->cipher) {
2322         case WLAN_CIPHER_SUITE_TKIP:
2323                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2324                 /* fall-through */
2325         case WLAN_CIPHER_SUITE_CCMP:
2326                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2327                 break;
2328         case WLAN_CIPHER_SUITE_AES_CMAC:
2329                 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
2330                 break;
2331         case WLAN_CIPHER_SUITE_WEP40:
2332         case WLAN_CIPHER_SUITE_WEP104:
2333                 /* For non-client mode, only use WEP keys for TX as we probably
2334                  * don't have a station yet anyway and would then have to keep
2335                  * track of the keys, linking them to each of the clients/peers
2336                  * as they appear. For now, don't do that, for performance WEP
2337                  * offload doesn't really matter much, but we need it for some
2338                  * other offload features in client mode.
2339                  */
2340                 if (vif->type != NL80211_IFTYPE_STATION)
2341                         return 0;
2342                 break;
2343         default:
2344                 /* currently FW supports only one optional cipher scheme */
2345                 if (hw->n_cipher_schemes &&
2346                     hw->cipher_schemes->cipher == key->cipher)
2347                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2348                 else
2349                         return -EOPNOTSUPP;
2350         }
2351
2352         mutex_lock(&mvm->mutex);
2353
2354         switch (cmd) {
2355         case SET_KEY:
2356                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
2357                      vif->type == NL80211_IFTYPE_AP) && !sta) {
2358                         /*
2359                          * GTK on AP interface is a TX-only key, return 0;
2360                          * on IBSS they're per-station and because we're lazy
2361                          * we don't support them for RX, so do the same.
2362                          */
2363                         ret = 0;
2364                         key->hw_key_idx = STA_KEY_IDX_INVALID;
2365                         break;
2366                 }
2367
2368                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
2369                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
2370                 if (ret) {
2371                         IWL_WARN(mvm, "set key failed\n");
2372                         /*
2373                          * can't add key for RX, but we don't need it
2374                          * in the device for TX so still return 0
2375                          */
2376                         key->hw_key_idx = STA_KEY_IDX_INVALID;
2377                         ret = 0;
2378                 }
2379
2380                 break;
2381         case DISABLE_KEY:
2382                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
2383                         ret = 0;
2384                         break;
2385                 }
2386
2387                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
2388                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
2389                 break;
2390         default:
2391                 ret = -EINVAL;
2392         }
2393
2394         mutex_unlock(&mvm->mutex);
2395         return ret;
2396 }
2397
2398 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
2399                                         struct ieee80211_vif *vif,
2400                                         struct ieee80211_key_conf *keyconf,
2401                                         struct ieee80211_sta *sta,
2402                                         u32 iv32, u16 *phase1key)
2403 {
2404         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2405
2406         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
2407                 return;
2408
2409         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
2410 }
2411
2412
2413 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
2414                                struct iwl_rx_packet *pkt, void *data)
2415 {
2416         struct iwl_mvm *mvm =
2417                 container_of(notif_wait, struct iwl_mvm, notif_wait);
2418         struct iwl_hs20_roc_res *resp;
2419         int resp_len = iwl_rx_packet_payload_len(pkt);
2420         struct iwl_mvm_time_event_data *te_data = data;
2421
2422         if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
2423                 return true;
2424
2425         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
2426                 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
2427                 return true;
2428         }
2429
2430         resp = (void *)pkt->data;
2431
2432         IWL_DEBUG_TE(mvm,
2433                      "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
2434                      resp->status, resp->event_unique_id);
2435
2436         te_data->uid = le32_to_cpu(resp->event_unique_id);
2437         IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
2438                      te_data->uid);
2439
2440         spin_lock_bh(&mvm->time_event_lock);
2441         list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
2442         spin_unlock_bh(&mvm->time_event_lock);
2443
2444         return true;
2445 }
2446
2447 #define AUX_ROC_MAX_DELAY_ON_CHANNEL 5000
2448 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
2449                                     struct ieee80211_channel *channel,
2450                                     struct ieee80211_vif *vif,
2451                                     int duration)
2452 {
2453         int res, time_reg = DEVICE_SYSTEM_TIME_REG;
2454         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2455         struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
2456         static const u8 time_event_response[] = { HOT_SPOT_CMD };
2457         struct iwl_notification_wait wait_time_event;
2458         struct iwl_hs20_roc_req aux_roc_req = {
2459                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
2460                 .id_and_color =
2461                         cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
2462                 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
2463                 /* Set the channel info data */
2464                 .channel_info.band = (channel->band == IEEE80211_BAND_2GHZ) ?
2465                         PHY_BAND_24 : PHY_BAND_5,
2466                 .channel_info.channel = channel->hw_value,
2467                 .channel_info.width = PHY_VHT_CHANNEL_MODE20,
2468                 /* Set the time and duration */
2469                 .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
2470                 .apply_time_max_delay =
2471                         cpu_to_le32(MSEC_TO_TU(AUX_ROC_MAX_DELAY_ON_CHANNEL)),
2472                 .duration = cpu_to_le32(MSEC_TO_TU(duration)),
2473          };
2474
2475         /* Set the node address */
2476         memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
2477
2478         lockdep_assert_held(&mvm->mutex);
2479
2480         spin_lock_bh(&mvm->time_event_lock);
2481
2482         if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
2483                 spin_unlock_bh(&mvm->time_event_lock);
2484                 return -EIO;
2485         }
2486
2487         te_data->vif = vif;
2488         te_data->duration = duration;
2489         te_data->id = HOT_SPOT_CMD;
2490
2491         spin_unlock_bh(&mvm->time_event_lock);
2492
2493         /*
2494          * Use a notification wait, which really just processes the
2495          * command response and doesn't wait for anything, in order
2496          * to be able to process the response and get the UID inside
2497          * the RX path. Using CMD_WANT_SKB doesn't work because it
2498          * stores the buffer and then wakes up this thread, by which
2499          * time another notification (that the time event started)
2500          * might already be processed unsuccessfully.
2501          */
2502         iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
2503                                    time_event_response,
2504                                    ARRAY_SIZE(time_event_response),
2505                                    iwl_mvm_rx_aux_roc, te_data);
2506
2507         res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
2508                                    &aux_roc_req);
2509
2510         if (res) {
2511                 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
2512                 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
2513                 goto out_clear_te;
2514         }
2515
2516         /* No need to wait for anything, so just pass 1 (0 isn't valid) */
2517         res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
2518         /* should never fail */
2519         WARN_ON_ONCE(res);
2520
2521         if (res) {
2522  out_clear_te:
2523                 spin_lock_bh(&mvm->time_event_lock);
2524                 iwl_mvm_te_clear_data(mvm, te_data);
2525                 spin_unlock_bh(&mvm->time_event_lock);
2526         }
2527
2528         return res;
2529 }
2530
2531 static int iwl_mvm_roc(struct ieee80211_hw *hw,
2532                        struct ieee80211_vif *vif,
2533                        struct ieee80211_channel *channel,
2534                        int duration,
2535                        enum ieee80211_roc_type type)
2536 {
2537         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2538         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2539         struct cfg80211_chan_def chandef;
2540         struct iwl_mvm_phy_ctxt *phy_ctxt;
2541         int ret, i;
2542
2543         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2544                            duration, type);
2545
2546         mutex_lock(&mvm->mutex);
2547
2548         switch (vif->type) {
2549         case NL80211_IFTYPE_STATION:
2550                 /* Use aux roc framework (HS20) */
2551                 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
2552                                                vif, duration);
2553                 goto out_unlock;
2554         case NL80211_IFTYPE_P2P_DEVICE:
2555                 /* handle below */
2556                 break;
2557         default:
2558                 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
2559                 ret = -EINVAL;
2560                 goto out_unlock;
2561         }
2562
2563         for (i = 0; i < NUM_PHY_CTX; i++) {
2564                 phy_ctxt = &mvm->phy_ctxts[i];
2565                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2566                         continue;
2567
2568                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2569                         /*
2570                          * Unbind the P2P_DEVICE from the current PHY context,
2571                          * and if the PHY context is not used remove it.
2572                          */
2573                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
2574                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2575                                 goto out_unlock;
2576
2577                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2578
2579                         /* Bind the P2P_DEVICE to the current PHY Context */
2580                         mvmvif->phy_ctxt = phy_ctxt;
2581
2582                         ret = iwl_mvm_binding_add_vif(mvm, vif);
2583                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2584                                 goto out_unlock;
2585
2586                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2587                         goto schedule_time_event;
2588                 }
2589         }
2590
2591         /* Need to update the PHY context only if the ROC channel changed */
2592         if (channel == mvmvif->phy_ctxt->channel)
2593                 goto schedule_time_event;
2594
2595         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2596
2597         /*
2598          * Change the PHY context configuration as it is currently referenced
2599          * only by the P2P Device MAC
2600          */
2601         if (mvmvif->phy_ctxt->ref == 1) {
2602                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2603                                                &chandef, 1, 1);
2604                 if (ret)
2605                         goto out_unlock;
2606         } else {
2607                 /*
2608                  * The PHY context is shared with other MACs. Need to remove the
2609                  * P2P Device from the binding, allocate an new PHY context and
2610                  * create a new binding
2611                  */
2612                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2613                 if (!phy_ctxt) {
2614                         ret = -ENOSPC;
2615                         goto out_unlock;
2616                 }
2617
2618                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2619                                                1, 1);
2620                 if (ret) {
2621                         IWL_ERR(mvm, "Failed to change PHY context\n");
2622                         goto out_unlock;
2623                 }
2624
2625                 /* Unbind the P2P_DEVICE from the current PHY context */
2626                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2627                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2628                         goto out_unlock;
2629
2630                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2631
2632                 /* Bind the P2P_DEVICE to the new allocated PHY context */
2633                 mvmvif->phy_ctxt = phy_ctxt;
2634
2635                 ret = iwl_mvm_binding_add_vif(mvm, vif);
2636                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2637                         goto out_unlock;
2638
2639                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2640         }
2641
2642 schedule_time_event:
2643         /* Schedule the time events */
2644         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2645
2646 out_unlock:
2647         mutex_unlock(&mvm->mutex);
2648         IWL_DEBUG_MAC80211(mvm, "leave\n");
2649         return ret;
2650 }
2651
2652 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2653 {
2654         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2655
2656         IWL_DEBUG_MAC80211(mvm, "enter\n");
2657
2658         mutex_lock(&mvm->mutex);
2659         iwl_mvm_stop_roc(mvm);
2660         mutex_unlock(&mvm->mutex);
2661
2662         IWL_DEBUG_MAC80211(mvm, "leave\n");
2663         return 0;
2664 }
2665
2666 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
2667                                  struct ieee80211_chanctx_conf *ctx)
2668 {
2669         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2670         struct iwl_mvm_phy_ctxt *phy_ctxt;
2671         int ret;
2672
2673         lockdep_assert_held(&mvm->mutex);
2674
2675         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2676
2677         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2678         if (!phy_ctxt) {
2679                 ret = -ENOSPC;
2680                 goto out;
2681         }
2682
2683         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2684                                        ctx->rx_chains_static,
2685                                        ctx->rx_chains_dynamic);
2686         if (ret) {
2687                 IWL_ERR(mvm, "Failed to add PHY context\n");
2688                 goto out;
2689         }
2690
2691         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
2692         *phy_ctxt_id = phy_ctxt->id;
2693 out:
2694         return ret;
2695 }
2696
2697 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2698                                struct ieee80211_chanctx_conf *ctx)
2699 {
2700         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2701         int ret;
2702
2703         mutex_lock(&mvm->mutex);
2704         ret = __iwl_mvm_add_chanctx(mvm, ctx);
2705         mutex_unlock(&mvm->mutex);
2706
2707         return ret;
2708 }
2709
2710 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
2711                                      struct ieee80211_chanctx_conf *ctx)
2712 {
2713         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2714         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2715
2716         lockdep_assert_held(&mvm->mutex);
2717
2718         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2719 }
2720
2721 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2722                                    struct ieee80211_chanctx_conf *ctx)
2723 {
2724         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2725
2726         mutex_lock(&mvm->mutex);
2727         __iwl_mvm_remove_chanctx(mvm, ctx);
2728         mutex_unlock(&mvm->mutex);
2729 }
2730
2731 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
2732                                    struct ieee80211_chanctx_conf *ctx,
2733                                    u32 changed)
2734 {
2735         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2736         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2737         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2738
2739         if (WARN_ONCE((phy_ctxt->ref > 1) &&
2740                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
2741                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2742                                    IEEE80211_CHANCTX_CHANGE_RADAR |
2743                                    IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
2744                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
2745                       phy_ctxt->ref, changed))
2746                 return;
2747
2748         mutex_lock(&mvm->mutex);
2749         iwl_mvm_bt_coex_vif_change(mvm);
2750         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2751                                  ctx->rx_chains_static,
2752                                  ctx->rx_chains_dynamic);
2753         mutex_unlock(&mvm->mutex);
2754 }
2755
2756 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
2757                                         struct ieee80211_vif *vif,
2758                                         struct ieee80211_chanctx_conf *ctx,
2759                                         bool switching_chanctx)
2760 {
2761         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2762         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2763         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2764         int ret;
2765
2766         lockdep_assert_held(&mvm->mutex);
2767
2768         mvmvif->phy_ctxt = phy_ctxt;
2769
2770         switch (vif->type) {
2771         case NL80211_IFTYPE_AP:
2772                 /* Unless it's a CSA flow we have nothing to do here */
2773                 if (vif->csa_active) {
2774                         mvmvif->ap_ibss_active = true;
2775                         break;
2776                 }
2777         case NL80211_IFTYPE_ADHOC:
2778                 /*
2779                  * The AP binding flow is handled as part of the start_ap flow
2780                  * (in bss_info_changed), similarly for IBSS.
2781                  */
2782                 ret = 0;
2783                 goto out;
2784         case NL80211_IFTYPE_STATION:
2785                 break;
2786         case NL80211_IFTYPE_MONITOR:
2787                 /* always disable PS when a monitor interface is active */
2788                 mvmvif->ps_disabled = true;
2789                 break;
2790         default:
2791                 ret = -EINVAL;
2792                 goto out;
2793         }
2794
2795         ret = iwl_mvm_binding_add_vif(mvm, vif);
2796         if (ret)
2797                 goto out;
2798
2799         /*
2800          * Power state must be updated before quotas,
2801          * otherwise fw will complain.
2802          */
2803         iwl_mvm_power_update_mac(mvm);
2804
2805         /* Setting the quota at this stage is only required for monitor
2806          * interfaces. For the other types, the bss_info changed flow
2807          * will handle quota settings.
2808          */
2809         if (vif->type == NL80211_IFTYPE_MONITOR) {
2810                 mvmvif->monitor_active = true;
2811                 ret = iwl_mvm_update_quotas(mvm, NULL);
2812                 if (ret)
2813                         goto out_remove_binding;
2814         }
2815
2816         /* Handle binding during CSA */
2817         if ((vif->type == NL80211_IFTYPE_AP) ||
2818             (switching_chanctx && (vif->type == NL80211_IFTYPE_STATION))) {
2819                 iwl_mvm_update_quotas(mvm, NULL);
2820                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2821         }
2822
2823         if (vif->csa_active && vif->type == NL80211_IFTYPE_STATION) {
2824                 struct iwl_mvm_sta *mvmsta;
2825
2826                 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
2827                                                           mvmvif->ap_sta_id);
2828
2829                 if (WARN_ON(!mvmsta))
2830                         goto out;
2831
2832                 /* TODO: only re-enable after the first beacon */
2833                 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
2834         }
2835
2836         goto out;
2837
2838 out_remove_binding:
2839         iwl_mvm_binding_remove_vif(mvm, vif);
2840         iwl_mvm_power_update_mac(mvm);
2841 out:
2842         if (ret)
2843                 mvmvif->phy_ctxt = NULL;
2844         return ret;
2845 }
2846 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
2847                                       struct ieee80211_vif *vif,
2848                                       struct ieee80211_chanctx_conf *ctx)
2849 {
2850         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2851         int ret;
2852
2853         mutex_lock(&mvm->mutex);
2854         ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
2855         mutex_unlock(&mvm->mutex);
2856
2857         return ret;
2858 }
2859
2860 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
2861                                            struct ieee80211_vif *vif,
2862                                            struct ieee80211_chanctx_conf *ctx,
2863                                            bool switching_chanctx)
2864 {
2865         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2866         struct ieee80211_vif *disabled_vif = NULL;
2867         struct iwl_mvm_sta *mvmsta;
2868
2869         lockdep_assert_held(&mvm->mutex);
2870
2871         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
2872
2873         switch (vif->type) {
2874         case NL80211_IFTYPE_ADHOC:
2875                 goto out;
2876         case NL80211_IFTYPE_MONITOR:
2877                 mvmvif->monitor_active = false;
2878                 mvmvif->ps_disabled = false;
2879                 break;
2880         case NL80211_IFTYPE_AP:
2881                 /* This part is triggered only during CSA */
2882                 if (!vif->csa_active || !mvmvif->ap_ibss_active)
2883                         goto out;
2884
2885                 /* Set CS bit on all the stations */
2886                 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
2887
2888                 /* Save blocked iface, the timeout is set on the next beacon */
2889                 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
2890
2891                 mvmvif->ap_ibss_active = false;
2892                 break;
2893         case NL80211_IFTYPE_STATION:
2894                 if (!switching_chanctx)
2895                         break;
2896
2897                 disabled_vif = vif;
2898
2899                 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
2900                                                           mvmvif->ap_sta_id);
2901
2902                 if (!WARN_ON(!mvmsta))
2903                         iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2904
2905                 iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
2906                 break;
2907         default:
2908                 break;
2909         }
2910
2911         iwl_mvm_update_quotas(mvm, disabled_vif);
2912         iwl_mvm_binding_remove_vif(mvm, vif);
2913
2914 out:
2915         mvmvif->phy_ctxt = NULL;
2916         iwl_mvm_power_update_mac(mvm);
2917 }
2918
2919 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
2920                                          struct ieee80211_vif *vif,
2921                                          struct ieee80211_chanctx_conf *ctx)
2922 {
2923         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2924
2925         mutex_lock(&mvm->mutex);
2926         __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
2927         mutex_unlock(&mvm->mutex);
2928 }
2929
2930 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
2931                                       struct ieee80211_vif_chanctx_switch *vifs,
2932                                       int n_vifs,
2933                                       enum ieee80211_chanctx_switch_mode mode)
2934 {
2935         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2936         int ret;
2937
2938         /* we only support SWAP_CONTEXTS and with a single-vif right now */
2939         if (mode != CHANCTX_SWMODE_SWAP_CONTEXTS || n_vifs > 1)
2940                 return -EOPNOTSUPP;
2941
2942         mutex_lock(&mvm->mutex);
2943         __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
2944         __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
2945
2946         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
2947         if (ret) {
2948                 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
2949                 goto out_reassign;
2950         }
2951
2952         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
2953                                            true);
2954         if (ret) {
2955                 IWL_ERR(mvm,
2956                         "failed to assign new_ctx during channel switch\n");
2957                 goto out_remove;
2958         }
2959
2960         /* we don't support TDLS during DCM - can be caused by channel switch */
2961         if (iwl_mvm_phy_ctx_count(mvm) > 1)
2962                 iwl_mvm_teardown_tdls_peers(mvm);
2963
2964         goto out;
2965
2966 out_remove:
2967         __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
2968
2969 out_reassign:
2970         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx);
2971         if (ret) {
2972                 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
2973                 goto out_restart;
2974         }
2975
2976         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
2977                                            true);
2978         if (ret) {
2979                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
2980                 goto out_restart;
2981         }
2982
2983         goto out;
2984
2985 out_restart:
2986         /* things keep failing, better restart the hw */
2987         iwl_mvm_nic_restart(mvm, false);
2988
2989 out:
2990         mutex_unlock(&mvm->mutex);
2991         return ret;
2992 }
2993
2994 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
2995                            struct ieee80211_sta *sta,
2996                            bool set)
2997 {
2998         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2999         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
3000
3001         if (!mvm_sta || !mvm_sta->vif) {
3002                 IWL_ERR(mvm, "Station is not associated to a vif\n");
3003                 return -EINVAL;
3004         }
3005
3006         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
3007 }
3008
3009 #ifdef CONFIG_NL80211_TESTMODE
3010 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
3011         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
3012         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
3013         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
3014 };
3015
3016 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
3017                                       struct ieee80211_vif *vif,
3018                                       void *data, int len)
3019 {
3020         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
3021         int err;
3022         u32 noa_duration;
3023
3024         err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
3025         if (err)
3026                 return err;
3027
3028         if (!tb[IWL_MVM_TM_ATTR_CMD])
3029                 return -EINVAL;
3030
3031         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
3032         case IWL_MVM_TM_CMD_SET_NOA:
3033                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
3034                     !vif->bss_conf.enable_beacon ||
3035                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
3036                         return -EINVAL;
3037
3038                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
3039                 if (noa_duration >= vif->bss_conf.beacon_int)
3040                         return -EINVAL;
3041
3042                 mvm->noa_duration = noa_duration;
3043                 mvm->noa_vif = vif;
3044
3045                 return iwl_mvm_update_quotas(mvm, NULL);
3046         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
3047                 /* must be associated client vif - ignore authorized */
3048                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
3049                     !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
3050                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
3051                         return -EINVAL;
3052
3053                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
3054                         return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3055                 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3056         }
3057
3058         return -EOPNOTSUPP;
3059 }
3060
3061 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
3062                                     struct ieee80211_vif *vif,
3063                                     void *data, int len)
3064 {
3065         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3066         int err;
3067
3068         mutex_lock(&mvm->mutex);
3069         err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
3070         mutex_unlock(&mvm->mutex);
3071
3072         return err;
3073 }
3074 #endif
3075
3076 static void iwl_mvm_channel_switch_beacon(struct ieee80211_hw *hw,
3077                                           struct ieee80211_vif *vif,
3078                                           struct cfg80211_chan_def *chandef)
3079 {
3080         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3081         struct ieee80211_vif *csa_vif;
3082
3083         mutex_lock(&mvm->mutex);
3084
3085         csa_vif = rcu_dereference_protected(mvm->csa_vif,
3086                                             lockdep_is_held(&mvm->mutex));
3087         if (WARN(csa_vif && csa_vif->csa_active,
3088                  "Another CSA is already in progress"))
3089                 goto out_unlock;
3090
3091         IWL_DEBUG_MAC80211(mvm, "CSA started to freq %d\n",
3092                            chandef->center_freq1);
3093         rcu_assign_pointer(mvm->csa_vif, vif);
3094
3095 out_unlock:
3096         mutex_unlock(&mvm->mutex);
3097 }
3098
3099 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
3100                               struct ieee80211_vif *vif, u32 queues, bool drop)
3101 {
3102         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3103         struct iwl_mvm_vif *mvmvif;
3104         struct iwl_mvm_sta *mvmsta;
3105
3106         if (!vif || vif->type != NL80211_IFTYPE_STATION)
3107                 return;
3108
3109         mutex_lock(&mvm->mutex);
3110         mvmvif = iwl_mvm_vif_from_mac80211(vif);
3111         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, mvmvif->ap_sta_id);
3112
3113         if (WARN_ON_ONCE(!mvmsta)) {
3114                 mutex_unlock(&mvm->mutex);
3115                 return;
3116         }
3117
3118         if (drop) {
3119                 if (iwl_mvm_flush_tx_path(mvm, mvmsta->tfd_queue_msk, true))
3120                         IWL_ERR(mvm, "flush request fail\n");
3121                 mutex_unlock(&mvm->mutex);
3122         } else {
3123                 u32 tfd_queue_msk = mvmsta->tfd_queue_msk;
3124                 mutex_unlock(&mvm->mutex);
3125
3126                 /* this can take a while, and we may need/want other operations
3127                  * to succeed while doing this, so do it without the mutex held
3128                  */
3129                 iwl_trans_wait_tx_queue_empty(mvm->trans, tfd_queue_msk);
3130         }
3131 }
3132
3133 const struct ieee80211_ops iwl_mvm_hw_ops = {
3134         .tx = iwl_mvm_mac_tx,
3135         .ampdu_action = iwl_mvm_mac_ampdu_action,
3136         .start = iwl_mvm_mac_start,
3137         .reconfig_complete = iwl_mvm_mac_reconfig_complete,
3138         .stop = iwl_mvm_mac_stop,
3139         .add_interface = iwl_mvm_mac_add_interface,
3140         .remove_interface = iwl_mvm_mac_remove_interface,
3141         .config = iwl_mvm_mac_config,
3142         .prepare_multicast = iwl_mvm_prepare_multicast,
3143         .configure_filter = iwl_mvm_configure_filter,
3144         .bss_info_changed = iwl_mvm_bss_info_changed,
3145         .hw_scan = iwl_mvm_mac_hw_scan,
3146         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
3147         .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
3148         .sta_state = iwl_mvm_mac_sta_state,
3149         .sta_notify = iwl_mvm_mac_sta_notify,
3150         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3151         .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
3152         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
3153         .sta_rc_update = iwl_mvm_sta_rc_update,
3154         .conf_tx = iwl_mvm_mac_conf_tx,
3155         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
3156         .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
3157         .flush = iwl_mvm_mac_flush,
3158         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
3159         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
3160         .set_key = iwl_mvm_mac_set_key,
3161         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
3162         .remain_on_channel = iwl_mvm_roc,
3163         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
3164         .add_chanctx = iwl_mvm_add_chanctx,
3165         .remove_chanctx = iwl_mvm_remove_chanctx,
3166         .change_chanctx = iwl_mvm_change_chanctx,
3167         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
3168         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
3169         .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
3170
3171         .start_ap = iwl_mvm_start_ap_ibss,
3172         .stop_ap = iwl_mvm_stop_ap_ibss,
3173         .join_ibss = iwl_mvm_start_ap_ibss,
3174         .leave_ibss = iwl_mvm_stop_ap_ibss,
3175
3176         .set_tim = iwl_mvm_set_tim,
3177
3178         .channel_switch_beacon = iwl_mvm_channel_switch_beacon,
3179
3180         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
3181
3182 #ifdef CONFIG_PM_SLEEP
3183         /* look at d3.c */
3184         .suspend = iwl_mvm_suspend,
3185         .resume = iwl_mvm_resume,
3186         .set_wakeup = iwl_mvm_set_wakeup,
3187         .set_rekey_data = iwl_mvm_set_rekey_data,
3188 #if IS_ENABLED(CONFIG_IPV6)
3189         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
3190 #endif
3191         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
3192 #endif
3193 };