iwlwifi: mvm: allow RSSI compensation
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / mvm / mac80211.c
... / ...
CommitLineData
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 *
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65#include <linux/kernel.h>
66#include <linux/slab.h>
67#include <linux/skbuff.h>
68#include <linux/netdevice.h>
69#include <linux/etherdevice.h>
70#include <linux/ip.h>
71#include <linux/if_arp.h>
72#include <linux/devcoredump.h>
73#include <net/mac80211.h>
74#include <net/ieee80211_radiotap.h>
75#include <net/tcp.h>
76
77#include "iwl-op-mode.h"
78#include "iwl-io.h"
79#include "mvm.h"
80#include "sta.h"
81#include "time-event.h"
82#include "iwl-eeprom-parse.h"
83#include "fw-api-scan.h"
84#include "iwl-phy-db.h"
85#include "testmode.h"
86#include "iwl-fw-error-dump.h"
87#include "iwl-prph.h"
88
89static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
90 {
91 .max = 1,
92 .types = BIT(NL80211_IFTYPE_STATION),
93 },
94 {
95 .max = 1,
96 .types = BIT(NL80211_IFTYPE_AP) |
97 BIT(NL80211_IFTYPE_P2P_CLIENT) |
98 BIT(NL80211_IFTYPE_P2P_GO),
99 },
100 {
101 .max = 1,
102 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
103 },
104};
105
106static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
107 {
108 .num_different_channels = 1,
109 .max_interfaces = 3,
110 .limits = iwl_mvm_limits,
111 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
112 },
113};
114
115#ifdef CONFIG_PM_SLEEP
116static const struct nl80211_wowlan_tcp_data_token_feature
117iwl_mvm_wowlan_tcp_token_feature = {
118 .min_len = 0,
119 .max_len = 255,
120 .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
121};
122
123static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
124 .tok = &iwl_mvm_wowlan_tcp_token_feature,
125 .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
126 sizeof(struct ethhdr) -
127 sizeof(struct iphdr) -
128 sizeof(struct tcphdr),
129 .data_interval_max = 65535, /* __le16 in API */
130 .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
131 sizeof(struct ethhdr) -
132 sizeof(struct iphdr) -
133 sizeof(struct tcphdr),
134 .seq = true,
135};
136#endif
137
138#ifdef CONFIG_IWLWIFI_BCAST_FILTERING
139/*
140 * Use the reserved field to indicate magic values.
141 * these values will only be used internally by the driver,
142 * and won't make it to the fw (reserved will be 0).
143 * BC_FILTER_MAGIC_IP - configure the val of this attribute to
144 * be the vif's ip address. in case there is not a single
145 * ip address (0, or more than 1), this attribute will
146 * be skipped.
147 * BC_FILTER_MAGIC_MAC - set the val of this attribute to
148 * the LSB bytes of the vif's mac address
149 */
150enum {
151 BC_FILTER_MAGIC_NONE = 0,
152 BC_FILTER_MAGIC_IP,
153 BC_FILTER_MAGIC_MAC,
154};
155
156static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
157 {
158 /* arp */
159 .discard = 0,
160 .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
161 .attrs = {
162 {
163 /* frame type - arp, hw type - ethernet */
164 .offset_type =
165 BCAST_FILTER_OFFSET_PAYLOAD_START,
166 .offset = sizeof(rfc1042_header),
167 .val = cpu_to_be32(0x08060001),
168 .mask = cpu_to_be32(0xffffffff),
169 },
170 {
171 /* arp dest ip */
172 .offset_type =
173 BCAST_FILTER_OFFSET_PAYLOAD_START,
174 .offset = sizeof(rfc1042_header) + 2 +
175 sizeof(struct arphdr) +
176 ETH_ALEN + sizeof(__be32) +
177 ETH_ALEN,
178 .mask = cpu_to_be32(0xffffffff),
179 /* mark it as special field */
180 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
181 },
182 },
183 },
184 {
185 /* dhcp offer bcast */
186 .discard = 0,
187 .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
188 .attrs = {
189 {
190 /* udp dest port - 68 (bootp client)*/
191 .offset_type = BCAST_FILTER_OFFSET_IP_END,
192 .offset = offsetof(struct udphdr, dest),
193 .val = cpu_to_be32(0x00440000),
194 .mask = cpu_to_be32(0xffff0000),
195 },
196 {
197 /* dhcp - lsb bytes of client hw address */
198 .offset_type = BCAST_FILTER_OFFSET_IP_END,
199 .offset = 38,
200 .mask = cpu_to_be32(0xffffffff),
201 /* mark it as special field */
202 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
203 },
204 },
205 },
206 /* last filter must be empty */
207 {},
208};
209#endif
210
211void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
212{
213 if (!iwl_mvm_is_d0i3_supported(mvm))
214 return;
215
216 IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
217 spin_lock_bh(&mvm->refs_lock);
218 mvm->refs[ref_type]++;
219 spin_unlock_bh(&mvm->refs_lock);
220 iwl_trans_ref(mvm->trans);
221}
222
223void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
224{
225 if (!iwl_mvm_is_d0i3_supported(mvm))
226 return;
227
228 IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
229 spin_lock_bh(&mvm->refs_lock);
230 WARN_ON(!mvm->refs[ref_type]--);
231 spin_unlock_bh(&mvm->refs_lock);
232 iwl_trans_unref(mvm->trans);
233}
234
235static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
236 enum iwl_mvm_ref_type except_ref)
237{
238 int i, j;
239
240 if (!iwl_mvm_is_d0i3_supported(mvm))
241 return;
242
243 spin_lock_bh(&mvm->refs_lock);
244 for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
245 if (except_ref == i || !mvm->refs[i])
246 continue;
247
248 IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
249 i, mvm->refs[i]);
250 for (j = 0; j < mvm->refs[i]; j++)
251 iwl_trans_unref(mvm->trans);
252 mvm->refs[i] = 0;
253 }
254 spin_unlock_bh(&mvm->refs_lock);
255}
256
257bool iwl_mvm_ref_taken(struct iwl_mvm *mvm)
258{
259 int i;
260 bool taken = false;
261
262 if (!iwl_mvm_is_d0i3_supported(mvm))
263 return true;
264
265 spin_lock_bh(&mvm->refs_lock);
266 for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
267 if (mvm->refs[i]) {
268 taken = true;
269 break;
270 }
271 }
272 spin_unlock_bh(&mvm->refs_lock);
273
274 return taken;
275}
276
277int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
278{
279 iwl_mvm_ref(mvm, ref_type);
280
281 if (!wait_event_timeout(mvm->d0i3_exit_waitq,
282 !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
283 HZ)) {
284 WARN_ON_ONCE(1);
285 iwl_mvm_unref(mvm, ref_type);
286 return -EIO;
287 }
288
289 return 0;
290}
291
292static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
293{
294 int i;
295
296 memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
297 for (i = 0; i < NUM_PHY_CTX; i++) {
298 mvm->phy_ctxts[i].id = i;
299 mvm->phy_ctxts[i].ref = 0;
300 }
301}
302
303int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
304{
305 struct ieee80211_hw *hw = mvm->hw;
306 int num_mac, ret, i;
307
308 /* Tell mac80211 our characteristics */
309 hw->flags = IEEE80211_HW_SIGNAL_DBM |
310 IEEE80211_HW_SPECTRUM_MGMT |
311 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
312 IEEE80211_HW_QUEUE_CONTROL |
313 IEEE80211_HW_WANT_MONITOR_VIF |
314 IEEE80211_HW_SUPPORTS_PS |
315 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
316 IEEE80211_HW_AMPDU_AGGREGATION |
317 IEEE80211_HW_TIMING_BEACON_ONLY |
318 IEEE80211_HW_CONNECTION_MONITOR |
319 IEEE80211_HW_CHANCTX_STA_CSA |
320 IEEE80211_HW_SUPPORTS_CLONED_SKBS;
321
322 hw->queues = mvm->first_agg_queue;
323 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
324 hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
325 IEEE80211_RADIOTAP_MCS_HAVE_STBC;
326 hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
327 IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
328 hw->rate_control_algorithm = "iwl-mvm-rs";
329
330 /*
331 * Enable 11w if advertised by firmware and software crypto
332 * is not enabled (as the firmware will interpret some mgmt
333 * packets, so enabling it with software crypto isn't safe)
334 */
335 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
336 !iwlwifi_mod_params.sw_crypto)
337 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
338
339 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT &&
340 !iwlwifi_mod_params.uapsd_disable) {
341 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
342 hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
343 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
344 }
345
346 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
347 mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
348 hw->flags |= IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS;
349 hw->wiphy->features |=
350 NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
351 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
352 }
353
354 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
355 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
356 hw->chanctx_data_size = sizeof(u16);
357
358 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
359 BIT(NL80211_IFTYPE_P2P_CLIENT) |
360 BIT(NL80211_IFTYPE_AP) |
361 BIT(NL80211_IFTYPE_P2P_GO) |
362 BIT(NL80211_IFTYPE_P2P_DEVICE) |
363 BIT(NL80211_IFTYPE_ADHOC);
364
365 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
366 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
367 REGULATORY_DISABLE_BEACON_HINTS;
368
369 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
370 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
371
372 hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
373
374 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
375 hw->wiphy->n_iface_combinations =
376 ARRAY_SIZE(iwl_mvm_iface_combinations);
377
378 hw->wiphy->max_remain_on_channel_duration = 10000;
379 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
380 /* we can compensate an offset of up to 3 channels = 15 MHz */
381 hw->wiphy->max_adj_channel_rssi_comp = 3 * 5;
382
383 /* Extract MAC address */
384 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
385 hw->wiphy->addresses = mvm->addresses;
386 hw->wiphy->n_addresses = 1;
387
388 /* Extract additional MAC addresses if available */
389 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
390 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
391
392 for (i = 1; i < num_mac; i++) {
393 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
394 ETH_ALEN);
395 mvm->addresses[i].addr[5]++;
396 hw->wiphy->n_addresses++;
397 }
398
399 iwl_mvm_reset_phy_ctxts(mvm);
400
401 hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm, false);
402
403 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
404
405 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
406 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
407 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
408 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
409 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
410 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
411
412 hw->wiphy->hw_version = mvm->trans->hw_id;
413
414 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
415 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
416 else
417 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
418
419 if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 10) {
420 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
421 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
422 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
423 /* we create the 802.11 header and zero length SSID IE. */
424 hw->wiphy->max_sched_scan_ie_len =
425 SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
426 }
427
428 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
429 NL80211_FEATURE_LOW_PRIORITY_SCAN |
430 NL80211_FEATURE_P2P_GO_OPPPS |
431 NL80211_FEATURE_DYNAMIC_SMPS |
432 NL80211_FEATURE_STATIC_SMPS |
433 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
434
435 if (mvm->fw->ucode_capa.capa[0] &
436 IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT)
437 hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
438 if (mvm->fw->ucode_capa.capa[0] &
439 IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT)
440 hw->wiphy->features |= NL80211_FEATURE_QUIET;
441
442 if (mvm->fw->ucode_capa.capa[0] &
443 IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT)
444 hw->wiphy->features |=
445 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
446
447 if (mvm->fw->ucode_capa.capa[0] &
448 IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT)
449 hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
450
451 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
452
453 /* currently FW API supports only one optional cipher scheme */
454 if (mvm->fw->cs[0].cipher) {
455 mvm->hw->n_cipher_schemes = 1;
456 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
457 }
458
459#ifdef CONFIG_PM_SLEEP
460 if (iwl_mvm_is_d0i3_supported(mvm) &&
461 device_can_wakeup(mvm->trans->dev)) {
462 mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
463 hw->wiphy->wowlan = &mvm->wowlan;
464 } else if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
465 mvm->trans->ops->d3_suspend &&
466 mvm->trans->ops->d3_resume &&
467 device_can_wakeup(mvm->trans->dev)) {
468 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
469 WIPHY_WOWLAN_DISCONNECT |
470 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
471 WIPHY_WOWLAN_RFKILL_RELEASE |
472 WIPHY_WOWLAN_NET_DETECT;
473 if (!iwlwifi_mod_params.sw_crypto)
474 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
475 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
476 WIPHY_WOWLAN_4WAY_HANDSHAKE;
477
478 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
479 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
480 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
481 mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
482 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
483 hw->wiphy->wowlan = &mvm->wowlan;
484 }
485#endif
486
487#ifdef CONFIG_IWLWIFI_BCAST_FILTERING
488 /* assign default bcast filtering configuration */
489 mvm->bcast_filters = iwl_mvm_default_bcast_filters;
490#endif
491
492 ret = iwl_mvm_leds_init(mvm);
493 if (ret)
494 return ret;
495
496 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_TDLS_SUPPORT) {
497 IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
498 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
499 }
500
501 if (mvm->fw->ucode_capa.capa[0] &
502 IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH) {
503 IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
504 hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
505 }
506
507 ret = ieee80211_register_hw(mvm->hw);
508 if (ret)
509 iwl_mvm_leds_exit(mvm);
510
511 return ret;
512}
513
514static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
515 struct ieee80211_sta *sta,
516 struct sk_buff *skb)
517{
518 struct iwl_mvm_sta *mvmsta;
519 bool defer = false;
520
521 /*
522 * double check the IN_D0I3 flag both before and after
523 * taking the spinlock, in order to prevent taking
524 * the spinlock when not needed.
525 */
526 if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
527 return false;
528
529 spin_lock(&mvm->d0i3_tx_lock);
530 /*
531 * testing the flag again ensures the skb dequeue
532 * loop (on d0i3 exit) hasn't run yet.
533 */
534 if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
535 goto out;
536
537 mvmsta = iwl_mvm_sta_from_mac80211(sta);
538 if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
539 mvmsta->sta_id != mvm->d0i3_ap_sta_id)
540 goto out;
541
542 __skb_queue_tail(&mvm->d0i3_tx, skb);
543 ieee80211_stop_queues(mvm->hw);
544
545 /* trigger wakeup */
546 iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
547 iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
548
549 defer = true;
550out:
551 spin_unlock(&mvm->d0i3_tx_lock);
552 return defer;
553}
554
555static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
556 struct ieee80211_tx_control *control,
557 struct sk_buff *skb)
558{
559 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
560 struct ieee80211_sta *sta = control->sta;
561 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
562 struct ieee80211_hdr *hdr = (void *)skb->data;
563
564 if (iwl_mvm_is_radio_killed(mvm)) {
565 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
566 goto drop;
567 }
568
569 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
570 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
571 !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
572 goto drop;
573
574 /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
575 if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
576 ieee80211_is_mgmt(hdr->frame_control) &&
577 !ieee80211_is_deauth(hdr->frame_control) &&
578 !ieee80211_is_disassoc(hdr->frame_control) &&
579 !ieee80211_is_action(hdr->frame_control)))
580 sta = NULL;
581
582 if (sta) {
583 if (iwl_mvm_defer_tx(mvm, sta, skb))
584 return;
585 if (iwl_mvm_tx_skb(mvm, skb, sta))
586 goto drop;
587 return;
588 }
589
590 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
591 goto drop;
592 return;
593 drop:
594 ieee80211_free_txskb(hw, skb);
595}
596
597static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
598{
599 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
600 return false;
601 return true;
602}
603
604static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
605{
606 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
607 return false;
608 if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
609 return true;
610
611 /* enabled by default */
612 return true;
613}
614
615static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
616 struct ieee80211_vif *vif,
617 enum ieee80211_ampdu_mlme_action action,
618 struct ieee80211_sta *sta, u16 tid,
619 u16 *ssn, u8 buf_size)
620{
621 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
622 int ret;
623 bool tx_agg_ref = false;
624
625 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
626 sta->addr, tid, action);
627
628 if (!(mvm->nvm_data->sku_cap_11n_enable))
629 return -EACCES;
630
631 /* return from D0i3 before starting a new Tx aggregation */
632 switch (action) {
633 case IEEE80211_AMPDU_TX_START:
634 case IEEE80211_AMPDU_TX_STOP_CONT:
635 case IEEE80211_AMPDU_TX_STOP_FLUSH:
636 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
637 case IEEE80211_AMPDU_TX_OPERATIONAL:
638 /*
639 * for tx start, wait synchronously until D0i3 exit to
640 * get the correct sequence number for the tid.
641 * additionally, some other ampdu actions use direct
642 * target access, which is not handled automatically
643 * by the trans layer (unlike commands), so wait for
644 * d0i3 exit in these cases as well.
645 */
646 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
647 if (ret)
648 return ret;
649
650 tx_agg_ref = true;
651 break;
652 default:
653 break;
654 }
655
656 mutex_lock(&mvm->mutex);
657
658 switch (action) {
659 case IEEE80211_AMPDU_RX_START:
660 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
661 ret = -EINVAL;
662 break;
663 }
664 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
665 break;
666 case IEEE80211_AMPDU_RX_STOP:
667 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
668 break;
669 case IEEE80211_AMPDU_TX_START:
670 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
671 ret = -EINVAL;
672 break;
673 }
674 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
675 break;
676 case IEEE80211_AMPDU_TX_STOP_CONT:
677 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
678 break;
679 case IEEE80211_AMPDU_TX_STOP_FLUSH:
680 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
681 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
682 break;
683 case IEEE80211_AMPDU_TX_OPERATIONAL:
684 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
685 break;
686 default:
687 WARN_ON_ONCE(1);
688 ret = -EINVAL;
689 break;
690 }
691 mutex_unlock(&mvm->mutex);
692
693 /*
694 * If the tid is marked as started, we won't use it for offloaded
695 * traffic on the next D0i3 entry. It's safe to unref.
696 */
697 if (tx_agg_ref)
698 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
699
700 return ret;
701}
702
703static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
704 struct ieee80211_vif *vif)
705{
706 struct iwl_mvm *mvm = data;
707 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
708
709 mvmvif->uploaded = false;
710 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
711
712 /* does this make sense at all? */
713 mvmvif->color++;
714
715 spin_lock_bh(&mvm->time_event_lock);
716 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
717 spin_unlock_bh(&mvm->time_event_lock);
718
719 mvmvif->phy_ctxt = NULL;
720 memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
721}
722
723static ssize_t iwl_mvm_read_coredump(char *buffer, loff_t offset, size_t count,
724 const void *data, size_t datalen)
725{
726 const struct iwl_mvm_dump_ptrs *dump_ptrs = data;
727 ssize_t bytes_read;
728 ssize_t bytes_read_trans;
729
730 if (offset < dump_ptrs->op_mode_len) {
731 bytes_read = min_t(ssize_t, count,
732 dump_ptrs->op_mode_len - offset);
733 memcpy(buffer, (u8 *)dump_ptrs->op_mode_ptr + offset,
734 bytes_read);
735 offset += bytes_read;
736 count -= bytes_read;
737
738 if (count == 0)
739 return bytes_read;
740 } else {
741 bytes_read = 0;
742 }
743
744 if (!dump_ptrs->trans_ptr)
745 return bytes_read;
746
747 offset -= dump_ptrs->op_mode_len;
748 bytes_read_trans = min_t(ssize_t, count,
749 dump_ptrs->trans_ptr->len - offset);
750 memcpy(buffer + bytes_read,
751 (u8 *)dump_ptrs->trans_ptr->data + offset,
752 bytes_read_trans);
753
754 return bytes_read + bytes_read_trans;
755}
756
757static void iwl_mvm_free_coredump(const void *data)
758{
759 const struct iwl_mvm_dump_ptrs *fw_error_dump = data;
760
761 vfree(fw_error_dump->op_mode_ptr);
762 vfree(fw_error_dump->trans_ptr);
763 kfree(fw_error_dump);
764}
765
766void iwl_mvm_fw_error_dump(struct iwl_mvm *mvm)
767{
768 struct iwl_fw_error_dump_file *dump_file;
769 struct iwl_fw_error_dump_data *dump_data;
770 struct iwl_fw_error_dump_info *dump_info;
771 struct iwl_mvm_dump_ptrs *fw_error_dump;
772 u32 sram_len, sram_ofs;
773 u32 file_len, rxf_len;
774 unsigned long flags;
775 int reg_val;
776
777 lockdep_assert_held(&mvm->mutex);
778
779 fw_error_dump = kzalloc(sizeof(*fw_error_dump), GFP_KERNEL);
780 if (!fw_error_dump)
781 return;
782
783 /* SRAM - include stack CCM if driver knows the values for it */
784 if (!mvm->cfg->dccm_offset || !mvm->cfg->dccm_len) {
785 const struct fw_img *img;
786
787 img = &mvm->fw->img[mvm->cur_ucode];
788 sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
789 sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
790 } else {
791 sram_ofs = mvm->cfg->dccm_offset;
792 sram_len = mvm->cfg->dccm_len;
793 }
794
795 /* reading buffer size */
796 reg_val = iwl_trans_read_prph(mvm->trans, RXF_SIZE_ADDR);
797 rxf_len = (reg_val & RXF_SIZE_BYTE_CNT_MSK) >> RXF_SIZE_BYTE_CND_POS;
798
799 /* the register holds the value divided by 128 */
800 rxf_len = rxf_len << 7;
801
802 file_len = sizeof(*dump_file) +
803 sizeof(*dump_data) * 3 +
804 sram_len +
805 rxf_len +
806 sizeof(*dump_info);
807
808 dump_file = vzalloc(file_len);
809 if (!dump_file) {
810 kfree(fw_error_dump);
811 return;
812 }
813
814 fw_error_dump->op_mode_ptr = dump_file;
815
816 dump_file->barker = cpu_to_le32(IWL_FW_ERROR_DUMP_BARKER);
817 dump_data = (void *)dump_file->data;
818
819 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_DEV_FW_INFO);
820 dump_data->len = cpu_to_le32(sizeof(*dump_info));
821 dump_info = (void *) dump_data->data;
822 dump_info->device_family =
823 mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000 ?
824 cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_7) :
825 cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_8);
826 memcpy(dump_info->fw_human_readable, mvm->fw->human_readable,
827 sizeof(dump_info->fw_human_readable));
828 strncpy(dump_info->dev_human_readable, mvm->cfg->name,
829 sizeof(dump_info->dev_human_readable));
830 strncpy(dump_info->bus_human_readable, mvm->dev->bus->name,
831 sizeof(dump_info->bus_human_readable));
832
833 dump_data = iwl_fw_error_next_data(dump_data);
834 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RXF);
835 dump_data->len = cpu_to_le32(rxf_len);
836
837 if (iwl_trans_grab_nic_access(mvm->trans, false, &flags)) {
838 u32 *rxf = (void *)dump_data->data;
839 int i;
840
841 for (i = 0; i < (rxf_len / sizeof(u32)); i++) {
842 iwl_trans_write_prph(mvm->trans,
843 RXF_LD_FENCE_OFFSET_ADDR,
844 i * sizeof(u32));
845 rxf[i] = iwl_trans_read_prph(mvm->trans,
846 RXF_FIFO_RD_FENCE_ADDR);
847 }
848 iwl_trans_release_nic_access(mvm->trans, &flags);
849 }
850
851 dump_data = iwl_fw_error_next_data(dump_data);
852 dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_SRAM);
853 dump_data->len = cpu_to_le32(sram_len);
854 iwl_trans_read_mem_bytes(mvm->trans, sram_ofs, dump_data->data,
855 sram_len);
856
857 fw_error_dump->trans_ptr = iwl_trans_dump_data(mvm->trans);
858 fw_error_dump->op_mode_len = file_len;
859 if (fw_error_dump->trans_ptr)
860 file_len += fw_error_dump->trans_ptr->len;
861 dump_file->file_len = cpu_to_le32(file_len);
862
863 dev_coredumpm(mvm->trans->dev, THIS_MODULE, fw_error_dump, 0,
864 GFP_KERNEL, iwl_mvm_read_coredump, iwl_mvm_free_coredump);
865}
866
867static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
868{
869 /* clear the D3 reconfig, we only need it to avoid dumping a
870 * firmware coredump on reconfiguration, we shouldn't do that
871 * on D3->D0 transition
872 */
873 if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status))
874 iwl_mvm_fw_error_dump(mvm);
875
876 iwl_trans_stop_device(mvm->trans);
877
878 mvm->scan_status = IWL_MVM_SCAN_NONE;
879 mvm->ps_disabled = false;
880 mvm->calibrating = false;
881
882 /* just in case one was running */
883 ieee80211_remain_on_channel_expired(mvm->hw);
884
885 ieee80211_iterate_active_interfaces_atomic(
886 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
887 iwl_mvm_cleanup_iterator, mvm);
888
889 mvm->p2p_device_vif = NULL;
890 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
891
892 iwl_mvm_reset_phy_ctxts(mvm);
893 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
894 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
895 memset(mvm->tfd_drained, 0, sizeof(mvm->tfd_drained));
896 memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
897 memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
898 memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
899 memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
900 memset(&mvm->bt_ack_kill_msk, 0, sizeof(mvm->bt_ack_kill_msk));
901 memset(&mvm->bt_cts_kill_msk, 0, sizeof(mvm->bt_cts_kill_msk));
902
903 ieee80211_wake_queues(mvm->hw);
904
905 /* cleanup all stale references (scan, roc), but keep the
906 * ucode_down ref until reconfig is complete */
907 iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
908
909 /* clear any stale d0i3 state */
910 clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
911
912 mvm->vif_count = 0;
913 mvm->rx_ba_sessions = 0;
914}
915
916int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
917{
918 int ret;
919
920 lockdep_assert_held(&mvm->mutex);
921
922 /* Clean up some internal and mac80211 state on restart */
923 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
924 iwl_mvm_restart_cleanup(mvm);
925
926 ret = iwl_mvm_up(mvm);
927
928 if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
929 /* Something went wrong - we need to finish some cleanup
930 * that normally iwl_mvm_mac_restart_complete() below
931 * would do.
932 */
933 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
934 iwl_mvm_d0i3_enable_tx(mvm, NULL);
935 }
936
937 return ret;
938}
939
940static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
941{
942 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
943 int ret;
944
945 mutex_lock(&mvm->mutex);
946 ret = __iwl_mvm_mac_start(mvm);
947 mutex_unlock(&mvm->mutex);
948
949 return ret;
950}
951
952static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
953{
954 int ret;
955
956 mutex_lock(&mvm->mutex);
957
958 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
959 iwl_mvm_d0i3_enable_tx(mvm, NULL);
960 ret = iwl_mvm_update_quotas(mvm, NULL);
961 if (ret)
962 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
963 ret);
964
965 /* allow transport/FW low power modes */
966 iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
967
968 /*
969 * If we have TDLS peers, remove them. We don't know the last seqno/PN
970 * of packets the FW sent out, so we must reconnect.
971 */
972 iwl_mvm_teardown_tdls_peers(mvm);
973
974 mutex_unlock(&mvm->mutex);
975}
976
977static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
978{
979 bool exit_now;
980
981 if (!iwl_mvm_is_d0i3_supported(mvm))
982 return;
983
984 mutex_lock(&mvm->d0i3_suspend_mutex);
985 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
986 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
987 &mvm->d0i3_suspend_flags);
988 mutex_unlock(&mvm->d0i3_suspend_mutex);
989
990 if (exit_now) {
991 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
992 _iwl_mvm_exit_d0i3(mvm);
993 }
994}
995
996static void
997iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
998 enum ieee80211_reconfig_type reconfig_type)
999{
1000 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1001
1002 switch (reconfig_type) {
1003 case IEEE80211_RECONFIG_TYPE_RESTART:
1004 iwl_mvm_restart_complete(mvm);
1005 break;
1006 case IEEE80211_RECONFIG_TYPE_SUSPEND:
1007 iwl_mvm_resume_complete(mvm);
1008 break;
1009 }
1010}
1011
1012void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1013{
1014 lockdep_assert_held(&mvm->mutex);
1015
1016 /* disallow low power states when the FW is down */
1017 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1018
1019 /* async_handlers_wk is now blocked */
1020
1021 /*
1022 * The work item could be running or queued if the
1023 * ROC time event stops just as we get here.
1024 */
1025 cancel_work_sync(&mvm->roc_done_wk);
1026
1027 iwl_trans_stop_device(mvm->trans);
1028
1029 iwl_mvm_async_handlers_purge(mvm);
1030 /* async_handlers_list is empty and will stay empty: HW is stopped */
1031
1032 /* the fw is stopped, the aux sta is dead: clean up driver state */
1033 iwl_mvm_del_aux_sta(mvm);
1034
1035 mvm->ucode_loaded = false;
1036}
1037
1038static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1039{
1040 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1041
1042 flush_work(&mvm->d0i3_exit_work);
1043 flush_work(&mvm->async_handlers_wk);
1044 flush_work(&mvm->fw_error_dump_wk);
1045
1046 mutex_lock(&mvm->mutex);
1047 __iwl_mvm_mac_stop(mvm);
1048 mutex_unlock(&mvm->mutex);
1049
1050 /*
1051 * The worker might have been waiting for the mutex, let it run and
1052 * discover that its list is now empty.
1053 */
1054 cancel_work_sync(&mvm->async_handlers_wk);
1055}
1056
1057static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1058{
1059 u16 i;
1060
1061 lockdep_assert_held(&mvm->mutex);
1062
1063 for (i = 0; i < NUM_PHY_CTX; i++)
1064 if (!mvm->phy_ctxts[i].ref)
1065 return &mvm->phy_ctxts[i];
1066
1067 IWL_ERR(mvm, "No available PHY context\n");
1068 return NULL;
1069}
1070
1071static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1072 s8 tx_power)
1073{
1074 /* FW is in charge of regulatory enforcement */
1075 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
1076 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
1077 .pwr_restriction = cpu_to_le16(tx_power),
1078 };
1079
1080 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0,
1081 sizeof(reduce_txpwr_cmd),
1082 &reduce_txpwr_cmd);
1083}
1084
1085static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1086 struct ieee80211_vif *vif)
1087{
1088 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1089 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1090 int ret;
1091
1092 /*
1093 * make sure D0i3 exit is completed, otherwise a target access
1094 * during tx queue configuration could be done when still in
1095 * D0i3 state.
1096 */
1097 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1098 if (ret)
1099 return ret;
1100
1101 /*
1102 * Not much to do here. The stack will not allow interface
1103 * types or combinations that we didn't advertise, so we
1104 * don't really have to check the types.
1105 */
1106
1107 mutex_lock(&mvm->mutex);
1108
1109 /* Allocate resources for the MAC context, and add it to the fw */
1110 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1111 if (ret)
1112 goto out_unlock;
1113
1114 /* Counting number of interfaces is needed for legacy PM */
1115 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1116 mvm->vif_count++;
1117
1118 /*
1119 * The AP binding flow can be done only after the beacon
1120 * template is configured (which happens only in the mac80211
1121 * start_ap() flow), and adding the broadcast station can happen
1122 * only after the binding.
1123 * In addition, since modifying the MAC before adding a bcast
1124 * station is not allowed by the FW, delay the adding of MAC context to
1125 * the point where we can also add the bcast station.
1126 * In short: there's not much we can do at this point, other than
1127 * allocating resources :)
1128 */
1129 if (vif->type == NL80211_IFTYPE_AP ||
1130 vif->type == NL80211_IFTYPE_ADHOC) {
1131 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1132 if (ret) {
1133 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1134 goto out_release;
1135 }
1136
1137 iwl_mvm_vif_dbgfs_register(mvm, vif);
1138 goto out_unlock;
1139 }
1140
1141 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1142 if (ret)
1143 goto out_release;
1144
1145 ret = iwl_mvm_power_update_mac(mvm);
1146 if (ret)
1147 goto out_release;
1148
1149 /* beacon filtering */
1150 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1151 if (ret)
1152 goto out_remove_mac;
1153
1154 if (!mvm->bf_allowed_vif &&
1155 vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1156 mvm->bf_allowed_vif = mvmvif;
1157 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1158 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1159 }
1160
1161 /*
1162 * P2P_DEVICE interface does not have a channel context assigned to it,
1163 * so a dedicated PHY context is allocated to it and the corresponding
1164 * MAC context is bound to it at this stage.
1165 */
1166 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1167
1168 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1169 if (!mvmvif->phy_ctxt) {
1170 ret = -ENOSPC;
1171 goto out_free_bf;
1172 }
1173
1174 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1175 ret = iwl_mvm_binding_add_vif(mvm, vif);
1176 if (ret)
1177 goto out_unref_phy;
1178
1179 ret = iwl_mvm_add_bcast_sta(mvm, vif);
1180 if (ret)
1181 goto out_unbind;
1182
1183 /* Save a pointer to p2p device vif, so it can later be used to
1184 * update the p2p device MAC when a GO is started/stopped */
1185 mvm->p2p_device_vif = vif;
1186 }
1187
1188 iwl_mvm_vif_dbgfs_register(mvm, vif);
1189 goto out_unlock;
1190
1191 out_unbind:
1192 iwl_mvm_binding_remove_vif(mvm, vif);
1193 out_unref_phy:
1194 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1195 out_free_bf:
1196 if (mvm->bf_allowed_vif == mvmvif) {
1197 mvm->bf_allowed_vif = NULL;
1198 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1199 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1200 }
1201 out_remove_mac:
1202 mvmvif->phy_ctxt = NULL;
1203 iwl_mvm_mac_ctxt_remove(mvm, vif);
1204 out_release:
1205 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1206 mvm->vif_count--;
1207
1208 iwl_mvm_mac_ctxt_release(mvm, vif);
1209 out_unlock:
1210 mutex_unlock(&mvm->mutex);
1211
1212 iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1213
1214 return ret;
1215}
1216
1217static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1218 struct ieee80211_vif *vif)
1219{
1220 u32 tfd_msk = iwl_mvm_mac_get_queues_mask(vif);
1221
1222 if (tfd_msk) {
1223 mutex_lock(&mvm->mutex);
1224 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
1225 mutex_unlock(&mvm->mutex);
1226 }
1227
1228 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1229 /*
1230 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1231 * We assume here that all the packets sent to the OFFCHANNEL
1232 * queue are sent in ROC session.
1233 */
1234 flush_work(&mvm->roc_done_wk);
1235 } else {
1236 /*
1237 * By now, all the AC queues are empty. The AGG queues are
1238 * empty too. We already got all the Tx responses for all the
1239 * packets in the queues. The drain work can have been
1240 * triggered. Flush it.
1241 */
1242 flush_work(&mvm->sta_drained_wk);
1243 }
1244}
1245
1246static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1247 struct ieee80211_vif *vif)
1248{
1249 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1250 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1251
1252 iwl_mvm_prepare_mac_removal(mvm, vif);
1253
1254 mutex_lock(&mvm->mutex);
1255
1256 if (mvm->bf_allowed_vif == mvmvif) {
1257 mvm->bf_allowed_vif = NULL;
1258 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1259 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1260 }
1261
1262 iwl_mvm_vif_dbgfs_clean(mvm, vif);
1263
1264 /*
1265 * For AP/GO interface, the tear down of the resources allocated to the
1266 * interface is be handled as part of the stop_ap flow.
1267 */
1268 if (vif->type == NL80211_IFTYPE_AP ||
1269 vif->type == NL80211_IFTYPE_ADHOC) {
1270#ifdef CONFIG_NL80211_TESTMODE
1271 if (vif == mvm->noa_vif) {
1272 mvm->noa_vif = NULL;
1273 mvm->noa_duration = 0;
1274 }
1275#endif
1276 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1277 goto out_release;
1278 }
1279
1280 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1281 mvm->p2p_device_vif = NULL;
1282 iwl_mvm_rm_bcast_sta(mvm, vif);
1283 iwl_mvm_binding_remove_vif(mvm, vif);
1284 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1285 mvmvif->phy_ctxt = NULL;
1286 }
1287
1288 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1289 mvm->vif_count--;
1290
1291 iwl_mvm_power_update_mac(mvm);
1292 iwl_mvm_mac_ctxt_remove(mvm, vif);
1293
1294out_release:
1295 iwl_mvm_mac_ctxt_release(mvm, vif);
1296 mutex_unlock(&mvm->mutex);
1297}
1298
1299static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1300{
1301 return 0;
1302}
1303
1304struct iwl_mvm_mc_iter_data {
1305 struct iwl_mvm *mvm;
1306 int port_id;
1307};
1308
1309static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1310 struct ieee80211_vif *vif)
1311{
1312 struct iwl_mvm_mc_iter_data *data = _data;
1313 struct iwl_mvm *mvm = data->mvm;
1314 struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1315 int ret, len;
1316
1317 /* if we don't have free ports, mcast frames will be dropped */
1318 if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1319 return;
1320
1321 if (vif->type != NL80211_IFTYPE_STATION ||
1322 !vif->bss_conf.assoc)
1323 return;
1324
1325 cmd->port_id = data->port_id++;
1326 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1327 len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1328
1329 ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
1330 if (ret)
1331 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1332}
1333
1334static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1335{
1336 struct iwl_mvm_mc_iter_data iter_data = {
1337 .mvm = mvm,
1338 };
1339
1340 lockdep_assert_held(&mvm->mutex);
1341
1342 if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1343 return;
1344
1345 ieee80211_iterate_active_interfaces_atomic(
1346 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1347 iwl_mvm_mc_iface_iterator, &iter_data);
1348}
1349
1350static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1351 struct netdev_hw_addr_list *mc_list)
1352{
1353 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1354 struct iwl_mcast_filter_cmd *cmd;
1355 struct netdev_hw_addr *addr;
1356 int addr_count;
1357 bool pass_all;
1358 int len;
1359
1360 addr_count = netdev_hw_addr_list_count(mc_list);
1361 pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1362 IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1363 if (pass_all)
1364 addr_count = 0;
1365
1366 len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1367 cmd = kzalloc(len, GFP_ATOMIC);
1368 if (!cmd)
1369 return 0;
1370
1371 if (pass_all) {
1372 cmd->pass_all = 1;
1373 return (u64)(unsigned long)cmd;
1374 }
1375
1376 netdev_hw_addr_list_for_each(addr, mc_list) {
1377 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1378 cmd->count, addr->addr);
1379 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1380 addr->addr, ETH_ALEN);
1381 cmd->count++;
1382 }
1383
1384 return (u64)(unsigned long)cmd;
1385}
1386
1387static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1388 unsigned int changed_flags,
1389 unsigned int *total_flags,
1390 u64 multicast)
1391{
1392 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1393 struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1394
1395 mutex_lock(&mvm->mutex);
1396
1397 /* replace previous configuration */
1398 kfree(mvm->mcast_filter_cmd);
1399 mvm->mcast_filter_cmd = cmd;
1400
1401 if (!cmd)
1402 goto out;
1403
1404 iwl_mvm_recalc_multicast(mvm);
1405out:
1406 mutex_unlock(&mvm->mutex);
1407 *total_flags = 0;
1408}
1409
1410#ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1411struct iwl_bcast_iter_data {
1412 struct iwl_mvm *mvm;
1413 struct iwl_bcast_filter_cmd *cmd;
1414 u8 current_filter;
1415};
1416
1417static void
1418iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1419 const struct iwl_fw_bcast_filter *in_filter,
1420 struct iwl_fw_bcast_filter *out_filter)
1421{
1422 struct iwl_fw_bcast_filter_attr *attr;
1423 int i;
1424
1425 memcpy(out_filter, in_filter, sizeof(*out_filter));
1426
1427 for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1428 attr = &out_filter->attrs[i];
1429
1430 if (!attr->mask)
1431 break;
1432
1433 switch (attr->reserved1) {
1434 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1435 if (vif->bss_conf.arp_addr_cnt != 1) {
1436 attr->mask = 0;
1437 continue;
1438 }
1439
1440 attr->val = vif->bss_conf.arp_addr_list[0];
1441 break;
1442 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1443 attr->val = *(__be32 *)&vif->addr[2];
1444 break;
1445 default:
1446 break;
1447 }
1448 attr->reserved1 = 0;
1449 out_filter->num_attrs++;
1450 }
1451}
1452
1453static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1454 struct ieee80211_vif *vif)
1455{
1456 struct iwl_bcast_iter_data *data = _data;
1457 struct iwl_mvm *mvm = data->mvm;
1458 struct iwl_bcast_filter_cmd *cmd = data->cmd;
1459 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1460 struct iwl_fw_bcast_mac *bcast_mac;
1461 int i;
1462
1463 if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1464 return;
1465
1466 bcast_mac = &cmd->macs[mvmvif->id];
1467
1468 /*
1469 * enable filtering only for associated stations, but not for P2P
1470 * Clients
1471 */
1472 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1473 !vif->bss_conf.assoc)
1474 return;
1475
1476 bcast_mac->default_discard = 1;
1477
1478 /* copy all configured filters */
1479 for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1480 /*
1481 * Make sure we don't exceed our filters limit.
1482 * if there is still a valid filter to be configured,
1483 * be on the safe side and just allow bcast for this mac.
1484 */
1485 if (WARN_ON_ONCE(data->current_filter >=
1486 ARRAY_SIZE(cmd->filters))) {
1487 bcast_mac->default_discard = 0;
1488 bcast_mac->attached_filters = 0;
1489 break;
1490 }
1491
1492 iwl_mvm_set_bcast_filter(vif,
1493 &mvm->bcast_filters[i],
1494 &cmd->filters[data->current_filter]);
1495
1496 /* skip current filter if it contains no attributes */
1497 if (!cmd->filters[data->current_filter].num_attrs)
1498 continue;
1499
1500 /* attach the filter to current mac */
1501 bcast_mac->attached_filters |=
1502 cpu_to_le16(BIT(data->current_filter));
1503
1504 data->current_filter++;
1505 }
1506}
1507
1508bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1509 struct iwl_bcast_filter_cmd *cmd)
1510{
1511 struct iwl_bcast_iter_data iter_data = {
1512 .mvm = mvm,
1513 .cmd = cmd,
1514 };
1515
1516 if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1517 return false;
1518
1519 memset(cmd, 0, sizeof(*cmd));
1520 cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1521 cmd->max_macs = ARRAY_SIZE(cmd->macs);
1522
1523#ifdef CONFIG_IWLWIFI_DEBUGFS
1524 /* use debugfs filters/macs if override is configured */
1525 if (mvm->dbgfs_bcast_filtering.override) {
1526 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1527 sizeof(cmd->filters));
1528 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1529 sizeof(cmd->macs));
1530 return true;
1531 }
1532#endif
1533
1534 /* if no filters are configured, do nothing */
1535 if (!mvm->bcast_filters)
1536 return false;
1537
1538 /* configure and attach these filters for each associated sta vif */
1539 ieee80211_iterate_active_interfaces(
1540 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1541 iwl_mvm_bcast_filter_iterator, &iter_data);
1542
1543 return true;
1544}
1545static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1546 struct ieee80211_vif *vif)
1547{
1548 struct iwl_bcast_filter_cmd cmd;
1549
1550 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1551 return 0;
1552
1553 if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1554 return 0;
1555
1556 return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1557 sizeof(cmd), &cmd);
1558}
1559#else
1560static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1561 struct ieee80211_vif *vif)
1562{
1563 return 0;
1564}
1565#endif
1566
1567static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1568 struct ieee80211_vif *vif,
1569 struct ieee80211_bss_conf *bss_conf,
1570 u32 changes)
1571{
1572 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1573 int ret;
1574
1575 /*
1576 * Re-calculate the tsf id, as the master-slave relations depend on the
1577 * beacon interval, which was not known when the station interface was
1578 * added.
1579 */
1580 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1581 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1582
1583 /*
1584 * If we're not associated yet, take the (new) BSSID before associating
1585 * so the firmware knows. If we're already associated, then use the old
1586 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
1587 * branch for disassociation below.
1588 */
1589 if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
1590 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1591
1592 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
1593 if (ret)
1594 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1595
1596 /* after sending it once, adopt mac80211 data */
1597 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1598 mvmvif->associated = bss_conf->assoc;
1599
1600 if (changes & BSS_CHANGED_ASSOC) {
1601 if (bss_conf->assoc) {
1602 /* add quota for this interface */
1603 ret = iwl_mvm_update_quotas(mvm, NULL);
1604 if (ret) {
1605 IWL_ERR(mvm, "failed to update quotas\n");
1606 return;
1607 }
1608
1609 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1610 &mvm->status)) {
1611 /*
1612 * If we're restarting then the firmware will
1613 * obviously have lost synchronisation with
1614 * the AP. It will attempt to synchronise by
1615 * itself, but we can make it more reliable by
1616 * scheduling a session protection time event.
1617 *
1618 * The firmware needs to receive a beacon to
1619 * catch up with synchronisation, use 110% of
1620 * the beacon interval.
1621 *
1622 * Set a large maximum delay to allow for more
1623 * than a single interface.
1624 */
1625 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1626 iwl_mvm_protect_session(mvm, vif, dur, dur,
1627 5 * dur, false);
1628 }
1629
1630 iwl_mvm_sf_update(mvm, vif, false);
1631 iwl_mvm_power_vif_assoc(mvm, vif);
1632 if (vif->p2p) {
1633 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1634 iwl_mvm_update_smps(mvm, vif,
1635 IWL_MVM_SMPS_REQ_PROT,
1636 IEEE80211_SMPS_DYNAMIC);
1637 }
1638 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1639 /*
1640 * If update fails - SF might be running in associated
1641 * mode while disassociated - which is forbidden.
1642 */
1643 WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1644 "Failed to update SF upon disassociation\n");
1645
1646 /* remove AP station now that the MAC is unassoc */
1647 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1648 if (ret)
1649 IWL_ERR(mvm, "failed to remove AP station\n");
1650
1651 if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1652 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1653 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1654 /* remove quota for this interface */
1655 ret = iwl_mvm_update_quotas(mvm, NULL);
1656 if (ret)
1657 IWL_ERR(mvm, "failed to update quotas\n");
1658
1659 if (vif->p2p)
1660 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1661
1662 /* this will take the cleared BSSID from bss_conf */
1663 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1664 if (ret)
1665 IWL_ERR(mvm,
1666 "failed to update MAC %pM (clear after unassoc)\n",
1667 vif->addr);
1668 }
1669
1670 iwl_mvm_recalc_multicast(mvm);
1671 iwl_mvm_configure_bcast_filter(mvm, vif);
1672
1673 /* reset rssi values */
1674 mvmvif->bf_data.ave_beacon_signal = 0;
1675
1676 iwl_mvm_bt_coex_vif_change(mvm);
1677 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1678 IEEE80211_SMPS_AUTOMATIC);
1679 } else if (changes & BSS_CHANGED_BEACON_INFO) {
1680 /*
1681 * We received a beacon _after_ association so
1682 * remove the session protection.
1683 */
1684 iwl_mvm_remove_time_event(mvm, mvmvif,
1685 &mvmvif->time_event_data);
1686 }
1687
1688 if (changes & BSS_CHANGED_BEACON_INFO) {
1689 iwl_mvm_sf_update(mvm, vif, false);
1690 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1691 }
1692
1693 if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS)) {
1694 ret = iwl_mvm_power_update_mac(mvm);
1695 if (ret)
1696 IWL_ERR(mvm, "failed to update power mode\n");
1697 }
1698
1699 if (changes & BSS_CHANGED_TXPOWER) {
1700 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1701 bss_conf->txpower);
1702 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1703 }
1704
1705 if (changes & BSS_CHANGED_CQM) {
1706 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
1707 /* reset cqm events tracking */
1708 mvmvif->bf_data.last_cqm_event = 0;
1709 if (mvmvif->bf_data.bf_enabled) {
1710 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1711 if (ret)
1712 IWL_ERR(mvm,
1713 "failed to update CQM thresholds\n");
1714 }
1715 }
1716
1717 if (changes & BSS_CHANGED_ARP_FILTER) {
1718 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
1719 iwl_mvm_configure_bcast_filter(mvm, vif);
1720 }
1721}
1722
1723static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1724 struct ieee80211_vif *vif)
1725{
1726 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1727 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1728 int ret;
1729
1730 /*
1731 * iwl_mvm_mac_ctxt_add() might read directly from the device
1732 * (the system time), so make sure it is available.
1733 */
1734 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
1735 if (ret)
1736 return ret;
1737
1738 mutex_lock(&mvm->mutex);
1739
1740 /* Send the beacon template */
1741 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1742 if (ret)
1743 goto out_unlock;
1744
1745 /*
1746 * Re-calculate the tsf id, as the master-slave relations depend on the
1747 * beacon interval, which was not known when the AP interface was added.
1748 */
1749 if (vif->type == NL80211_IFTYPE_AP)
1750 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1751
1752 /* Add the mac context */
1753 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1754 if (ret)
1755 goto out_unlock;
1756
1757 /* Perform the binding */
1758 ret = iwl_mvm_binding_add_vif(mvm, vif);
1759 if (ret)
1760 goto out_remove;
1761
1762 /* Send the bcast station. At this stage the TBTT and DTIM time events
1763 * are added and applied to the scheduler */
1764 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1765 if (ret)
1766 goto out_unbind;
1767
1768 /* must be set before quota calculations */
1769 mvmvif->ap_ibss_active = true;
1770
1771 /* power updated needs to be done before quotas */
1772 iwl_mvm_power_update_mac(mvm);
1773
1774 ret = iwl_mvm_update_quotas(mvm, NULL);
1775 if (ret)
1776 goto out_quota_failed;
1777
1778 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1779 if (vif->p2p && mvm->p2p_device_vif)
1780 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
1781
1782 iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1783
1784 iwl_mvm_bt_coex_vif_change(mvm);
1785
1786 /* we don't support TDLS during DCM */
1787 if (iwl_mvm_phy_ctx_count(mvm) > 1)
1788 iwl_mvm_teardown_tdls_peers(mvm);
1789
1790 mutex_unlock(&mvm->mutex);
1791 return 0;
1792
1793out_quota_failed:
1794 iwl_mvm_power_update_mac(mvm);
1795 mvmvif->ap_ibss_active = false;
1796 iwl_mvm_send_rm_bcast_sta(mvm, vif);
1797out_unbind:
1798 iwl_mvm_binding_remove_vif(mvm, vif);
1799out_remove:
1800 iwl_mvm_mac_ctxt_remove(mvm, vif);
1801out_unlock:
1802 mutex_unlock(&mvm->mutex);
1803 iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
1804 return ret;
1805}
1806
1807static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1808 struct ieee80211_vif *vif)
1809{
1810 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1811 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1812
1813 iwl_mvm_prepare_mac_removal(mvm, vif);
1814
1815 mutex_lock(&mvm->mutex);
1816
1817 /* Handle AP stop while in CSA */
1818 if (rcu_access_pointer(mvm->csa_vif) == vif) {
1819 iwl_mvm_remove_time_event(mvm, mvmvif,
1820 &mvmvif->time_event_data);
1821 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1822 }
1823
1824 if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
1825 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1826 mvm->csa_tx_block_bcn_timeout = 0;
1827 }
1828
1829 mvmvif->ap_ibss_active = false;
1830 mvm->ap_last_beacon_gp2 = 0;
1831
1832 iwl_mvm_bt_coex_vif_change(mvm);
1833
1834 iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
1835
1836 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1837 if (vif->p2p && mvm->p2p_device_vif)
1838 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
1839
1840 iwl_mvm_update_quotas(mvm, NULL);
1841 iwl_mvm_send_rm_bcast_sta(mvm, vif);
1842 iwl_mvm_binding_remove_vif(mvm, vif);
1843
1844 iwl_mvm_power_update_mac(mvm);
1845
1846 iwl_mvm_mac_ctxt_remove(mvm, vif);
1847
1848 mutex_unlock(&mvm->mutex);
1849}
1850
1851static void
1852iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1853 struct ieee80211_vif *vif,
1854 struct ieee80211_bss_conf *bss_conf,
1855 u32 changes)
1856{
1857 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1858
1859 /* Changes will be applied when the AP/IBSS is started */
1860 if (!mvmvif->ap_ibss_active)
1861 return;
1862
1863 if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
1864 BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
1865 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
1866 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1867
1868 /* Need to send a new beacon template to the FW */
1869 if (changes & BSS_CHANGED_BEACON &&
1870 iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1871 IWL_WARN(mvm, "Failed updating beacon data\n");
1872
1873 if (changes & BSS_CHANGED_TXPOWER) {
1874 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1875 bss_conf->txpower);
1876 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1877 }
1878
1879}
1880
1881static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1882 struct ieee80211_vif *vif,
1883 struct ieee80211_bss_conf *bss_conf,
1884 u32 changes)
1885{
1886 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1887
1888 /*
1889 * iwl_mvm_bss_info_changed_station() might call
1890 * iwl_mvm_protect_session(), which reads directly from
1891 * the device (the system time), so make sure it is available.
1892 */
1893 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
1894 return;
1895
1896 mutex_lock(&mvm->mutex);
1897
1898 if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
1899 iwl_mvm_scan_offload_stop(mvm, true);
1900
1901 switch (vif->type) {
1902 case NL80211_IFTYPE_STATION:
1903 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1904 break;
1905 case NL80211_IFTYPE_AP:
1906 case NL80211_IFTYPE_ADHOC:
1907 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1908 break;
1909 default:
1910 /* shouldn't happen */
1911 WARN_ON_ONCE(1);
1912 }
1913
1914 mutex_unlock(&mvm->mutex);
1915 iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
1916}
1917
1918static int iwl_mvm_cancel_scan_wait_notif(struct iwl_mvm *mvm,
1919 enum iwl_scan_status scan_type)
1920{
1921 int ret;
1922 bool wait_for_handlers = false;
1923
1924 mutex_lock(&mvm->mutex);
1925
1926 if (mvm->scan_status != scan_type) {
1927 ret = 0;
1928 /* make sure there are no pending notifications */
1929 wait_for_handlers = true;
1930 goto out;
1931 }
1932
1933 switch (scan_type) {
1934 case IWL_MVM_SCAN_SCHED:
1935 ret = iwl_mvm_scan_offload_stop(mvm, true);
1936 break;
1937 case IWL_MVM_SCAN_OS:
1938 ret = iwl_mvm_cancel_scan(mvm);
1939 break;
1940 case IWL_MVM_SCAN_NONE:
1941 default:
1942 WARN_ON_ONCE(1);
1943 ret = -EINVAL;
1944 break;
1945 }
1946 if (ret)
1947 goto out;
1948
1949 wait_for_handlers = true;
1950out:
1951 mutex_unlock(&mvm->mutex);
1952
1953 /* make sure we consume the completion notification */
1954 if (wait_for_handlers)
1955 iwl_mvm_wait_for_async_handlers(mvm);
1956
1957 return ret;
1958}
1959static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1960 struct ieee80211_vif *vif,
1961 struct ieee80211_scan_request *hw_req)
1962{
1963 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1964 struct cfg80211_scan_request *req = &hw_req->req;
1965 int ret;
1966
1967 if (req->n_channels == 0 ||
1968 req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1969 return -EINVAL;
1970
1971 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1972 ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_SCHED);
1973 if (ret)
1974 return ret;
1975 }
1976
1977 mutex_lock(&mvm->mutex);
1978
1979 if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
1980 ret = -EBUSY;
1981 goto out;
1982 }
1983
1984 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1985
1986 if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1987 ret = iwl_mvm_scan_umac(mvm, vif, hw_req);
1988 else if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1989 ret = iwl_mvm_unified_scan_lmac(mvm, vif, hw_req);
1990 else
1991 ret = iwl_mvm_scan_request(mvm, vif, req);
1992
1993 if (ret)
1994 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1995out:
1996 mutex_unlock(&mvm->mutex);
1997 return ret;
1998}
1999
2000static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2001 struct ieee80211_vif *vif)
2002{
2003 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2004
2005 mutex_lock(&mvm->mutex);
2006
2007 iwl_mvm_cancel_scan(mvm);
2008
2009 mutex_unlock(&mvm->mutex);
2010}
2011
2012static void
2013iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2014 struct ieee80211_sta *sta, u16 tids,
2015 int num_frames,
2016 enum ieee80211_frame_release_type reason,
2017 bool more_data)
2018{
2019 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2020
2021 /* Called when we need to transmit (a) frame(s) from mac80211 */
2022
2023 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2024 tids, more_data, false);
2025}
2026
2027static void
2028iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2029 struct ieee80211_sta *sta, u16 tids,
2030 int num_frames,
2031 enum ieee80211_frame_release_type reason,
2032 bool more_data)
2033{
2034 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2035
2036 /* Called when we need to transmit (a) frame(s) from agg queue */
2037
2038 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2039 tids, more_data, true);
2040}
2041
2042static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2043 struct ieee80211_vif *vif,
2044 enum sta_notify_cmd cmd,
2045 struct ieee80211_sta *sta)
2046{
2047 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2048 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2049 int tid;
2050
2051 switch (cmd) {
2052 case STA_NOTIFY_SLEEP:
2053 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
2054 ieee80211_sta_block_awake(hw, sta, true);
2055 spin_lock_bh(&mvmsta->lock);
2056 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2057 struct iwl_mvm_tid_data *tid_data;
2058
2059 tid_data = &mvmsta->tid_data[tid];
2060 if (tid_data->state != IWL_AGG_ON &&
2061 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2062 continue;
2063 if (iwl_mvm_tid_queued(tid_data) == 0)
2064 continue;
2065 ieee80211_sta_set_buffered(sta, tid, true);
2066 }
2067 spin_unlock_bh(&mvmsta->lock);
2068 /*
2069 * The fw updates the STA to be asleep. Tx packets on the Tx
2070 * queues to this station will not be transmitted. The fw will
2071 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2072 */
2073 break;
2074 case STA_NOTIFY_AWAKE:
2075 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
2076 break;
2077 iwl_mvm_sta_modify_ps_wake(mvm, sta);
2078 break;
2079 default:
2080 break;
2081 }
2082}
2083
2084static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2085 struct ieee80211_vif *vif,
2086 struct ieee80211_sta *sta)
2087{
2088 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2089 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
2090
2091 /*
2092 * This is called before mac80211 does RCU synchronisation,
2093 * so here we already invalidate our internal RCU-protected
2094 * station pointer. The rest of the code will thus no longer
2095 * be able to find the station this way, and we don't rely
2096 * on further RCU synchronisation after the sta_state()
2097 * callback deleted the station.
2098 */
2099 mutex_lock(&mvm->mutex);
2100 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2101 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2102 ERR_PTR(-ENOENT));
2103 mutex_unlock(&mvm->mutex);
2104}
2105
2106static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2107 struct ieee80211_vif *vif,
2108 struct ieee80211_sta *sta,
2109 enum ieee80211_sta_state old_state,
2110 enum ieee80211_sta_state new_state)
2111{
2112 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2113 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2114 int ret;
2115
2116 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2117 sta->addr, old_state, new_state);
2118
2119 /* this would be a mac80211 bug ... but don't crash */
2120 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2121 return -EINVAL;
2122
2123 /* if a STA is being removed, reuse its ID */
2124 flush_work(&mvm->sta_drained_wk);
2125
2126 mutex_lock(&mvm->mutex);
2127 if (old_state == IEEE80211_STA_NOTEXIST &&
2128 new_state == IEEE80211_STA_NONE) {
2129 /*
2130 * Firmware bug - it'll crash if the beacon interval is less
2131 * than 16. We can't avoid connecting at all, so refuse the
2132 * station state change, this will cause mac80211 to abandon
2133 * attempts to connect to this AP, and eventually wpa_s will
2134 * blacklist the AP...
2135 */
2136 if (vif->type == NL80211_IFTYPE_STATION &&
2137 vif->bss_conf.beacon_int < 16) {
2138 IWL_ERR(mvm,
2139 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2140 sta->addr, vif->bss_conf.beacon_int);
2141 ret = -EINVAL;
2142 goto out_unlock;
2143 }
2144
2145 if (sta->tdls &&
2146 (vif->p2p ||
2147 iwl_mvm_tdls_sta_count(mvm, NULL) ==
2148 IWL_MVM_TDLS_STA_COUNT ||
2149 iwl_mvm_phy_ctx_count(mvm) > 1)) {
2150 IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2151 ret = -EBUSY;
2152 goto out_unlock;
2153 }
2154
2155 ret = iwl_mvm_add_sta(mvm, vif, sta);
2156 if (sta->tdls && ret == 0)
2157 iwl_mvm_recalc_tdls_state(mvm, vif, true);
2158 } else if (old_state == IEEE80211_STA_NONE &&
2159 new_state == IEEE80211_STA_AUTH) {
2160 /*
2161 * EBS may be disabled due to previous failures reported by FW.
2162 * Reset EBS status here assuming environment has been changed.
2163 */
2164 mvm->last_ebs_successful = true;
2165 ret = 0;
2166 } else if (old_state == IEEE80211_STA_AUTH &&
2167 new_state == IEEE80211_STA_ASSOC) {
2168 ret = iwl_mvm_update_sta(mvm, vif, sta);
2169 if (ret == 0)
2170 iwl_mvm_rs_rate_init(mvm, sta,
2171 mvmvif->phy_ctxt->channel->band,
2172 true);
2173 } else if (old_state == IEEE80211_STA_ASSOC &&
2174 new_state == IEEE80211_STA_AUTHORIZED) {
2175
2176 /* we don't support TDLS during DCM */
2177 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2178 iwl_mvm_teardown_tdls_peers(mvm);
2179
2180 /* enable beacon filtering */
2181 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2182 ret = 0;
2183 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
2184 new_state == IEEE80211_STA_ASSOC) {
2185 /* disable beacon filtering */
2186 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
2187 ret = 0;
2188 } else if (old_state == IEEE80211_STA_ASSOC &&
2189 new_state == IEEE80211_STA_AUTH) {
2190 ret = 0;
2191 } else if (old_state == IEEE80211_STA_AUTH &&
2192 new_state == IEEE80211_STA_NONE) {
2193 ret = 0;
2194 } else if (old_state == IEEE80211_STA_NONE &&
2195 new_state == IEEE80211_STA_NOTEXIST) {
2196 ret = iwl_mvm_rm_sta(mvm, vif, sta);
2197 if (sta->tdls)
2198 iwl_mvm_recalc_tdls_state(mvm, vif, false);
2199 } else {
2200 ret = -EIO;
2201 }
2202 out_unlock:
2203 mutex_unlock(&mvm->mutex);
2204
2205 if (sta->tdls && ret == 0) {
2206 if (old_state == IEEE80211_STA_NOTEXIST &&
2207 new_state == IEEE80211_STA_NONE)
2208 ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2209 else if (old_state == IEEE80211_STA_NONE &&
2210 new_state == IEEE80211_STA_NOTEXIST)
2211 ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2212 }
2213
2214 return ret;
2215}
2216
2217static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2218{
2219 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2220
2221 mvm->rts_threshold = value;
2222
2223 return 0;
2224}
2225
2226static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2227 struct ieee80211_vif *vif,
2228 struct ieee80211_sta *sta, u32 changed)
2229{
2230 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2231
2232 if (vif->type == NL80211_IFTYPE_STATION &&
2233 changed & IEEE80211_RC_NSS_CHANGED)
2234 iwl_mvm_sf_update(mvm, vif, false);
2235}
2236
2237static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2238 struct ieee80211_vif *vif, u16 ac,
2239 const struct ieee80211_tx_queue_params *params)
2240{
2241 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2242 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2243
2244 mvmvif->queue_params[ac] = *params;
2245
2246 /*
2247 * No need to update right away, we'll get BSS_CHANGED_QOS
2248 * The exception is P2P_DEVICE interface which needs immediate update.
2249 */
2250 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2251 int ret;
2252
2253 mutex_lock(&mvm->mutex);
2254 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2255 mutex_unlock(&mvm->mutex);
2256 return ret;
2257 }
2258 return 0;
2259}
2260
2261static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2262 struct ieee80211_vif *vif)
2263{
2264 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2265 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
2266 200 + vif->bss_conf.beacon_int);
2267 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
2268 100 + vif->bss_conf.beacon_int);
2269
2270 if (WARN_ON_ONCE(vif->bss_conf.assoc))
2271 return;
2272
2273 /*
2274 * iwl_mvm_protect_session() reads directly from the device
2275 * (the system time), so make sure it is available.
2276 */
2277 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2278 return;
2279
2280 mutex_lock(&mvm->mutex);
2281 /* Try really hard to protect the session and hear a beacon */
2282 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
2283 mutex_unlock(&mvm->mutex);
2284
2285 iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
2286}
2287
2288static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2289 struct ieee80211_vif *vif,
2290 struct cfg80211_sched_scan_request *req,
2291 struct ieee80211_scan_ies *ies)
2292{
2293 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2294 int ret;
2295
2296 if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
2297 ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_OS);
2298 if (ret)
2299 return ret;
2300 }
2301
2302 mutex_lock(&mvm->mutex);
2303
2304 /* Newest FW fixes sched scan while connected on another interface */
2305 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
2306 if (!vif->bss_conf.idle) {
2307 ret = -EBUSY;
2308 goto out;
2309 }
2310 } else if (!iwl_mvm_is_idle(mvm)) {
2311 ret = -EBUSY;
2312 goto out;
2313 }
2314
2315 if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
2316 ret = -EBUSY;
2317 goto out;
2318 }
2319
2320 ret = iwl_mvm_scan_offload_start(mvm, vif, req, ies);
2321 if (ret)
2322 mvm->scan_status = IWL_MVM_SCAN_NONE;
2323
2324out:
2325 mutex_unlock(&mvm->mutex);
2326 return ret;
2327}
2328
2329static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2330 struct ieee80211_vif *vif)
2331{
2332 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2333 int ret;
2334
2335 mutex_lock(&mvm->mutex);
2336 ret = iwl_mvm_scan_offload_stop(mvm, false);
2337 mutex_unlock(&mvm->mutex);
2338 iwl_mvm_wait_for_async_handlers(mvm);
2339
2340 return ret;
2341
2342}
2343
2344static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2345 enum set_key_cmd cmd,
2346 struct ieee80211_vif *vif,
2347 struct ieee80211_sta *sta,
2348 struct ieee80211_key_conf *key)
2349{
2350 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2351 int ret;
2352
2353 if (iwlwifi_mod_params.sw_crypto) {
2354 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2355 return -EOPNOTSUPP;
2356 }
2357
2358 switch (key->cipher) {
2359 case WLAN_CIPHER_SUITE_TKIP:
2360 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2361 /* fall-through */
2362 case WLAN_CIPHER_SUITE_CCMP:
2363 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2364 break;
2365 case WLAN_CIPHER_SUITE_AES_CMAC:
2366 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
2367 break;
2368 case WLAN_CIPHER_SUITE_WEP40:
2369 case WLAN_CIPHER_SUITE_WEP104:
2370 /* For non-client mode, only use WEP keys for TX as we probably
2371 * don't have a station yet anyway and would then have to keep
2372 * track of the keys, linking them to each of the clients/peers
2373 * as they appear. For now, don't do that, for performance WEP
2374 * offload doesn't really matter much, but we need it for some
2375 * other offload features in client mode.
2376 */
2377 if (vif->type != NL80211_IFTYPE_STATION)
2378 return 0;
2379 break;
2380 default:
2381 /* currently FW supports only one optional cipher scheme */
2382 if (hw->n_cipher_schemes &&
2383 hw->cipher_schemes->cipher == key->cipher)
2384 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2385 else
2386 return -EOPNOTSUPP;
2387 }
2388
2389 mutex_lock(&mvm->mutex);
2390
2391 switch (cmd) {
2392 case SET_KEY:
2393 if ((vif->type == NL80211_IFTYPE_ADHOC ||
2394 vif->type == NL80211_IFTYPE_AP) && !sta) {
2395 /*
2396 * GTK on AP interface is a TX-only key, return 0;
2397 * on IBSS they're per-station and because we're lazy
2398 * we don't support them for RX, so do the same.
2399 */
2400 ret = 0;
2401 key->hw_key_idx = STA_KEY_IDX_INVALID;
2402 break;
2403 }
2404
2405 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
2406 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
2407 if (ret) {
2408 IWL_WARN(mvm, "set key failed\n");
2409 /*
2410 * can't add key for RX, but we don't need it
2411 * in the device for TX so still return 0
2412 */
2413 key->hw_key_idx = STA_KEY_IDX_INVALID;
2414 ret = 0;
2415 }
2416
2417 break;
2418 case DISABLE_KEY:
2419 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
2420 ret = 0;
2421 break;
2422 }
2423
2424 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
2425 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
2426 break;
2427 default:
2428 ret = -EINVAL;
2429 }
2430
2431 mutex_unlock(&mvm->mutex);
2432 return ret;
2433}
2434
2435static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
2436 struct ieee80211_vif *vif,
2437 struct ieee80211_key_conf *keyconf,
2438 struct ieee80211_sta *sta,
2439 u32 iv32, u16 *phase1key)
2440{
2441 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2442
2443 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
2444 return;
2445
2446 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
2447}
2448
2449
2450static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
2451 struct iwl_rx_packet *pkt, void *data)
2452{
2453 struct iwl_mvm *mvm =
2454 container_of(notif_wait, struct iwl_mvm, notif_wait);
2455 struct iwl_hs20_roc_res *resp;
2456 int resp_len = iwl_rx_packet_payload_len(pkt);
2457 struct iwl_mvm_time_event_data *te_data = data;
2458
2459 if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
2460 return true;
2461
2462 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
2463 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
2464 return true;
2465 }
2466
2467 resp = (void *)pkt->data;
2468
2469 IWL_DEBUG_TE(mvm,
2470 "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
2471 resp->status, resp->event_unique_id);
2472
2473 te_data->uid = le32_to_cpu(resp->event_unique_id);
2474 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
2475 te_data->uid);
2476
2477 spin_lock_bh(&mvm->time_event_lock);
2478 list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
2479 spin_unlock_bh(&mvm->time_event_lock);
2480
2481 return true;
2482}
2483
2484#define AUX_ROC_MAX_DELAY_ON_CHANNEL 5000
2485static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
2486 struct ieee80211_channel *channel,
2487 struct ieee80211_vif *vif,
2488 int duration)
2489{
2490 int res, time_reg = DEVICE_SYSTEM_TIME_REG;
2491 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2492 struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
2493 static const u8 time_event_response[] = { HOT_SPOT_CMD };
2494 struct iwl_notification_wait wait_time_event;
2495 struct iwl_hs20_roc_req aux_roc_req = {
2496 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
2497 .id_and_color =
2498 cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
2499 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
2500 /* Set the channel info data */
2501 .channel_info.band = (channel->band == IEEE80211_BAND_2GHZ) ?
2502 PHY_BAND_24 : PHY_BAND_5,
2503 .channel_info.channel = channel->hw_value,
2504 .channel_info.width = PHY_VHT_CHANNEL_MODE20,
2505 /* Set the time and duration */
2506 .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
2507 .apply_time_max_delay =
2508 cpu_to_le32(MSEC_TO_TU(AUX_ROC_MAX_DELAY_ON_CHANNEL)),
2509 .duration = cpu_to_le32(MSEC_TO_TU(duration)),
2510 };
2511
2512 /* Set the node address */
2513 memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
2514
2515 lockdep_assert_held(&mvm->mutex);
2516
2517 spin_lock_bh(&mvm->time_event_lock);
2518
2519 if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
2520 spin_unlock_bh(&mvm->time_event_lock);
2521 return -EIO;
2522 }
2523
2524 te_data->vif = vif;
2525 te_data->duration = duration;
2526 te_data->id = HOT_SPOT_CMD;
2527
2528 spin_unlock_bh(&mvm->time_event_lock);
2529
2530 /*
2531 * Use a notification wait, which really just processes the
2532 * command response and doesn't wait for anything, in order
2533 * to be able to process the response and get the UID inside
2534 * the RX path. Using CMD_WANT_SKB doesn't work because it
2535 * stores the buffer and then wakes up this thread, by which
2536 * time another notification (that the time event started)
2537 * might already be processed unsuccessfully.
2538 */
2539 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
2540 time_event_response,
2541 ARRAY_SIZE(time_event_response),
2542 iwl_mvm_rx_aux_roc, te_data);
2543
2544 res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
2545 &aux_roc_req);
2546
2547 if (res) {
2548 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
2549 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
2550 goto out_clear_te;
2551 }
2552
2553 /* No need to wait for anything, so just pass 1 (0 isn't valid) */
2554 res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
2555 /* should never fail */
2556 WARN_ON_ONCE(res);
2557
2558 if (res) {
2559 out_clear_te:
2560 spin_lock_bh(&mvm->time_event_lock);
2561 iwl_mvm_te_clear_data(mvm, te_data);
2562 spin_unlock_bh(&mvm->time_event_lock);
2563 }
2564
2565 return res;
2566}
2567
2568static int iwl_mvm_roc(struct ieee80211_hw *hw,
2569 struct ieee80211_vif *vif,
2570 struct ieee80211_channel *channel,
2571 int duration,
2572 enum ieee80211_roc_type type)
2573{
2574 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2575 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2576 struct cfg80211_chan_def chandef;
2577 struct iwl_mvm_phy_ctxt *phy_ctxt;
2578 int ret, i;
2579
2580 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2581 duration, type);
2582
2583 mutex_lock(&mvm->mutex);
2584
2585 switch (vif->type) {
2586 case NL80211_IFTYPE_STATION:
2587 if (mvm->fw->ucode_capa.capa[0] &
2588 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT) {
2589 /* Use aux roc framework (HS20) */
2590 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
2591 vif, duration);
2592 goto out_unlock;
2593 }
2594 IWL_ERR(mvm, "hotspot not supported\n");
2595 ret = -EINVAL;
2596 goto out_unlock;
2597 case NL80211_IFTYPE_P2P_DEVICE:
2598 /* handle below */
2599 break;
2600 default:
2601 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
2602 ret = -EINVAL;
2603 goto out_unlock;
2604 }
2605
2606 for (i = 0; i < NUM_PHY_CTX; i++) {
2607 phy_ctxt = &mvm->phy_ctxts[i];
2608 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2609 continue;
2610
2611 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2612 /*
2613 * Unbind the P2P_DEVICE from the current PHY context,
2614 * and if the PHY context is not used remove it.
2615 */
2616 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2617 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2618 goto out_unlock;
2619
2620 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2621
2622 /* Bind the P2P_DEVICE to the current PHY Context */
2623 mvmvif->phy_ctxt = phy_ctxt;
2624
2625 ret = iwl_mvm_binding_add_vif(mvm, vif);
2626 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2627 goto out_unlock;
2628
2629 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2630 goto schedule_time_event;
2631 }
2632 }
2633
2634 /* Need to update the PHY context only if the ROC channel changed */
2635 if (channel == mvmvif->phy_ctxt->channel)
2636 goto schedule_time_event;
2637
2638 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2639
2640 /*
2641 * Change the PHY context configuration as it is currently referenced
2642 * only by the P2P Device MAC
2643 */
2644 if (mvmvif->phy_ctxt->ref == 1) {
2645 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2646 &chandef, 1, 1);
2647 if (ret)
2648 goto out_unlock;
2649 } else {
2650 /*
2651 * The PHY context is shared with other MACs. Need to remove the
2652 * P2P Device from the binding, allocate an new PHY context and
2653 * create a new binding
2654 */
2655 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2656 if (!phy_ctxt) {
2657 ret = -ENOSPC;
2658 goto out_unlock;
2659 }
2660
2661 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2662 1, 1);
2663 if (ret) {
2664 IWL_ERR(mvm, "Failed to change PHY context\n");
2665 goto out_unlock;
2666 }
2667
2668 /* Unbind the P2P_DEVICE from the current PHY context */
2669 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2670 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2671 goto out_unlock;
2672
2673 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2674
2675 /* Bind the P2P_DEVICE to the new allocated PHY context */
2676 mvmvif->phy_ctxt = phy_ctxt;
2677
2678 ret = iwl_mvm_binding_add_vif(mvm, vif);
2679 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2680 goto out_unlock;
2681
2682 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2683 }
2684
2685schedule_time_event:
2686 /* Schedule the time events */
2687 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2688
2689out_unlock:
2690 mutex_unlock(&mvm->mutex);
2691 IWL_DEBUG_MAC80211(mvm, "leave\n");
2692 return ret;
2693}
2694
2695static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2696{
2697 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2698
2699 IWL_DEBUG_MAC80211(mvm, "enter\n");
2700
2701 mutex_lock(&mvm->mutex);
2702 iwl_mvm_stop_roc(mvm);
2703 mutex_unlock(&mvm->mutex);
2704
2705 IWL_DEBUG_MAC80211(mvm, "leave\n");
2706 return 0;
2707}
2708
2709static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
2710 struct ieee80211_chanctx_conf *ctx)
2711{
2712 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2713 struct iwl_mvm_phy_ctxt *phy_ctxt;
2714 int ret;
2715
2716 lockdep_assert_held(&mvm->mutex);
2717
2718 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2719
2720 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2721 if (!phy_ctxt) {
2722 ret = -ENOSPC;
2723 goto out;
2724 }
2725
2726 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2727 ctx->rx_chains_static,
2728 ctx->rx_chains_dynamic);
2729 if (ret) {
2730 IWL_ERR(mvm, "Failed to add PHY context\n");
2731 goto out;
2732 }
2733
2734 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
2735 *phy_ctxt_id = phy_ctxt->id;
2736out:
2737 return ret;
2738}
2739
2740static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2741 struct ieee80211_chanctx_conf *ctx)
2742{
2743 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2744 int ret;
2745
2746 mutex_lock(&mvm->mutex);
2747 ret = __iwl_mvm_add_chanctx(mvm, ctx);
2748 mutex_unlock(&mvm->mutex);
2749
2750 return ret;
2751}
2752
2753static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
2754 struct ieee80211_chanctx_conf *ctx)
2755{
2756 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2757 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2758
2759 lockdep_assert_held(&mvm->mutex);
2760
2761 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2762}
2763
2764static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2765 struct ieee80211_chanctx_conf *ctx)
2766{
2767 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2768
2769 mutex_lock(&mvm->mutex);
2770 __iwl_mvm_remove_chanctx(mvm, ctx);
2771 mutex_unlock(&mvm->mutex);
2772}
2773
2774static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
2775 struct ieee80211_chanctx_conf *ctx,
2776 u32 changed)
2777{
2778 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2779 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2780 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2781
2782 if (WARN_ONCE((phy_ctxt->ref > 1) &&
2783 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
2784 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2785 IEEE80211_CHANCTX_CHANGE_RADAR |
2786 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
2787 "Cannot change PHY. Ref=%d, changed=0x%X\n",
2788 phy_ctxt->ref, changed))
2789 return;
2790
2791 mutex_lock(&mvm->mutex);
2792 iwl_mvm_bt_coex_vif_change(mvm);
2793 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2794 ctx->rx_chains_static,
2795 ctx->rx_chains_dynamic);
2796 mutex_unlock(&mvm->mutex);
2797}
2798
2799static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
2800 struct ieee80211_vif *vif,
2801 struct ieee80211_chanctx_conf *ctx,
2802 bool switching_chanctx)
2803{
2804 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2805 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2806 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2807 int ret;
2808
2809 lockdep_assert_held(&mvm->mutex);
2810
2811 mvmvif->phy_ctxt = phy_ctxt;
2812
2813 switch (vif->type) {
2814 case NL80211_IFTYPE_AP:
2815 /* only needed if we're switching chanctx (i.e. during CSA) */
2816 if (switching_chanctx) {
2817 mvmvif->ap_ibss_active = true;
2818 break;
2819 }
2820 case NL80211_IFTYPE_ADHOC:
2821 /*
2822 * The AP binding flow is handled as part of the start_ap flow
2823 * (in bss_info_changed), similarly for IBSS.
2824 */
2825 ret = 0;
2826 goto out;
2827 case NL80211_IFTYPE_STATION:
2828 break;
2829 case NL80211_IFTYPE_MONITOR:
2830 /* always disable PS when a monitor interface is active */
2831 mvmvif->ps_disabled = true;
2832 break;
2833 default:
2834 ret = -EINVAL;
2835 goto out;
2836 }
2837
2838 ret = iwl_mvm_binding_add_vif(mvm, vif);
2839 if (ret)
2840 goto out;
2841
2842 /*
2843 * Power state must be updated before quotas,
2844 * otherwise fw will complain.
2845 */
2846 iwl_mvm_power_update_mac(mvm);
2847
2848 /* Setting the quota at this stage is only required for monitor
2849 * interfaces. For the other types, the bss_info changed flow
2850 * will handle quota settings.
2851 */
2852 if (vif->type == NL80211_IFTYPE_MONITOR) {
2853 mvmvif->monitor_active = true;
2854 ret = iwl_mvm_update_quotas(mvm, NULL);
2855 if (ret)
2856 goto out_remove_binding;
2857 }
2858
2859 /* Handle binding during CSA */
2860 if (vif->type == NL80211_IFTYPE_AP) {
2861 iwl_mvm_update_quotas(mvm, NULL);
2862 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2863 }
2864
2865 if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
2866 u32 duration = 2 * vif->bss_conf.beacon_int;
2867
2868 /* iwl_mvm_protect_session() reads directly from the
2869 * device (the system time), so make sure it is
2870 * available.
2871 */
2872 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
2873 if (ret)
2874 goto out_remove_binding;
2875
2876 /* Protect the session to make sure we hear the first
2877 * beacon on the new channel.
2878 */
2879 iwl_mvm_protect_session(mvm, vif, duration, duration,
2880 vif->bss_conf.beacon_int / 2,
2881 true);
2882
2883 iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
2884
2885 iwl_mvm_update_quotas(mvm, NULL);
2886 }
2887
2888 goto out;
2889
2890out_remove_binding:
2891 iwl_mvm_binding_remove_vif(mvm, vif);
2892 iwl_mvm_power_update_mac(mvm);
2893out:
2894 if (ret)
2895 mvmvif->phy_ctxt = NULL;
2896 return ret;
2897}
2898static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
2899 struct ieee80211_vif *vif,
2900 struct ieee80211_chanctx_conf *ctx)
2901{
2902 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2903 int ret;
2904
2905 mutex_lock(&mvm->mutex);
2906 ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
2907 mutex_unlock(&mvm->mutex);
2908
2909 return ret;
2910}
2911
2912static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
2913 struct ieee80211_vif *vif,
2914 struct ieee80211_chanctx_conf *ctx,
2915 bool switching_chanctx)
2916{
2917 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2918 struct ieee80211_vif *disabled_vif = NULL;
2919
2920 lockdep_assert_held(&mvm->mutex);
2921
2922 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
2923
2924 switch (vif->type) {
2925 case NL80211_IFTYPE_ADHOC:
2926 goto out;
2927 case NL80211_IFTYPE_MONITOR:
2928 mvmvif->monitor_active = false;
2929 mvmvif->ps_disabled = false;
2930 break;
2931 case NL80211_IFTYPE_AP:
2932 /* This part is triggered only during CSA */
2933 if (!switching_chanctx || !mvmvif->ap_ibss_active)
2934 goto out;
2935
2936 mvmvif->csa_countdown = false;
2937
2938 /* Set CS bit on all the stations */
2939 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
2940
2941 /* Save blocked iface, the timeout is set on the next beacon */
2942 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
2943
2944 mvmvif->ap_ibss_active = false;
2945 break;
2946 case NL80211_IFTYPE_STATION:
2947 if (!switching_chanctx)
2948 break;
2949
2950 disabled_vif = vif;
2951
2952 iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
2953 break;
2954 default:
2955 break;
2956 }
2957
2958 iwl_mvm_update_quotas(mvm, disabled_vif);
2959 iwl_mvm_binding_remove_vif(mvm, vif);
2960
2961out:
2962 mvmvif->phy_ctxt = NULL;
2963 iwl_mvm_power_update_mac(mvm);
2964}
2965
2966static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
2967 struct ieee80211_vif *vif,
2968 struct ieee80211_chanctx_conf *ctx)
2969{
2970 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2971
2972 mutex_lock(&mvm->mutex);
2973 __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
2974 mutex_unlock(&mvm->mutex);
2975}
2976
2977static int
2978iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
2979 struct ieee80211_vif_chanctx_switch *vifs)
2980{
2981 int ret;
2982
2983 mutex_lock(&mvm->mutex);
2984 __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
2985 __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
2986
2987 ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
2988 if (ret) {
2989 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
2990 goto out_reassign;
2991 }
2992
2993 ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
2994 true);
2995 if (ret) {
2996 IWL_ERR(mvm,
2997 "failed to assign new_ctx during channel switch\n");
2998 goto out_remove;
2999 }
3000
3001 /* we don't support TDLS during DCM - can be caused by channel switch */
3002 if (iwl_mvm_phy_ctx_count(mvm) > 1)
3003 iwl_mvm_teardown_tdls_peers(mvm);
3004
3005 goto out;
3006
3007out_remove:
3008 __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
3009
3010out_reassign:
3011 if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
3012 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
3013 goto out_restart;
3014 }
3015
3016 if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3017 true)) {
3018 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3019 goto out_restart;
3020 }
3021
3022 goto out;
3023
3024out_restart:
3025 /* things keep failing, better restart the hw */
3026 iwl_mvm_nic_restart(mvm, false);
3027
3028out:
3029 mutex_unlock(&mvm->mutex);
3030
3031 return ret;
3032}
3033
3034static int
3035iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
3036 struct ieee80211_vif_chanctx_switch *vifs)
3037{
3038 int ret;
3039
3040 mutex_lock(&mvm->mutex);
3041 __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3042
3043 ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3044 true);
3045 if (ret) {
3046 IWL_ERR(mvm,
3047 "failed to assign new_ctx during channel switch\n");
3048 goto out_reassign;
3049 }
3050
3051 goto out;
3052
3053out_reassign:
3054 if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3055 true)) {
3056 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3057 goto out_restart;
3058 }
3059
3060 goto out;
3061
3062out_restart:
3063 /* things keep failing, better restart the hw */
3064 iwl_mvm_nic_restart(mvm, false);
3065
3066out:
3067 mutex_unlock(&mvm->mutex);
3068
3069 return ret;
3070}
3071
3072static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
3073 struct ieee80211_vif_chanctx_switch *vifs,
3074 int n_vifs,
3075 enum ieee80211_chanctx_switch_mode mode)
3076{
3077 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3078 int ret;
3079
3080 /* we only support a single-vif right now */
3081 if (n_vifs > 1)
3082 return -EOPNOTSUPP;
3083
3084 switch (mode) {
3085 case CHANCTX_SWMODE_SWAP_CONTEXTS:
3086 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
3087 break;
3088 case CHANCTX_SWMODE_REASSIGN_VIF:
3089 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
3090 break;
3091 default:
3092 ret = -EOPNOTSUPP;
3093 break;
3094 }
3095
3096 return ret;
3097}
3098
3099static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
3100 struct ieee80211_sta *sta,
3101 bool set)
3102{
3103 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3104 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
3105
3106 if (!mvm_sta || !mvm_sta->vif) {
3107 IWL_ERR(mvm, "Station is not associated to a vif\n");
3108 return -EINVAL;
3109 }
3110
3111 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
3112}
3113
3114#ifdef CONFIG_NL80211_TESTMODE
3115static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
3116 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
3117 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
3118 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
3119};
3120
3121static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
3122 struct ieee80211_vif *vif,
3123 void *data, int len)
3124{
3125 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
3126 int err;
3127 u32 noa_duration;
3128
3129 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
3130 if (err)
3131 return err;
3132
3133 if (!tb[IWL_MVM_TM_ATTR_CMD])
3134 return -EINVAL;
3135
3136 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
3137 case IWL_MVM_TM_CMD_SET_NOA:
3138 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
3139 !vif->bss_conf.enable_beacon ||
3140 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
3141 return -EINVAL;
3142
3143 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
3144 if (noa_duration >= vif->bss_conf.beacon_int)
3145 return -EINVAL;
3146
3147 mvm->noa_duration = noa_duration;
3148 mvm->noa_vif = vif;
3149
3150 return iwl_mvm_update_quotas(mvm, NULL);
3151 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
3152 /* must be associated client vif - ignore authorized */
3153 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
3154 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
3155 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
3156 return -EINVAL;
3157
3158 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
3159 return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3160 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3161 }
3162
3163 return -EOPNOTSUPP;
3164}
3165
3166static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
3167 struct ieee80211_vif *vif,
3168 void *data, int len)
3169{
3170 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3171 int err;
3172
3173 mutex_lock(&mvm->mutex);
3174 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
3175 mutex_unlock(&mvm->mutex);
3176
3177 return err;
3178}
3179#endif
3180
3181static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
3182 struct ieee80211_vif *vif,
3183 struct ieee80211_channel_switch *chsw)
3184{
3185 /* By implementing this operation, we prevent mac80211 from
3186 * starting its own channel switch timer, so that we can call
3187 * ieee80211_chswitch_done() ourselves at the right time
3188 * (which is when the absence time event starts).
3189 */
3190
3191 IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
3192 "dummy channel switch op\n");
3193}
3194
3195static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
3196 struct ieee80211_vif *vif,
3197 struct ieee80211_channel_switch *chsw)
3198{
3199 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3200 struct ieee80211_vif *csa_vif;
3201 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3202 u32 apply_time;
3203 int ret;
3204
3205 mutex_lock(&mvm->mutex);
3206
3207 IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
3208 chsw->chandef.center_freq1);
3209
3210 switch (vif->type) {
3211 case NL80211_IFTYPE_AP:
3212 csa_vif =
3213 rcu_dereference_protected(mvm->csa_vif,
3214 lockdep_is_held(&mvm->mutex));
3215 if (WARN_ONCE(csa_vif && csa_vif->csa_active,
3216 "Another CSA is already in progress")) {
3217 ret = -EBUSY;
3218 goto out_unlock;
3219 }
3220
3221 rcu_assign_pointer(mvm->csa_vif, vif);
3222
3223 if (WARN_ONCE(mvmvif->csa_countdown,
3224 "Previous CSA countdown didn't complete")) {
3225 ret = -EBUSY;
3226 goto out_unlock;
3227 }
3228
3229 break;
3230 case NL80211_IFTYPE_STATION:
3231 /* Schedule the time event to a bit before beacon 1,
3232 * to make sure we're in the new channel when the
3233 * GO/AP arrives.
3234 */
3235 apply_time = chsw->device_timestamp +
3236 ((vif->bss_conf.beacon_int * (chsw->count - 1) -
3237 IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
3238
3239 if (chsw->block_tx)
3240 iwl_mvm_csa_client_absent(mvm, vif);
3241
3242 iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
3243 apply_time);
3244 if (mvmvif->bf_data.bf_enabled) {
3245 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3246 if (ret)
3247 goto out_unlock;
3248 }
3249
3250 break;
3251 default:
3252 break;
3253 }
3254
3255 mvmvif->ps_disabled = true;
3256
3257 ret = iwl_mvm_power_update_ps(mvm);
3258 if (ret)
3259 goto out_unlock;
3260
3261 /* we won't be on this channel any longer */
3262 iwl_mvm_teardown_tdls_peers(mvm);
3263
3264out_unlock:
3265 mutex_unlock(&mvm->mutex);
3266
3267 return ret;
3268}
3269
3270static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
3271 struct ieee80211_vif *vif)
3272{
3273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3274 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3275 int ret;
3276
3277 mutex_lock(&mvm->mutex);
3278
3279 if (vif->type == NL80211_IFTYPE_STATION) {
3280 struct iwl_mvm_sta *mvmsta;
3281
3282 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
3283 mvmvif->ap_sta_id);
3284
3285 if (WARN_ON(!mvmsta)) {
3286 ret = -EIO;
3287 goto out_unlock;
3288 }
3289
3290 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
3291
3292 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3293
3294 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3295 if (ret)
3296 goto out_unlock;
3297
3298 iwl_mvm_stop_session_protection(mvm, vif);
3299 }
3300
3301 mvmvif->ps_disabled = false;
3302
3303 ret = iwl_mvm_power_update_ps(mvm);
3304
3305out_unlock:
3306 mutex_unlock(&mvm->mutex);
3307
3308 return ret;
3309}
3310
3311static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
3312 struct ieee80211_vif *vif, u32 queues, bool drop)
3313{
3314 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3315 struct iwl_mvm_vif *mvmvif;
3316 struct iwl_mvm_sta *mvmsta;
3317 struct ieee80211_sta *sta;
3318 int i;
3319 u32 msk = 0;
3320
3321 if (!vif || vif->type != NL80211_IFTYPE_STATION)
3322 return;
3323
3324 mutex_lock(&mvm->mutex);
3325 mvmvif = iwl_mvm_vif_from_mac80211(vif);
3326
3327 /* flush the AP-station and all TDLS peers */
3328 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3329 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3330 lockdep_is_held(&mvm->mutex));
3331 if (IS_ERR_OR_NULL(sta))
3332 continue;
3333
3334 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3335 if (mvmsta->vif != vif)
3336 continue;
3337
3338 /* make sure only TDLS peers or the AP are flushed */
3339 WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
3340
3341 msk |= mvmsta->tfd_queue_msk;
3342 }
3343
3344 if (drop) {
3345 if (iwl_mvm_flush_tx_path(mvm, msk, true))
3346 IWL_ERR(mvm, "flush request fail\n");
3347 mutex_unlock(&mvm->mutex);
3348 } else {
3349 mutex_unlock(&mvm->mutex);
3350
3351 /* this can take a while, and we may need/want other operations
3352 * to succeed while doing this, so do it without the mutex held
3353 */
3354 iwl_trans_wait_tx_queue_empty(mvm->trans, msk);
3355 }
3356}
3357
3358const struct ieee80211_ops iwl_mvm_hw_ops = {
3359 .tx = iwl_mvm_mac_tx,
3360 .ampdu_action = iwl_mvm_mac_ampdu_action,
3361 .start = iwl_mvm_mac_start,
3362 .reconfig_complete = iwl_mvm_mac_reconfig_complete,
3363 .stop = iwl_mvm_mac_stop,
3364 .add_interface = iwl_mvm_mac_add_interface,
3365 .remove_interface = iwl_mvm_mac_remove_interface,
3366 .config = iwl_mvm_mac_config,
3367 .prepare_multicast = iwl_mvm_prepare_multicast,
3368 .configure_filter = iwl_mvm_configure_filter,
3369 .bss_info_changed = iwl_mvm_bss_info_changed,
3370 .hw_scan = iwl_mvm_mac_hw_scan,
3371 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
3372 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
3373 .sta_state = iwl_mvm_mac_sta_state,
3374 .sta_notify = iwl_mvm_mac_sta_notify,
3375 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3376 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
3377 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
3378 .sta_rc_update = iwl_mvm_sta_rc_update,
3379 .conf_tx = iwl_mvm_mac_conf_tx,
3380 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
3381 .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
3382 .flush = iwl_mvm_mac_flush,
3383 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
3384 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
3385 .set_key = iwl_mvm_mac_set_key,
3386 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
3387 .remain_on_channel = iwl_mvm_roc,
3388 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
3389 .add_chanctx = iwl_mvm_add_chanctx,
3390 .remove_chanctx = iwl_mvm_remove_chanctx,
3391 .change_chanctx = iwl_mvm_change_chanctx,
3392 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
3393 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
3394 .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
3395
3396 .start_ap = iwl_mvm_start_ap_ibss,
3397 .stop_ap = iwl_mvm_stop_ap_ibss,
3398 .join_ibss = iwl_mvm_start_ap_ibss,
3399 .leave_ibss = iwl_mvm_stop_ap_ibss,
3400
3401 .set_tim = iwl_mvm_set_tim,
3402
3403 .channel_switch = iwl_mvm_channel_switch,
3404 .pre_channel_switch = iwl_mvm_pre_channel_switch,
3405 .post_channel_switch = iwl_mvm_post_channel_switch,
3406
3407 .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
3408 .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
3409 .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
3410
3411 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
3412
3413#ifdef CONFIG_PM_SLEEP
3414 /* look at d3.c */
3415 .suspend = iwl_mvm_suspend,
3416 .resume = iwl_mvm_resume,
3417 .set_wakeup = iwl_mvm_set_wakeup,
3418 .set_rekey_data = iwl_mvm_set_rekey_data,
3419#if IS_ENABLED(CONFIG_IPV6)
3420 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
3421#endif
3422 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
3423#endif
3424};
This page took 0.042948 seconds and 5 git commands to generate.