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