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