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