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