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