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