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