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