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