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