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_DYNAMIC_SMPS |
280 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
282 hw->queues = mvm->first_agg_queue;
283 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
284 hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
285 IEEE80211_RADIOTAP_MCS_HAVE_STBC;
286 hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
287 hw->rate_control_algorithm = "iwl-mvm-rs";
290 * Enable 11w if advertised by firmware and software crypto
291 * is not enabled (as the firmware will interpret some mgmt
292 * packets, so enabling it with software crypto isn't safe)
294 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
295 !iwlwifi_mod_params.sw_crypto)
296 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
298 if (0 && mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT) {
299 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
300 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
301 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
304 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
305 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
306 hw->chanctx_data_size = sizeof(u16);
308 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
309 BIT(NL80211_IFTYPE_P2P_CLIENT) |
310 BIT(NL80211_IFTYPE_AP) |
311 BIT(NL80211_IFTYPE_P2P_GO) |
312 BIT(NL80211_IFTYPE_P2P_DEVICE);
314 /* IBSS has bugs in older versions */
315 if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 8)
316 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
318 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
319 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
320 REGULATORY_DISABLE_BEACON_HINTS;
322 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
323 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
325 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
326 hw->wiphy->n_iface_combinations =
327 ARRAY_SIZE(iwl_mvm_iface_combinations);
329 hw->wiphy->max_remain_on_channel_duration = 10000;
330 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
332 /* Extract MAC address */
333 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
334 hw->wiphy->addresses = mvm->addresses;
335 hw->wiphy->n_addresses = 1;
337 /* Extract additional MAC addresses if available */
338 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
339 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
341 for (i = 1; i < num_mac; i++) {
342 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
344 mvm->addresses[i].addr[5]++;
345 hw->wiphy->n_addresses++;
348 iwl_mvm_reset_phy_ctxts(mvm);
350 hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
352 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
354 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
355 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
356 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
357 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
358 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
359 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
361 hw->wiphy->hw_version = mvm->trans->hw_id;
363 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
364 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
366 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
368 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SCHED_SCAN) {
369 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
370 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
371 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
372 /* we create the 802.11 header and zero length SSID IE. */
373 hw->wiphy->max_sched_scan_ie_len =
374 SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
377 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
378 NL80211_FEATURE_P2P_GO_OPPPS;
380 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
382 /* currently FW API supports only one optional cipher scheme */
383 if (mvm->fw->cs[0].cipher) {
384 mvm->hw->n_cipher_schemes = 1;
385 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
388 #ifdef CONFIG_PM_SLEEP
389 if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
390 mvm->trans->ops->d3_suspend &&
391 mvm->trans->ops->d3_resume &&
392 device_can_wakeup(mvm->trans->dev)) {
393 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
394 WIPHY_WOWLAN_DISCONNECT |
395 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
396 WIPHY_WOWLAN_RFKILL_RELEASE;
397 if (!iwlwifi_mod_params.sw_crypto)
398 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
399 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
400 WIPHY_WOWLAN_4WAY_HANDSHAKE;
402 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
403 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
404 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
405 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
406 hw->wiphy->wowlan = &mvm->wowlan;
410 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
411 /* assign default bcast filtering configuration */
412 mvm->bcast_filters = iwl_mvm_default_bcast_filters;
415 ret = iwl_mvm_leds_init(mvm);
419 ret = ieee80211_register_hw(mvm->hw);
421 iwl_mvm_leds_exit(mvm);
426 static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
427 struct ieee80211_sta *sta,
430 struct iwl_mvm_sta *mvmsta;
434 * double check the IN_D0I3 flag both before and after
435 * taking the spinlock, in order to prevent taking
436 * the spinlock when not needed.
438 if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
441 spin_lock(&mvm->d0i3_tx_lock);
443 * testing the flag again ensures the skb dequeue
444 * loop (on d0i3 exit) hasn't run yet.
446 if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
449 mvmsta = iwl_mvm_sta_from_mac80211(sta);
450 if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
451 mvmsta->sta_id != mvm->d0i3_ap_sta_id)
454 __skb_queue_tail(&mvm->d0i3_tx, skb);
455 ieee80211_stop_queues(mvm->hw);
458 iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
459 iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
463 spin_unlock(&mvm->d0i3_tx_lock);
467 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
468 struct ieee80211_tx_control *control,
471 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
472 struct ieee80211_sta *sta = control->sta;
473 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
474 struct ieee80211_hdr *hdr = (void *)skb->data;
476 if (iwl_mvm_is_radio_killed(mvm)) {
477 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
481 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
482 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
485 /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
486 if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
487 ieee80211_is_mgmt(hdr->frame_control) &&
488 !ieee80211_is_deauth(hdr->frame_control) &&
489 !ieee80211_is_disassoc(hdr->frame_control) &&
490 !ieee80211_is_action(hdr->frame_control)))
494 if (iwl_mvm_defer_tx(mvm, sta, skb))
496 if (iwl_mvm_tx_skb(mvm, skb, sta))
501 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
505 ieee80211_free_txskb(hw, skb);
508 static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
510 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
515 static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
517 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
519 if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
522 /* enabled by default */
526 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
527 struct ieee80211_vif *vif,
528 enum ieee80211_ampdu_mlme_action action,
529 struct ieee80211_sta *sta, u16 tid,
530 u16 *ssn, u8 buf_size)
532 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
534 bool tx_agg_ref = false;
536 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
537 sta->addr, tid, action);
539 if (!(mvm->nvm_data->sku_cap_11n_enable))
542 /* return from D0i3 before starting a new Tx aggregation */
543 if (action == IEEE80211_AMPDU_TX_START) {
544 iwl_mvm_ref(mvm, IWL_MVM_REF_TX_AGG);
548 * wait synchronously until D0i3 exit to get the correct
549 * sequence number for the tid
551 if (!wait_event_timeout(mvm->d0i3_exit_waitq,
552 !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status), HZ)) {
554 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
559 mutex_lock(&mvm->mutex);
562 case IEEE80211_AMPDU_RX_START:
563 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
567 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
569 case IEEE80211_AMPDU_RX_STOP:
570 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
572 case IEEE80211_AMPDU_TX_START:
573 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
577 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
579 case IEEE80211_AMPDU_TX_STOP_CONT:
580 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
582 case IEEE80211_AMPDU_TX_STOP_FLUSH:
583 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
584 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
586 case IEEE80211_AMPDU_TX_OPERATIONAL:
587 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
594 mutex_unlock(&mvm->mutex);
597 * If the tid is marked as started, we won't use it for offloaded
598 * traffic on the next D0i3 entry. It's safe to unref.
601 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
606 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
607 struct ieee80211_vif *vif)
609 struct iwl_mvm *mvm = data;
610 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
612 mvmvif->uploaded = false;
613 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
615 /* does this make sense at all? */
618 spin_lock_bh(&mvm->time_event_lock);
619 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
620 spin_unlock_bh(&mvm->time_event_lock);
622 mvmvif->phy_ctxt = NULL;
625 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
627 #ifdef CONFIG_IWLWIFI_DEBUGFS
628 static char *env[] = { "DRIVER=iwlwifi", "EVENT=error_dump", NULL };
630 iwl_mvm_fw_error_dump(mvm);
632 /* notify the userspace about the error we had */
633 kobject_uevent_env(&mvm->hw->wiphy->dev.kobj, KOBJ_CHANGE, env);
636 iwl_trans_stop_device(mvm->trans);
638 mvm->scan_status = IWL_MVM_SCAN_NONE;
640 /* just in case one was running */
641 ieee80211_remain_on_channel_expired(mvm->hw);
643 ieee80211_iterate_active_interfaces_atomic(
644 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
645 iwl_mvm_cleanup_iterator, mvm);
647 mvm->p2p_device_vif = NULL;
648 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
650 iwl_mvm_reset_phy_ctxts(mvm);
651 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
652 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
654 ieee80211_wake_queues(mvm->hw);
656 /* cleanup all stale references (scan, roc), but keep the
657 * ucode_down ref until reconfig is complete */
658 iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
661 mvm->rx_ba_sessions = 0;
664 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
666 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
669 mutex_lock(&mvm->mutex);
671 /* Clean up some internal and mac80211 state on restart */
672 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
673 iwl_mvm_restart_cleanup(mvm);
675 ret = iwl_mvm_up(mvm);
676 mutex_unlock(&mvm->mutex);
681 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
683 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
686 mutex_lock(&mvm->mutex);
688 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
689 iwl_mvm_d0i3_enable_tx(mvm, NULL);
690 ret = iwl_mvm_update_quotas(mvm, NULL);
692 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
695 /* allow transport/FW low power modes */
696 iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
698 mutex_unlock(&mvm->mutex);
701 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
703 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
705 flush_work(&mvm->d0i3_exit_work);
706 flush_work(&mvm->async_handlers_wk);
708 mutex_lock(&mvm->mutex);
710 /* disallow low power states when the FW is down */
711 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
713 /* async_handlers_wk is now blocked */
716 * The work item could be running or queued if the
717 * ROC time event stops just as we get here.
719 cancel_work_sync(&mvm->roc_done_wk);
721 iwl_trans_stop_device(mvm->trans);
723 iwl_mvm_async_handlers_purge(mvm);
724 /* async_handlers_list is empty and will stay empty: HW is stopped */
726 /* the fw is stopped, the aux sta is dead: clean up driver state */
727 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
729 mutex_unlock(&mvm->mutex);
732 * The worker might have been waiting for the mutex, let it run and
733 * discover that its list is now empty.
735 cancel_work_sync(&mvm->async_handlers_wk);
738 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
742 lockdep_assert_held(&mvm->mutex);
744 for (i = 0; i < NUM_PHY_CTX; i++)
745 if (!mvm->phy_ctxts[i].ref)
746 return &mvm->phy_ctxts[i];
748 IWL_ERR(mvm, "No available PHY context\n");
752 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
755 /* FW is in charge of regulatory enforcement */
756 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
757 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
758 .pwr_restriction = cpu_to_le16(tx_power),
761 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
762 sizeof(reduce_txpwr_cmd),
766 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
767 struct ieee80211_vif *vif)
769 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
770 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
774 * Not much to do here. The stack will not allow interface
775 * types or combinations that we didn't advertise, so we
776 * don't really have to check the types.
779 mutex_lock(&mvm->mutex);
781 /* Allocate resources for the MAC context, and add it to the fw */
782 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
786 /* Counting number of interfaces is needed for legacy PM */
787 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
791 * The AP binding flow can be done only after the beacon
792 * template is configured (which happens only in the mac80211
793 * start_ap() flow), and adding the broadcast station can happen
794 * only after the binding.
795 * In addition, since modifying the MAC before adding a bcast
796 * station is not allowed by the FW, delay the adding of MAC context to
797 * the point where we can also add the bcast station.
798 * In short: there's not much we can do at this point, other than
799 * allocating resources :)
801 if (vif->type == NL80211_IFTYPE_AP ||
802 vif->type == NL80211_IFTYPE_ADHOC) {
803 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
804 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
806 ieee80211_vif_type_p2p(vif));
808 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
812 iwl_mvm_vif_dbgfs_register(mvm, vif);
816 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
820 ret = iwl_mvm_power_update_mac(mvm, vif);
824 /* beacon filtering */
825 ret = iwl_mvm_disable_beacon_filter(mvm, vif, CMD_SYNC);
829 if (!mvm->bf_allowed_vif &&
830 vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
831 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
832 mvm->bf_allowed_vif = mvmvif;
833 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
834 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
838 * P2P_DEVICE interface does not have a channel context assigned to it,
839 * so a dedicated PHY context is allocated to it and the corresponding
840 * MAC context is bound to it at this stage.
842 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
844 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
845 if (!mvmvif->phy_ctxt) {
850 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
851 ret = iwl_mvm_binding_add_vif(mvm, vif);
855 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
859 /* Save a pointer to p2p device vif, so it can later be used to
860 * update the p2p device MAC when a GO is started/stopped */
861 mvm->p2p_device_vif = vif;
864 iwl_mvm_vif_dbgfs_register(mvm, vif);
868 iwl_mvm_binding_remove_vif(mvm, vif);
870 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
872 if (mvm->bf_allowed_vif == mvmvif) {
873 mvm->bf_allowed_vif = NULL;
874 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
875 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
878 mvmvif->phy_ctxt = NULL;
879 iwl_mvm_mac_ctxt_remove(mvm, vif);
881 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
884 iwl_mvm_mac_ctxt_release(mvm, vif);
886 mutex_unlock(&mvm->mutex);
891 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
892 struct ieee80211_vif *vif)
896 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
897 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
898 tfd_msk |= BIT(vif->hw_queue[ac]);
900 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
901 tfd_msk |= BIT(vif->cab_queue);
904 mutex_lock(&mvm->mutex);
905 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
906 mutex_unlock(&mvm->mutex);
909 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
911 * Flush the ROC worker which will flush the OFFCHANNEL queue.
912 * We assume here that all the packets sent to the OFFCHANNEL
913 * queue are sent in ROC session.
915 flush_work(&mvm->roc_done_wk);
918 * By now, all the AC queues are empty. The AGG queues are
919 * empty too. We already got all the Tx responses for all the
920 * packets in the queues. The drain work can have been
921 * triggered. Flush it.
923 flush_work(&mvm->sta_drained_wk);
927 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
928 struct ieee80211_vif *vif)
930 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
931 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
933 iwl_mvm_prepare_mac_removal(mvm, vif);
935 mutex_lock(&mvm->mutex);
937 if (mvm->bf_allowed_vif == mvmvif) {
938 mvm->bf_allowed_vif = NULL;
939 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
940 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
943 iwl_mvm_vif_dbgfs_clean(mvm, vif);
946 * For AP/GO interface, the tear down of the resources allocated to the
947 * interface is be handled as part of the stop_ap flow.
949 if (vif->type == NL80211_IFTYPE_AP ||
950 vif->type == NL80211_IFTYPE_ADHOC) {
951 #ifdef CONFIG_NL80211_TESTMODE
952 if (vif == mvm->noa_vif) {
954 mvm->noa_duration = 0;
957 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
961 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
962 mvm->p2p_device_vif = NULL;
963 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
964 iwl_mvm_binding_remove_vif(mvm, vif);
965 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
966 mvmvif->phy_ctxt = NULL;
969 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
972 iwl_mvm_power_update_mac(mvm, vif);
973 iwl_mvm_mac_ctxt_remove(mvm, vif);
976 iwl_mvm_mac_ctxt_release(mvm, vif);
977 mutex_unlock(&mvm->mutex);
980 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
985 struct iwl_mvm_mc_iter_data {
990 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
991 struct ieee80211_vif *vif)
993 struct iwl_mvm_mc_iter_data *data = _data;
994 struct iwl_mvm *mvm = data->mvm;
995 struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
998 /* if we don't have free ports, mcast frames will be dropped */
999 if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1002 if (vif->type != NL80211_IFTYPE_STATION ||
1003 !vif->bss_conf.assoc)
1006 cmd->port_id = data->port_id++;
1007 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1008 len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1010 ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC, len, cmd);
1012 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1015 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1017 struct iwl_mvm_mc_iter_data iter_data = {
1021 lockdep_assert_held(&mvm->mutex);
1023 if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1026 ieee80211_iterate_active_interfaces(
1027 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1028 iwl_mvm_mc_iface_iterator, &iter_data);
1031 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1032 struct netdev_hw_addr_list *mc_list)
1034 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1035 struct iwl_mcast_filter_cmd *cmd;
1036 struct netdev_hw_addr *addr;
1037 int addr_count = netdev_hw_addr_list_count(mc_list);
1038 bool pass_all = false;
1041 if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
1046 len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1047 cmd = kzalloc(len, GFP_ATOMIC);
1053 return (u64)(unsigned long)cmd;
1056 netdev_hw_addr_list_for_each(addr, mc_list) {
1057 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1058 cmd->count, addr->addr);
1059 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1060 addr->addr, ETH_ALEN);
1064 return (u64)(unsigned long)cmd;
1067 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1068 unsigned int changed_flags,
1069 unsigned int *total_flags,
1072 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1073 struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1075 mutex_lock(&mvm->mutex);
1077 /* replace previous configuration */
1078 kfree(mvm->mcast_filter_cmd);
1079 mvm->mcast_filter_cmd = cmd;
1084 iwl_mvm_recalc_multicast(mvm);
1086 mutex_unlock(&mvm->mutex);
1090 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1091 struct iwl_bcast_iter_data {
1092 struct iwl_mvm *mvm;
1093 struct iwl_bcast_filter_cmd *cmd;
1098 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1099 const struct iwl_fw_bcast_filter *in_filter,
1100 struct iwl_fw_bcast_filter *out_filter)
1102 struct iwl_fw_bcast_filter_attr *attr;
1105 memcpy(out_filter, in_filter, sizeof(*out_filter));
1107 for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1108 attr = &out_filter->attrs[i];
1113 switch (attr->reserved1) {
1114 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1115 if (vif->bss_conf.arp_addr_cnt != 1) {
1120 attr->val = vif->bss_conf.arp_addr_list[0];
1122 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1123 attr->val = *(__be32 *)&vif->addr[2];
1128 attr->reserved1 = 0;
1129 out_filter->num_attrs++;
1133 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1134 struct ieee80211_vif *vif)
1136 struct iwl_bcast_iter_data *data = _data;
1137 struct iwl_mvm *mvm = data->mvm;
1138 struct iwl_bcast_filter_cmd *cmd = data->cmd;
1139 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1140 struct iwl_fw_bcast_mac *bcast_mac;
1143 if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1146 bcast_mac = &cmd->macs[mvmvif->id];
1148 /* enable filtering only for associated stations */
1149 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1152 bcast_mac->default_discard = 1;
1154 /* copy all configured filters */
1155 for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1157 * Make sure we don't exceed our filters limit.
1158 * if there is still a valid filter to be configured,
1159 * be on the safe side and just allow bcast for this mac.
1161 if (WARN_ON_ONCE(data->current_filter >=
1162 ARRAY_SIZE(cmd->filters))) {
1163 bcast_mac->default_discard = 0;
1164 bcast_mac->attached_filters = 0;
1168 iwl_mvm_set_bcast_filter(vif,
1169 &mvm->bcast_filters[i],
1170 &cmd->filters[data->current_filter]);
1172 /* skip current filter if it contains no attributes */
1173 if (!cmd->filters[data->current_filter].num_attrs)
1176 /* attach the filter to current mac */
1177 bcast_mac->attached_filters |=
1178 cpu_to_le16(BIT(data->current_filter));
1180 data->current_filter++;
1184 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1185 struct iwl_bcast_filter_cmd *cmd)
1187 struct iwl_bcast_iter_data iter_data = {
1192 memset(cmd, 0, sizeof(*cmd));
1193 cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1194 cmd->max_macs = ARRAY_SIZE(cmd->macs);
1196 #ifdef CONFIG_IWLWIFI_DEBUGFS
1197 /* use debugfs filters/macs if override is configured */
1198 if (mvm->dbgfs_bcast_filtering.override) {
1199 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1200 sizeof(cmd->filters));
1201 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1207 /* if no filters are configured, do nothing */
1208 if (!mvm->bcast_filters)
1211 /* configure and attach these filters for each associated sta vif */
1212 ieee80211_iterate_active_interfaces(
1213 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1214 iwl_mvm_bcast_filter_iterator, &iter_data);
1218 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1219 struct ieee80211_vif *vif)
1221 struct iwl_bcast_filter_cmd cmd;
1223 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1226 if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1229 return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, CMD_SYNC,
1233 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1234 struct ieee80211_vif *vif)
1240 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1241 struct ieee80211_vif *vif,
1242 struct ieee80211_bss_conf *bss_conf,
1245 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1249 * Re-calculate the tsf id, as the master-slave relations depend on the
1250 * beacon interval, which was not known when the station interface was
1253 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1254 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1256 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1258 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1260 if (changes & BSS_CHANGED_ASSOC) {
1261 if (bss_conf->assoc) {
1262 /* add quota for this interface */
1263 ret = iwl_mvm_update_quotas(mvm, vif);
1265 IWL_ERR(mvm, "failed to update quotas\n");
1269 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1272 * If we're restarting then the firmware will
1273 * obviously have lost synchronisation with
1274 * the AP. It will attempt to synchronise by
1275 * itself, but we can make it more reliable by
1276 * scheduling a session protection time event.
1278 * The firmware needs to receive a beacon to
1279 * catch up with synchronisation, use 110% of
1280 * the beacon interval.
1282 * Set a large maximum delay to allow for more
1283 * than a single interface.
1285 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1286 iwl_mvm_protect_session(mvm, vif, dur, dur,
1290 iwl_mvm_sf_update(mvm, vif, false);
1291 iwl_mvm_power_vif_assoc(mvm, vif);
1293 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1294 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1296 * If update fails - SF might be running in associated
1297 * mode while disassociated - which is forbidden.
1299 WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1300 "Failed to update SF upon disassociation\n");
1302 /* remove AP station now that the MAC is unassoc */
1303 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1305 IWL_ERR(mvm, "failed to remove AP station\n");
1307 if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1308 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1309 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1310 /* remove quota for this interface */
1311 ret = iwl_mvm_update_quotas(mvm, NULL);
1313 IWL_ERR(mvm, "failed to update quotas\n");
1316 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1319 iwl_mvm_recalc_multicast(mvm);
1320 iwl_mvm_configure_bcast_filter(mvm, vif);
1322 /* reset rssi values */
1323 mvmvif->bf_data.ave_beacon_signal = 0;
1325 iwl_mvm_bt_coex_vif_change(mvm);
1326 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1327 IEEE80211_SMPS_AUTOMATIC);
1328 } else if (changes & BSS_CHANGED_BEACON_INFO) {
1330 * We received a beacon _after_ association so
1331 * remove the session protection.
1333 iwl_mvm_remove_time_event(mvm, mvmvif,
1334 &mvmvif->time_event_data);
1335 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, CMD_SYNC));
1336 } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS |
1338 ret = iwl_mvm_power_update_mac(mvm, vif);
1340 IWL_ERR(mvm, "failed to update power mode\n");
1342 if (changes & BSS_CHANGED_TXPOWER) {
1343 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1345 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1348 if (changes & BSS_CHANGED_CQM) {
1349 IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
1350 /* reset cqm events tracking */
1351 mvmvif->bf_data.last_cqm_event = 0;
1352 ret = iwl_mvm_update_beacon_filter(mvm, vif, false, CMD_SYNC);
1354 IWL_ERR(mvm, "failed to update CQM thresholds\n");
1357 if (changes & BSS_CHANGED_ARP_FILTER) {
1358 IWL_DEBUG_MAC80211(mvm, "arp filter changed");
1359 iwl_mvm_configure_bcast_filter(mvm, vif);
1363 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1364 struct ieee80211_vif *vif)
1366 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1367 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1370 mutex_lock(&mvm->mutex);
1372 /* Send the beacon template */
1373 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1378 * Re-calculate the tsf id, as the master-slave relations depend on the
1379 * beacon interval, which was not known when the AP interface was added.
1381 if (vif->type == NL80211_IFTYPE_AP)
1382 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1384 /* Add the mac context */
1385 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1389 /* Perform the binding */
1390 ret = iwl_mvm_binding_add_vif(mvm, vif);
1394 /* Send the bcast station. At this stage the TBTT and DTIM time events
1395 * are added and applied to the scheduler */
1396 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
1400 /* must be set before quota calculations */
1401 mvmvif->ap_ibss_active = true;
1403 /* power updated needs to be done before quotas */
1404 iwl_mvm_power_update_mac(mvm, vif);
1406 ret = iwl_mvm_update_quotas(mvm, vif);
1408 goto out_quota_failed;
1410 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1411 if (vif->p2p && mvm->p2p_device_vif)
1412 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
1414 iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1416 iwl_mvm_bt_coex_vif_change(mvm);
1418 mutex_unlock(&mvm->mutex);
1422 iwl_mvm_power_update_mac(mvm, vif);
1423 mvmvif->ap_ibss_active = false;
1424 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1426 iwl_mvm_binding_remove_vif(mvm, vif);
1428 iwl_mvm_mac_ctxt_remove(mvm, vif);
1430 mutex_unlock(&mvm->mutex);
1434 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1435 struct ieee80211_vif *vif)
1437 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1438 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1440 iwl_mvm_prepare_mac_removal(mvm, vif);
1442 mutex_lock(&mvm->mutex);
1444 mvmvif->ap_ibss_active = false;
1446 iwl_mvm_bt_coex_vif_change(mvm);
1448 iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
1450 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1451 if (vif->p2p && mvm->p2p_device_vif)
1452 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
1454 iwl_mvm_update_quotas(mvm, NULL);
1455 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1456 iwl_mvm_binding_remove_vif(mvm, vif);
1458 iwl_mvm_power_update_mac(mvm, vif);
1460 iwl_mvm_mac_ctxt_remove(mvm, vif);
1462 mutex_unlock(&mvm->mutex);
1466 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1467 struct ieee80211_vif *vif,
1468 struct ieee80211_bss_conf *bss_conf,
1471 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1473 /* Changes will be applied when the AP/IBSS is started */
1474 if (!mvmvif->ap_ibss_active)
1477 if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
1478 BSS_CHANGED_BANDWIDTH) &&
1479 iwl_mvm_mac_ctxt_changed(mvm, vif))
1480 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1482 /* Need to send a new beacon template to the FW */
1483 if (changes & BSS_CHANGED_BEACON &&
1484 iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1485 IWL_WARN(mvm, "Failed updating beacon data\n");
1488 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1489 struct ieee80211_vif *vif,
1490 struct ieee80211_bss_conf *bss_conf,
1493 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1495 mutex_lock(&mvm->mutex);
1497 switch (vif->type) {
1498 case NL80211_IFTYPE_STATION:
1499 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1501 case NL80211_IFTYPE_AP:
1502 case NL80211_IFTYPE_ADHOC:
1503 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1506 /* shouldn't happen */
1510 mutex_unlock(&mvm->mutex);
1513 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1514 struct ieee80211_vif *vif,
1515 struct cfg80211_scan_request *req)
1517 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1520 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
1523 mutex_lock(&mvm->mutex);
1525 switch (mvm->scan_status) {
1526 case IWL_MVM_SCAN_SCHED:
1527 ret = iwl_mvm_sched_scan_stop(mvm);
1533 case IWL_MVM_SCAN_NONE:
1540 iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1542 ret = iwl_mvm_scan_request(mvm, vif, req);
1544 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1546 mutex_unlock(&mvm->mutex);
1547 /* make sure to flush the Rx handler before the next scan arrives */
1548 iwl_mvm_wait_for_async_handlers(mvm);
1552 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1553 struct ieee80211_vif *vif)
1555 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1557 mutex_lock(&mvm->mutex);
1559 iwl_mvm_cancel_scan(mvm);
1561 mutex_unlock(&mvm->mutex);
1565 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1566 struct ieee80211_sta *sta, u16 tids,
1568 enum ieee80211_frame_release_type reason,
1571 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1573 /* Called when we need to transmit (a) frame(s) from mac80211 */
1575 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1576 tids, more_data, false);
1580 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
1581 struct ieee80211_sta *sta, u16 tids,
1583 enum ieee80211_frame_release_type reason,
1586 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1588 /* Called when we need to transmit (a) frame(s) from agg queue */
1590 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1591 tids, more_data, true);
1594 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1595 struct ieee80211_vif *vif,
1596 enum sta_notify_cmd cmd,
1597 struct ieee80211_sta *sta)
1599 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1600 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1604 case STA_NOTIFY_SLEEP:
1605 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1606 ieee80211_sta_block_awake(hw, sta, true);
1607 spin_lock_bh(&mvmsta->lock);
1608 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1609 struct iwl_mvm_tid_data *tid_data;
1611 tid_data = &mvmsta->tid_data[tid];
1612 if (tid_data->state != IWL_AGG_ON &&
1613 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
1615 if (iwl_mvm_tid_queued(tid_data) == 0)
1617 ieee80211_sta_set_buffered(sta, tid, true);
1619 spin_unlock_bh(&mvmsta->lock);
1621 * The fw updates the STA to be asleep. Tx packets on the Tx
1622 * queues to this station will not be transmitted. The fw will
1623 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1626 case STA_NOTIFY_AWAKE:
1627 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1629 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1636 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
1637 struct ieee80211_vif *vif,
1638 struct ieee80211_sta *sta)
1640 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1641 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1644 * This is called before mac80211 does RCU synchronisation,
1645 * so here we already invalidate our internal RCU-protected
1646 * station pointer. The rest of the code will thus no longer
1647 * be able to find the station this way, and we don't rely
1648 * on further RCU synchronisation after the sta_state()
1649 * callback deleted the station.
1651 mutex_lock(&mvm->mutex);
1652 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
1653 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1655 mutex_unlock(&mvm->mutex);
1658 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1659 struct ieee80211_vif *vif,
1660 struct ieee80211_sta *sta,
1661 enum ieee80211_sta_state old_state,
1662 enum ieee80211_sta_state new_state)
1664 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1665 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1668 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1669 sta->addr, old_state, new_state);
1671 /* this would be a mac80211 bug ... but don't crash */
1672 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1675 /* if a STA is being removed, reuse its ID */
1676 flush_work(&mvm->sta_drained_wk);
1678 mutex_lock(&mvm->mutex);
1679 if (old_state == IEEE80211_STA_NOTEXIST &&
1680 new_state == IEEE80211_STA_NONE) {
1682 * Firmware bug - it'll crash if the beacon interval is less
1683 * than 16. We can't avoid connecting at all, so refuse the
1684 * station state change, this will cause mac80211 to abandon
1685 * attempts to connect to this AP, and eventually wpa_s will
1686 * blacklist the AP...
1688 if (vif->type == NL80211_IFTYPE_STATION &&
1689 vif->bss_conf.beacon_int < 16) {
1691 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1692 sta->addr, vif->bss_conf.beacon_int);
1696 ret = iwl_mvm_add_sta(mvm, vif, sta);
1697 } else if (old_state == IEEE80211_STA_NONE &&
1698 new_state == IEEE80211_STA_AUTH) {
1700 } else if (old_state == IEEE80211_STA_AUTH &&
1701 new_state == IEEE80211_STA_ASSOC) {
1702 ret = iwl_mvm_update_sta(mvm, vif, sta);
1704 iwl_mvm_rs_rate_init(mvm, sta,
1705 mvmvif->phy_ctxt->channel->band,
1707 } else if (old_state == IEEE80211_STA_ASSOC &&
1708 new_state == IEEE80211_STA_AUTHORIZED) {
1709 /* enable beacon filtering */
1710 if (vif->bss_conf.dtim_period)
1711 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif,
1714 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1715 new_state == IEEE80211_STA_ASSOC) {
1716 /* disable beacon filtering */
1717 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, CMD_SYNC));
1719 } else if (old_state == IEEE80211_STA_ASSOC &&
1720 new_state == IEEE80211_STA_AUTH) {
1722 } else if (old_state == IEEE80211_STA_AUTH &&
1723 new_state == IEEE80211_STA_NONE) {
1725 } else if (old_state == IEEE80211_STA_NONE &&
1726 new_state == IEEE80211_STA_NOTEXIST) {
1727 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1732 mutex_unlock(&mvm->mutex);
1737 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1739 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1741 mvm->rts_threshold = value;
1746 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
1747 struct ieee80211_vif *vif,
1748 struct ieee80211_sta *sta, u32 changed)
1750 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1752 if (vif->type == NL80211_IFTYPE_STATION &&
1753 changed & IEEE80211_RC_NSS_CHANGED)
1754 iwl_mvm_sf_update(mvm, vif, false);
1757 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1758 struct ieee80211_vif *vif, u16 ac,
1759 const struct ieee80211_tx_queue_params *params)
1761 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1762 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1764 mvmvif->queue_params[ac] = *params;
1767 * No need to update right away, we'll get BSS_CHANGED_QOS
1768 * The exception is P2P_DEVICE interface which needs immediate update.
1770 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1773 mutex_lock(&mvm->mutex);
1774 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1775 mutex_unlock(&mvm->mutex);
1781 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1782 struct ieee80211_vif *vif)
1784 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1785 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1786 200 + vif->bss_conf.beacon_int);
1787 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1788 100 + vif->bss_conf.beacon_int);
1790 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1793 mutex_lock(&mvm->mutex);
1794 /* Try really hard to protect the session and hear a beacon */
1795 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
1796 mutex_unlock(&mvm->mutex);
1799 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
1800 struct ieee80211_vif *vif,
1801 struct cfg80211_sched_scan_request *req,
1802 struct ieee80211_sched_scan_ies *ies)
1804 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1807 mutex_lock(&mvm->mutex);
1809 switch (mvm->scan_status) {
1810 case IWL_MVM_SCAN_OS:
1811 IWL_DEBUG_SCAN(mvm, "Stopping previous scan for sched_scan\n");
1812 ret = iwl_mvm_cancel_scan(mvm);
1819 * iwl_mvm_rx_scan_complete() will be called soon but will
1820 * not reset the scan status as it won't be IWL_MVM_SCAN_OS
1821 * any more since we queue the next scan immediately (below).
1822 * We make sure it is called before the next scan starts by
1823 * flushing the async-handlers work.
1826 case IWL_MVM_SCAN_NONE:
1833 mvm->scan_status = IWL_MVM_SCAN_SCHED;
1835 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1839 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1843 ret = iwl_mvm_sched_scan_start(mvm, req);
1847 mvm->scan_status = IWL_MVM_SCAN_NONE;
1849 mutex_unlock(&mvm->mutex);
1850 /* make sure to flush the Rx handler before the next scan arrives */
1851 iwl_mvm_wait_for_async_handlers(mvm);
1855 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
1856 struct ieee80211_vif *vif)
1858 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1861 mutex_lock(&mvm->mutex);
1862 ret = iwl_mvm_sched_scan_stop(mvm);
1863 mutex_unlock(&mvm->mutex);
1864 iwl_mvm_wait_for_async_handlers(mvm);
1869 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1870 enum set_key_cmd cmd,
1871 struct ieee80211_vif *vif,
1872 struct ieee80211_sta *sta,
1873 struct ieee80211_key_conf *key)
1875 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1878 if (iwlwifi_mod_params.sw_crypto) {
1879 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1883 switch (key->cipher) {
1884 case WLAN_CIPHER_SUITE_TKIP:
1885 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1887 case WLAN_CIPHER_SUITE_CCMP:
1888 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1890 case WLAN_CIPHER_SUITE_AES_CMAC:
1891 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1893 case WLAN_CIPHER_SUITE_WEP40:
1894 case WLAN_CIPHER_SUITE_WEP104:
1896 * Support for TX only, at least for now, so accept
1897 * the key and do nothing else. Then mac80211 will
1898 * pass it for TX but we don't have to use it for RX.
1902 /* currently FW supports only one optional cipher scheme */
1903 if (hw->n_cipher_schemes &&
1904 hw->cipher_schemes->cipher == key->cipher)
1905 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
1910 mutex_lock(&mvm->mutex);
1914 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1915 vif->type == NL80211_IFTYPE_AP) && !sta) {
1917 * GTK on AP interface is a TX-only key, return 0;
1918 * on IBSS they're per-station and because we're lazy
1919 * we don't support them for RX, so do the same.
1922 key->hw_key_idx = STA_KEY_IDX_INVALID;
1926 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1927 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1929 IWL_WARN(mvm, "set key failed\n");
1931 * can't add key for RX, but we don't need it
1932 * in the device for TX so still return 0
1934 key->hw_key_idx = STA_KEY_IDX_INVALID;
1940 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1945 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1946 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1952 mutex_unlock(&mvm->mutex);
1956 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1957 struct ieee80211_vif *vif,
1958 struct ieee80211_key_conf *keyconf,
1959 struct ieee80211_sta *sta,
1960 u32 iv32, u16 *phase1key)
1962 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1964 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1967 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1971 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1972 struct ieee80211_vif *vif,
1973 struct ieee80211_channel *channel,
1975 enum ieee80211_roc_type type)
1977 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1978 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1979 struct cfg80211_chan_def chandef;
1980 struct iwl_mvm_phy_ctxt *phy_ctxt;
1983 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1986 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1987 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1991 mutex_lock(&mvm->mutex);
1993 for (i = 0; i < NUM_PHY_CTX; i++) {
1994 phy_ctxt = &mvm->phy_ctxts[i];
1995 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1998 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2000 * Unbind the P2P_DEVICE from the current PHY context,
2001 * and if the PHY context is not used remove it.
2003 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2004 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2007 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2009 /* Bind the P2P_DEVICE to the current PHY Context */
2010 mvmvif->phy_ctxt = phy_ctxt;
2012 ret = iwl_mvm_binding_add_vif(mvm, vif);
2013 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2016 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2017 goto schedule_time_event;
2021 /* Need to update the PHY context only if the ROC channel changed */
2022 if (channel == mvmvif->phy_ctxt->channel)
2023 goto schedule_time_event;
2025 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2028 * Change the PHY context configuration as it is currently referenced
2029 * only by the P2P Device MAC
2031 if (mvmvif->phy_ctxt->ref == 1) {
2032 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2038 * The PHY context is shared with other MACs. Need to remove the
2039 * P2P Device from the binding, allocate an new PHY context and
2040 * create a new binding
2042 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2048 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2051 IWL_ERR(mvm, "Failed to change PHY context\n");
2055 /* Unbind the P2P_DEVICE from the current PHY context */
2056 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2057 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2060 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2062 /* Bind the P2P_DEVICE to the new allocated PHY context */
2063 mvmvif->phy_ctxt = phy_ctxt;
2065 ret = iwl_mvm_binding_add_vif(mvm, vif);
2066 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2069 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2072 schedule_time_event:
2073 /* Schedule the time events */
2074 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2077 mutex_unlock(&mvm->mutex);
2078 IWL_DEBUG_MAC80211(mvm, "leave\n");
2082 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2084 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2086 IWL_DEBUG_MAC80211(mvm, "enter\n");
2088 mutex_lock(&mvm->mutex);
2089 iwl_mvm_stop_p2p_roc(mvm);
2090 mutex_unlock(&mvm->mutex);
2092 IWL_DEBUG_MAC80211(mvm, "leave\n");
2096 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2097 struct ieee80211_chanctx_conf *ctx)
2099 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2100 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2101 struct iwl_mvm_phy_ctxt *phy_ctxt;
2104 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2106 mutex_lock(&mvm->mutex);
2107 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2113 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2114 ctx->rx_chains_static,
2115 ctx->rx_chains_dynamic);
2117 IWL_ERR(mvm, "Failed to add PHY context\n");
2121 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
2122 *phy_ctxt_id = phy_ctxt->id;
2124 mutex_unlock(&mvm->mutex);
2128 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2129 struct ieee80211_chanctx_conf *ctx)
2131 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2132 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2133 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2135 mutex_lock(&mvm->mutex);
2136 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2137 mutex_unlock(&mvm->mutex);
2140 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
2141 struct ieee80211_chanctx_conf *ctx,
2144 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2145 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2146 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2148 if (WARN_ONCE((phy_ctxt->ref > 1) &&
2149 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
2150 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2151 IEEE80211_CHANCTX_CHANGE_RADAR |
2152 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
2153 "Cannot change PHY. Ref=%d, changed=0x%X\n",
2154 phy_ctxt->ref, changed))
2157 mutex_lock(&mvm->mutex);
2158 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2159 ctx->rx_chains_static,
2160 ctx->rx_chains_dynamic);
2161 iwl_mvm_bt_coex_vif_change(mvm);
2162 mutex_unlock(&mvm->mutex);
2165 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
2166 struct ieee80211_vif *vif,
2167 struct ieee80211_chanctx_conf *ctx)
2169 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2170 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2171 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2172 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2175 mutex_lock(&mvm->mutex);
2177 mvmvif->phy_ctxt = phy_ctxt;
2179 switch (vif->type) {
2180 case NL80211_IFTYPE_AP:
2181 case NL80211_IFTYPE_ADHOC:
2183 * The AP binding flow is handled as part of the start_ap flow
2184 * (in bss_info_changed), similarly for IBSS.
2188 case NL80211_IFTYPE_STATION:
2189 case NL80211_IFTYPE_MONITOR:
2196 ret = iwl_mvm_binding_add_vif(mvm, vif);
2201 * Power state must be updated before quotas,
2202 * otherwise fw will complain.
2204 iwl_mvm_power_update_mac(mvm, vif);
2206 /* Setting the quota at this stage is only required for monitor
2207 * interfaces. For the other types, the bss_info changed flow
2208 * will handle quota settings.
2210 if (vif->type == NL80211_IFTYPE_MONITOR) {
2211 mvmvif->monitor_active = true;
2212 ret = iwl_mvm_update_quotas(mvm, vif);
2214 goto out_remove_binding;
2220 iwl_mvm_binding_remove_vif(mvm, vif);
2221 iwl_mvm_power_update_mac(mvm, vif);
2223 mutex_unlock(&mvm->mutex);
2225 mvmvif->phy_ctxt = NULL;
2229 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
2230 struct ieee80211_vif *vif,
2231 struct ieee80211_chanctx_conf *ctx)
2233 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2234 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2236 mutex_lock(&mvm->mutex);
2238 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
2240 switch (vif->type) {
2241 case NL80211_IFTYPE_AP:
2242 case NL80211_IFTYPE_ADHOC:
2244 case NL80211_IFTYPE_MONITOR:
2245 mvmvif->monitor_active = false;
2246 iwl_mvm_update_quotas(mvm, NULL);
2252 iwl_mvm_binding_remove_vif(mvm, vif);
2253 iwl_mvm_power_update_mac(mvm, vif);
2256 mvmvif->phy_ctxt = NULL;
2257 mutex_unlock(&mvm->mutex);
2260 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
2261 struct ieee80211_sta *sta,
2264 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2265 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
2267 if (!mvm_sta || !mvm_sta->vif) {
2268 IWL_ERR(mvm, "Station is not associated to a vif\n");
2272 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
2275 #ifdef CONFIG_NL80211_TESTMODE
2276 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
2277 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
2278 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
2279 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
2282 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
2283 struct ieee80211_vif *vif,
2284 void *data, int len)
2286 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
2290 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
2294 if (!tb[IWL_MVM_TM_ATTR_CMD])
2297 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
2298 case IWL_MVM_TM_CMD_SET_NOA:
2299 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
2300 !vif->bss_conf.enable_beacon ||
2301 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
2304 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
2305 if (noa_duration >= vif->bss_conf.beacon_int)
2308 mvm->noa_duration = noa_duration;
2311 return iwl_mvm_update_quotas(mvm, NULL);
2312 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
2313 /* must be associated client vif - ignore authorized */
2314 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
2315 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
2316 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
2319 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
2320 return iwl_mvm_enable_beacon_filter(mvm, vif,
2322 return iwl_mvm_disable_beacon_filter(mvm, vif, CMD_SYNC);
2328 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
2329 struct ieee80211_vif *vif,
2330 void *data, int len)
2332 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2335 mutex_lock(&mvm->mutex);
2336 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
2337 mutex_unlock(&mvm->mutex);
2343 const struct ieee80211_ops iwl_mvm_hw_ops = {
2344 .tx = iwl_mvm_mac_tx,
2345 .ampdu_action = iwl_mvm_mac_ampdu_action,
2346 .start = iwl_mvm_mac_start,
2347 .restart_complete = iwl_mvm_mac_restart_complete,
2348 .stop = iwl_mvm_mac_stop,
2349 .add_interface = iwl_mvm_mac_add_interface,
2350 .remove_interface = iwl_mvm_mac_remove_interface,
2351 .config = iwl_mvm_mac_config,
2352 .prepare_multicast = iwl_mvm_prepare_multicast,
2353 .configure_filter = iwl_mvm_configure_filter,
2354 .bss_info_changed = iwl_mvm_bss_info_changed,
2355 .hw_scan = iwl_mvm_mac_hw_scan,
2356 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
2357 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
2358 .sta_state = iwl_mvm_mac_sta_state,
2359 .sta_notify = iwl_mvm_mac_sta_notify,
2360 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
2361 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
2362 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
2363 .sta_rc_update = iwl_mvm_sta_rc_update,
2364 .conf_tx = iwl_mvm_mac_conf_tx,
2365 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
2366 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
2367 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
2368 .set_key = iwl_mvm_mac_set_key,
2369 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
2370 .remain_on_channel = iwl_mvm_roc,
2371 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
2372 .add_chanctx = iwl_mvm_add_chanctx,
2373 .remove_chanctx = iwl_mvm_remove_chanctx,
2374 .change_chanctx = iwl_mvm_change_chanctx,
2375 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
2376 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
2378 .start_ap = iwl_mvm_start_ap_ibss,
2379 .stop_ap = iwl_mvm_stop_ap_ibss,
2380 .join_ibss = iwl_mvm_start_ap_ibss,
2381 .leave_ibss = iwl_mvm_stop_ap_ibss,
2383 .set_tim = iwl_mvm_set_tim,
2385 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
2387 #ifdef CONFIG_PM_SLEEP
2389 .suspend = iwl_mvm_suspend,
2390 .resume = iwl_mvm_resume,
2391 .set_wakeup = iwl_mvm_set_wakeup,
2392 .set_rekey_data = iwl_mvm_set_rekey_data,
2393 #if IS_ENABLED(CONFIG_IPV6)
2394 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
2396 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,