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