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