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