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