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