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