1 /******************************************************************************
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.
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
63 #include <linux/kernel.h>
64 #include <linux/slab.h>
65 #include <linux/skbuff.h>
66 #include <linux/netdevice.h>
67 #include <linux/etherdevice.h>
69 #include <linux/if_arp.h>
70 #include <net/mac80211.h>
71 #include <net/ieee80211_radiotap.h>
74 #include "iwl-op-mode.h"
78 #include "time-event.h"
79 #include "iwl-eeprom-parse.h"
80 #include "fw-api-scan.h"
81 #include "iwl-phy-db.h"
84 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
87 .types = BIT(NL80211_IFTYPE_STATION),
91 .types = BIT(NL80211_IFTYPE_AP) |
92 BIT(NL80211_IFTYPE_P2P_CLIENT) |
93 BIT(NL80211_IFTYPE_P2P_GO),
97 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
101 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
103 .num_different_channels = 1,
105 .limits = iwl_mvm_limits,
106 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
110 #ifdef CONFIG_PM_SLEEP
111 static const struct nl80211_wowlan_tcp_data_token_feature
112 iwl_mvm_wowlan_tcp_token_feature = {
115 .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
118 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
119 .tok = &iwl_mvm_wowlan_tcp_token_feature,
120 .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
121 sizeof(struct ethhdr) -
122 sizeof(struct iphdr) -
123 sizeof(struct tcphdr),
124 .data_interval_max = 65535, /* __le16 in API */
125 .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
126 sizeof(struct ethhdr) -
127 sizeof(struct iphdr) -
128 sizeof(struct tcphdr),
133 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
135 * Use the reserved field to indicate magic values.
136 * these values will only be used internally by the driver,
137 * and won't make it to the fw (reserved will be 0).
138 * BC_FILTER_MAGIC_IP - configure the val of this attribute to
139 * be the vif's ip address. in case there is not a single
140 * ip address (0, or more than 1), this attribute will
142 * BC_FILTER_MAGIC_MAC - set the val of this attribute to
143 * the LSB bytes of the vif's mac address
146 BC_FILTER_MAGIC_NONE = 0,
151 static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
155 .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
158 /* frame type - arp, hw type - ethernet */
160 BCAST_FILTER_OFFSET_PAYLOAD_START,
161 .offset = sizeof(rfc1042_header),
162 .val = cpu_to_be32(0x08060001),
163 .mask = cpu_to_be32(0xffffffff),
168 BCAST_FILTER_OFFSET_PAYLOAD_START,
169 .offset = sizeof(rfc1042_header) + 2 +
170 sizeof(struct arphdr) +
171 ETH_ALEN + sizeof(__be32) +
173 .mask = cpu_to_be32(0xffffffff),
174 /* mark it as special field */
175 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
180 /* dhcp offer bcast */
182 .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
185 /* udp dest port - 68 (bootp client)*/
186 .offset_type = BCAST_FILTER_OFFSET_IP_END,
187 .offset = offsetof(struct udphdr, dest),
188 .val = cpu_to_be32(0x00440000),
189 .mask = cpu_to_be32(0xffff0000),
192 /* dhcp - lsb bytes of client hw address */
193 .offset_type = BCAST_FILTER_OFFSET_IP_END,
195 .mask = cpu_to_be32(0xffffffff),
196 /* mark it as special field */
197 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
201 /* last filter must be empty */
206 void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
208 if (!iwl_mvm_is_d0i3_supported(mvm))
211 IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
212 WARN_ON(test_and_set_bit(ref_type, mvm->ref_bitmap));
213 iwl_trans_ref(mvm->trans);
216 void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
218 if (!iwl_mvm_is_d0i3_supported(mvm))
221 IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
222 WARN_ON(!test_and_clear_bit(ref_type, mvm->ref_bitmap));
223 iwl_trans_unref(mvm->trans);
227 iwl_mvm_unref_all_except(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref)
231 if (!iwl_mvm_is_d0i3_supported(mvm))
234 for_each_set_bit(i, mvm->ref_bitmap, IWL_MVM_REF_COUNT) {
238 IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d\n", i);
239 clear_bit(i, mvm->ref_bitmap);
240 iwl_trans_unref(mvm->trans);
244 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
248 memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
249 for (i = 0; i < NUM_PHY_CTX; i++) {
250 mvm->phy_ctxts[i].id = i;
251 mvm->phy_ctxts[i].ref = 0;
255 static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
257 /* we create the 802.11 header and SSID element */
258 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
259 return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
260 return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
263 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
265 struct ieee80211_hw *hw = mvm->hw;
268 /* Tell mac80211 our characteristics */
269 hw->flags = IEEE80211_HW_SIGNAL_DBM |
270 IEEE80211_HW_SPECTRUM_MGMT |
271 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
272 IEEE80211_HW_QUEUE_CONTROL |
273 IEEE80211_HW_WANT_MONITOR_VIF |
274 IEEE80211_HW_SUPPORTS_PS |
275 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
276 IEEE80211_HW_AMPDU_AGGREGATION |
277 IEEE80211_HW_TIMING_BEACON_ONLY |
278 IEEE80211_HW_CONNECTION_MONITOR |
279 IEEE80211_HW_SUPPORTS_UAPSD |
280 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
281 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
283 hw->queues = mvm->first_agg_queue;
284 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
285 hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
286 IEEE80211_RADIOTAP_MCS_HAVE_STBC;
287 hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
288 hw->rate_control_algorithm = "iwl-mvm-rs";
289 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
290 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
293 * Enable 11w if advertised by firmware and software crypto
294 * is not enabled (as the firmware will interpret some mgmt
295 * packets, so enabling it with software crypto isn't safe)
297 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
298 !iwlwifi_mod_params.sw_crypto)
299 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
301 /* Disable uAPSD due to firmware issues */
303 hw->flags &= ~IEEE80211_HW_SUPPORTS_UAPSD;
305 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
306 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
307 hw->chanctx_data_size = sizeof(u16);
309 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
310 BIT(NL80211_IFTYPE_P2P_CLIENT) |
311 BIT(NL80211_IFTYPE_AP) |
312 BIT(NL80211_IFTYPE_P2P_GO) |
313 BIT(NL80211_IFTYPE_P2P_DEVICE) |
314 BIT(NL80211_IFTYPE_ADHOC);
316 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
317 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
318 REGULATORY_DISABLE_BEACON_HINTS;
320 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
321 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
323 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_CSA_FLOW)
324 hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
326 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
327 hw->wiphy->n_iface_combinations =
328 ARRAY_SIZE(iwl_mvm_iface_combinations);
330 hw->wiphy->max_remain_on_channel_duration = 10000;
331 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
333 /* Extract MAC address */
334 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
335 hw->wiphy->addresses = mvm->addresses;
336 hw->wiphy->n_addresses = 1;
338 /* Extract additional MAC addresses if available */
339 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
340 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
342 for (i = 1; i < num_mac; i++) {
343 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
345 mvm->addresses[i].addr[5]++;
346 hw->wiphy->n_addresses++;
349 iwl_mvm_reset_phy_ctxts(mvm);
351 hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
353 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
355 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
356 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
357 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
358 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
359 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
360 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
362 hw->wiphy->hw_version = mvm->trans->hw_id;
364 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
365 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
367 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
369 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
370 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
371 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
372 /* we create the 802.11 header and zero length SSID IE. */
373 hw->wiphy->max_sched_scan_ie_len = SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
375 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
376 NL80211_FEATURE_P2P_GO_OPPPS;
378 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
380 /* currently FW API supports only one optional cipher scheme */
381 if (mvm->fw->cs[0].cipher) {
382 mvm->hw->n_cipher_schemes = 1;
383 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
386 #ifdef CONFIG_PM_SLEEP
387 if (iwl_mvm_is_d0i3_supported(mvm) &&
388 device_can_wakeup(mvm->trans->dev)) {
389 mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
390 hw->wiphy->wowlan = &mvm->wowlan;
391 } else if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
392 mvm->trans->ops->d3_suspend &&
393 mvm->trans->ops->d3_resume &&
394 device_can_wakeup(mvm->trans->dev)) {
395 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
396 WIPHY_WOWLAN_DISCONNECT |
397 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
398 WIPHY_WOWLAN_RFKILL_RELEASE;
399 if (!iwlwifi_mod_params.sw_crypto)
400 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
401 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
402 WIPHY_WOWLAN_4WAY_HANDSHAKE;
404 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
405 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
406 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
407 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
408 hw->wiphy->wowlan = &mvm->wowlan;
412 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
413 /* assign default bcast filtering configuration */
414 mvm->bcast_filters = iwl_mvm_default_bcast_filters;
417 ret = iwl_mvm_leds_init(mvm);
421 ret = ieee80211_register_hw(mvm->hw);
423 iwl_mvm_leds_exit(mvm);
428 static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
429 struct ieee80211_sta *sta,
432 struct iwl_mvm_sta *mvmsta;
436 * double check the IN_D0I3 flag both before and after
437 * taking the spinlock, in order to prevent taking
438 * the spinlock when not needed.
440 if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
443 spin_lock(&mvm->d0i3_tx_lock);
445 * testing the flag again ensures the skb dequeue
446 * loop (on d0i3 exit) hasn't run yet.
448 if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
451 mvmsta = iwl_mvm_sta_from_mac80211(sta);
452 if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
453 mvmsta->sta_id != mvm->d0i3_ap_sta_id)
456 __skb_queue_tail(&mvm->d0i3_tx, skb);
457 ieee80211_stop_queues(mvm->hw);
460 iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
461 iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
465 spin_unlock(&mvm->d0i3_tx_lock);
469 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
470 struct ieee80211_tx_control *control,
473 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
474 struct ieee80211_sta *sta = control->sta;
475 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
476 struct ieee80211_hdr *hdr = (void *)skb->data;
478 if (iwl_mvm_is_radio_killed(mvm)) {
479 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
483 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
484 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
487 /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
488 if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
489 ieee80211_is_mgmt(hdr->frame_control) &&
490 !ieee80211_is_deauth(hdr->frame_control) &&
491 !ieee80211_is_disassoc(hdr->frame_control) &&
492 !ieee80211_is_action(hdr->frame_control)))
496 if (iwl_mvm_defer_tx(mvm, sta, skb))
498 if (iwl_mvm_tx_skb(mvm, skb, sta))
503 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
507 ieee80211_free_txskb(hw, skb);
510 static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
512 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
517 static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
519 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
521 if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
524 /* enabled by default */
528 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
529 struct ieee80211_vif *vif,
530 enum ieee80211_ampdu_mlme_action action,
531 struct ieee80211_sta *sta, u16 tid,
532 u16 *ssn, u8 buf_size)
534 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
536 bool tx_agg_ref = false;
538 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
539 sta->addr, tid, action);
541 if (!(mvm->nvm_data->sku_cap_11n_enable))
544 /* return from D0i3 before starting a new Tx aggregation */
546 case IEEE80211_AMPDU_TX_START:
547 case IEEE80211_AMPDU_TX_STOP_CONT:
548 case IEEE80211_AMPDU_TX_STOP_FLUSH:
549 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
550 case IEEE80211_AMPDU_TX_OPERATIONAL:
551 iwl_mvm_ref(mvm, IWL_MVM_REF_TX_AGG);
555 * for tx start, wait synchronously until D0i3 exit to
556 * get the correct sequence number for the tid.
557 * additionally, some other ampdu actions use direct
558 * target access, which is not handled automatically
559 * by the trans layer (unlike commands), so wait for
560 * d0i3 exit in these cases as well.
562 if (!wait_event_timeout(mvm->d0i3_exit_waitq,
563 !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status), HZ)) {
565 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
573 mutex_lock(&mvm->mutex);
576 case IEEE80211_AMPDU_RX_START:
577 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
581 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
583 case IEEE80211_AMPDU_RX_STOP:
584 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
586 case IEEE80211_AMPDU_TX_START:
587 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
591 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
593 case IEEE80211_AMPDU_TX_STOP_CONT:
594 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
596 case IEEE80211_AMPDU_TX_STOP_FLUSH:
597 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
598 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
600 case IEEE80211_AMPDU_TX_OPERATIONAL:
601 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
608 mutex_unlock(&mvm->mutex);
611 * If the tid is marked as started, we won't use it for offloaded
612 * traffic on the next D0i3 entry. It's safe to unref.
615 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
620 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
621 struct ieee80211_vif *vif)
623 struct iwl_mvm *mvm = data;
624 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
626 mvmvif->uploaded = false;
627 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
629 /* does this make sense at all? */
632 spin_lock_bh(&mvm->time_event_lock);
633 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
634 spin_unlock_bh(&mvm->time_event_lock);
636 mvmvif->phy_ctxt = NULL;
639 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
641 #ifdef CONFIG_IWLWIFI_DEBUGFS
642 static char *env[] = { "DRIVER=iwlwifi", "EVENT=error_dump", NULL };
644 iwl_mvm_fw_error_dump(mvm);
646 /* notify the userspace about the error we had */
647 kobject_uevent_env(&mvm->hw->wiphy->dev.kobj, KOBJ_CHANGE, env);
650 iwl_trans_stop_device(mvm->trans);
652 mvm->scan_status = IWL_MVM_SCAN_NONE;
654 /* just in case one was running */
655 ieee80211_remain_on_channel_expired(mvm->hw);
657 ieee80211_iterate_active_interfaces_atomic(
658 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
659 iwl_mvm_cleanup_iterator, mvm);
661 mvm->p2p_device_vif = NULL;
662 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
664 iwl_mvm_reset_phy_ctxts(mvm);
665 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
666 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
668 ieee80211_wake_queues(mvm->hw);
670 /* cleanup all stale references (scan, roc), but keep the
671 * ucode_down ref until reconfig is complete */
672 iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
675 mvm->rx_ba_sessions = 0;
678 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
680 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
683 mutex_lock(&mvm->mutex);
685 /* Clean up some internal and mac80211 state on restart */
686 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
687 iwl_mvm_restart_cleanup(mvm);
689 ret = iwl_mvm_up(mvm);
690 mutex_unlock(&mvm->mutex);
695 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
697 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
700 mutex_lock(&mvm->mutex);
702 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
703 iwl_mvm_d0i3_enable_tx(mvm, NULL);
704 ret = iwl_mvm_update_quotas(mvm, NULL);
706 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
709 /* allow transport/FW low power modes */
710 iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
712 mutex_unlock(&mvm->mutex);
715 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
717 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
719 flush_work(&mvm->d0i3_exit_work);
720 flush_work(&mvm->async_handlers_wk);
722 mutex_lock(&mvm->mutex);
724 /* disallow low power states when the FW is down */
725 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
727 /* async_handlers_wk is now blocked */
730 * The work item could be running or queued if the
731 * ROC time event stops just as we get here.
733 cancel_work_sync(&mvm->roc_done_wk);
735 iwl_trans_stop_device(mvm->trans);
737 iwl_mvm_async_handlers_purge(mvm);
738 /* async_handlers_list is empty and will stay empty: HW is stopped */
740 /* the fw is stopped, the aux sta is dead: clean up driver state */
741 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
743 mutex_unlock(&mvm->mutex);
746 * The worker might have been waiting for the mutex, let it run and
747 * discover that its list is now empty.
749 cancel_work_sync(&mvm->async_handlers_wk);
752 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
756 lockdep_assert_held(&mvm->mutex);
758 for (i = 0; i < NUM_PHY_CTX; i++)
759 if (!mvm->phy_ctxts[i].ref)
760 return &mvm->phy_ctxts[i];
762 IWL_ERR(mvm, "No available PHY context\n");
766 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
769 /* FW is in charge of regulatory enforcement */
770 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
771 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
772 .pwr_restriction = cpu_to_le16(tx_power),
775 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
776 sizeof(reduce_txpwr_cmd),
780 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
781 struct ieee80211_vif *vif)
783 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
784 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
788 * Not much to do here. The stack will not allow interface
789 * types or combinations that we didn't advertise, so we
790 * don't really have to check the types.
793 mutex_lock(&mvm->mutex);
795 /* Allocate resources for the MAC context, and add it to the fw */
796 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
800 /* Counting number of interfaces is needed for legacy PM */
801 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
805 * The AP binding flow can be done only after the beacon
806 * template is configured (which happens only in the mac80211
807 * start_ap() flow), and adding the broadcast station can happen
808 * only after the binding.
809 * In addition, since modifying the MAC before adding a bcast
810 * station is not allowed by the FW, delay the adding of MAC context to
811 * the point where we can also add the bcast station.
812 * In short: there's not much we can do at this point, other than
813 * allocating resources :)
815 if (vif->type == NL80211_IFTYPE_AP ||
816 vif->type == NL80211_IFTYPE_ADHOC) {
817 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
818 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
820 ieee80211_vif_type_p2p(vif));
822 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
826 iwl_mvm_vif_dbgfs_register(mvm, vif);
830 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
834 ret = iwl_mvm_power_update_mac(mvm, vif);
838 /* beacon filtering */
839 ret = iwl_mvm_disable_beacon_filter(mvm, vif, CMD_SYNC);
843 if (!mvm->bf_allowed_vif &&
844 vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
845 mvm->bf_allowed_vif = mvmvif;
846 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
847 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
851 * P2P_DEVICE interface does not have a channel context assigned to it,
852 * so a dedicated PHY context is allocated to it and the corresponding
853 * MAC context is bound to it at this stage.
855 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
857 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
858 if (!mvmvif->phy_ctxt) {
863 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
864 ret = iwl_mvm_binding_add_vif(mvm, vif);
868 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
872 /* Save a pointer to p2p device vif, so it can later be used to
873 * update the p2p device MAC when a GO is started/stopped */
874 mvm->p2p_device_vif = vif;
877 iwl_mvm_vif_dbgfs_register(mvm, vif);
881 iwl_mvm_binding_remove_vif(mvm, vif);
883 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
885 if (mvm->bf_allowed_vif == mvmvif) {
886 mvm->bf_allowed_vif = NULL;
887 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
888 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
891 mvmvif->phy_ctxt = NULL;
892 iwl_mvm_mac_ctxt_remove(mvm, vif);
894 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
897 iwl_mvm_mac_ctxt_release(mvm, vif);
899 mutex_unlock(&mvm->mutex);
904 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
905 struct ieee80211_vif *vif)
909 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
910 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
911 tfd_msk |= BIT(vif->hw_queue[ac]);
913 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
914 tfd_msk |= BIT(vif->cab_queue);
917 mutex_lock(&mvm->mutex);
918 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
919 mutex_unlock(&mvm->mutex);
922 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
924 * Flush the ROC worker which will flush the OFFCHANNEL queue.
925 * We assume here that all the packets sent to the OFFCHANNEL
926 * queue are sent in ROC session.
928 flush_work(&mvm->roc_done_wk);
931 * By now, all the AC queues are empty. The AGG queues are
932 * empty too. We already got all the Tx responses for all the
933 * packets in the queues. The drain work can have been
934 * triggered. Flush it.
936 flush_work(&mvm->sta_drained_wk);
940 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
941 struct ieee80211_vif *vif)
943 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
944 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
946 iwl_mvm_prepare_mac_removal(mvm, vif);
948 mutex_lock(&mvm->mutex);
950 if (mvm->bf_allowed_vif == mvmvif) {
951 mvm->bf_allowed_vif = NULL;
952 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
953 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
956 iwl_mvm_vif_dbgfs_clean(mvm, vif);
959 * For AP/GO interface, the tear down of the resources allocated to the
960 * interface is be handled as part of the stop_ap flow.
962 if (vif->type == NL80211_IFTYPE_AP ||
963 vif->type == NL80211_IFTYPE_ADHOC) {
964 #ifdef CONFIG_NL80211_TESTMODE
965 if (vif == mvm->noa_vif) {
967 mvm->noa_duration = 0;
970 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
974 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
975 mvm->p2p_device_vif = NULL;
976 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
977 iwl_mvm_binding_remove_vif(mvm, vif);
978 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
979 mvmvif->phy_ctxt = NULL;
982 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
985 iwl_mvm_power_update_mac(mvm, vif);
986 iwl_mvm_mac_ctxt_remove(mvm, vif);
989 iwl_mvm_mac_ctxt_release(mvm, vif);
990 mutex_unlock(&mvm->mutex);
993 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
998 struct iwl_mvm_mc_iter_data {
1003 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1004 struct ieee80211_vif *vif)
1006 struct iwl_mvm_mc_iter_data *data = _data;
1007 struct iwl_mvm *mvm = data->mvm;
1008 struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1011 /* if we don't have free ports, mcast frames will be dropped */
1012 if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1015 if (vif->type != NL80211_IFTYPE_STATION ||
1016 !vif->bss_conf.assoc)
1019 cmd->port_id = data->port_id++;
1020 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1021 len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1023 ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
1025 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1028 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1030 struct iwl_mvm_mc_iter_data iter_data = {
1034 lockdep_assert_held(&mvm->mutex);
1036 if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1039 ieee80211_iterate_active_interfaces_atomic(
1040 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1041 iwl_mvm_mc_iface_iterator, &iter_data);
1044 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1045 struct netdev_hw_addr_list *mc_list)
1047 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1048 struct iwl_mcast_filter_cmd *cmd;
1049 struct netdev_hw_addr *addr;
1050 int addr_count = netdev_hw_addr_list_count(mc_list);
1051 bool pass_all = false;
1054 if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
1059 len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1060 cmd = kzalloc(len, GFP_ATOMIC);
1066 return (u64)(unsigned long)cmd;
1069 netdev_hw_addr_list_for_each(addr, mc_list) {
1070 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1071 cmd->count, addr->addr);
1072 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1073 addr->addr, ETH_ALEN);
1077 return (u64)(unsigned long)cmd;
1080 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1081 unsigned int changed_flags,
1082 unsigned int *total_flags,
1085 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1086 struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1088 mutex_lock(&mvm->mutex);
1090 /* replace previous configuration */
1091 kfree(mvm->mcast_filter_cmd);
1092 mvm->mcast_filter_cmd = cmd;
1097 iwl_mvm_recalc_multicast(mvm);
1099 mutex_unlock(&mvm->mutex);
1103 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1104 struct iwl_bcast_iter_data {
1105 struct iwl_mvm *mvm;
1106 struct iwl_bcast_filter_cmd *cmd;
1111 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1112 const struct iwl_fw_bcast_filter *in_filter,
1113 struct iwl_fw_bcast_filter *out_filter)
1115 struct iwl_fw_bcast_filter_attr *attr;
1118 memcpy(out_filter, in_filter, sizeof(*out_filter));
1120 for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1121 attr = &out_filter->attrs[i];
1126 switch (attr->reserved1) {
1127 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1128 if (vif->bss_conf.arp_addr_cnt != 1) {
1133 attr->val = vif->bss_conf.arp_addr_list[0];
1135 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1136 attr->val = *(__be32 *)&vif->addr[2];
1141 attr->reserved1 = 0;
1142 out_filter->num_attrs++;
1146 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1147 struct ieee80211_vif *vif)
1149 struct iwl_bcast_iter_data *data = _data;
1150 struct iwl_mvm *mvm = data->mvm;
1151 struct iwl_bcast_filter_cmd *cmd = data->cmd;
1152 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1153 struct iwl_fw_bcast_mac *bcast_mac;
1156 if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1159 bcast_mac = &cmd->macs[mvmvif->id];
1161 /* enable filtering only for associated stations */
1162 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1165 bcast_mac->default_discard = 1;
1167 /* copy all configured filters */
1168 for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1170 * Make sure we don't exceed our filters limit.
1171 * if there is still a valid filter to be configured,
1172 * be on the safe side and just allow bcast for this mac.
1174 if (WARN_ON_ONCE(data->current_filter >=
1175 ARRAY_SIZE(cmd->filters))) {
1176 bcast_mac->default_discard = 0;
1177 bcast_mac->attached_filters = 0;
1181 iwl_mvm_set_bcast_filter(vif,
1182 &mvm->bcast_filters[i],
1183 &cmd->filters[data->current_filter]);
1185 /* skip current filter if it contains no attributes */
1186 if (!cmd->filters[data->current_filter].num_attrs)
1189 /* attach the filter to current mac */
1190 bcast_mac->attached_filters |=
1191 cpu_to_le16(BIT(data->current_filter));
1193 data->current_filter++;
1197 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1198 struct iwl_bcast_filter_cmd *cmd)
1200 struct iwl_bcast_iter_data iter_data = {
1205 memset(cmd, 0, sizeof(*cmd));
1206 cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1207 cmd->max_macs = ARRAY_SIZE(cmd->macs);
1209 #ifdef CONFIG_IWLWIFI_DEBUGFS
1210 /* use debugfs filters/macs if override is configured */
1211 if (mvm->dbgfs_bcast_filtering.override) {
1212 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1213 sizeof(cmd->filters));
1214 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1220 /* if no filters are configured, do nothing */
1221 if (!mvm->bcast_filters)
1224 /* configure and attach these filters for each associated sta vif */
1225 ieee80211_iterate_active_interfaces(
1226 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1227 iwl_mvm_bcast_filter_iterator, &iter_data);
1231 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1232 struct ieee80211_vif *vif)
1234 struct iwl_bcast_filter_cmd cmd;
1236 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1239 /* bcast filtering isn't supported for P2P client */
1243 if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1246 return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, CMD_SYNC,
1250 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1251 struct ieee80211_vif *vif)
1257 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1258 struct ieee80211_vif *vif,
1259 struct ieee80211_bss_conf *bss_conf,
1262 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1266 * Re-calculate the tsf id, as the master-slave relations depend on the
1267 * beacon interval, which was not known when the station interface was
1270 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1271 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1273 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1275 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1277 if (changes & BSS_CHANGED_ASSOC) {
1278 if (bss_conf->assoc) {
1279 /* add quota for this interface */
1280 ret = iwl_mvm_update_quotas(mvm, vif);
1282 IWL_ERR(mvm, "failed to update quotas\n");
1286 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1289 * If we're restarting then the firmware will
1290 * obviously have lost synchronisation with
1291 * the AP. It will attempt to synchronise by
1292 * itself, but we can make it more reliable by
1293 * scheduling a session protection time event.
1295 * The firmware needs to receive a beacon to
1296 * catch up with synchronisation, use 110% of
1297 * the beacon interval.
1299 * Set a large maximum delay to allow for more
1300 * than a single interface.
1302 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1303 iwl_mvm_protect_session(mvm, vif, dur, dur,
1307 iwl_mvm_sf_update(mvm, vif, false);
1308 iwl_mvm_power_vif_assoc(mvm, vif);
1310 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1311 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1313 * If update fails - SF might be running in associated
1314 * mode while disassociated - which is forbidden.
1316 WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1317 "Failed to update SF upon disassociation\n");
1319 /* remove AP station now that the MAC is unassoc */
1320 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1322 IWL_ERR(mvm, "failed to remove AP station\n");
1324 if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1325 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1326 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1327 /* remove quota for this interface */
1328 ret = iwl_mvm_update_quotas(mvm, NULL);
1330 IWL_ERR(mvm, "failed to update quotas\n");
1333 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1336 iwl_mvm_recalc_multicast(mvm);
1337 iwl_mvm_configure_bcast_filter(mvm, vif);
1339 /* reset rssi values */
1340 mvmvif->bf_data.ave_beacon_signal = 0;
1342 iwl_mvm_bt_coex_vif_change(mvm);
1343 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1344 IEEE80211_SMPS_AUTOMATIC);
1345 } else if (changes & BSS_CHANGED_BEACON_INFO) {
1347 * We received a beacon _after_ association so
1348 * remove the session protection.
1350 iwl_mvm_remove_time_event(mvm, mvmvif,
1351 &mvmvif->time_event_data);
1352 iwl_mvm_sf_update(mvm, vif, false);
1353 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, CMD_SYNC));
1354 } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS |
1356 ret = iwl_mvm_power_update_mac(mvm, vif);
1358 IWL_ERR(mvm, "failed to update power mode\n");
1360 if (changes & BSS_CHANGED_TXPOWER) {
1361 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1363 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1366 if (changes & BSS_CHANGED_CQM) {
1367 IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
1368 /* reset cqm events tracking */
1369 mvmvif->bf_data.last_cqm_event = 0;
1370 ret = iwl_mvm_update_beacon_filter(mvm, vif, false, CMD_SYNC);
1372 IWL_ERR(mvm, "failed to update CQM thresholds\n");
1375 if (changes & BSS_CHANGED_ARP_FILTER) {
1376 IWL_DEBUG_MAC80211(mvm, "arp filter changed");
1377 iwl_mvm_configure_bcast_filter(mvm, vif);
1381 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1382 struct ieee80211_vif *vif)
1384 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1385 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1388 mutex_lock(&mvm->mutex);
1390 /* Send the beacon template */
1391 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1396 * Re-calculate the tsf id, as the master-slave relations depend on the
1397 * beacon interval, which was not known when the AP interface was added.
1399 if (vif->type == NL80211_IFTYPE_AP)
1400 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1402 /* Add the mac context */
1403 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1407 /* Perform the binding */
1408 ret = iwl_mvm_binding_add_vif(mvm, vif);
1412 /* Send the bcast station. At this stage the TBTT and DTIM time events
1413 * are added and applied to the scheduler */
1414 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
1418 /* must be set before quota calculations */
1419 mvmvif->ap_ibss_active = true;
1421 /* power updated needs to be done before quotas */
1422 iwl_mvm_power_update_mac(mvm, vif);
1424 ret = iwl_mvm_update_quotas(mvm, vif);
1426 goto out_quota_failed;
1428 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1429 if (vif->p2p && mvm->p2p_device_vif)
1430 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
1432 iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1434 iwl_mvm_bt_coex_vif_change(mvm);
1436 mutex_unlock(&mvm->mutex);
1440 iwl_mvm_power_update_mac(mvm, vif);
1441 mvmvif->ap_ibss_active = false;
1442 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1444 iwl_mvm_binding_remove_vif(mvm, vif);
1446 iwl_mvm_mac_ctxt_remove(mvm, vif);
1448 mutex_unlock(&mvm->mutex);
1452 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1453 struct ieee80211_vif *vif)
1455 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1456 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1458 iwl_mvm_prepare_mac_removal(mvm, vif);
1460 mutex_lock(&mvm->mutex);
1462 mvmvif->ap_ibss_active = false;
1464 iwl_mvm_bt_coex_vif_change(mvm);
1466 iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
1468 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1469 if (vif->p2p && mvm->p2p_device_vif)
1470 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
1472 iwl_mvm_update_quotas(mvm, NULL);
1473 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1474 iwl_mvm_binding_remove_vif(mvm, vif);
1476 iwl_mvm_power_update_mac(mvm, vif);
1478 iwl_mvm_mac_ctxt_remove(mvm, vif);
1480 mutex_unlock(&mvm->mutex);
1484 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1485 struct ieee80211_vif *vif,
1486 struct ieee80211_bss_conf *bss_conf,
1489 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1491 /* Changes will be applied when the AP/IBSS is started */
1492 if (!mvmvif->ap_ibss_active)
1495 if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
1496 BSS_CHANGED_BANDWIDTH) &&
1497 iwl_mvm_mac_ctxt_changed(mvm, vif))
1498 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1500 /* Need to send a new beacon template to the FW */
1501 if (changes & BSS_CHANGED_BEACON &&
1502 iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1503 IWL_WARN(mvm, "Failed updating beacon data\n");
1506 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1507 struct ieee80211_vif *vif,
1508 struct ieee80211_bss_conf *bss_conf,
1511 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1513 mutex_lock(&mvm->mutex);
1515 switch (vif->type) {
1516 case NL80211_IFTYPE_STATION:
1517 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1519 case NL80211_IFTYPE_AP:
1520 case NL80211_IFTYPE_ADHOC:
1521 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1524 /* shouldn't happen */
1528 mutex_unlock(&mvm->mutex);
1531 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1532 struct ieee80211_vif *vif,
1533 struct cfg80211_scan_request *req)
1535 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1538 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
1541 mutex_lock(&mvm->mutex);
1543 switch (mvm->scan_status) {
1544 case IWL_MVM_SCAN_SCHED:
1545 ret = iwl_mvm_sched_scan_stop(mvm);
1551 case IWL_MVM_SCAN_NONE:
1558 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1560 ret = iwl_mvm_scan_request(mvm, vif, req);
1562 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1564 mutex_unlock(&mvm->mutex);
1565 /* make sure to flush the Rx handler before the next scan arrives */
1566 iwl_mvm_wait_for_async_handlers(mvm);
1570 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1571 struct ieee80211_vif *vif)
1573 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1575 mutex_lock(&mvm->mutex);
1577 iwl_mvm_cancel_scan(mvm);
1579 mutex_unlock(&mvm->mutex);
1583 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1584 struct ieee80211_sta *sta, u16 tids,
1586 enum ieee80211_frame_release_type reason,
1589 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1591 /* Called when we need to transmit (a) frame(s) from mac80211 */
1593 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1594 tids, more_data, false);
1598 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
1599 struct ieee80211_sta *sta, u16 tids,
1601 enum ieee80211_frame_release_type reason,
1604 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1606 /* Called when we need to transmit (a) frame(s) from agg queue */
1608 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1609 tids, more_data, true);
1612 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1613 struct ieee80211_vif *vif,
1614 enum sta_notify_cmd cmd,
1615 struct ieee80211_sta *sta)
1617 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1618 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1622 case STA_NOTIFY_SLEEP:
1623 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1624 ieee80211_sta_block_awake(hw, sta, true);
1625 spin_lock_bh(&mvmsta->lock);
1626 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1627 struct iwl_mvm_tid_data *tid_data;
1629 tid_data = &mvmsta->tid_data[tid];
1630 if (tid_data->state != IWL_AGG_ON &&
1631 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
1633 if (iwl_mvm_tid_queued(tid_data) == 0)
1635 ieee80211_sta_set_buffered(sta, tid, true);
1637 spin_unlock_bh(&mvmsta->lock);
1639 * The fw updates the STA to be asleep. Tx packets on the Tx
1640 * queues to this station will not be transmitted. The fw will
1641 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1644 case STA_NOTIFY_AWAKE:
1645 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1647 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1654 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
1655 struct ieee80211_vif *vif,
1656 struct ieee80211_sta *sta)
1658 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1659 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1662 * This is called before mac80211 does RCU synchronisation,
1663 * so here we already invalidate our internal RCU-protected
1664 * station pointer. The rest of the code will thus no longer
1665 * be able to find the station this way, and we don't rely
1666 * on further RCU synchronisation after the sta_state()
1667 * callback deleted the station.
1669 mutex_lock(&mvm->mutex);
1670 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
1671 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1673 mutex_unlock(&mvm->mutex);
1676 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1677 struct ieee80211_vif *vif,
1678 struct ieee80211_sta *sta,
1679 enum ieee80211_sta_state old_state,
1680 enum ieee80211_sta_state new_state)
1682 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1683 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1686 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1687 sta->addr, old_state, new_state);
1689 /* this would be a mac80211 bug ... but don't crash */
1690 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1693 /* if a STA is being removed, reuse its ID */
1694 flush_work(&mvm->sta_drained_wk);
1696 mutex_lock(&mvm->mutex);
1697 if (old_state == IEEE80211_STA_NOTEXIST &&
1698 new_state == IEEE80211_STA_NONE) {
1700 * Firmware bug - it'll crash if the beacon interval is less
1701 * than 16. We can't avoid connecting at all, so refuse the
1702 * station state change, this will cause mac80211 to abandon
1703 * attempts to connect to this AP, and eventually wpa_s will
1704 * blacklist the AP...
1706 if (vif->type == NL80211_IFTYPE_STATION &&
1707 vif->bss_conf.beacon_int < 16) {
1709 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1710 sta->addr, vif->bss_conf.beacon_int);
1714 ret = iwl_mvm_add_sta(mvm, vif, sta);
1715 } else if (old_state == IEEE80211_STA_NONE &&
1716 new_state == IEEE80211_STA_AUTH) {
1718 * EBS may be disabled due to previous failures reported by FW.
1719 * Reset EBS status here assuming environment has been changed.
1721 mvm->last_ebs_successful = true;
1723 } else if (old_state == IEEE80211_STA_AUTH &&
1724 new_state == IEEE80211_STA_ASSOC) {
1725 ret = iwl_mvm_update_sta(mvm, vif, sta);
1727 iwl_mvm_rs_rate_init(mvm, sta,
1728 mvmvif->phy_ctxt->channel->band,
1730 } else if (old_state == IEEE80211_STA_ASSOC &&
1731 new_state == IEEE80211_STA_AUTHORIZED) {
1732 /* enable beacon filtering */
1733 if (vif->bss_conf.dtim_period)
1734 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif,
1737 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1738 new_state == IEEE80211_STA_ASSOC) {
1739 /* disable beacon filtering */
1740 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, CMD_SYNC));
1742 } else if (old_state == IEEE80211_STA_ASSOC &&
1743 new_state == IEEE80211_STA_AUTH) {
1745 } else if (old_state == IEEE80211_STA_AUTH &&
1746 new_state == IEEE80211_STA_NONE) {
1748 } else if (old_state == IEEE80211_STA_NONE &&
1749 new_state == IEEE80211_STA_NOTEXIST) {
1750 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1755 mutex_unlock(&mvm->mutex);
1760 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1762 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1764 mvm->rts_threshold = value;
1769 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
1770 struct ieee80211_vif *vif,
1771 struct ieee80211_sta *sta, u32 changed)
1773 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1775 if (vif->type == NL80211_IFTYPE_STATION &&
1776 changed & IEEE80211_RC_NSS_CHANGED)
1777 iwl_mvm_sf_update(mvm, vif, false);
1780 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1781 struct ieee80211_vif *vif, u16 ac,
1782 const struct ieee80211_tx_queue_params *params)
1784 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1785 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1787 mvmvif->queue_params[ac] = *params;
1790 * No need to update right away, we'll get BSS_CHANGED_QOS
1791 * The exception is P2P_DEVICE interface which needs immediate update.
1793 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1796 mutex_lock(&mvm->mutex);
1797 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1798 mutex_unlock(&mvm->mutex);
1804 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1805 struct ieee80211_vif *vif)
1807 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1808 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1809 200 + vif->bss_conf.beacon_int);
1810 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1811 100 + vif->bss_conf.beacon_int);
1813 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1816 mutex_lock(&mvm->mutex);
1817 /* Try really hard to protect the session and hear a beacon */
1818 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
1819 mutex_unlock(&mvm->mutex);
1822 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
1823 struct ieee80211_vif *vif,
1824 struct cfg80211_sched_scan_request *req,
1825 struct ieee80211_sched_scan_ies *ies)
1827 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1830 mutex_lock(&mvm->mutex);
1832 if (!iwl_mvm_is_idle(mvm)) {
1837 switch (mvm->scan_status) {
1838 case IWL_MVM_SCAN_OS:
1839 IWL_DEBUG_SCAN(mvm, "Stopping previous scan for sched_scan\n");
1840 ret = iwl_mvm_cancel_scan(mvm);
1847 * iwl_mvm_rx_scan_complete() will be called soon but will
1848 * not reset the scan status as it won't be IWL_MVM_SCAN_OS
1849 * any more since we queue the next scan immediately (below).
1850 * We make sure it is called before the next scan starts by
1851 * flushing the async-handlers work.
1854 case IWL_MVM_SCAN_NONE:
1861 mvm->scan_status = IWL_MVM_SCAN_SCHED;
1863 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1867 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1871 ret = iwl_mvm_sched_scan_start(mvm, req);
1875 mvm->scan_status = IWL_MVM_SCAN_NONE;
1877 mutex_unlock(&mvm->mutex);
1878 /* make sure to flush the Rx handler before the next scan arrives */
1879 iwl_mvm_wait_for_async_handlers(mvm);
1883 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
1884 struct ieee80211_vif *vif)
1886 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1889 mutex_lock(&mvm->mutex);
1890 ret = iwl_mvm_sched_scan_stop(mvm);
1891 mutex_unlock(&mvm->mutex);
1892 iwl_mvm_wait_for_async_handlers(mvm);
1897 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1898 enum set_key_cmd cmd,
1899 struct ieee80211_vif *vif,
1900 struct ieee80211_sta *sta,
1901 struct ieee80211_key_conf *key)
1903 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1906 if (iwlwifi_mod_params.sw_crypto) {
1907 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1911 switch (key->cipher) {
1912 case WLAN_CIPHER_SUITE_TKIP:
1913 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1915 case WLAN_CIPHER_SUITE_CCMP:
1916 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1918 case WLAN_CIPHER_SUITE_AES_CMAC:
1919 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1921 case WLAN_CIPHER_SUITE_WEP40:
1922 case WLAN_CIPHER_SUITE_WEP104:
1924 * Support for TX only, at least for now, so accept
1925 * the key and do nothing else. Then mac80211 will
1926 * pass it for TX but we don't have to use it for RX.
1930 /* currently FW supports only one optional cipher scheme */
1931 if (hw->n_cipher_schemes &&
1932 hw->cipher_schemes->cipher == key->cipher)
1933 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
1938 mutex_lock(&mvm->mutex);
1942 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1943 vif->type == NL80211_IFTYPE_AP) && !sta) {
1945 * GTK on AP interface is a TX-only key, return 0;
1946 * on IBSS they're per-station and because we're lazy
1947 * we don't support them for RX, so do the same.
1950 key->hw_key_idx = STA_KEY_IDX_INVALID;
1954 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1955 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1957 IWL_WARN(mvm, "set key failed\n");
1959 * can't add key for RX, but we don't need it
1960 * in the device for TX so still return 0
1962 key->hw_key_idx = STA_KEY_IDX_INVALID;
1968 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1973 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1974 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1980 mutex_unlock(&mvm->mutex);
1984 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1985 struct ieee80211_vif *vif,
1986 struct ieee80211_key_conf *keyconf,
1987 struct ieee80211_sta *sta,
1988 u32 iv32, u16 *phase1key)
1990 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1992 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1995 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1999 static int iwl_mvm_roc(struct ieee80211_hw *hw,
2000 struct ieee80211_vif *vif,
2001 struct ieee80211_channel *channel,
2003 enum ieee80211_roc_type type)
2005 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2006 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2007 struct cfg80211_chan_def chandef;
2008 struct iwl_mvm_phy_ctxt *phy_ctxt;
2011 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2014 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
2015 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
2019 mutex_lock(&mvm->mutex);
2021 for (i = 0; i < NUM_PHY_CTX; i++) {
2022 phy_ctxt = &mvm->phy_ctxts[i];
2023 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2026 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2028 * Unbind the P2P_DEVICE from the current PHY context,
2029 * and if the PHY context is not used remove it.
2031 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2032 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2035 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2037 /* Bind the P2P_DEVICE to the current PHY Context */
2038 mvmvif->phy_ctxt = phy_ctxt;
2040 ret = iwl_mvm_binding_add_vif(mvm, vif);
2041 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2044 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2045 goto schedule_time_event;
2049 /* Need to update the PHY context only if the ROC channel changed */
2050 if (channel == mvmvif->phy_ctxt->channel)
2051 goto schedule_time_event;
2053 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2056 * Change the PHY context configuration as it is currently referenced
2057 * only by the P2P Device MAC
2059 if (mvmvif->phy_ctxt->ref == 1) {
2060 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2066 * The PHY context is shared with other MACs. Need to remove the
2067 * P2P Device from the binding, allocate an new PHY context and
2068 * create a new binding
2070 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2076 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2079 IWL_ERR(mvm, "Failed to change PHY context\n");
2083 /* Unbind the P2P_DEVICE from the current PHY context */
2084 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2085 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2088 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2090 /* Bind the P2P_DEVICE to the new allocated PHY context */
2091 mvmvif->phy_ctxt = phy_ctxt;
2093 ret = iwl_mvm_binding_add_vif(mvm, vif);
2094 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2097 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2100 schedule_time_event:
2101 /* Schedule the time events */
2102 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2105 mutex_unlock(&mvm->mutex);
2106 IWL_DEBUG_MAC80211(mvm, "leave\n");
2110 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2112 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2114 IWL_DEBUG_MAC80211(mvm, "enter\n");
2116 mutex_lock(&mvm->mutex);
2117 iwl_mvm_stop_p2p_roc(mvm);
2118 mutex_unlock(&mvm->mutex);
2120 IWL_DEBUG_MAC80211(mvm, "leave\n");
2124 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2125 struct ieee80211_chanctx_conf *ctx)
2127 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2128 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2129 struct iwl_mvm_phy_ctxt *phy_ctxt;
2132 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2134 mutex_lock(&mvm->mutex);
2135 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2141 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2142 ctx->rx_chains_static,
2143 ctx->rx_chains_dynamic);
2145 IWL_ERR(mvm, "Failed to add PHY context\n");
2149 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
2150 *phy_ctxt_id = phy_ctxt->id;
2152 mutex_unlock(&mvm->mutex);
2156 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2157 struct ieee80211_chanctx_conf *ctx)
2159 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2160 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2161 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2163 mutex_lock(&mvm->mutex);
2164 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2165 mutex_unlock(&mvm->mutex);
2168 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
2169 struct ieee80211_chanctx_conf *ctx,
2172 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2173 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2174 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2176 if (WARN_ONCE((phy_ctxt->ref > 1) &&
2177 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
2178 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2179 IEEE80211_CHANCTX_CHANGE_RADAR |
2180 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
2181 "Cannot change PHY. Ref=%d, changed=0x%X\n",
2182 phy_ctxt->ref, changed))
2185 mutex_lock(&mvm->mutex);
2186 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2187 ctx->rx_chains_static,
2188 ctx->rx_chains_dynamic);
2189 iwl_mvm_bt_coex_vif_change(mvm);
2190 mutex_unlock(&mvm->mutex);
2193 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
2194 struct ieee80211_vif *vif,
2195 struct ieee80211_chanctx_conf *ctx)
2197 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2198 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2199 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2200 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2203 mutex_lock(&mvm->mutex);
2205 mvmvif->phy_ctxt = phy_ctxt;
2207 switch (vif->type) {
2208 case NL80211_IFTYPE_AP:
2209 /* Unless it's a CSA flow we have nothing to do here */
2210 if (vif->csa_active) {
2211 mvmvif->ap_ibss_active = true;
2214 case NL80211_IFTYPE_ADHOC:
2216 * The AP binding flow is handled as part of the start_ap flow
2217 * (in bss_info_changed), similarly for IBSS.
2221 case NL80211_IFTYPE_STATION:
2222 case NL80211_IFTYPE_MONITOR:
2229 ret = iwl_mvm_binding_add_vif(mvm, vif);
2234 * Power state must be updated before quotas,
2235 * otherwise fw will complain.
2237 iwl_mvm_power_update_mac(mvm, vif);
2239 /* Setting the quota at this stage is only required for monitor
2240 * interfaces. For the other types, the bss_info changed flow
2241 * will handle quota settings.
2243 if (vif->type == NL80211_IFTYPE_MONITOR) {
2244 mvmvif->monitor_active = true;
2245 ret = iwl_mvm_update_quotas(mvm, vif);
2247 goto out_remove_binding;
2250 /* Handle binding during CSA */
2251 if (vif->type == NL80211_IFTYPE_AP) {
2252 iwl_mvm_update_quotas(mvm, vif);
2253 iwl_mvm_mac_ctxt_changed(mvm, vif);
2259 iwl_mvm_binding_remove_vif(mvm, vif);
2260 iwl_mvm_power_update_mac(mvm, vif);
2262 mutex_unlock(&mvm->mutex);
2264 mvmvif->phy_ctxt = NULL;
2268 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
2269 struct ieee80211_vif *vif,
2270 struct ieee80211_chanctx_conf *ctx)
2272 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2275 mutex_lock(&mvm->mutex);
2277 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
2279 switch (vif->type) {
2280 case NL80211_IFTYPE_ADHOC:
2282 case NL80211_IFTYPE_MONITOR:
2283 mvmvif->monitor_active = false;
2284 iwl_mvm_update_quotas(mvm, NULL);
2286 case NL80211_IFTYPE_AP:
2287 /* This part is triggered only during CSA */
2288 if (!vif->csa_active || !mvmvif->ap_ibss_active)
2291 mvmvif->ap_ibss_active = false;
2292 iwl_mvm_update_quotas(mvm, NULL);
2293 /*TODO: bt_coex notification here? */
2298 iwl_mvm_binding_remove_vif(mvm, vif);
2299 iwl_mvm_power_update_mac(mvm, vif);
2302 mvmvif->phy_ctxt = NULL;
2303 mutex_unlock(&mvm->mutex);
2306 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
2307 struct ieee80211_sta *sta,
2310 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2311 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
2313 if (!mvm_sta || !mvm_sta->vif) {
2314 IWL_ERR(mvm, "Station is not associated to a vif\n");
2318 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
2321 #ifdef CONFIG_NL80211_TESTMODE
2322 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
2323 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
2324 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
2325 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
2328 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
2329 struct ieee80211_vif *vif,
2330 void *data, int len)
2332 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
2336 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
2340 if (!tb[IWL_MVM_TM_ATTR_CMD])
2343 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
2344 case IWL_MVM_TM_CMD_SET_NOA:
2345 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
2346 !vif->bss_conf.enable_beacon ||
2347 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
2350 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
2351 if (noa_duration >= vif->bss_conf.beacon_int)
2354 mvm->noa_duration = noa_duration;
2357 return iwl_mvm_update_quotas(mvm, NULL);
2358 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
2359 /* must be associated client vif - ignore authorized */
2360 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
2361 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
2362 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
2365 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
2366 return iwl_mvm_enable_beacon_filter(mvm, vif,
2368 return iwl_mvm_disable_beacon_filter(mvm, vif, CMD_SYNC);
2374 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
2375 struct ieee80211_vif *vif,
2376 void *data, int len)
2378 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2381 mutex_lock(&mvm->mutex);
2382 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
2383 mutex_unlock(&mvm->mutex);
2389 static void iwl_mvm_channel_switch_beacon(struct ieee80211_hw *hw,
2390 struct ieee80211_vif *vif,
2391 struct cfg80211_chan_def *chandef)
2393 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2395 mutex_lock(&mvm->mutex);
2396 if (WARN(mvm->csa_vif && mvm->csa_vif->csa_active,
2397 "Another CSA is already in progress"))
2400 IWL_DEBUG_MAC80211(mvm, "CSA started to freq %d\n",
2401 chandef->center_freq1);
2405 mutex_unlock(&mvm->mutex);
2408 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
2409 struct ieee80211_vif *vif, u32 queues, bool drop)
2411 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2412 struct iwl_mvm_vif *mvmvif;
2413 struct iwl_mvm_sta *mvmsta;
2415 if (!vif || vif->type != NL80211_IFTYPE_STATION)
2418 mutex_lock(&mvm->mutex);
2419 mvmvif = iwl_mvm_vif_from_mac80211(vif);
2420 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, mvmvif->ap_sta_id);
2422 if (WARN_ON_ONCE(!mvmsta))
2426 if (iwl_mvm_flush_tx_path(mvm, mvmsta->tfd_queue_msk, true))
2427 IWL_ERR(mvm, "flush request fail\n");
2429 iwl_trans_wait_tx_queue_empty(mvm->trans,
2430 mvmsta->tfd_queue_msk);
2433 mutex_unlock(&mvm->mutex);
2436 const struct ieee80211_ops iwl_mvm_hw_ops = {
2437 .tx = iwl_mvm_mac_tx,
2438 .ampdu_action = iwl_mvm_mac_ampdu_action,
2439 .start = iwl_mvm_mac_start,
2440 .restart_complete = iwl_mvm_mac_restart_complete,
2441 .stop = iwl_mvm_mac_stop,
2442 .add_interface = iwl_mvm_mac_add_interface,
2443 .remove_interface = iwl_mvm_mac_remove_interface,
2444 .config = iwl_mvm_mac_config,
2445 .prepare_multicast = iwl_mvm_prepare_multicast,
2446 .configure_filter = iwl_mvm_configure_filter,
2447 .bss_info_changed = iwl_mvm_bss_info_changed,
2448 .hw_scan = iwl_mvm_mac_hw_scan,
2449 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
2450 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
2451 .sta_state = iwl_mvm_mac_sta_state,
2452 .sta_notify = iwl_mvm_mac_sta_notify,
2453 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
2454 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
2455 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
2456 .sta_rc_update = iwl_mvm_sta_rc_update,
2457 .conf_tx = iwl_mvm_mac_conf_tx,
2458 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
2459 .flush = iwl_mvm_mac_flush,
2460 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
2461 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
2462 .set_key = iwl_mvm_mac_set_key,
2463 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
2464 .remain_on_channel = iwl_mvm_roc,
2465 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
2466 .add_chanctx = iwl_mvm_add_chanctx,
2467 .remove_chanctx = iwl_mvm_remove_chanctx,
2468 .change_chanctx = iwl_mvm_change_chanctx,
2469 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
2470 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
2472 .start_ap = iwl_mvm_start_ap_ibss,
2473 .stop_ap = iwl_mvm_stop_ap_ibss,
2474 .join_ibss = iwl_mvm_start_ap_ibss,
2475 .leave_ibss = iwl_mvm_stop_ap_ibss,
2477 .set_tim = iwl_mvm_set_tim,
2479 .channel_switch_beacon = iwl_mvm_channel_switch_beacon,
2481 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
2483 #ifdef CONFIG_PM_SLEEP
2485 .suspend = iwl_mvm_suspend,
2486 .resume = iwl_mvm_resume,
2487 .set_wakeup = iwl_mvm_set_wakeup,
2488 .set_rekey_data = iwl_mvm_set_rekey_data,
2489 #if IS_ENABLED(CONFIG_IPV6)
2490 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
2492 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,