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