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