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