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