iwlwifi: mvm: support beacon statistics for BSS client
[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
4bfa47f3 889void iwl_mvm_fw_error_dump(struct iwl_mvm *mvm)
655e6d6d
EG
890{
891 struct iwl_fw_error_dump_file *dump_file;
892 struct iwl_fw_error_dump_data *dump_data;
893 struct iwl_fw_error_dump_info *dump_info;
a549b296 894 struct iwl_fw_error_dump_mem *dump_mem;
48eb7b34 895 struct iwl_mvm_dump_ptrs *fw_error_dump;
655e6d6d 896 u32 sram_len, sram_ofs;
04fd2c28 897 u32 file_len, fifo_data_len = 0;
addfaada 898 u32 smem_len = mvm->cfg->smem_len;
86138324 899 u32 sram2_len = mvm->cfg->dccm2_len;
655e6d6d
EG
900
901 lockdep_assert_held(&mvm->mutex);
902
addfaada 903 /* W/A for 8000 HW family A-step */
86138324
IY
904 if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_8000 &&
905 CSR_HW_REV_STEP(mvm->trans->hw_rev) == SILICON_A_STEP) {
906 if (smem_len)
907 smem_len = 0x38000;
908
909 if (sram2_len)
910 sram2_len = 0x10000;
911 }
addfaada 912
aadede6e 913 fw_error_dump = kzalloc(sizeof(*fw_error_dump), GFP_KERNEL);
48eb7b34
EG
914 if (!fw_error_dump)
915 return;
916
f53bf4c7
LK
917 /* SRAM - include stack CCM if driver knows the values for it */
918 if (!mvm->cfg->dccm_offset || !mvm->cfg->dccm_len) {
919 const struct fw_img *img;
920
921 img = &mvm->fw->img[mvm->cur_ucode];
922 sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
923 sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
924 } else {
925 sram_ofs = mvm->cfg->dccm_offset;
926 sram_len = mvm->cfg->dccm_len;
927 }
655e6d6d 928
04fd2c28
LK
929 /* reading RXF/TXF sizes */
930 if (test_bit(STATUS_FW_ERROR, &mvm->trans->status)) {
931 struct iwl_mvm_shared_mem_cfg *mem_cfg = &mvm->shared_mem_cfg;
932 int i;
933
934 fifo_data_len = 0;
935
936 /* Count RXF size */
937 for (i = 0; i < ARRAY_SIZE(mem_cfg->rxfifo_size); i++) {
938 if (!mem_cfg->rxfifo_size[i])
939 continue;
940
941 /* Add header info */
942 fifo_data_len += mem_cfg->rxfifo_size[i] +
943 sizeof(*dump_data) +
944 sizeof(struct iwl_fw_error_dump_fifo);
945 }
946
947 for (i = 0; i < ARRAY_SIZE(mem_cfg->txfifo_size); i++) {
948 if (!mem_cfg->txfifo_size[i])
949 continue;
655e6d6d 950
04fd2c28
LK
951 /* Add header info */
952 fifo_data_len += mem_cfg->txfifo_size[i] +
953 sizeof(*dump_data) +
954 sizeof(struct iwl_fw_error_dump_fifo);
955 }
956 }
655e6d6d
EG
957
958 file_len = sizeof(*dump_file) +
04fd2c28 959 sizeof(*dump_data) * 2 +
a549b296 960 sram_len + sizeof(*dump_mem) +
04fd2c28 961 fifo_data_len +
655e6d6d
EG
962 sizeof(*dump_info);
963
addfaada
LK
964 /* Make room for the SMEM, if it exists */
965 if (smem_len)
a549b296 966 file_len += sizeof(*dump_data) + sizeof(*dump_mem) + smem_len;
addfaada 967
86138324
IY
968 /* Make room for the secondary SRAM, if it exists */
969 if (sram2_len)
970 file_len += sizeof(*dump_data) + sizeof(*dump_mem) + sram2_len;
971
5bfe6f53 972 dump_file = vzalloc(file_len);
48eb7b34
EG
973 if (!dump_file) {
974 kfree(fw_error_dump);
655e6d6d 975 return;
48eb7b34 976 }
655e6d6d 977
48eb7b34 978 fw_error_dump->op_mode_ptr = dump_file;
655e6d6d
EG
979
980 dump_file->barker = cpu_to_le32(IWL_FW_ERROR_DUMP_BARKER);
655e6d6d
EG
981 dump_data = (void *)dump_file->data;
982
983 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_DEV_FW_INFO);
984 dump_data->len = cpu_to_le32(sizeof(*dump_info));
985 dump_info = (void *) dump_data->data;
986 dump_info->device_family =
987 mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000 ?
988 cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_7) :
989 cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_8);
435da2ce 990 dump_info->hw_step = cpu_to_le32(CSR_HW_REV_STEP(mvm->trans->hw_rev));
655e6d6d
EG
991 memcpy(dump_info->fw_human_readable, mvm->fw->human_readable,
992 sizeof(dump_info->fw_human_readable));
993 strncpy(dump_info->dev_human_readable, mvm->cfg->name,
994 sizeof(dump_info->dev_human_readable));
995 strncpy(dump_info->bus_human_readable, mvm->dev->bus->name,
996 sizeof(dump_info->bus_human_readable));
997
998 dump_data = iwl_fw_error_next_data(dump_data);
04fd2c28
LK
999 /* We only dump the FIFOs if the FW is in error state */
1000 if (test_bit(STATUS_FW_ERROR, &mvm->trans->status))
1001 iwl_mvm_dump_fifos(mvm, &dump_data);
655e6d6d 1002
a549b296
EG
1003 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
1004 dump_data->len = cpu_to_le32(sram_len + sizeof(*dump_mem));
1005 dump_mem = (void *)dump_data->data;
1006 dump_mem->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_SRAM);
1007 dump_mem->offset = cpu_to_le32(sram_ofs);
1008 iwl_trans_read_mem_bytes(mvm->trans, sram_ofs, dump_mem->data,
655e6d6d
EG
1009 sram_len);
1010
addfaada
LK
1011 if (smem_len) {
1012 dump_data = iwl_fw_error_next_data(dump_data);
e06d8437
EG
1013 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
1014 dump_data->len = cpu_to_le32(smem_len + sizeof(*dump_mem));
1015 dump_mem = (void *)dump_data->data;
1016 dump_mem->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_SMEM);
1017 dump_mem->offset = cpu_to_le32(mvm->cfg->smem_offset);
addfaada 1018 iwl_trans_read_mem_bytes(mvm->trans, mvm->cfg->smem_offset,
e06d8437 1019 dump_mem->data, smem_len);
addfaada
LK
1020 }
1021
86138324
IY
1022 if (sram2_len) {
1023 dump_data = iwl_fw_error_next_data(dump_data);
1024 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
1025 dump_data->len = cpu_to_le32(sram2_len + sizeof(*dump_mem));
1026 dump_mem = (void *)dump_data->data;
1027 dump_mem->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_SRAM);
1028 dump_mem->offset = cpu_to_le32(mvm->cfg->dccm2_offset);
1029 iwl_trans_read_mem_bytes(mvm->trans, mvm->cfg->dccm2_offset,
1030 dump_mem->data, sram2_len);
1031 }
1032
48eb7b34
EG
1033 fw_error_dump->trans_ptr = iwl_trans_dump_data(mvm->trans);
1034 fw_error_dump->op_mode_len = file_len;
1035 if (fw_error_dump->trans_ptr)
1036 file_len += fw_error_dump->trans_ptr->len;
1037 dump_file->file_len = cpu_to_le32(file_len);
4bfa47f3 1038
aadede6e
JB
1039 dev_coredumpm(mvm->trans->dev, THIS_MODULE, fw_error_dump, 0,
1040 GFP_KERNEL, iwl_mvm_read_coredump, iwl_mvm_free_coredump);
655e6d6d 1041}
655e6d6d 1042
8ca151b5
JB
1043static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1044{
58629d9d
JB
1045 /* clear the D3 reconfig, we only need it to avoid dumping a
1046 * firmware coredump on reconfiguration, we shouldn't do that
1047 * on D3->D0 transition
1048 */
1049 if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status))
1050 iwl_mvm_fw_error_dump(mvm);
1bd3cbc1 1051
744cb695
EP
1052 /* cleanup all stale references (scan, roc), but keep the
1053 * ucode_down ref until reconfig is complete
1054 */
1055 iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
1056
8ca151b5 1057 iwl_trans_stop_device(mvm->trans);
8ca151b5
JB
1058
1059 mvm->scan_status = IWL_MVM_SCAN_NONE;
b1873300 1060 mvm->ps_disabled = false;
31b8b343 1061 mvm->calibrating = false;
8ca151b5
JB
1062
1063 /* just in case one was running */
1064 ieee80211_remain_on_channel_expired(mvm->hw);
1065
1066 ieee80211_iterate_active_interfaces_atomic(
1067 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1068 iwl_mvm_cleanup_iterator, mvm);
1069
fe0f2de3 1070 mvm->p2p_device_vif = NULL;
37577fe2 1071 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
fe0f2de3
IP
1072
1073 iwl_mvm_reset_phy_ctxts(mvm);
8ca151b5
JB
1074 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1075 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
a0f6bf2a 1076 memset(mvm->tfd_drained, 0, sizeof(mvm->tfd_drained));
8a275bad
EG
1077 memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1078 memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
1079 memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1080 memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
1081 memset(&mvm->bt_ack_kill_msk, 0, sizeof(mvm->bt_ack_kill_msk));
1082 memset(&mvm->bt_cts_kill_msk, 0, sizeof(mvm->bt_cts_kill_msk));
8ca151b5
JB
1083
1084 ieee80211_wake_queues(mvm->hw);
1085
228670b2
EP
1086 /* clear any stale d0i3 state */
1087 clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
1088
8ca151b5 1089 mvm->vif_count = 0;
113a0447 1090 mvm->rx_ba_sessions = 0;
91a8bcde
JB
1091
1092 /* keep statistics ticking */
1093 iwl_mvm_accu_radio_stats(mvm);
8ca151b5
JB
1094}
1095
a0a09243 1096int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
8ca151b5 1097{
8ca151b5
JB
1098 int ret;
1099
a0a09243 1100 lockdep_assert_held(&mvm->mutex);
8ca151b5
JB
1101
1102 /* Clean up some internal and mac80211 state on restart */
1103 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1104 iwl_mvm_restart_cleanup(mvm);
1105
1106 ret = iwl_mvm_up(mvm);
c47af22a
JB
1107
1108 if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1109 /* Something went wrong - we need to finish some cleanup
1110 * that normally iwl_mvm_mac_restart_complete() below
1111 * would do.
1112 */
1113 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1114 iwl_mvm_d0i3_enable_tx(mvm, NULL);
1115 }
1116
a0a09243
LC
1117 return ret;
1118}
1119
1120static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1121{
1122 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1123 int ret;
1124
37948fcf
EP
1125 /* Some hw restart cleanups must not hold the mutex */
1126 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1127 /*
1128 * Make sure we are out of d0i3. This is needed
1129 * to make sure the reference accounting is correct
1130 * (and there is no stale d0i3_exit_work).
1131 */
1132 wait_event_timeout(mvm->d0i3_exit_waitq,
1133 !test_bit(IWL_MVM_STATUS_IN_D0I3,
1134 &mvm->status),
1135 HZ);
1136 }
1137
a0a09243
LC
1138 mutex_lock(&mvm->mutex);
1139 ret = __iwl_mvm_mac_start(mvm);
8ca151b5
JB
1140 mutex_unlock(&mvm->mutex);
1141
1142 return ret;
1143}
1144
cf2c92d8 1145static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
8ca151b5 1146{
8ca151b5
JB
1147 int ret;
1148
1149 mutex_lock(&mvm->mutex);
1150
1151 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
b2492501 1152 iwl_mvm_d0i3_enable_tx(mvm, NULL);
0166230c 1153 ret = iwl_mvm_update_quotas(mvm, NULL);
8ca151b5
JB
1154 if (ret)
1155 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1156 ret);
1157
7498cf4c
EP
1158 /* allow transport/FW low power modes */
1159 iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1160
cbd2ae2d
AN
1161 /*
1162 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1163 * of packets the FW sent out, so we must reconnect.
1164 */
1165 iwl_mvm_teardown_tdls_peers(mvm);
1166
8ca151b5
JB
1167 mutex_unlock(&mvm->mutex);
1168}
1169
088070a2
EP
1170static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
1171{
1172 bool exit_now;
1173
1174 if (!iwl_mvm_is_d0i3_supported(mvm))
1175 return;
1176
1177 mutex_lock(&mvm->d0i3_suspend_mutex);
1178 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1179 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1180 &mvm->d0i3_suspend_flags);
1181 mutex_unlock(&mvm->d0i3_suspend_mutex);
1182
1183 if (exit_now) {
1184 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
1185 _iwl_mvm_exit_d0i3(mvm);
1186 }
6735943f
EP
1187
1188 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND)
1189 if (!wait_event_timeout(mvm->d0i3_exit_waitq,
1190 !test_bit(IWL_MVM_STATUS_IN_D0I3,
1191 &mvm->status),
1192 HZ))
1193 WARN_ONCE(1, "D0i3 exit on resume timed out\n");
088070a2
EP
1194}
1195
cf2c92d8
EP
1196static void
1197iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1198 enum ieee80211_reconfig_type reconfig_type)
1199{
1200 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1201
1202 switch (reconfig_type) {
1203 case IEEE80211_RECONFIG_TYPE_RESTART:
1204 iwl_mvm_restart_complete(mvm);
1205 break;
1206 case IEEE80211_RECONFIG_TYPE_SUSPEND:
088070a2 1207 iwl_mvm_resume_complete(mvm);
cf2c92d8
EP
1208 break;
1209 }
1210}
1211
a0a09243 1212void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
8ca151b5 1213{
a0a09243 1214 lockdep_assert_held(&mvm->mutex);
7498cf4c 1215
91a8bcde
JB
1216 /* firmware counters are obviously reset now, but we shouldn't
1217 * partially track so also clear the fw_reset_accu counters.
1218 */
1219 memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1220
0a79a0c0
EP
1221 /*
1222 * Disallow low power states when the FW is down by taking
1223 * the UCODE_DOWN ref. in case of ongoing hw restart the
1224 * ref is already taken, so don't take it again.
1225 */
1226 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1227 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
7498cf4c 1228
8ca151b5
JB
1229 /* async_handlers_wk is now blocked */
1230
1231 /*
1232 * The work item could be running or queued if the
1233 * ROC time event stops just as we get here.
1234 */
1235 cancel_work_sync(&mvm->roc_done_wk);
1236
1237 iwl_trans_stop_device(mvm->trans);
8ca151b5
JB
1238
1239 iwl_mvm_async_handlers_purge(mvm);
1240 /* async_handlers_list is empty and will stay empty: HW is stopped */
1241
1242 /* the fw is stopped, the aux sta is dead: clean up driver state */
712b24ad 1243 iwl_mvm_del_aux_sta(mvm);
8ca151b5 1244
0a79a0c0
EP
1245 /*
1246 * Clear IN_HW_RESTART flag when stopping the hw (as restart_complete()
1247 * won't be called in this case).
1248 */
1249 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1250
bc44886d 1251 mvm->ucode_loaded = false;
a0a09243 1252}
bc44886d 1253
a0a09243
LC
1254static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1255{
1256 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1257
1258 flush_work(&mvm->d0i3_exit_work);
1259 flush_work(&mvm->async_handlers_wk);
4bfa47f3 1260 flush_work(&mvm->fw_error_dump_wk);
a0a09243
LC
1261
1262 mutex_lock(&mvm->mutex);
1263 __iwl_mvm_mac_stop(mvm);
8ca151b5
JB
1264 mutex_unlock(&mvm->mutex);
1265
1266 /*
1267 * The worker might have been waiting for the mutex, let it run and
1268 * discover that its list is now empty.
1269 */
1270 cancel_work_sync(&mvm->async_handlers_wk);
1271}
1272
fe0f2de3
IP
1273static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1274{
1275 u16 i;
1276
1277 lockdep_assert_held(&mvm->mutex);
1278
1279 for (i = 0; i < NUM_PHY_CTX; i++)
1280 if (!mvm->phy_ctxts[i].ref)
1281 return &mvm->phy_ctxts[i];
1282
1283 IWL_ERR(mvm, "No available PHY context\n");
1284 return NULL;
1285}
1286
ee9c6cb0
EG
1287static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1288 s8 tx_power)
1289{
1290 /* FW is in charge of regulatory enforcement */
1291 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
1292 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
1293 .pwr_restriction = cpu_to_le16(tx_power),
1294 };
1295
a1022927 1296 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0,
ee9c6cb0
EG
1297 sizeof(reduce_txpwr_cmd),
1298 &reduce_txpwr_cmd);
1299}
1300
8ca151b5
JB
1301static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1302 struct ieee80211_vif *vif)
1303{
1304 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1305 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1306 int ret;
1307
d40fc489
GG
1308 /*
1309 * make sure D0i3 exit is completed, otherwise a target access
1310 * during tx queue configuration could be done when still in
1311 * D0i3 state.
1312 */
1313 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1314 if (ret)
1315 return ret;
1316
8ca151b5
JB
1317 /*
1318 * Not much to do here. The stack will not allow interface
1319 * types or combinations that we didn't advertise, so we
1320 * don't really have to check the types.
1321 */
1322
1323 mutex_lock(&mvm->mutex);
1324
33cef925
JB
1325 /* make sure that beacon statistics don't go backwards with FW reset */
1326 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1327 mvmvif->beacon_stats.accu_num_beacons +=
1328 mvmvif->beacon_stats.num_beacons;
1329
e89044d7 1330 /* Allocate resources for the MAC context, and add it to the fw */
8ca151b5
JB
1331 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1332 if (ret)
1333 goto out_unlock;
1334
1c2abf72 1335 /* Counting number of interfaces is needed for legacy PM */
ea183d02
IP
1336 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1337 mvm->vif_count++;
ea183d02 1338
8ca151b5
JB
1339 /*
1340 * The AP binding flow can be done only after the beacon
1341 * template is configured (which happens only in the mac80211
1342 * start_ap() flow), and adding the broadcast station can happen
1343 * only after the binding.
1344 * In addition, since modifying the MAC before adding a bcast
1345 * station is not allowed by the FW, delay the adding of MAC context to
1346 * the point where we can also add the bcast station.
1347 * In short: there's not much we can do at this point, other than
1348 * allocating resources :)
1349 */
5023d966
JB
1350 if (vif->type == NL80211_IFTYPE_AP ||
1351 vif->type == NL80211_IFTYPE_ADHOC) {
013290aa 1352 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
8ca151b5
JB
1353 if (ret) {
1354 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1355 goto out_release;
1356 }
1357
77740cb4 1358 iwl_mvm_vif_dbgfs_register(mvm, vif);
8ca151b5
JB
1359 goto out_unlock;
1360 }
1361
8ca151b5
JB
1362 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1363 if (ret)
1364 goto out_release;
1365
999609f1 1366 ret = iwl_mvm_power_update_mac(mvm);
e5e7aa8e 1367 if (ret)
fd66fc1c 1368 goto out_remove_mac;
8ca151b5 1369
7df15b1e 1370 /* beacon filtering */
a1022927 1371 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
bd3351ba
EP
1372 if (ret)
1373 goto out_remove_mac;
1374
7df15b1e 1375 if (!mvm->bf_allowed_vif &&
73e5f2c5 1376 vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
7df15b1e 1377 mvm->bf_allowed_vif = mvmvif;
a20fd398
AO
1378 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1379 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
7df15b1e
HG
1380 }
1381
8ca151b5
JB
1382 /*
1383 * P2P_DEVICE interface does not have a channel context assigned to it,
1384 * so a dedicated PHY context is allocated to it and the corresponding
1385 * MAC context is bound to it at this stage.
1386 */
1387 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
8ca151b5 1388
fe0f2de3
IP
1389 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1390 if (!mvmvif->phy_ctxt) {
1391 ret = -ENOSPC;
bd3351ba 1392 goto out_free_bf;
fe0f2de3 1393 }
8ca151b5 1394
53a9d61e 1395 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
1396 ret = iwl_mvm_binding_add_vif(mvm, vif);
1397 if (ret)
53a9d61e 1398 goto out_unref_phy;
8ca151b5 1399
013290aa 1400 ret = iwl_mvm_add_bcast_sta(mvm, vif);
8ca151b5
JB
1401 if (ret)
1402 goto out_unbind;
1403
1404 /* Save a pointer to p2p device vif, so it can later be used to
1405 * update the p2p device MAC when a GO is started/stopped */
1406 mvm->p2p_device_vif = vif;
1407 }
1408
63494374 1409 iwl_mvm_vif_dbgfs_register(mvm, vif);
8ca151b5
JB
1410 goto out_unlock;
1411
1412 out_unbind:
1413 iwl_mvm_binding_remove_vif(mvm, vif);
53a9d61e 1414 out_unref_phy:
fe0f2de3 1415 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
bd3351ba
EP
1416 out_free_bf:
1417 if (mvm->bf_allowed_vif == mvmvif) {
1418 mvm->bf_allowed_vif = NULL;
a20fd398
AO
1419 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1420 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
bd3351ba 1421 }
8ca151b5
JB
1422 out_remove_mac:
1423 mvmvif->phy_ctxt = NULL;
1424 iwl_mvm_mac_ctxt_remove(mvm, vif);
1425 out_release:
5ee2b215
AB
1426 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1427 mvm->vif_count--;
1c2abf72 1428
8ca151b5
JB
1429 iwl_mvm_mac_ctxt_release(mvm, vif);
1430 out_unlock:
1431 mutex_unlock(&mvm->mutex);
1432
d40fc489
GG
1433 iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1434
8ca151b5
JB
1435 return ret;
1436}
1437
38a12b5b
JB
1438static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1439 struct ieee80211_vif *vif)
8ca151b5 1440{
d92b732e 1441 u32 tfd_msk = iwl_mvm_mac_get_queues_mask(vif);
8ca151b5
JB
1442
1443 if (tfd_msk) {
1444 mutex_lock(&mvm->mutex);
1445 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
1446 mutex_unlock(&mvm->mutex);
1447 }
1448
1449 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1450 /*
1451 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1452 * We assume here that all the packets sent to the OFFCHANNEL
1453 * queue are sent in ROC session.
1454 */
1455 flush_work(&mvm->roc_done_wk);
1456 } else {
1457 /*
1458 * By now, all the AC queues are empty. The AGG queues are
1459 * empty too. We already got all the Tx responses for all the
1460 * packets in the queues. The drain work can have been
0742a75a 1461 * triggered. Flush it.
8ca151b5
JB
1462 */
1463 flush_work(&mvm->sta_drained_wk);
1464 }
38a12b5b
JB
1465}
1466
1467static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1468 struct ieee80211_vif *vif)
1469{
1470 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1471 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1472
1473 iwl_mvm_prepare_mac_removal(mvm, vif);
8ca151b5
JB
1474
1475 mutex_lock(&mvm->mutex);
1476
7df15b1e
HG
1477 if (mvm->bf_allowed_vif == mvmvif) {
1478 mvm->bf_allowed_vif = NULL;
a20fd398
AO
1479 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1480 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
7df15b1e
HG
1481 }
1482
63494374
JB
1483 iwl_mvm_vif_dbgfs_clean(mvm, vif);
1484
8ca151b5
JB
1485 /*
1486 * For AP/GO interface, the tear down of the resources allocated to the
38a12b5b 1487 * interface is be handled as part of the stop_ap flow.
8ca151b5 1488 */
5023d966
JB
1489 if (vif->type == NL80211_IFTYPE_AP ||
1490 vif->type == NL80211_IFTYPE_ADHOC) {
507cadf2
DS
1491#ifdef CONFIG_NL80211_TESTMODE
1492 if (vif == mvm->noa_vif) {
1493 mvm->noa_vif = NULL;
1494 mvm->noa_duration = 0;
1495 }
1496#endif
013290aa 1497 iwl_mvm_dealloc_bcast_sta(mvm, vif);
8ca151b5
JB
1498 goto out_release;
1499 }
1500
1501 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1502 mvm->p2p_device_vif = NULL;
013290aa 1503 iwl_mvm_rm_bcast_sta(mvm, vif);
8ca151b5 1504 iwl_mvm_binding_remove_vif(mvm, vif);
fe0f2de3 1505 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
1506 mvmvif->phy_ctxt = NULL;
1507 }
1508
5ee2b215 1509 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
8ca151b5 1510 mvm->vif_count--;
1c2abf72 1511
999609f1 1512 iwl_mvm_power_update_mac(mvm);
8ca151b5
JB
1513 iwl_mvm_mac_ctxt_remove(mvm, vif);
1514
1515out_release:
1516 iwl_mvm_mac_ctxt_release(mvm, vif);
1517 mutex_unlock(&mvm->mutex);
1518}
1519
1520static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
88f2fd73 1521{
8ca151b5
JB
1522 return 0;
1523}
1524
e59647ea
EP
1525struct iwl_mvm_mc_iter_data {
1526 struct iwl_mvm *mvm;
1527 int port_id;
1528};
1529
1530static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1531 struct ieee80211_vif *vif)
1532{
1533 struct iwl_mvm_mc_iter_data *data = _data;
1534 struct iwl_mvm *mvm = data->mvm;
1535 struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1536 int ret, len;
1537
1538 /* if we don't have free ports, mcast frames will be dropped */
1539 if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1540 return;
1541
1542 if (vif->type != NL80211_IFTYPE_STATION ||
1543 !vif->bss_conf.assoc)
1544 return;
1545
1546 cmd->port_id = data->port_id++;
1547 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1548 len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1549
1c4abec0 1550 ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
e59647ea
EP
1551 if (ret)
1552 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1553}
1554
1555static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1556{
1557 struct iwl_mvm_mc_iter_data iter_data = {
1558 .mvm = mvm,
88f2fd73
MG
1559 };
1560
e59647ea
EP
1561 lockdep_assert_held(&mvm->mutex);
1562
1563 if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1564 return;
1565
1c4abec0 1566 ieee80211_iterate_active_interfaces_atomic(
e59647ea
EP
1567 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1568 iwl_mvm_mc_iface_iterator, &iter_data);
88f2fd73
MG
1569}
1570
e59647ea
EP
1571static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1572 struct netdev_hw_addr_list *mc_list)
8ca151b5 1573{
e59647ea
EP
1574 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1575 struct iwl_mcast_filter_cmd *cmd;
1576 struct netdev_hw_addr *addr;
f3bd58f4
MS
1577 int addr_count;
1578 bool pass_all;
e59647ea
EP
1579 int len;
1580
f3bd58f4
MS
1581 addr_count = netdev_hw_addr_list_count(mc_list);
1582 pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1583 IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1584 if (pass_all)
e59647ea 1585 addr_count = 0;
e59647ea
EP
1586
1587 len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1588 cmd = kzalloc(len, GFP_ATOMIC);
1589 if (!cmd)
1590 return 0;
1591
1592 if (pass_all) {
1593 cmd->pass_all = 1;
1594 return (u64)(unsigned long)cmd;
1595 }
1596
1597 netdev_hw_addr_list_for_each(addr, mc_list) {
1598 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1599 cmd->count, addr->addr);
1600 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1601 addr->addr, ETH_ALEN);
1602 cmd->count++;
1603 }
1604
1605 return (u64)(unsigned long)cmd;
8ca151b5
JB
1606}
1607
1608static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1609 unsigned int changed_flags,
1610 unsigned int *total_flags,
1611 u64 multicast)
1612{
e59647ea
EP
1613 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1614 struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
8ca151b5 1615
e59647ea 1616 mutex_lock(&mvm->mutex);
51b6b9e0 1617
e59647ea
EP
1618 /* replace previous configuration */
1619 kfree(mvm->mcast_filter_cmd);
1620 mvm->mcast_filter_cmd = cmd;
51b6b9e0 1621
e59647ea
EP
1622 if (!cmd)
1623 goto out;
51b6b9e0 1624
e59647ea
EP
1625 iwl_mvm_recalc_multicast(mvm);
1626out:
1627 mutex_unlock(&mvm->mutex);
1628 *total_flags = 0;
51b6b9e0
EG
1629}
1630
c87163b9
EP
1631#ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1632struct iwl_bcast_iter_data {
1633 struct iwl_mvm *mvm;
1634 struct iwl_bcast_filter_cmd *cmd;
1635 u8 current_filter;
1636};
1637
1638static void
1639iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1640 const struct iwl_fw_bcast_filter *in_filter,
1641 struct iwl_fw_bcast_filter *out_filter)
1642{
1643 struct iwl_fw_bcast_filter_attr *attr;
1644 int i;
1645
1646 memcpy(out_filter, in_filter, sizeof(*out_filter));
1647
1648 for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1649 attr = &out_filter->attrs[i];
1650
1651 if (!attr->mask)
1652 break;
1653
2ee8f021
EP
1654 switch (attr->reserved1) {
1655 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1656 if (vif->bss_conf.arp_addr_cnt != 1) {
1657 attr->mask = 0;
1658 continue;
1659 }
1660
1661 attr->val = vif->bss_conf.arp_addr_list[0];
1662 break;
1663 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1664 attr->val = *(__be32 *)&vif->addr[2];
1665 break;
1666 default:
1667 break;
1668 }
1669 attr->reserved1 = 0;
c87163b9
EP
1670 out_filter->num_attrs++;
1671 }
1672}
1673
1674static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1675 struct ieee80211_vif *vif)
1676{
1677 struct iwl_bcast_iter_data *data = _data;
1678 struct iwl_mvm *mvm = data->mvm;
1679 struct iwl_bcast_filter_cmd *cmd = data->cmd;
1680 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1681 struct iwl_fw_bcast_mac *bcast_mac;
1682 int i;
1683
1684 if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1685 return;
1686
1687 bcast_mac = &cmd->macs[mvmvif->id];
1688
e48393e8
IP
1689 /*
1690 * enable filtering only for associated stations, but not for P2P
1691 * Clients
1692 */
1693 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1694 !vif->bss_conf.assoc)
c87163b9
EP
1695 return;
1696
1697 bcast_mac->default_discard = 1;
1698
1699 /* copy all configured filters */
1700 for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1701 /*
1702 * Make sure we don't exceed our filters limit.
1703 * if there is still a valid filter to be configured,
1704 * be on the safe side and just allow bcast for this mac.
1705 */
1706 if (WARN_ON_ONCE(data->current_filter >=
1707 ARRAY_SIZE(cmd->filters))) {
1708 bcast_mac->default_discard = 0;
1709 bcast_mac->attached_filters = 0;
1710 break;
1711 }
1712
1713 iwl_mvm_set_bcast_filter(vif,
1714 &mvm->bcast_filters[i],
1715 &cmd->filters[data->current_filter]);
1716
1717 /* skip current filter if it contains no attributes */
1718 if (!cmd->filters[data->current_filter].num_attrs)
1719 continue;
1720
1721 /* attach the filter to current mac */
1722 bcast_mac->attached_filters |=
1723 cpu_to_le16(BIT(data->current_filter));
1724
1725 data->current_filter++;
1726 }
1727}
1728
de06a59e
EP
1729bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1730 struct iwl_bcast_filter_cmd *cmd)
c87163b9 1731{
c87163b9
EP
1732 struct iwl_bcast_iter_data iter_data = {
1733 .mvm = mvm,
de06a59e 1734 .cmd = cmd,
c87163b9
EP
1735 };
1736
3b8983b1
MS
1737 if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1738 return false;
1739
de06a59e
EP
1740 memset(cmd, 0, sizeof(*cmd));
1741 cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1742 cmd->max_macs = ARRAY_SIZE(cmd->macs);
1743
1744#ifdef CONFIG_IWLWIFI_DEBUGFS
1745 /* use debugfs filters/macs if override is configured */
1746 if (mvm->dbgfs_bcast_filtering.override) {
1747 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1748 sizeof(cmd->filters));
1749 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1750 sizeof(cmd->macs));
1751 return true;
1752 }
1753#endif
c87163b9
EP
1754
1755 /* if no filters are configured, do nothing */
1756 if (!mvm->bcast_filters)
de06a59e 1757 return false;
c87163b9
EP
1758
1759 /* configure and attach these filters for each associated sta vif */
1760 ieee80211_iterate_active_interfaces(
1761 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1762 iwl_mvm_bcast_filter_iterator, &iter_data);
1763
de06a59e
EP
1764 return true;
1765}
1766static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1767 struct ieee80211_vif *vif)
1768{
1769 struct iwl_bcast_filter_cmd cmd;
1770
1771 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1772 return 0;
1773
1774 if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1775 return 0;
1776
a1022927 1777 return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
c87163b9
EP
1778 sizeof(cmd), &cmd);
1779}
1780#else
1781static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1782 struct ieee80211_vif *vif)
1783{
1784 return 0;
1785}
1786#endif
1787
8ca151b5
JB
1788static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1789 struct ieee80211_vif *vif,
1790 struct ieee80211_bss_conf *bss_conf,
1791 u32 changes)
1792{
1793 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1794 int ret;
1795
6e97b0d2
IP
1796 /*
1797 * Re-calculate the tsf id, as the master-slave relations depend on the
1798 * beacon interval, which was not known when the station interface was
1799 * added.
1800 */
1801 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1802 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1803
3dfd3a97
JB
1804 /*
1805 * If we're not associated yet, take the (new) BSSID before associating
1806 * so the firmware knows. If we're already associated, then use the old
1807 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
1808 * branch for disassociation below.
1809 */
1810 if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
1811 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1812
1813 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
8ca151b5
JB
1814 if (ret)
1815 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1816
3dfd3a97
JB
1817 /* after sending it once, adopt mac80211 data */
1818 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1819 mvmvif->associated = bss_conf->assoc;
1820
8ca151b5
JB
1821 if (changes & BSS_CHANGED_ASSOC) {
1822 if (bss_conf->assoc) {
33cef925
JB
1823 /* clear statistics to get clean beacon counter */
1824 iwl_mvm_request_statistics(mvm, true);
1825 memset(&mvmvif->beacon_stats, 0,
1826 sizeof(mvmvif->beacon_stats));
1827
8ca151b5 1828 /* add quota for this interface */
0166230c 1829 ret = iwl_mvm_update_quotas(mvm, NULL);
8ca151b5
JB
1830 if (ret) {
1831 IWL_ERR(mvm, "failed to update quotas\n");
1832 return;
1833 }
016d27e1
JB
1834
1835 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1836 &mvm->status)) {
1837 /*
1838 * If we're restarting then the firmware will
1839 * obviously have lost synchronisation with
1840 * the AP. It will attempt to synchronise by
1841 * itself, but we can make it more reliable by
1842 * scheduling a session protection time event.
1843 *
1844 * The firmware needs to receive a beacon to
1845 * catch up with synchronisation, use 110% of
1846 * the beacon interval.
1847 *
1848 * Set a large maximum delay to allow for more
1849 * than a single interface.
1850 */
1851 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1852 iwl_mvm_protect_session(mvm, vif, dur, dur,
d20d37bc 1853 5 * dur, false);
016d27e1 1854 }
1f3b0ff8
LE
1855
1856 iwl_mvm_sf_update(mvm, vif, false);
175a70b7 1857 iwl_mvm_power_vif_assoc(mvm, vif);
697162a1 1858 if (vif->p2p) {
29a90a49 1859 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
697162a1
EG
1860 iwl_mvm_update_smps(mvm, vif,
1861 IWL_MVM_SMPS_REQ_PROT,
1862 IEEE80211_SMPS_DYNAMIC);
1863 }
8ca151b5 1864 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1f3b0ff8
LE
1865 /*
1866 * If update fails - SF might be running in associated
1867 * mode while disassociated - which is forbidden.
1868 */
1869 WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1870 "Failed to update SF upon disassociation\n");
1871
8ca151b5
JB
1872 /* remove AP station now that the MAC is unassoc */
1873 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1874 if (ret)
1875 IWL_ERR(mvm, "failed to remove AP station\n");
37577fe2
EP
1876
1877 if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1878 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
8ca151b5
JB
1879 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1880 /* remove quota for this interface */
0166230c 1881 ret = iwl_mvm_update_quotas(mvm, NULL);
8ca151b5
JB
1882 if (ret)
1883 IWL_ERR(mvm, "failed to update quotas\n");
29a90a49
EP
1884
1885 if (vif->p2p)
1886 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
3dfd3a97
JB
1887
1888 /* this will take the cleared BSSID from bss_conf */
1889 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1890 if (ret)
1891 IWL_ERR(mvm,
1892 "failed to update MAC %pM (clear after unassoc)\n",
1893 vif->addr);
8ca151b5 1894 }
a20fd398 1895
e59647ea 1896 iwl_mvm_recalc_multicast(mvm);
c87163b9 1897 iwl_mvm_configure_bcast_filter(mvm, vif);
e59647ea 1898
a20fd398
AO
1899 /* reset rssi values */
1900 mvmvif->bf_data.ave_beacon_signal = 0;
1901
8e484f0b 1902 iwl_mvm_bt_coex_vif_change(mvm);
f94045ed
EG
1903 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1904 IEEE80211_SMPS_AUTOMATIC);
989c6505 1905 } else if (changes & BSS_CHANGED_BEACON_INFO) {
210a544e
JB
1906 /*
1907 * We received a beacon _after_ association so
1908 * remove the session protection.
1909 */
1910 iwl_mvm_remove_time_event(mvm, mvmvif,
1911 &mvmvif->time_event_data);
8ca151b5 1912 }
cc87d322
EH
1913
1914 if (changes & BSS_CHANGED_BEACON_INFO) {
1915 iwl_mvm_sf_update(mvm, vif, false);
1916 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1917 }
1918
1bc10d3b
JB
1919 if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS)) {
1920 ret = iwl_mvm_power_update_mac(mvm);
1921 if (ret)
1922 IWL_ERR(mvm, "failed to update power mode\n");
1923 }
1924
88f2fd73
MG
1925 if (changes & BSS_CHANGED_TXPOWER) {
1926 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1927 bss_conf->txpower);
1928 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1929 }
a20fd398
AO
1930
1931 if (changes & BSS_CHANGED_CQM) {
3c6acb61 1932 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
a20fd398
AO
1933 /* reset cqm events tracking */
1934 mvmvif->bf_data.last_cqm_event = 0;
fa7b2e7f
AA
1935 if (mvmvif->bf_data.bf_enabled) {
1936 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1937 if (ret)
1938 IWL_ERR(mvm,
1939 "failed to update CQM thresholds\n");
1940 }
a20fd398 1941 }
2ee8f021
EP
1942
1943 if (changes & BSS_CHANGED_ARP_FILTER) {
3c6acb61 1944 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
2ee8f021
EP
1945 iwl_mvm_configure_bcast_filter(mvm, vif);
1946 }
8ca151b5
JB
1947}
1948
5023d966
JB
1949static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1950 struct ieee80211_vif *vif)
8ca151b5
JB
1951{
1952 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1953 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1954 int ret;
1955
576eeee9
EP
1956 /*
1957 * iwl_mvm_mac_ctxt_add() might read directly from the device
1958 * (the system time), so make sure it is available.
1959 */
1960 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
1961 if (ret)
1962 return ret;
1963
8ca151b5
JB
1964 mutex_lock(&mvm->mutex);
1965
1966 /* Send the beacon template */
1967 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1968 if (ret)
1969 goto out_unlock;
1970
6e97b0d2
IP
1971 /*
1972 * Re-calculate the tsf id, as the master-slave relations depend on the
1973 * beacon interval, which was not known when the AP interface was added.
1974 */
1975 if (vif->type == NL80211_IFTYPE_AP)
1976 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1977
8ca151b5
JB
1978 /* Add the mac context */
1979 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1980 if (ret)
1981 goto out_unlock;
1982
1983 /* Perform the binding */
1984 ret = iwl_mvm_binding_add_vif(mvm, vif);
1985 if (ret)
1986 goto out_remove;
1987
8ca151b5
JB
1988 /* Send the bcast station. At this stage the TBTT and DTIM time events
1989 * are added and applied to the scheduler */
013290aa 1990 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
8ca151b5
JB
1991 if (ret)
1992 goto out_unbind;
1993
5691e218
IP
1994 /* must be set before quota calculations */
1995 mvmvif->ap_ibss_active = true;
1996
a11e144e 1997 /* power updated needs to be done before quotas */
999609f1 1998 iwl_mvm_power_update_mac(mvm);
a11e144e 1999
0166230c 2000 ret = iwl_mvm_update_quotas(mvm, NULL);
8ca151b5 2001 if (ret)
a11e144e 2002 goto out_quota_failed;
8ca151b5 2003
5023d966 2004 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
8ca151b5 2005 if (vif->p2p && mvm->p2p_device_vif)
3dfd3a97 2006 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
8ca151b5 2007
29a90a49
EP
2008 iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
2009
8e484f0b 2010 iwl_mvm_bt_coex_vif_change(mvm);
dac94da8 2011
f697267f
AN
2012 /* we don't support TDLS during DCM */
2013 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2014 iwl_mvm_teardown_tdls_peers(mvm);
2015
8ca151b5
JB
2016 mutex_unlock(&mvm->mutex);
2017 return 0;
2018
a11e144e 2019out_quota_failed:
999609f1 2020 iwl_mvm_power_update_mac(mvm);
5691e218 2021 mvmvif->ap_ibss_active = false;
013290aa 2022 iwl_mvm_send_rm_bcast_sta(mvm, vif);
8ca151b5
JB
2023out_unbind:
2024 iwl_mvm_binding_remove_vif(mvm, vif);
2025out_remove:
2026 iwl_mvm_mac_ctxt_remove(mvm, vif);
2027out_unlock:
2028 mutex_unlock(&mvm->mutex);
576eeee9 2029 iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
8ca151b5
JB
2030 return ret;
2031}
2032
5023d966
JB
2033static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2034 struct ieee80211_vif *vif)
8ca151b5
JB
2035{
2036 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2037 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2038
38a12b5b
JB
2039 iwl_mvm_prepare_mac_removal(mvm, vif);
2040
8ca151b5
JB
2041 mutex_lock(&mvm->mutex);
2042
664322fa 2043 /* Handle AP stop while in CSA */
7f0a7c67
AO
2044 if (rcu_access_pointer(mvm->csa_vif) == vif) {
2045 iwl_mvm_remove_time_event(mvm, mvmvif,
2046 &mvmvif->time_event_data);
664322fa 2047 RCU_INIT_POINTER(mvm->csa_vif, NULL);
7f0a7c67 2048 }
664322fa 2049
003e5236
AO
2050 if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2051 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2052 mvm->csa_tx_block_bcn_timeout = 0;
2053 }
2054
5023d966 2055 mvmvif->ap_ibss_active = false;
1c87bbad 2056 mvm->ap_last_beacon_gp2 = 0;
8ca151b5 2057
8e484f0b 2058 iwl_mvm_bt_coex_vif_change(mvm);
dac94da8 2059
29a90a49
EP
2060 iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
2061
5023d966 2062 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
8ca151b5 2063 if (vif->p2p && mvm->p2p_device_vif)
3dfd3a97 2064 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
8ca151b5 2065
0166230c 2066 iwl_mvm_update_quotas(mvm, NULL);
013290aa 2067 iwl_mvm_send_rm_bcast_sta(mvm, vif);
8ca151b5 2068 iwl_mvm_binding_remove_vif(mvm, vif);
a11e144e 2069
999609f1 2070 iwl_mvm_power_update_mac(mvm);
a11e144e 2071
8ca151b5
JB
2072 iwl_mvm_mac_ctxt_remove(mvm, vif);
2073
2074 mutex_unlock(&mvm->mutex);
2075}
2076
5023d966
JB
2077static void
2078iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2079 struct ieee80211_vif *vif,
2080 struct ieee80211_bss_conf *bss_conf,
2081 u32 changes)
8ca151b5 2082{
be2056fc 2083 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8a5e3660 2084
be2056fc
IP
2085 /* Changes will be applied when the AP/IBSS is started */
2086 if (!mvmvif->ap_ibss_active)
2087 return;
2088
863230da 2089 if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
f7d8b702 2090 BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3dfd3a97 2091 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
863230da 2092 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
8a5e3660 2093
8ca151b5 2094 /* Need to send a new beacon template to the FW */
863230da
JB
2095 if (changes & BSS_CHANGED_BEACON &&
2096 iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2097 IWL_WARN(mvm, "Failed updating beacon data\n");
79b7a69d
HD
2098
2099 if (changes & BSS_CHANGED_TXPOWER) {
2100 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2101 bss_conf->txpower);
2102 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2103 }
2104
8ca151b5
JB
2105}
2106
2107static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2108 struct ieee80211_vif *vif,
2109 struct ieee80211_bss_conf *bss_conf,
2110 u32 changes)
2111{
2112 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2113
576eeee9
EP
2114 /*
2115 * iwl_mvm_bss_info_changed_station() might call
2116 * iwl_mvm_protect_session(), which reads directly from
2117 * the device (the system time), so make sure it is available.
2118 */
2119 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
2120 return;
2121
8ca151b5
JB
2122 mutex_lock(&mvm->mutex);
2123
723f02ed 2124 if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
fb98be5e 2125 iwl_mvm_scan_offload_stop(mvm, true);
723f02ed 2126
8ca151b5
JB
2127 switch (vif->type) {
2128 case NL80211_IFTYPE_STATION:
2129 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2130 break;
2131 case NL80211_IFTYPE_AP:
5023d966
JB
2132 case NL80211_IFTYPE_ADHOC:
2133 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
8ca151b5
JB
2134 break;
2135 default:
2136 /* shouldn't happen */
2137 WARN_ON_ONCE(1);
2138 }
2139
2140 mutex_unlock(&mvm->mutex);
576eeee9 2141 iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
8ca151b5
JB
2142}
2143
4660dfbb
EP
2144static int iwl_mvm_cancel_scan_wait_notif(struct iwl_mvm *mvm,
2145 enum iwl_scan_status scan_type)
2146{
2147 int ret;
2148 bool wait_for_handlers = false;
2149
2150 mutex_lock(&mvm->mutex);
2151
2152 if (mvm->scan_status != scan_type) {
2153 ret = 0;
2154 /* make sure there are no pending notifications */
2155 wait_for_handlers = true;
2156 goto out;
2157 }
2158
2159 switch (scan_type) {
2160 case IWL_MVM_SCAN_SCHED:
2161 ret = iwl_mvm_scan_offload_stop(mvm, true);
2162 break;
2163 case IWL_MVM_SCAN_OS:
2164 ret = iwl_mvm_cancel_scan(mvm);
2165 break;
2166 case IWL_MVM_SCAN_NONE:
2167 default:
2168 WARN_ON_ONCE(1);
2169 ret = -EINVAL;
2170 break;
2171 }
2172 if (ret)
2173 goto out;
2174
2175 wait_for_handlers = true;
2176out:
2177 mutex_unlock(&mvm->mutex);
2178
2179 /* make sure we consume the completion notification */
2180 if (wait_for_handlers)
2181 iwl_mvm_wait_for_async_handlers(mvm);
2182
2183 return ret;
2184}
8ca151b5
JB
2185static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2186 struct ieee80211_vif *vif,
c56ef672 2187 struct ieee80211_scan_request *hw_req)
8ca151b5
JB
2188{
2189 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
c56ef672 2190 struct cfg80211_scan_request *req = &hw_req->req;
8ca151b5
JB
2191 int ret;
2192
762533ba
DS
2193 if (req->n_channels == 0 ||
2194 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
8ca151b5
JB
2195 return -EINVAL;
2196
d2496221
DS
2197 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2198 ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_SCHED);
2199 if (ret)
2200 return ret;
2201 }
4660dfbb 2202
8ca151b5
JB
2203 mutex_lock(&mvm->mutex);
2204
4660dfbb 2205 if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
8ca151b5 2206 ret = -EBUSY;
519e2026
AN
2207 goto out;
2208 }
8ca151b5 2209
519e2026
AN
2210 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
2211
b975e55a 2212 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
d2496221 2213 ret = iwl_mvm_scan_umac(mvm, vif, hw_req);
fb98be5e 2214 else
1f940386 2215 ret = iwl_mvm_unified_scan_lmac(mvm, vif, hw_req);
fb98be5e 2216
519e2026
AN
2217 if (ret)
2218 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2219out:
8ca151b5 2220 mutex_unlock(&mvm->mutex);
8ca151b5
JB
2221 return ret;
2222}
2223
2224static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2225 struct ieee80211_vif *vif)
2226{
2227 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2228
2229 mutex_lock(&mvm->mutex);
2230
2231 iwl_mvm_cancel_scan(mvm);
2232
2233 mutex_unlock(&mvm->mutex);
2234}
2235
2236static void
2237iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3e56eadf 2238 struct ieee80211_sta *sta, u16 tids,
8ca151b5
JB
2239 int num_frames,
2240 enum ieee80211_frame_release_type reason,
2241 bool more_data)
2242{
2243 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5 2244
3e56eadf 2245 /* Called when we need to transmit (a) frame(s) from mac80211 */
8ca151b5 2246
3e56eadf
JB
2247 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2248 tids, more_data, false);
2249}
2250
2251static void
2252iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2253 struct ieee80211_sta *sta, u16 tids,
2254 int num_frames,
2255 enum ieee80211_frame_release_type reason,
2256 bool more_data)
2257{
2258 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2259
2260 /* Called when we need to transmit (a) frame(s) from agg queue */
2261
2262 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2263 tids, more_data, true);
8ca151b5
JB
2264}
2265
2266static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2267 struct ieee80211_vif *vif,
2268 enum sta_notify_cmd cmd,
2269 struct ieee80211_sta *sta)
2270{
2271 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5b577a90 2272 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3e56eadf 2273 int tid;
8ca151b5
JB
2274
2275 switch (cmd) {
2276 case STA_NOTIFY_SLEEP:
e3d4bc8c 2277 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
8ca151b5 2278 ieee80211_sta_block_awake(hw, sta, true);
3e56eadf
JB
2279 spin_lock_bh(&mvmsta->lock);
2280 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2281 struct iwl_mvm_tid_data *tid_data;
2282
2283 tid_data = &mvmsta->tid_data[tid];
2284 if (tid_data->state != IWL_AGG_ON &&
2285 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2286 continue;
2287 if (iwl_mvm_tid_queued(tid_data) == 0)
2288 continue;
2289 ieee80211_sta_set_buffered(sta, tid, true);
2290 }
2291 spin_unlock_bh(&mvmsta->lock);
8ca151b5
JB
2292 /*
2293 * The fw updates the STA to be asleep. Tx packets on the Tx
2294 * queues to this station will not be transmitted. The fw will
2295 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2296 */
2297 break;
2298 case STA_NOTIFY_AWAKE:
881acd89 2299 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
8ca151b5 2300 break;
9cc40712 2301 iwl_mvm_sta_modify_ps_wake(mvm, sta);
8ca151b5
JB
2302 break;
2303 default:
2304 break;
2305 }
2306}
2307
1ddbbb0c
JB
2308static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2309 struct ieee80211_vif *vif,
2310 struct ieee80211_sta *sta)
2311{
2312 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
9d8ce6af 2313 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1ddbbb0c
JB
2314
2315 /*
2316 * This is called before mac80211 does RCU synchronisation,
2317 * so here we already invalidate our internal RCU-protected
2318 * station pointer. The rest of the code will thus no longer
2319 * be able to find the station this way, and we don't rely
2320 * on further RCU synchronisation after the sta_state()
2321 * callback deleted the station.
2322 */
2323 mutex_lock(&mvm->mutex);
2324 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2325 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2326 ERR_PTR(-ENOENT));
2327 mutex_unlock(&mvm->mutex);
2328}
2329
bd1ba664
JB
2330static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2331 const u8 *bssid)
2332{
2333 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2334 return;
2335
2336 if (iwlwifi_mod_params.uapsd_disable) {
2337 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2338 return;
2339 }
2340
2341 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2342}
2343
8ca151b5
JB
2344static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2345 struct ieee80211_vif *vif,
2346 struct ieee80211_sta *sta,
2347 enum ieee80211_sta_state old_state,
2348 enum ieee80211_sta_state new_state)
2349{
2350 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2351 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2352 int ret;
2353
2354 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2355 sta->addr, old_state, new_state);
2356
2357 /* this would be a mac80211 bug ... but don't crash */
2358 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2359 return -EINVAL;
2360
2361 /* if a STA is being removed, reuse its ID */
2362 flush_work(&mvm->sta_drained_wk);
2363
2364 mutex_lock(&mvm->mutex);
2365 if (old_state == IEEE80211_STA_NOTEXIST &&
2366 new_state == IEEE80211_STA_NONE) {
48bc1307
JB
2367 /*
2368 * Firmware bug - it'll crash if the beacon interval is less
2369 * than 16. We can't avoid connecting at all, so refuse the
2370 * station state change, this will cause mac80211 to abandon
2371 * attempts to connect to this AP, and eventually wpa_s will
2372 * blacklist the AP...
2373 */
2374 if (vif->type == NL80211_IFTYPE_STATION &&
2375 vif->bss_conf.beacon_int < 16) {
2376 IWL_ERR(mvm,
2377 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2378 sta->addr, vif->bss_conf.beacon_int);
2379 ret = -EINVAL;
2380 goto out_unlock;
2381 }
cf7b491d
AN
2382
2383 if (sta->tdls &&
2384 (vif->p2p ||
fa3d07e4
AN
2385 iwl_mvm_tdls_sta_count(mvm, NULL) ==
2386 IWL_MVM_TDLS_STA_COUNT ||
cf7b491d
AN
2387 iwl_mvm_phy_ctx_count(mvm) > 1)) {
2388 IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2389 ret = -EBUSY;
2390 goto out_unlock;
2391 }
2392
8ca151b5 2393 ret = iwl_mvm_add_sta(mvm, vif, sta);
fa3d07e4
AN
2394 if (sta->tdls && ret == 0)
2395 iwl_mvm_recalc_tdls_state(mvm, vif, true);
8ca151b5
JB
2396 } else if (old_state == IEEE80211_STA_NONE &&
2397 new_state == IEEE80211_STA_AUTH) {
e820c2da
HD
2398 /*
2399 * EBS may be disabled due to previous failures reported by FW.
2400 * Reset EBS status here assuming environment has been changed.
2401 */
2402 mvm->last_ebs_successful = true;
bd1ba664 2403 iwl_mvm_check_uapsd(mvm, vif, sta->addr);
8ca151b5
JB
2404 ret = 0;
2405 } else if (old_state == IEEE80211_STA_AUTH &&
2406 new_state == IEEE80211_STA_ASSOC) {
7a453973
JB
2407 ret = iwl_mvm_update_sta(mvm, vif, sta);
2408 if (ret == 0)
2409 iwl_mvm_rs_rate_init(mvm, sta,
b87c2179
ES
2410 mvmvif->phy_ctxt->channel->band,
2411 true);
8ca151b5
JB
2412 } else if (old_state == IEEE80211_STA_ASSOC &&
2413 new_state == IEEE80211_STA_AUTHORIZED) {
f59e0e3c
AN
2414
2415 /* we don't support TDLS during DCM */
2416 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2417 iwl_mvm_teardown_tdls_peers(mvm);
2418
7df15b1e 2419 /* enable beacon filtering */
fa7b2e7f 2420 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
8ca151b5
JB
2421 ret = 0;
2422 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
2423 new_state == IEEE80211_STA_ASSOC) {
7df15b1e 2424 /* disable beacon filtering */
a1022927 2425 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
8ca151b5
JB
2426 ret = 0;
2427 } else if (old_state == IEEE80211_STA_ASSOC &&
2428 new_state == IEEE80211_STA_AUTH) {
2429 ret = 0;
2430 } else if (old_state == IEEE80211_STA_AUTH &&
2431 new_state == IEEE80211_STA_NONE) {
2432 ret = 0;
2433 } else if (old_state == IEEE80211_STA_NONE &&
2434 new_state == IEEE80211_STA_NOTEXIST) {
2435 ret = iwl_mvm_rm_sta(mvm, vif, sta);
fa3d07e4
AN
2436 if (sta->tdls)
2437 iwl_mvm_recalc_tdls_state(mvm, vif, false);
8ca151b5
JB
2438 } else {
2439 ret = -EIO;
2440 }
48bc1307 2441 out_unlock:
8ca151b5
JB
2442 mutex_unlock(&mvm->mutex);
2443
9c126cd6
LK
2444 if (sta->tdls && ret == 0) {
2445 if (old_state == IEEE80211_STA_NOTEXIST &&
2446 new_state == IEEE80211_STA_NONE)
2447 ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2448 else if (old_state == IEEE80211_STA_NONE &&
2449 new_state == IEEE80211_STA_NOTEXIST)
2450 ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2451 }
2452
8ca151b5
JB
2453 return ret;
2454}
2455
2456static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2457{
2458 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2459
2460 mvm->rts_threshold = value;
2461
2462 return 0;
2463}
2464
1f3b0ff8
LE
2465static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2466 struct ieee80211_vif *vif,
2467 struct ieee80211_sta *sta, u32 changed)
2468{
2469 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2470
2471 if (vif->type == NL80211_IFTYPE_STATION &&
2472 changed & IEEE80211_RC_NSS_CHANGED)
2473 iwl_mvm_sf_update(mvm, vif, false);
2474}
2475
8ca151b5
JB
2476static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2477 struct ieee80211_vif *vif, u16 ac,
2478 const struct ieee80211_tx_queue_params *params)
2479{
2480 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2481 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2482
2483 mvmvif->queue_params[ac] = *params;
2484
2485 /*
2486 * No need to update right away, we'll get BSS_CHANGED_QOS
2487 * The exception is P2P_DEVICE interface which needs immediate update.
2488 */
2489 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2490 int ret;
2491
2492 mutex_lock(&mvm->mutex);
3dfd3a97 2493 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
8ca151b5
JB
2494 mutex_unlock(&mvm->mutex);
2495 return ret;
2496 }
2497 return 0;
2498}
2499
2500static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2501 struct ieee80211_vif *vif)
2502{
2503 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2504 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
2505 200 + vif->bss_conf.beacon_int);
2506 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
2507 100 + vif->bss_conf.beacon_int);
2508
2509 if (WARN_ON_ONCE(vif->bss_conf.assoc))
2510 return;
2511
576eeee9
EP
2512 /*
2513 * iwl_mvm_protect_session() reads directly from the device
2514 * (the system time), so make sure it is available.
2515 */
2516 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2517 return;
2518
8ca151b5
JB
2519 mutex_lock(&mvm->mutex);
2520 /* Try really hard to protect the session and hear a beacon */
d20d37bc 2521 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
8ca151b5 2522 mutex_unlock(&mvm->mutex);
576eeee9
EP
2523
2524 iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
8ca151b5
JB
2525}
2526
35a000b7
DS
2527static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2528 struct ieee80211_vif *vif,
2529 struct cfg80211_sched_scan_request *req,
633e2713 2530 struct ieee80211_scan_ies *ies)
35a000b7
DS
2531{
2532 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2533 int ret;
2534
d2496221
DS
2535 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2536 ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_OS);
2537 if (ret)
2538 return ret;
2539 }
4660dfbb 2540
35a000b7
DS
2541 mutex_lock(&mvm->mutex);
2542
1f940386 2543 if (!vif->bss_conf.idle) {
bd5e4744
DS
2544 ret = -EBUSY;
2545 goto out;
2546 }
2547
4660dfbb 2548 if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
35a000b7
DS
2549 ret = -EBUSY;
2550 goto out;
2551 }
2552
b141c235 2553 ret = iwl_mvm_scan_offload_start(mvm, vif, req, ies);
35a000b7 2554 if (ret)
b141c235 2555 mvm->scan_status = IWL_MVM_SCAN_NONE;
d2496221 2556
35a000b7
DS
2557out:
2558 mutex_unlock(&mvm->mutex);
2559 return ret;
2560}
2561
37e3308c
JB
2562static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2563 struct ieee80211_vif *vif)
35a000b7
DS
2564{
2565 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
33ea27f6 2566 int ret;
35a000b7
DS
2567
2568 mutex_lock(&mvm->mutex);
fb98be5e 2569 ret = iwl_mvm_scan_offload_stop(mvm, false);
35a000b7 2570 mutex_unlock(&mvm->mutex);
33ea27f6 2571 iwl_mvm_wait_for_async_handlers(mvm);
37e3308c 2572
33ea27f6 2573 return ret;
d2496221 2574
35a000b7
DS
2575}
2576
8ca151b5
JB
2577static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2578 enum set_key_cmd cmd,
2579 struct ieee80211_vif *vif,
2580 struct ieee80211_sta *sta,
2581 struct ieee80211_key_conf *key)
2582{
2583 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2584 int ret;
2585
2586 if (iwlwifi_mod_params.sw_crypto) {
2587 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2588 return -EOPNOTSUPP;
2589 }
2590
2591 switch (key->cipher) {
2592 case WLAN_CIPHER_SUITE_TKIP:
2593 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2594 /* fall-through */
2595 case WLAN_CIPHER_SUITE_CCMP:
2596 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2597 break;
2598 case WLAN_CIPHER_SUITE_AES_CMAC:
2599 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
2600 break;
2601 case WLAN_CIPHER_SUITE_WEP40:
2602 case WLAN_CIPHER_SUITE_WEP104:
ba3943b0
JB
2603 /* For non-client mode, only use WEP keys for TX as we probably
2604 * don't have a station yet anyway and would then have to keep
2605 * track of the keys, linking them to each of the clients/peers
2606 * as they appear. For now, don't do that, for performance WEP
2607 * offload doesn't really matter much, but we need it for some
2608 * other offload features in client mode.
8ca151b5 2609 */
ba3943b0
JB
2610 if (vif->type != NL80211_IFTYPE_STATION)
2611 return 0;
2612 break;
8ca151b5 2613 default:
e36e5433
MS
2614 /* currently FW supports only one optional cipher scheme */
2615 if (hw->n_cipher_schemes &&
2616 hw->cipher_schemes->cipher == key->cipher)
2617 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2618 else
2619 return -EOPNOTSUPP;
8ca151b5
JB
2620 }
2621
2622 mutex_lock(&mvm->mutex);
2623
2624 switch (cmd) {
2625 case SET_KEY:
5023d966
JB
2626 if ((vif->type == NL80211_IFTYPE_ADHOC ||
2627 vif->type == NL80211_IFTYPE_AP) && !sta) {
2628 /*
2629 * GTK on AP interface is a TX-only key, return 0;
2630 * on IBSS they're per-station and because we're lazy
2631 * we don't support them for RX, so do the same.
2632 */
6caffd4f
JB
2633 ret = 0;
2634 key->hw_key_idx = STA_KEY_IDX_INVALID;
2635 break;
2636 }
2637
8ca151b5
JB
2638 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
2639 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
2640 if (ret) {
2641 IWL_WARN(mvm, "set key failed\n");
2642 /*
2643 * can't add key for RX, but we don't need it
2644 * in the device for TX so still return 0
2645 */
6caffd4f 2646 key->hw_key_idx = STA_KEY_IDX_INVALID;
8ca151b5
JB
2647 ret = 0;
2648 }
2649
2650 break;
2651 case DISABLE_KEY:
6caffd4f
JB
2652 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
2653 ret = 0;
2654 break;
2655 }
2656
8ca151b5
JB
2657 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
2658 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
2659 break;
2660 default:
2661 ret = -EINVAL;
2662 }
2663
2664 mutex_unlock(&mvm->mutex);
2665 return ret;
2666}
2667
2668static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
2669 struct ieee80211_vif *vif,
2670 struct ieee80211_key_conf *keyconf,
2671 struct ieee80211_sta *sta,
2672 u32 iv32, u16 *phase1key)
2673{
2674 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2675
5023d966
JB
2676 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
2677 return;
2678
8ca151b5
JB
2679 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
2680}
2681
2682
b112889c
AM
2683static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
2684 struct iwl_rx_packet *pkt, void *data)
2685{
2686 struct iwl_mvm *mvm =
2687 container_of(notif_wait, struct iwl_mvm, notif_wait);
2688 struct iwl_hs20_roc_res *resp;
2689 int resp_len = iwl_rx_packet_payload_len(pkt);
2690 struct iwl_mvm_time_event_data *te_data = data;
2691
2692 if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
2693 return true;
2694
2695 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
2696 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
2697 return true;
2698 }
2699
2700 resp = (void *)pkt->data;
2701
2702 IWL_DEBUG_TE(mvm,
2703 "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
2704 resp->status, resp->event_unique_id);
2705
2706 te_data->uid = le32_to_cpu(resp->event_unique_id);
2707 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
2708 te_data->uid);
2709
2710 spin_lock_bh(&mvm->time_event_lock);
2711 list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
2712 spin_unlock_bh(&mvm->time_event_lock);
2713
2714 return true;
2715}
2716
2717#define AUX_ROC_MAX_DELAY_ON_CHANNEL 5000
2718static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
2719 struct ieee80211_channel *channel,
2720 struct ieee80211_vif *vif,
2721 int duration)
2722{
2723 int res, time_reg = DEVICE_SYSTEM_TIME_REG;
2724 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2725 struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
2726 static const u8 time_event_response[] = { HOT_SPOT_CMD };
2727 struct iwl_notification_wait wait_time_event;
2728 struct iwl_hs20_roc_req aux_roc_req = {
2729 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
2730 .id_and_color =
2731 cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
2732 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
2733 /* Set the channel info data */
2734 .channel_info.band = (channel->band == IEEE80211_BAND_2GHZ) ?
2735 PHY_BAND_24 : PHY_BAND_5,
2736 .channel_info.channel = channel->hw_value,
2737 .channel_info.width = PHY_VHT_CHANNEL_MODE20,
2738 /* Set the time and duration */
2739 .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
2740 .apply_time_max_delay =
2741 cpu_to_le32(MSEC_TO_TU(AUX_ROC_MAX_DELAY_ON_CHANNEL)),
2742 .duration = cpu_to_le32(MSEC_TO_TU(duration)),
2743 };
2744
2745 /* Set the node address */
2746 memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
2747
a6cc5163
MG
2748 lockdep_assert_held(&mvm->mutex);
2749
2750 spin_lock_bh(&mvm->time_event_lock);
2751
2752 if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
2753 spin_unlock_bh(&mvm->time_event_lock);
2754 return -EIO;
2755 }
2756
b112889c
AM
2757 te_data->vif = vif;
2758 te_data->duration = duration;
2759 te_data->id = HOT_SPOT_CMD;
2760
b112889c
AM
2761 spin_unlock_bh(&mvm->time_event_lock);
2762
2763 /*
2764 * Use a notification wait, which really just processes the
2765 * command response and doesn't wait for anything, in order
2766 * to be able to process the response and get the UID inside
2767 * the RX path. Using CMD_WANT_SKB doesn't work because it
2768 * stores the buffer and then wakes up this thread, by which
2769 * time another notification (that the time event started)
2770 * might already be processed unsuccessfully.
2771 */
2772 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
2773 time_event_response,
2774 ARRAY_SIZE(time_event_response),
2775 iwl_mvm_rx_aux_roc, te_data);
2776
2777 res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
2778 &aux_roc_req);
2779
2780 if (res) {
2781 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
2782 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
2783 goto out_clear_te;
2784 }
2785
2786 /* No need to wait for anything, so just pass 1 (0 isn't valid) */
2787 res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
2788 /* should never fail */
2789 WARN_ON_ONCE(res);
2790
2791 if (res) {
2792 out_clear_te:
2793 spin_lock_bh(&mvm->time_event_lock);
2794 iwl_mvm_te_clear_data(mvm, te_data);
2795 spin_unlock_bh(&mvm->time_event_lock);
2796 }
2797
2798 return res;
2799}
2800
8ca151b5
JB
2801static int iwl_mvm_roc(struct ieee80211_hw *hw,
2802 struct ieee80211_vif *vif,
2803 struct ieee80211_channel *channel,
d339d5ca
IP
2804 int duration,
2805 enum ieee80211_roc_type type)
8ca151b5
JB
2806{
2807 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3 2808 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 2809 struct cfg80211_chan_def chandef;
31d385ae
IP
2810 struct iwl_mvm_phy_ctxt *phy_ctxt;
2811 int ret, i;
2812
2813 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2814 duration, type);
8ca151b5 2815
a6cc5163
MG
2816 mutex_lock(&mvm->mutex);
2817
b112889c
AM
2818 switch (vif->type) {
2819 case NL80211_IFTYPE_STATION:
5ac6c72e
LC
2820 if (mvm->fw->ucode_capa.capa[0] &
2821 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT) {
2822 /* Use aux roc framework (HS20) */
2823 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
2824 vif, duration);
2825 goto out_unlock;
2826 }
2827 IWL_ERR(mvm, "hotspot not supported\n");
2828 ret = -EINVAL;
a6cc5163 2829 goto out_unlock;
b112889c
AM
2830 case NL80211_IFTYPE_P2P_DEVICE:
2831 /* handle below */
2832 break;
2833 default:
2834 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
a6cc5163
MG
2835 ret = -EINVAL;
2836 goto out_unlock;
8ca151b5
JB
2837 }
2838
31d385ae
IP
2839 for (i = 0; i < NUM_PHY_CTX; i++) {
2840 phy_ctxt = &mvm->phy_ctxts[i];
2841 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2842 continue;
2843
2844 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2845 /*
2846 * Unbind the P2P_DEVICE from the current PHY context,
2847 * and if the PHY context is not used remove it.
2848 */
2849 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2850 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2851 goto out_unlock;
2852
2853 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2854
2855 /* Bind the P2P_DEVICE to the current PHY Context */
2856 mvmvif->phy_ctxt = phy_ctxt;
2857
2858 ret = iwl_mvm_binding_add_vif(mvm, vif);
2859 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2860 goto out_unlock;
2861
2862 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2863 goto schedule_time_event;
2864 }
2865 }
2866
2867 /* Need to update the PHY context only if the ROC channel changed */
2868 if (channel == mvmvif->phy_ctxt->channel)
2869 goto schedule_time_event;
2870
8ca151b5 2871 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
8ca151b5 2872
31d385ae
IP
2873 /*
2874 * Change the PHY context configuration as it is currently referenced
2875 * only by the P2P Device MAC
2876 */
2877 if (mvmvif->phy_ctxt->ref == 1) {
2878 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2879 &chandef, 1, 1);
2880 if (ret)
2881 goto out_unlock;
2882 } else {
2883 /*
2884 * The PHY context is shared with other MACs. Need to remove the
2885 * P2P Device from the binding, allocate an new PHY context and
2886 * create a new binding
2887 */
2888 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2889 if (!phy_ctxt) {
2890 ret = -ENOSPC;
2891 goto out_unlock;
2892 }
2893
2894 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2895 1, 1);
2896 if (ret) {
2897 IWL_ERR(mvm, "Failed to change PHY context\n");
2898 goto out_unlock;
2899 }
2900
2901 /* Unbind the P2P_DEVICE from the current PHY context */
2902 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2903 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2904 goto out_unlock;
2905
2906 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2907
2908 /* Bind the P2P_DEVICE to the new allocated PHY context */
2909 mvmvif->phy_ctxt = phy_ctxt;
2910
2911 ret = iwl_mvm_binding_add_vif(mvm, vif);
2912 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2913 goto out_unlock;
2914
2915 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2916 }
2917
2918schedule_time_event:
8ca151b5 2919 /* Schedule the time events */
e635c797 2920 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
8ca151b5 2921
31d385ae 2922out_unlock:
8ca151b5
JB
2923 mutex_unlock(&mvm->mutex);
2924 IWL_DEBUG_MAC80211(mvm, "leave\n");
8ca151b5
JB
2925 return ret;
2926}
2927
2928static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2929{
2930 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2931
2932 IWL_DEBUG_MAC80211(mvm, "enter\n");
2933
2934 mutex_lock(&mvm->mutex);
bf5da87f 2935 iwl_mvm_stop_roc(mvm);
8ca151b5
JB
2936 mutex_unlock(&mvm->mutex);
2937
2938 IWL_DEBUG_MAC80211(mvm, "leave\n");
2939 return 0;
2940}
2941
b08c1d97
LC
2942static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
2943 struct ieee80211_chanctx_conf *ctx)
8ca151b5 2944{
fe0f2de3
IP
2945 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2946 struct iwl_mvm_phy_ctxt *phy_ctxt;
8ca151b5
JB
2947 int ret;
2948
b08c1d97
LC
2949 lockdep_assert_held(&mvm->mutex);
2950
53a9d61e 2951 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
fe0f2de3 2952
fe0f2de3
IP
2953 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2954 if (!phy_ctxt) {
2955 ret = -ENOSPC;
2956 goto out;
2957 }
8ca151b5 2958
dcbc3e1a 2959 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
53a9d61e
IP
2960 ctx->rx_chains_static,
2961 ctx->rx_chains_dynamic);
fe0f2de3
IP
2962 if (ret) {
2963 IWL_ERR(mvm, "Failed to add PHY context\n");
2964 goto out;
2965 }
2966
53a9d61e 2967 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
fe0f2de3
IP
2968 *phy_ctxt_id = phy_ctxt->id;
2969out:
b08c1d97
LC
2970 return ret;
2971}
2972
2973static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2974 struct ieee80211_chanctx_conf *ctx)
2975{
2976 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2977 int ret;
2978
2979 mutex_lock(&mvm->mutex);
2980 ret = __iwl_mvm_add_chanctx(mvm, ctx);
8ca151b5 2981 mutex_unlock(&mvm->mutex);
b08c1d97 2982
8ca151b5
JB
2983 return ret;
2984}
2985
b08c1d97
LC
2986static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
2987 struct ieee80211_chanctx_conf *ctx)
2988{
2989 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2990 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2991
2992 lockdep_assert_held(&mvm->mutex);
2993
2994 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2995}
2996
8ca151b5
JB
2997static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2998 struct ieee80211_chanctx_conf *ctx)
2999{
3000 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5
JB
3001
3002 mutex_lock(&mvm->mutex);
b08c1d97 3003 __iwl_mvm_remove_chanctx(mvm, ctx);
8ca151b5
JB
3004 mutex_unlock(&mvm->mutex);
3005}
3006
3007static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3008 struct ieee80211_chanctx_conf *ctx,
3009 u32 changed)
3010{
3011 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
3012 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3013 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5 3014
31d385ae
IP
3015 if (WARN_ONCE((phy_ctxt->ref > 1) &&
3016 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3017 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2dceedae
AN
3018 IEEE80211_CHANCTX_CHANGE_RADAR |
3019 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
31d385ae
IP
3020 "Cannot change PHY. Ref=%d, changed=0x%X\n",
3021 phy_ctxt->ref, changed))
3022 return;
3023
8ca151b5 3024 mutex_lock(&mvm->mutex);
4d66449a 3025 iwl_mvm_bt_coex_vif_change(mvm);
dcbc3e1a 3026 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
8ca151b5
JB
3027 ctx->rx_chains_static,
3028 ctx->rx_chains_dynamic);
3029 mutex_unlock(&mvm->mutex);
3030}
3031
b08c1d97
LC
3032static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
3033 struct ieee80211_vif *vif,
f0c97783
LC
3034 struct ieee80211_chanctx_conf *ctx,
3035 bool switching_chanctx)
8ca151b5 3036{
fe0f2de3
IP
3037 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3038 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5
JB
3039 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3040 int ret;
3041
b08c1d97 3042 lockdep_assert_held(&mvm->mutex);
8ca151b5 3043
fe0f2de3 3044 mvmvif->phy_ctxt = phy_ctxt;
8ca151b5
JB
3045
3046 switch (vif->type) {
3047 case NL80211_IFTYPE_AP:
4741dd04
LC
3048 /* only needed if we're switching chanctx (i.e. during CSA) */
3049 if (switching_chanctx) {
bd3398e2
AO
3050 mvmvif->ap_ibss_active = true;
3051 break;
3052 }
5023d966 3053 case NL80211_IFTYPE_ADHOC:
8ca151b5
JB
3054 /*
3055 * The AP binding flow is handled as part of the start_ap flow
5023d966 3056 * (in bss_info_changed), similarly for IBSS.
8ca151b5
JB
3057 */
3058 ret = 0;
b08c1d97 3059 goto out;
8ca151b5 3060 case NL80211_IFTYPE_STATION:
2533edce 3061 break;
8ca151b5 3062 case NL80211_IFTYPE_MONITOR:
2533edce
LC
3063 /* always disable PS when a monitor interface is active */
3064 mvmvif->ps_disabled = true;
8ca151b5
JB
3065 break;
3066 default:
3067 ret = -EINVAL;
b08c1d97 3068 goto out;
8ca151b5
JB
3069 }
3070
3071 ret = iwl_mvm_binding_add_vif(mvm, vif);
3072 if (ret)
b08c1d97 3073 goto out;
8ca151b5
JB
3074
3075 /*
92d85562
AB
3076 * Power state must be updated before quotas,
3077 * otherwise fw will complain.
3078 */
999609f1 3079 iwl_mvm_power_update_mac(mvm);
92d85562
AB
3080
3081 /* Setting the quota at this stage is only required for monitor
8ca151b5
JB
3082 * interfaces. For the other types, the bss_info changed flow
3083 * will handle quota settings.
3084 */
3085 if (vif->type == NL80211_IFTYPE_MONITOR) {
1e1391ca 3086 mvmvif->monitor_active = true;
0166230c 3087 ret = iwl_mvm_update_quotas(mvm, NULL);
8ca151b5
JB
3088 if (ret)
3089 goto out_remove_binding;
3090 }
3091
bd3398e2 3092 /* Handle binding during CSA */
a57c688d 3093 if (vif->type == NL80211_IFTYPE_AP) {
0166230c 3094 iwl_mvm_update_quotas(mvm, NULL);
3dfd3a97 3095 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
bd3398e2
AO
3096 }
3097
4741dd04 3098 if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
686e7fe1
LC
3099 u32 duration = 2 * vif->bss_conf.beacon_int;
3100
3101 /* iwl_mvm_protect_session() reads directly from the
3102 * device (the system time), so make sure it is
3103 * available.
3104 */
3105 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
3106 if (ret)
3107 goto out_remove_binding;
3108
3109 /* Protect the session to make sure we hear the first
3110 * beacon on the new channel.
3111 */
3112 iwl_mvm_protect_session(mvm, vif, duration, duration,
3113 vif->bss_conf.beacon_int / 2,
3114 true);
3115
3116 iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
3117
a57c688d 3118 iwl_mvm_update_quotas(mvm, NULL);
0ce04ce7
LC
3119 }
3120
b08c1d97 3121 goto out;
8ca151b5 3122
b08c1d97 3123out_remove_binding:
8ca151b5 3124 iwl_mvm_binding_remove_vif(mvm, vif);
999609f1 3125 iwl_mvm_power_update_mac(mvm);
b08c1d97 3126out:
8ca151b5
JB
3127 if (ret)
3128 mvmvif->phy_ctxt = NULL;
3129 return ret;
3130}
b08c1d97
LC
3131static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
3132 struct ieee80211_vif *vif,
3133 struct ieee80211_chanctx_conf *ctx)
8ca151b5
JB
3134{
3135 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
b08c1d97 3136 int ret;
8ca151b5
JB
3137
3138 mutex_lock(&mvm->mutex);
f0c97783 3139 ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
b08c1d97
LC
3140 mutex_unlock(&mvm->mutex);
3141
3142 return ret;
3143}
3144
3145static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
3146 struct ieee80211_vif *vif,
f0c97783
LC
3147 struct ieee80211_chanctx_conf *ctx,
3148 bool switching_chanctx)
b08c1d97
LC
3149{
3150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
f0c97783 3151 struct ieee80211_vif *disabled_vif = NULL;
b08c1d97
LC
3152
3153 lockdep_assert_held(&mvm->mutex);
8ca151b5
JB
3154
3155 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
3156
8ca151b5 3157 switch (vif->type) {
5023d966 3158 case NL80211_IFTYPE_ADHOC:
b08c1d97 3159 goto out;
8ca151b5 3160 case NL80211_IFTYPE_MONITOR:
1e1391ca 3161 mvmvif->monitor_active = false;
2533edce 3162 mvmvif->ps_disabled = false;
8ca151b5 3163 break;
bd3398e2
AO
3164 case NL80211_IFTYPE_AP:
3165 /* This part is triggered only during CSA */
4741dd04 3166 if (!switching_chanctx || !mvmvif->ap_ibss_active)
b08c1d97 3167 goto out;
bd3398e2 3168
7ef0aab6
AO
3169 mvmvif->csa_countdown = false;
3170
003e5236
AO
3171 /* Set CS bit on all the stations */
3172 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
3173
3174 /* Save blocked iface, the timeout is set on the next beacon */
3175 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
3176
bd3398e2 3177 mvmvif->ap_ibss_active = false;
f0c97783
LC
3178 break;
3179 case NL80211_IFTYPE_STATION:
3180 if (!switching_chanctx)
3181 break;
3182
3183 disabled_vif = vif;
3184
3dfd3a97 3185 iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
f0c97783 3186 break;
8ca151b5
JB
3187 default:
3188 break;
3189 }
3190
f0c97783 3191 iwl_mvm_update_quotas(mvm, disabled_vif);
1e1391ca 3192 iwl_mvm_binding_remove_vif(mvm, vif);
1c2abf72 3193
b08c1d97 3194out:
a11e144e 3195 mvmvif->phy_ctxt = NULL;
999609f1 3196 iwl_mvm_power_update_mac(mvm);
b08c1d97
LC
3197}
3198
3199static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
3200 struct ieee80211_vif *vif,
3201 struct ieee80211_chanctx_conf *ctx)
3202{
3203 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3204
3205 mutex_lock(&mvm->mutex);
f0c97783 3206 __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
8ca151b5
JB
3207 mutex_unlock(&mvm->mutex);
3208}
3209
50cc9574
LC
3210static int
3211iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
3212 struct ieee80211_vif_chanctx_switch *vifs)
b08c1d97 3213{
b08c1d97
LC
3214 int ret;
3215
b08c1d97 3216 mutex_lock(&mvm->mutex);
f0c97783 3217 __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
b08c1d97
LC
3218 __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
3219
3220 ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
3221 if (ret) {
3222 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
3223 goto out_reassign;
3224 }
3225
f0c97783
LC
3226 ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3227 true);
b08c1d97
LC
3228 if (ret) {
3229 IWL_ERR(mvm,
3230 "failed to assign new_ctx during channel switch\n");
3231 goto out_remove;
3232 }
3233
f697267f
AN
3234 /* we don't support TDLS during DCM - can be caused by channel switch */
3235 if (iwl_mvm_phy_ctx_count(mvm) > 1)
3236 iwl_mvm_teardown_tdls_peers(mvm);
3237
b08c1d97
LC
3238 goto out;
3239
3240out_remove:
3241 __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
3242
3243out_reassign:
6fd1fb63 3244 if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
b08c1d97
LC
3245 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
3246 goto out_restart;
3247 }
3248
6fd1fb63
LC
3249 if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3250 true)) {
b08c1d97
LC
3251 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3252 goto out_restart;
3253 }
3254
3255 goto out;
3256
3257out_restart:
3258 /* things keep failing, better restart the hw */
3259 iwl_mvm_nic_restart(mvm, false);
3260
3261out:
3262 mutex_unlock(&mvm->mutex);
50cc9574
LC
3263
3264 return ret;
3265}
3266
48a256e8
LC
3267static int
3268iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
3269 struct ieee80211_vif_chanctx_switch *vifs)
3270{
3271 int ret;
3272
3273 mutex_lock(&mvm->mutex);
3274 __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3275
3276 ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3277 true);
3278 if (ret) {
3279 IWL_ERR(mvm,
3280 "failed to assign new_ctx during channel switch\n");
3281 goto out_reassign;
3282 }
3283
3284 goto out;
3285
3286out_reassign:
3287 if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3288 true)) {
3289 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3290 goto out_restart;
3291 }
3292
3293 goto out;
3294
3295out_restart:
3296 /* things keep failing, better restart the hw */
3297 iwl_mvm_nic_restart(mvm, false);
3298
3299out:
3300 mutex_unlock(&mvm->mutex);
3301
3302 return ret;
3303}
3304
50cc9574
LC
3305static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
3306 struct ieee80211_vif_chanctx_switch *vifs,
3307 int n_vifs,
3308 enum ieee80211_chanctx_switch_mode mode)
3309{
3310 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3311 int ret;
3312
3313 /* we only support a single-vif right now */
3314 if (n_vifs > 1)
3315 return -EOPNOTSUPP;
3316
3317 switch (mode) {
3318 case CHANCTX_SWMODE_SWAP_CONTEXTS:
3319 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
3320 break;
3321 case CHANCTX_SWMODE_REASSIGN_VIF:
48a256e8 3322 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
50cc9574
LC
3323 break;
3324 default:
3325 ret = -EOPNOTSUPP;
3326 break;
3327 }
3328
b08c1d97
LC
3329 return ret;
3330}
3331
8ca151b5
JB
3332static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
3333 struct ieee80211_sta *sta,
3334 bool set)
3335{
3336 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
9d8ce6af 3337 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
3338
3339 if (!mvm_sta || !mvm_sta->vif) {
3340 IWL_ERR(mvm, "Station is not associated to a vif\n");
3341 return -EINVAL;
3342 }
3343
3344 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
3345}
3346
507cadf2
DS
3347#ifdef CONFIG_NL80211_TESTMODE
3348static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
3349 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
3350 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
f6c6ad42 3351 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
507cadf2
DS
3352};
3353
3354static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
3355 struct ieee80211_vif *vif,
3356 void *data, int len)
3357{
3358 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
3359 int err;
3360 u32 noa_duration;
3361
3362 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
3363 if (err)
3364 return err;
3365
3366 if (!tb[IWL_MVM_TM_ATTR_CMD])
3367 return -EINVAL;
3368
3369 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
3370 case IWL_MVM_TM_CMD_SET_NOA:
3371 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
3372 !vif->bss_conf.enable_beacon ||
3373 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
3374 return -EINVAL;
3375
3376 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
3377 if (noa_duration >= vif->bss_conf.beacon_int)
3378 return -EINVAL;
3379
3380 mvm->noa_duration = noa_duration;
3381 mvm->noa_vif = vif;
3382
0166230c 3383 return iwl_mvm_update_quotas(mvm, NULL);
f6c6ad42
JB
3384 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
3385 /* must be associated client vif - ignore authorized */
3386 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
3387 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
3388 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
3389 return -EINVAL;
3390
3391 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
a1022927
EG
3392 return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3393 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
507cadf2
DS
3394 }
3395
3396 return -EOPNOTSUPP;
3397}
3398
3399static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
3400 struct ieee80211_vif *vif,
3401 void *data, int len)
3402{
3403 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3404 int err;
3405
3406 mutex_lock(&mvm->mutex);
3407 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
3408 mutex_unlock(&mvm->mutex);
3409
3410 return err;
3411}
3412#endif
3413
622e3f9b
LC
3414static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
3415 struct ieee80211_vif *vif,
3416 struct ieee80211_channel_switch *chsw)
3417{
3418 /* By implementing this operation, we prevent mac80211 from
3419 * starting its own channel switch timer, so that we can call
3420 * ieee80211_chswitch_done() ourselves at the right time
3421 * (which is when the absence time event starts).
3422 */
3423
3424 IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
3425 "dummy channel switch op\n");
3426}
3427
f028905c
LC
3428static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
3429 struct ieee80211_vif *vif,
3430 struct ieee80211_channel_switch *chsw)
bd3398e2
AO
3431{
3432 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
664322fa 3433 struct ieee80211_vif *csa_vif;
f6c34820 3434 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
dc88b4ba 3435 u32 apply_time;
f028905c 3436 int ret;
bd3398e2
AO
3437
3438 mutex_lock(&mvm->mutex);
664322fa 3439
6b20d774 3440 IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
f028905c 3441 chsw->chandef.center_freq1);
6b20d774
LC
3442
3443 switch (vif->type) {
3444 case NL80211_IFTYPE_AP:
3445 csa_vif =
3446 rcu_dereference_protected(mvm->csa_vif,
3447 lockdep_is_held(&mvm->mutex));
3448 if (WARN_ONCE(csa_vif && csa_vif->csa_active,
3449 "Another CSA is already in progress")) {
3450 ret = -EBUSY;
3451 goto out_unlock;
3452 }
3453
3454 rcu_assign_pointer(mvm->csa_vif, vif);
7ef0aab6 3455
7ef0aab6
AO
3456 if (WARN_ONCE(mvmvif->csa_countdown,
3457 "Previous CSA countdown didn't complete")) {
3458 ret = -EBUSY;
3459 goto out_unlock;
3460 }
3461
6b20d774 3462 break;
dc88b4ba 3463 case NL80211_IFTYPE_STATION:
4500e133
LC
3464 /* Schedule the time event to a bit before beacon 1,
3465 * to make sure we're in the new channel when the
3466 * GO/AP arrives.
3467 */
3468 apply_time = chsw->device_timestamp +
3469 ((vif->bss_conf.beacon_int * (chsw->count - 1) -
3470 IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
dc88b4ba
LC
3471
3472 if (chsw->block_tx)
3473 iwl_mvm_csa_client_absent(mvm, vif);
3474
4500e133 3475 iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
dc88b4ba 3476 apply_time);
c6e0a3e0
LC
3477 if (mvmvif->bf_data.bf_enabled) {
3478 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3479 if (ret)
3480 goto out_unlock;
3481 }
3482
dc88b4ba 3483 break;
6b20d774
LC
3484 default:
3485 break;
3486 }
bd3398e2 3487
f6c34820
LC
3488 mvmvif->ps_disabled = true;
3489
3490 ret = iwl_mvm_power_update_ps(mvm);
3491 if (ret)
3492 goto out_unlock;
f028905c 3493
e198f5e7
AN
3494 /* we won't be on this channel any longer */
3495 iwl_mvm_teardown_tdls_peers(mvm);
3496
bd3398e2
AO
3497out_unlock:
3498 mutex_unlock(&mvm->mutex);
f028905c
LC
3499
3500 return ret;
bd3398e2
AO
3501}
3502
f6c34820
LC
3503static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
3504 struct ieee80211_vif *vif)
3505{
3506 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3507 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3508 int ret;
3509
3510 mutex_lock(&mvm->mutex);
3511
a57c688d
LC
3512 if (vif->type == NL80211_IFTYPE_STATION) {
3513 struct iwl_mvm_sta *mvmsta;
3514
3515 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
3516 mvmvif->ap_sta_id);
3517
3518 if (WARN_ON(!mvmsta)) {
3519 ret = -EIO;
3520 goto out_unlock;
3521 }
3522
3523 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
3524
3525 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
c6e0a3e0
LC
3526
3527 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3528 if (ret)
3529 goto out_unlock;
686e7fe1
LC
3530
3531 iwl_mvm_stop_session_protection(mvm, vif);
a57c688d
LC
3532 }
3533
f6c34820
LC
3534 mvmvif->ps_disabled = false;
3535
3536 ret = iwl_mvm_power_update_ps(mvm);
3537
a57c688d 3538out_unlock:
f6c34820
LC
3539 mutex_unlock(&mvm->mutex);
3540
3541 return ret;
3542}
3543
c5b0e7c0
EG
3544static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
3545 struct ieee80211_vif *vif, u32 queues, bool drop)
3546{
3547 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3548 struct iwl_mvm_vif *mvmvif;
3549 struct iwl_mvm_sta *mvmsta;
a0f6bf2a
AN
3550 struct ieee80211_sta *sta;
3551 int i;
3552 u32 msk = 0;
c5b0e7c0
EG
3553
3554 if (!vif || vif->type != NL80211_IFTYPE_STATION)
3555 return;
3556
3557 mutex_lock(&mvm->mutex);
3558 mvmvif = iwl_mvm_vif_from_mac80211(vif);
c5b0e7c0 3559
a0f6bf2a
AN
3560 /* flush the AP-station and all TDLS peers */
3561 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3562 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3563 lockdep_is_held(&mvm->mutex));
3564 if (IS_ERR_OR_NULL(sta))
3565 continue;
3566
3567 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3568 if (mvmsta->vif != vif)
3569 continue;
3570
3571 /* make sure only TDLS peers or the AP are flushed */
3572 WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
3573
3574 msk |= mvmsta->tfd_queue_msk;
480acbce 3575 }
c5b0e7c0 3576
6d440b25
EG
3577 if (drop) {
3578 if (iwl_mvm_flush_tx_path(mvm, msk, true))
3579 IWL_ERR(mvm, "flush request fail\n");
3580 mutex_unlock(&mvm->mutex);
3581 } else {
3582 mutex_unlock(&mvm->mutex);
4e6c48e0 3583
6d440b25
EG
3584 /* this can take a while, and we may need/want other operations
3585 * to succeed while doing this, so do it without the mutex held
3586 */
3587 iwl_trans_wait_tx_queue_empty(mvm->trans, msk);
3588 }
c5b0e7c0
EG
3589}
3590
91a8bcde
JB
3591static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
3592 struct survey_info *survey)
3593{
3594 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3595 int ret;
3596
3597 memset(survey, 0, sizeof(*survey));
3598
3599 /* only support global statistics right now */
3600 if (idx != 0)
3601 return -ENOENT;
3602
3603 if (!(mvm->fw->ucode_capa.capa[0] &
3604 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
3605 return -ENOENT;
3606
3607 mutex_lock(&mvm->mutex);
3608
3609 if (mvm->ucode_loaded) {
33cef925 3610 ret = iwl_mvm_request_statistics(mvm, false);
91a8bcde
JB
3611 if (ret)
3612 goto out;
3613 }
3614
3615 survey->filled = SURVEY_INFO_TIME |
3616 SURVEY_INFO_TIME_RX |
3617 SURVEY_INFO_TIME_TX |
3618 SURVEY_INFO_TIME_SCAN;
3619 survey->time = mvm->accu_radio_stats.on_time_rf +
3620 mvm->radio_stats.on_time_rf;
3621 do_div(survey->time, USEC_PER_MSEC);
3622
3623 survey->time_rx = mvm->accu_radio_stats.rx_time +
3624 mvm->radio_stats.rx_time;
3625 do_div(survey->time_rx, USEC_PER_MSEC);
3626
3627 survey->time_tx = mvm->accu_radio_stats.tx_time +
3628 mvm->radio_stats.tx_time;
3629 do_div(survey->time_tx, USEC_PER_MSEC);
3630
3631 survey->time_scan = mvm->accu_radio_stats.on_time_scan +
3632 mvm->radio_stats.on_time_scan;
3633 do_div(survey->time_scan, USEC_PER_MSEC);
3634
3635 out:
3636 mutex_unlock(&mvm->mutex);
3637 return ret;
3638}
3639
33cef925
JB
3640static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
3641 struct ieee80211_vif *vif,
3642 struct ieee80211_sta *sta,
3643 struct station_info *sinfo)
3644{
3645 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3646 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3647 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3648
3649 if (!(mvm->fw->ucode_capa.capa[0] &
3650 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
3651 return;
3652
3653 /* if beacon filtering isn't on mac80211 does it anyway */
3654 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
3655 return;
3656
3657 if (!vif->bss_conf.assoc)
3658 return;
3659
3660 mutex_lock(&mvm->mutex);
3661
3662 if (mvmvif->ap_sta_id != mvmsta->sta_id)
3663 goto unlock;
3664
3665 if (iwl_mvm_request_statistics(mvm, false))
3666 goto unlock;
3667
3668 sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
3669 mvmvif->beacon_stats.accu_num_beacons;
3670 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX);
3671 if (mvmvif->beacon_stats.avg_signal) {
3672 /* firmware only reports a value after RXing a few beacons */
3673 sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
3674 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
3675 }
3676 unlock:
3677 mutex_unlock(&mvm->mutex);
3678}
3679
e5209263 3680const struct ieee80211_ops iwl_mvm_hw_ops = {
8ca151b5
JB
3681 .tx = iwl_mvm_mac_tx,
3682 .ampdu_action = iwl_mvm_mac_ampdu_action,
3683 .start = iwl_mvm_mac_start,
cf2c92d8 3684 .reconfig_complete = iwl_mvm_mac_reconfig_complete,
8ca151b5
JB
3685 .stop = iwl_mvm_mac_stop,
3686 .add_interface = iwl_mvm_mac_add_interface,
3687 .remove_interface = iwl_mvm_mac_remove_interface,
3688 .config = iwl_mvm_mac_config,
e59647ea 3689 .prepare_multicast = iwl_mvm_prepare_multicast,
8ca151b5
JB
3690 .configure_filter = iwl_mvm_configure_filter,
3691 .bss_info_changed = iwl_mvm_bss_info_changed,
3692 .hw_scan = iwl_mvm_mac_hw_scan,
3693 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1ddbbb0c 3694 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
8ca151b5
JB
3695 .sta_state = iwl_mvm_mac_sta_state,
3696 .sta_notify = iwl_mvm_mac_sta_notify,
3697 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3e56eadf 3698 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
8ca151b5 3699 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1f3b0ff8 3700 .sta_rc_update = iwl_mvm_sta_rc_update,
8ca151b5
JB
3701 .conf_tx = iwl_mvm_mac_conf_tx,
3702 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
07ecd897 3703 .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
c5b0e7c0 3704 .flush = iwl_mvm_mac_flush,
35a000b7
DS
3705 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
3706 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
8ca151b5
JB
3707 .set_key = iwl_mvm_mac_set_key,
3708 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
3709 .remain_on_channel = iwl_mvm_roc,
3710 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
8ca151b5
JB
3711 .add_chanctx = iwl_mvm_add_chanctx,
3712 .remove_chanctx = iwl_mvm_remove_chanctx,
3713 .change_chanctx = iwl_mvm_change_chanctx,
3714 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
3715 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
b08c1d97 3716 .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
8ca151b5 3717
5023d966
JB
3718 .start_ap = iwl_mvm_start_ap_ibss,
3719 .stop_ap = iwl_mvm_stop_ap_ibss,
3720 .join_ibss = iwl_mvm_start_ap_ibss,
3721 .leave_ibss = iwl_mvm_stop_ap_ibss,
8ca151b5
JB
3722
3723 .set_tim = iwl_mvm_set_tim,
3724
622e3f9b 3725 .channel_switch = iwl_mvm_channel_switch,
f028905c 3726 .pre_channel_switch = iwl_mvm_pre_channel_switch,
f6c34820 3727 .post_channel_switch = iwl_mvm_post_channel_switch,
bd3398e2 3728
1d3c3f63
AN
3729 .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
3730 .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
3731 .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
3732
507cadf2
DS
3733 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
3734
8ca151b5
JB
3735#ifdef CONFIG_PM_SLEEP
3736 /* look at d3.c */
3737 .suspend = iwl_mvm_suspend,
3738 .resume = iwl_mvm_resume,
3739 .set_wakeup = iwl_mvm_set_wakeup,
3740 .set_rekey_data = iwl_mvm_set_rekey_data,
3741#if IS_ENABLED(CONFIG_IPV6)
3742 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
3743#endif
3744 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
3745#endif
91a8bcde 3746 .get_survey = iwl_mvm_mac_get_survey,
33cef925 3747 .sta_statistics = iwl_mvm_mac_sta_statistics,
8ca151b5 3748};
This page took 2.209443 seconds and 5 git commands to generate.