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