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