iwlwifi: mvm: implement AP/GO uAPSD support
[linux-2.6/btrfs-unstable.git] / drivers / net / wireless / iwlwifi / mvm / mac80211.c
blob5b9cfe1f35bdd12ffc55418179b715d746e96b2b
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.
6 * GPL LICENSE SUMMARY
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,
22 * USA
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
31 * BSD LICENSE
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
38 * are met:
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
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>
68 #include <linux/ip.h>
69 #include <net/mac80211.h>
70 #include <net/tcp.h>
72 #include "iwl-op-mode.h"
73 #include "iwl-io.h"
74 #include "mvm.h"
75 #include "sta.h"
76 #include "time-event.h"
77 #include "iwl-eeprom-parse.h"
78 #include "fw-api-scan.h"
79 #include "iwl-phy-db.h"
80 #include "testmode.h"
82 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
84 .max = 1,
85 .types = BIT(NL80211_IFTYPE_STATION),
88 .max = 1,
89 .types = BIT(NL80211_IFTYPE_AP) |
90 BIT(NL80211_IFTYPE_P2P_CLIENT) |
91 BIT(NL80211_IFTYPE_P2P_GO),
94 .max = 1,
95 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
99 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
101 .num_different_channels = 1,
102 .max_interfaces = 3,
103 .limits = iwl_mvm_limits,
104 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
108 #ifdef CONFIG_PM_SLEEP
109 static const struct nl80211_wowlan_tcp_data_token_feature
110 iwl_mvm_wowlan_tcp_token_feature = {
111 .min_len = 0,
112 .max_len = 255,
113 .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
116 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
117 .tok = &iwl_mvm_wowlan_tcp_token_feature,
118 .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
119 sizeof(struct ethhdr) -
120 sizeof(struct iphdr) -
121 sizeof(struct tcphdr),
122 .data_interval_max = 65535, /* __le16 in API */
123 .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
124 sizeof(struct ethhdr) -
125 sizeof(struct iphdr) -
126 sizeof(struct tcphdr),
127 .seq = true,
129 #endif
131 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
133 int i;
135 memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
136 for (i = 0; i < NUM_PHY_CTX; i++) {
137 mvm->phy_ctxts[i].id = i;
138 mvm->phy_ctxts[i].ref = 0;
142 static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
144 /* we create the 802.11 header and SSID element */
145 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
146 return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
147 return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
150 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
152 struct ieee80211_hw *hw = mvm->hw;
153 int num_mac, ret, i;
155 /* Tell mac80211 our characteristics */
156 hw->flags = IEEE80211_HW_SIGNAL_DBM |
157 IEEE80211_HW_SPECTRUM_MGMT |
158 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
159 IEEE80211_HW_QUEUE_CONTROL |
160 IEEE80211_HW_WANT_MONITOR_VIF |
161 IEEE80211_HW_SUPPORTS_PS |
162 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
163 IEEE80211_HW_AMPDU_AGGREGATION |
164 IEEE80211_HW_TIMING_BEACON_ONLY |
165 IEEE80211_HW_CONNECTION_MONITOR |
166 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
167 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
169 hw->queues = mvm->first_agg_queue;
170 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
171 hw->rate_control_algorithm = "iwl-mvm-rs";
174 * Enable 11w if advertised by firmware and software crypto
175 * is not enabled (as the firmware will interpret some mgmt
176 * packets, so enabling it with software crypto isn't safe)
178 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
179 !iwlwifi_mod_params.sw_crypto)
180 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
182 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT) {
183 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
184 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
185 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
188 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
189 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
190 hw->chanctx_data_size = sizeof(u16);
192 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
193 BIT(NL80211_IFTYPE_P2P_CLIENT) |
194 BIT(NL80211_IFTYPE_AP) |
195 BIT(NL80211_IFTYPE_P2P_GO) |
196 BIT(NL80211_IFTYPE_P2P_DEVICE);
198 /* IBSS has bugs in older versions */
199 if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 8)
200 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
202 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
203 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
204 REGULATORY_DISABLE_BEACON_HINTS;
206 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
207 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
209 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
210 hw->wiphy->n_iface_combinations =
211 ARRAY_SIZE(iwl_mvm_iface_combinations);
213 hw->wiphy->max_remain_on_channel_duration = 10000;
214 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
216 /* Extract MAC address */
217 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
218 hw->wiphy->addresses = mvm->addresses;
219 hw->wiphy->n_addresses = 1;
221 /* Extract additional MAC addresses if available */
222 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
223 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
225 for (i = 1; i < num_mac; i++) {
226 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
227 ETH_ALEN);
228 mvm->addresses[i].addr[5]++;
229 hw->wiphy->n_addresses++;
232 iwl_mvm_reset_phy_ctxts(mvm);
234 hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
236 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
238 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
239 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
240 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
241 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
242 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
243 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
245 hw->wiphy->hw_version = mvm->trans->hw_id;
247 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
248 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
249 else
250 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
252 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SCHED_SCAN) {
253 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
254 hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
255 hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
256 /* we create the 802.11 header and zero length SSID IE. */
257 hw->wiphy->max_sched_scan_ie_len =
258 SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
261 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
262 NL80211_FEATURE_P2P_GO_OPPPS |
263 NL80211_FEATURE_LOW_PRIORITY_SCAN;
265 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
267 /* currently FW API supports only one optional cipher scheme */
268 if (mvm->fw->cs[0].cipher) {
269 mvm->hw->n_cipher_schemes = 1;
270 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
273 #ifdef CONFIG_PM_SLEEP
274 if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
275 mvm->trans->ops->d3_suspend &&
276 mvm->trans->ops->d3_resume &&
277 device_can_wakeup(mvm->trans->dev)) {
278 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
279 WIPHY_WOWLAN_DISCONNECT |
280 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
281 WIPHY_WOWLAN_RFKILL_RELEASE;
282 if (!iwlwifi_mod_params.sw_crypto)
283 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
284 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
285 WIPHY_WOWLAN_4WAY_HANDSHAKE;
287 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
288 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
289 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
290 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
291 hw->wiphy->wowlan = &mvm->wowlan;
293 #endif
295 ret = iwl_mvm_leds_init(mvm);
296 if (ret)
297 return ret;
299 ret = ieee80211_register_hw(mvm->hw);
300 if (ret)
301 iwl_mvm_leds_exit(mvm);
303 return ret;
306 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
307 struct ieee80211_tx_control *control,
308 struct sk_buff *skb)
310 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
311 struct ieee80211_sta *sta = control->sta;
312 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
313 struct ieee80211_hdr *hdr = (void *)skb->data;
315 if (iwl_mvm_is_radio_killed(mvm)) {
316 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
317 goto drop;
320 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
321 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
322 goto drop;
324 /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
325 if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
326 ieee80211_is_mgmt(hdr->frame_control) &&
327 !ieee80211_is_deauth(hdr->frame_control) &&
328 !ieee80211_is_disassoc(hdr->frame_control) &&
329 !ieee80211_is_action(hdr->frame_control)))
330 sta = NULL;
332 if (sta) {
333 if (iwl_mvm_tx_skb(mvm, skb, sta))
334 goto drop;
335 return;
338 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
339 goto drop;
340 return;
341 drop:
342 ieee80211_free_txskb(hw, skb);
345 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
346 struct ieee80211_vif *vif,
347 enum ieee80211_ampdu_mlme_action action,
348 struct ieee80211_sta *sta, u16 tid,
349 u16 *ssn, u8 buf_size)
351 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
352 int ret;
354 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
355 sta->addr, tid, action);
357 if (!(mvm->nvm_data->sku_cap_11n_enable))
358 return -EACCES;
360 mutex_lock(&mvm->mutex);
362 switch (action) {
363 case IEEE80211_AMPDU_RX_START:
364 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
365 ret = -EINVAL;
366 break;
368 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
369 break;
370 case IEEE80211_AMPDU_RX_STOP:
371 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
372 break;
373 case IEEE80211_AMPDU_TX_START:
374 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
375 ret = -EINVAL;
376 break;
378 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
379 break;
380 case IEEE80211_AMPDU_TX_STOP_CONT:
381 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
382 break;
383 case IEEE80211_AMPDU_TX_STOP_FLUSH:
384 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
385 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
386 break;
387 case IEEE80211_AMPDU_TX_OPERATIONAL:
388 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
389 break;
390 default:
391 WARN_ON_ONCE(1);
392 ret = -EINVAL;
393 break;
395 mutex_unlock(&mvm->mutex);
397 return ret;
400 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
401 struct ieee80211_vif *vif)
403 struct iwl_mvm *mvm = data;
404 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
406 mvmvif->uploaded = false;
407 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
409 /* does this make sense at all? */
410 mvmvif->color++;
412 spin_lock_bh(&mvm->time_event_lock);
413 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
414 spin_unlock_bh(&mvm->time_event_lock);
416 mvmvif->phy_ctxt = NULL;
419 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
421 iwl_trans_stop_device(mvm->trans);
423 mvm->scan_status = IWL_MVM_SCAN_NONE;
425 /* just in case one was running */
426 ieee80211_remain_on_channel_expired(mvm->hw);
428 ieee80211_iterate_active_interfaces_atomic(
429 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
430 iwl_mvm_cleanup_iterator, mvm);
432 mvm->p2p_device_vif = NULL;
434 iwl_mvm_reset_phy_ctxts(mvm);
435 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
436 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
438 ieee80211_wake_queues(mvm->hw);
440 mvm->vif_count = 0;
441 mvm->rx_ba_sessions = 0;
444 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
446 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
447 int ret;
449 mutex_lock(&mvm->mutex);
451 /* Clean up some internal and mac80211 state on restart */
452 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
453 iwl_mvm_restart_cleanup(mvm);
455 ret = iwl_mvm_up(mvm);
456 mutex_unlock(&mvm->mutex);
458 return ret;
461 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
463 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
464 int ret;
466 mutex_lock(&mvm->mutex);
468 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
469 ret = iwl_mvm_update_quotas(mvm, NULL);
470 if (ret)
471 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
472 ret);
474 mutex_unlock(&mvm->mutex);
477 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
479 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
481 flush_work(&mvm->async_handlers_wk);
483 mutex_lock(&mvm->mutex);
484 /* async_handlers_wk is now blocked */
487 * The work item could be running or queued if the
488 * ROC time event stops just as we get here.
490 cancel_work_sync(&mvm->roc_done_wk);
492 iwl_trans_stop_device(mvm->trans);
494 iwl_mvm_async_handlers_purge(mvm);
495 /* async_handlers_list is empty and will stay empty: HW is stopped */
497 /* the fw is stopped, the aux sta is dead: clean up driver state */
498 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
500 mutex_unlock(&mvm->mutex);
503 * The worker might have been waiting for the mutex, let it run and
504 * discover that its list is now empty.
506 cancel_work_sync(&mvm->async_handlers_wk);
509 static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
510 struct ieee80211_vif *vif)
512 struct iwl_mvm *mvm = data;
514 iwl_mvm_power_update_mode(mvm, vif);
517 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
519 u16 i;
521 lockdep_assert_held(&mvm->mutex);
523 for (i = 0; i < NUM_PHY_CTX; i++)
524 if (!mvm->phy_ctxts[i].ref)
525 return &mvm->phy_ctxts[i];
527 IWL_ERR(mvm, "No available PHY context\n");
528 return NULL;
531 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
532 s8 tx_power)
534 /* FW is in charge of regulatory enforcement */
535 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
536 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
537 .pwr_restriction = cpu_to_le16(tx_power),
540 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
541 sizeof(reduce_txpwr_cmd),
542 &reduce_txpwr_cmd);
545 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
546 struct ieee80211_vif *vif)
548 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
549 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
550 int ret;
553 * Not much to do here. The stack will not allow interface
554 * types or combinations that we didn't advertise, so we
555 * don't really have to check the types.
558 mutex_lock(&mvm->mutex);
560 /* Allocate resources for the MAC context, and add it to the fw */
561 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
562 if (ret)
563 goto out_unlock;
565 /* Counting number of interfaces is needed for legacy PM */
566 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
567 mvm->vif_count++;
570 * The AP binding flow can be done only after the beacon
571 * template is configured (which happens only in the mac80211
572 * start_ap() flow), and adding the broadcast station can happen
573 * only after the binding.
574 * In addition, since modifying the MAC before adding a bcast
575 * station is not allowed by the FW, delay the adding of MAC context to
576 * the point where we can also add the bcast station.
577 * In short: there's not much we can do at this point, other than
578 * allocating resources :)
580 if (vif->type == NL80211_IFTYPE_AP ||
581 vif->type == NL80211_IFTYPE_ADHOC) {
582 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
583 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
584 qmask);
585 if (ret) {
586 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
587 goto out_release;
590 iwl_mvm_vif_dbgfs_register(mvm, vif);
591 goto out_unlock;
594 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
595 if (ret)
596 goto out_release;
598 iwl_mvm_power_disable(mvm, vif);
600 /* beacon filtering */
601 ret = iwl_mvm_disable_beacon_filter(mvm, vif);
602 if (ret)
603 goto out_remove_mac;
605 if (!mvm->bf_allowed_vif &&
606 vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
607 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
608 mvm->bf_allowed_vif = mvmvif;
609 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
610 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
614 * P2P_DEVICE interface does not have a channel context assigned to it,
615 * so a dedicated PHY context is allocated to it and the corresponding
616 * MAC context is bound to it at this stage.
618 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
620 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
621 if (!mvmvif->phy_ctxt) {
622 ret = -ENOSPC;
623 goto out_free_bf;
626 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
627 ret = iwl_mvm_binding_add_vif(mvm, vif);
628 if (ret)
629 goto out_unref_phy;
631 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
632 if (ret)
633 goto out_unbind;
635 /* Save a pointer to p2p device vif, so it can later be used to
636 * update the p2p device MAC when a GO is started/stopped */
637 mvm->p2p_device_vif = vif;
640 iwl_mvm_vif_dbgfs_register(mvm, vif);
641 goto out_unlock;
643 out_unbind:
644 iwl_mvm_binding_remove_vif(mvm, vif);
645 out_unref_phy:
646 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
647 out_free_bf:
648 if (mvm->bf_allowed_vif == mvmvif) {
649 mvm->bf_allowed_vif = NULL;
650 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
651 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
653 out_remove_mac:
654 mvmvif->phy_ctxt = NULL;
655 iwl_mvm_mac_ctxt_remove(mvm, vif);
656 out_release:
657 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
658 mvm->vif_count--;
660 /* TODO: remove this when legacy PM will be discarded */
661 ieee80211_iterate_active_interfaces(
662 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
663 iwl_mvm_power_update_iterator, mvm);
665 iwl_mvm_mac_ctxt_release(mvm, vif);
666 out_unlock:
667 mutex_unlock(&mvm->mutex);
669 return ret;
672 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
673 struct ieee80211_vif *vif)
675 u32 tfd_msk = 0, ac;
677 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
678 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
679 tfd_msk |= BIT(vif->hw_queue[ac]);
681 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
682 tfd_msk |= BIT(vif->cab_queue);
684 if (tfd_msk) {
685 mutex_lock(&mvm->mutex);
686 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
687 mutex_unlock(&mvm->mutex);
690 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
692 * Flush the ROC worker which will flush the OFFCHANNEL queue.
693 * We assume here that all the packets sent to the OFFCHANNEL
694 * queue are sent in ROC session.
696 flush_work(&mvm->roc_done_wk);
697 } else {
699 * By now, all the AC queues are empty. The AGG queues are
700 * empty too. We already got all the Tx responses for all the
701 * packets in the queues. The drain work can have been
702 * triggered. Flush it.
704 flush_work(&mvm->sta_drained_wk);
708 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
709 struct ieee80211_vif *vif)
711 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
712 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
714 iwl_mvm_prepare_mac_removal(mvm, vif);
716 mutex_lock(&mvm->mutex);
718 if (mvm->bf_allowed_vif == mvmvif) {
719 mvm->bf_allowed_vif = NULL;
720 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
721 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
724 iwl_mvm_vif_dbgfs_clean(mvm, vif);
727 * For AP/GO interface, the tear down of the resources allocated to the
728 * interface is be handled as part of the stop_ap flow.
730 if (vif->type == NL80211_IFTYPE_AP ||
731 vif->type == NL80211_IFTYPE_ADHOC) {
732 #ifdef CONFIG_NL80211_TESTMODE
733 if (vif == mvm->noa_vif) {
734 mvm->noa_vif = NULL;
735 mvm->noa_duration = 0;
737 #endif
738 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
739 goto out_release;
742 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
743 mvm->p2p_device_vif = NULL;
744 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
745 iwl_mvm_binding_remove_vif(mvm, vif);
746 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
747 mvmvif->phy_ctxt = NULL;
750 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
751 mvm->vif_count--;
753 /* TODO: remove this when legacy PM will be discarded */
754 ieee80211_iterate_active_interfaces(
755 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
756 iwl_mvm_power_update_iterator, mvm);
758 iwl_mvm_mac_ctxt_remove(mvm, vif);
760 out_release:
761 iwl_mvm_mac_ctxt_release(mvm, vif);
762 mutex_unlock(&mvm->mutex);
765 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
767 return 0;
770 struct iwl_mvm_mc_iter_data {
771 struct iwl_mvm *mvm;
772 int port_id;
775 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
776 struct ieee80211_vif *vif)
778 struct iwl_mvm_mc_iter_data *data = _data;
779 struct iwl_mvm *mvm = data->mvm;
780 struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
781 int ret, len;
783 /* if we don't have free ports, mcast frames will be dropped */
784 if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
785 return;
787 if (vif->type != NL80211_IFTYPE_STATION ||
788 !vif->bss_conf.assoc)
789 return;
791 cmd->port_id = data->port_id++;
792 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
793 len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
795 ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC, len, cmd);
796 if (ret)
797 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
800 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
802 struct iwl_mvm_mc_iter_data iter_data = {
803 .mvm = mvm,
806 lockdep_assert_held(&mvm->mutex);
808 if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
809 return;
811 ieee80211_iterate_active_interfaces(
812 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
813 iwl_mvm_mc_iface_iterator, &iter_data);
816 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
817 struct netdev_hw_addr_list *mc_list)
819 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
820 struct iwl_mcast_filter_cmd *cmd;
821 struct netdev_hw_addr *addr;
822 int addr_count = netdev_hw_addr_list_count(mc_list);
823 bool pass_all = false;
824 int len;
826 if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
827 pass_all = true;
828 addr_count = 0;
831 len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
832 cmd = kzalloc(len, GFP_ATOMIC);
833 if (!cmd)
834 return 0;
836 if (pass_all) {
837 cmd->pass_all = 1;
838 return (u64)(unsigned long)cmd;
841 netdev_hw_addr_list_for_each(addr, mc_list) {
842 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
843 cmd->count, addr->addr);
844 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
845 addr->addr, ETH_ALEN);
846 cmd->count++;
849 return (u64)(unsigned long)cmd;
852 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
853 unsigned int changed_flags,
854 unsigned int *total_flags,
855 u64 multicast)
857 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
858 struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
860 mutex_lock(&mvm->mutex);
862 /* replace previous configuration */
863 kfree(mvm->mcast_filter_cmd);
864 mvm->mcast_filter_cmd = cmd;
866 if (!cmd)
867 goto out;
869 iwl_mvm_recalc_multicast(mvm);
870 out:
871 mutex_unlock(&mvm->mutex);
872 *total_flags = 0;
875 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
876 struct ieee80211_vif *vif,
877 struct ieee80211_bss_conf *bss_conf,
878 u32 changes)
880 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
881 int ret;
884 * Re-calculate the tsf id, as the master-slave relations depend on the
885 * beacon interval, which was not known when the station interface was
886 * added.
888 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
889 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
891 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
892 if (ret)
893 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
895 if (changes & BSS_CHANGED_ASSOC) {
896 if (bss_conf->assoc) {
897 /* add quota for this interface */
898 ret = iwl_mvm_update_quotas(mvm, vif);
899 if (ret) {
900 IWL_ERR(mvm, "failed to update quotas\n");
901 return;
904 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
905 &mvm->status)) {
907 * If we're restarting then the firmware will
908 * obviously have lost synchronisation with
909 * the AP. It will attempt to synchronise by
910 * itself, but we can make it more reliable by
911 * scheduling a session protection time event.
913 * The firmware needs to receive a beacon to
914 * catch up with synchronisation, use 110% of
915 * the beacon interval.
917 * Set a large maximum delay to allow for more
918 * than a single interface.
920 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
921 iwl_mvm_protect_session(mvm, vif, dur, dur,
922 5 * dur);
925 iwl_mvm_sf_update(mvm, vif, false);
926 iwl_mvm_power_vif_assoc(mvm, vif);
927 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
929 * If update fails - SF might be running in associated
930 * mode while disassociated - which is forbidden.
932 WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
933 "Failed to update SF upon disassociation\n");
935 /* remove AP station now that the MAC is unassoc */
936 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
937 if (ret)
938 IWL_ERR(mvm, "failed to remove AP station\n");
939 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
940 /* remove quota for this interface */
941 ret = iwl_mvm_update_quotas(mvm, NULL);
942 if (ret)
943 IWL_ERR(mvm, "failed to update quotas\n");
946 iwl_mvm_recalc_multicast(mvm);
948 /* reset rssi values */
949 mvmvif->bf_data.ave_beacon_signal = 0;
951 if (!(mvm->fw->ucode_capa.flags &
952 IWL_UCODE_TLV_FLAGS_PM_CMD_SUPPORT)) {
953 /* Workaround for FW bug, otherwise FW disables device
954 * power save upon disassociation
956 ret = iwl_mvm_power_update_mode(mvm, vif);
957 if (ret)
958 IWL_ERR(mvm, "failed to update power mode\n");
960 iwl_mvm_bt_coex_vif_change(mvm);
961 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
962 IEEE80211_SMPS_AUTOMATIC);
963 } else if (changes & BSS_CHANGED_BEACON_INFO) {
965 * We received a beacon _after_ association so
966 * remove the session protection.
968 iwl_mvm_remove_time_event(mvm, mvmvif,
969 &mvmvif->time_event_data);
970 } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS |
971 BSS_CHANGED_QOS)) {
972 ret = iwl_mvm_power_update_mode(mvm, vif);
973 if (ret)
974 IWL_ERR(mvm, "failed to update power mode\n");
976 if (changes & BSS_CHANGED_TXPOWER) {
977 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
978 bss_conf->txpower);
979 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
982 if (changes & BSS_CHANGED_CQM) {
983 IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
984 /* reset cqm events tracking */
985 mvmvif->bf_data.last_cqm_event = 0;
986 ret = iwl_mvm_update_beacon_filter(mvm, vif);
987 if (ret)
988 IWL_ERR(mvm, "failed to update CQM thresholds\n");
992 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
993 struct ieee80211_vif *vif)
995 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
996 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
997 int ret;
999 mutex_lock(&mvm->mutex);
1001 /* Send the beacon template */
1002 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1003 if (ret)
1004 goto out_unlock;
1007 * Re-calculate the tsf id, as the master-slave relations depend on the
1008 * beacon interval, which was not known when the AP interface was added.
1010 if (vif->type == NL80211_IFTYPE_AP)
1011 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1013 /* Add the mac context */
1014 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1015 if (ret)
1016 goto out_unlock;
1018 /* Perform the binding */
1019 ret = iwl_mvm_binding_add_vif(mvm, vif);
1020 if (ret)
1021 goto out_remove;
1023 mvmvif->ap_ibss_active = true;
1025 /* Send the bcast station. At this stage the TBTT and DTIM time events
1026 * are added and applied to the scheduler */
1027 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
1028 if (ret)
1029 goto out_unbind;
1031 /* must be set before quota calculations */
1032 mvmvif->ap_ibss_active = true;
1034 /* power updated needs to be done before quotas */
1035 mvm->bound_vif_cnt++;
1036 iwl_mvm_power_update_binding(mvm, vif, true);
1038 ret = iwl_mvm_update_quotas(mvm, vif);
1039 if (ret)
1040 goto out_quota_failed;
1042 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1043 if (vif->p2p && mvm->p2p_device_vif)
1044 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
1046 iwl_mvm_bt_coex_vif_change(mvm);
1048 mutex_unlock(&mvm->mutex);
1049 return 0;
1051 out_quota_failed:
1052 mvm->bound_vif_cnt--;
1053 iwl_mvm_power_update_binding(mvm, vif, false);
1054 mvmvif->ap_ibss_active = false;
1055 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1056 out_unbind:
1057 iwl_mvm_binding_remove_vif(mvm, vif);
1058 out_remove:
1059 iwl_mvm_mac_ctxt_remove(mvm, vif);
1060 out_unlock:
1061 mutex_unlock(&mvm->mutex);
1062 return ret;
1065 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1066 struct ieee80211_vif *vif)
1068 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1069 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1071 iwl_mvm_prepare_mac_removal(mvm, vif);
1073 mutex_lock(&mvm->mutex);
1075 mvmvif->ap_ibss_active = false;
1077 iwl_mvm_bt_coex_vif_change(mvm);
1079 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1080 if (vif->p2p && mvm->p2p_device_vif)
1081 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
1083 iwl_mvm_update_quotas(mvm, NULL);
1084 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1085 iwl_mvm_binding_remove_vif(mvm, vif);
1087 mvm->bound_vif_cnt--;
1088 iwl_mvm_power_update_binding(mvm, vif, false);
1090 iwl_mvm_mac_ctxt_remove(mvm, vif);
1092 mutex_unlock(&mvm->mutex);
1095 static void
1096 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1097 struct ieee80211_vif *vif,
1098 struct ieee80211_bss_conf *bss_conf,
1099 u32 changes)
1101 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1102 enum ieee80211_bss_change ht_change = BSS_CHANGED_ERP_CTS_PROT |
1103 BSS_CHANGED_HT |
1104 BSS_CHANGED_BANDWIDTH;
1105 int ret;
1107 /* Changes will be applied when the AP/IBSS is started */
1108 if (!mvmvif->ap_ibss_active)
1109 return;
1111 if (changes & ht_change) {
1112 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1113 if (ret)
1114 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1117 /* Need to send a new beacon template to the FW */
1118 if (changes & BSS_CHANGED_BEACON) {
1119 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1120 IWL_WARN(mvm, "Failed updating beacon data\n");
1124 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1125 struct ieee80211_vif *vif,
1126 struct ieee80211_bss_conf *bss_conf,
1127 u32 changes)
1129 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1131 mutex_lock(&mvm->mutex);
1133 switch (vif->type) {
1134 case NL80211_IFTYPE_STATION:
1135 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1136 break;
1137 case NL80211_IFTYPE_AP:
1138 case NL80211_IFTYPE_ADHOC:
1139 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1140 break;
1141 default:
1142 /* shouldn't happen */
1143 WARN_ON_ONCE(1);
1146 mutex_unlock(&mvm->mutex);
1149 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1150 struct ieee80211_vif *vif,
1151 struct cfg80211_scan_request *req)
1153 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1154 int ret;
1156 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
1157 return -EINVAL;
1159 mutex_lock(&mvm->mutex);
1161 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1162 ret = iwl_mvm_scan_request(mvm, vif, req);
1163 else
1164 ret = -EBUSY;
1166 mutex_unlock(&mvm->mutex);
1168 return ret;
1171 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1172 struct ieee80211_vif *vif)
1174 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1176 mutex_lock(&mvm->mutex);
1178 iwl_mvm_cancel_scan(mvm);
1180 mutex_unlock(&mvm->mutex);
1183 static void
1184 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1185 struct ieee80211_sta *sta, u16 tids,
1186 int num_frames,
1187 enum ieee80211_frame_release_type reason,
1188 bool more_data)
1190 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1192 /* Called when we need to transmit (a) frame(s) from mac80211 */
1194 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1195 tids, more_data, false);
1198 static void
1199 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
1200 struct ieee80211_sta *sta, u16 tids,
1201 int num_frames,
1202 enum ieee80211_frame_release_type reason,
1203 bool more_data)
1205 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1207 /* Called when we need to transmit (a) frame(s) from agg queue */
1209 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1210 tids, more_data, true);
1213 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1214 struct ieee80211_vif *vif,
1215 enum sta_notify_cmd cmd,
1216 struct ieee80211_sta *sta)
1218 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1219 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1220 int tid;
1222 switch (cmd) {
1223 case STA_NOTIFY_SLEEP:
1224 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1225 ieee80211_sta_block_awake(hw, sta, true);
1226 spin_lock_bh(&mvmsta->lock);
1227 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1228 struct iwl_mvm_tid_data *tid_data;
1230 tid_data = &mvmsta->tid_data[tid];
1231 if (tid_data->state != IWL_AGG_ON &&
1232 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
1233 continue;
1234 if (iwl_mvm_tid_queued(tid_data) == 0)
1235 continue;
1236 ieee80211_sta_set_buffered(sta, tid, true);
1238 spin_unlock_bh(&mvmsta->lock);
1240 * The fw updates the STA to be asleep. Tx packets on the Tx
1241 * queues to this station will not be transmitted. The fw will
1242 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1244 break;
1245 case STA_NOTIFY_AWAKE:
1246 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1247 break;
1248 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1249 break;
1250 default:
1251 break;
1255 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
1256 struct ieee80211_vif *vif,
1257 struct ieee80211_sta *sta)
1259 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1260 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1263 * This is called before mac80211 does RCU synchronisation,
1264 * so here we already invalidate our internal RCU-protected
1265 * station pointer. The rest of the code will thus no longer
1266 * be able to find the station this way, and we don't rely
1267 * on further RCU synchronisation after the sta_state()
1268 * callback deleted the station.
1270 mutex_lock(&mvm->mutex);
1271 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
1272 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1273 ERR_PTR(-ENOENT));
1274 mutex_unlock(&mvm->mutex);
1277 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1278 struct ieee80211_vif *vif,
1279 struct ieee80211_sta *sta,
1280 enum ieee80211_sta_state old_state,
1281 enum ieee80211_sta_state new_state)
1283 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1284 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1285 int ret;
1287 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1288 sta->addr, old_state, new_state);
1290 /* this would be a mac80211 bug ... but don't crash */
1291 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1292 return -EINVAL;
1294 /* if a STA is being removed, reuse its ID */
1295 flush_work(&mvm->sta_drained_wk);
1297 mutex_lock(&mvm->mutex);
1298 if (old_state == IEEE80211_STA_NOTEXIST &&
1299 new_state == IEEE80211_STA_NONE) {
1301 * Firmware bug - it'll crash if the beacon interval is less
1302 * than 16. We can't avoid connecting at all, so refuse the
1303 * station state change, this will cause mac80211 to abandon
1304 * attempts to connect to this AP, and eventually wpa_s will
1305 * blacklist the AP...
1307 if (vif->type == NL80211_IFTYPE_STATION &&
1308 vif->bss_conf.beacon_int < 16) {
1309 IWL_ERR(mvm,
1310 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1311 sta->addr, vif->bss_conf.beacon_int);
1312 ret = -EINVAL;
1313 goto out_unlock;
1315 ret = iwl_mvm_add_sta(mvm, vif, sta);
1316 } else if (old_state == IEEE80211_STA_NONE &&
1317 new_state == IEEE80211_STA_AUTH) {
1318 ret = 0;
1319 } else if (old_state == IEEE80211_STA_AUTH &&
1320 new_state == IEEE80211_STA_ASSOC) {
1321 ret = iwl_mvm_update_sta(mvm, vif, sta);
1322 if (ret == 0)
1323 iwl_mvm_rs_rate_init(mvm, sta,
1324 mvmvif->phy_ctxt->channel->band,
1325 true);
1326 } else if (old_state == IEEE80211_STA_ASSOC &&
1327 new_state == IEEE80211_STA_AUTHORIZED) {
1328 /* enable beacon filtering */
1329 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
1330 ret = 0;
1331 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1332 new_state == IEEE80211_STA_ASSOC) {
1333 /* disable beacon filtering */
1334 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
1335 ret = 0;
1336 } else if (old_state == IEEE80211_STA_ASSOC &&
1337 new_state == IEEE80211_STA_AUTH) {
1338 ret = 0;
1339 } else if (old_state == IEEE80211_STA_AUTH &&
1340 new_state == IEEE80211_STA_NONE) {
1341 ret = 0;
1342 } else if (old_state == IEEE80211_STA_NONE &&
1343 new_state == IEEE80211_STA_NOTEXIST) {
1344 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1345 } else {
1346 ret = -EIO;
1348 out_unlock:
1349 mutex_unlock(&mvm->mutex);
1351 return ret;
1354 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1356 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1358 mvm->rts_threshold = value;
1360 return 0;
1363 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
1364 struct ieee80211_vif *vif,
1365 struct ieee80211_sta *sta, u32 changed)
1367 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1369 if (vif->type == NL80211_IFTYPE_STATION &&
1370 changed & IEEE80211_RC_NSS_CHANGED)
1371 iwl_mvm_sf_update(mvm, vif, false);
1374 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1375 struct ieee80211_vif *vif, u16 ac,
1376 const struct ieee80211_tx_queue_params *params)
1378 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1379 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1381 mvmvif->queue_params[ac] = *params;
1384 * No need to update right away, we'll get BSS_CHANGED_QOS
1385 * The exception is P2P_DEVICE interface which needs immediate update.
1387 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1388 int ret;
1390 mutex_lock(&mvm->mutex);
1391 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1392 mutex_unlock(&mvm->mutex);
1393 return ret;
1395 return 0;
1398 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1399 struct ieee80211_vif *vif)
1401 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1402 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1403 200 + vif->bss_conf.beacon_int);
1404 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1405 100 + vif->bss_conf.beacon_int);
1407 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1408 return;
1410 mutex_lock(&mvm->mutex);
1411 /* Try really hard to protect the session and hear a beacon */
1412 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
1413 mutex_unlock(&mvm->mutex);
1416 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
1417 struct ieee80211_vif *vif,
1418 struct cfg80211_sched_scan_request *req,
1419 struct ieee80211_sched_scan_ies *ies)
1421 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1422 int ret;
1424 mutex_lock(&mvm->mutex);
1426 if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
1427 IWL_DEBUG_SCAN(mvm,
1428 "SCHED SCAN request during internal scan - abort\n");
1429 ret = -EBUSY;
1430 goto out;
1433 mvm->scan_status = IWL_MVM_SCAN_SCHED;
1435 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1436 if (ret)
1437 goto err;
1439 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1440 if (ret)
1441 goto err;
1443 ret = iwl_mvm_sched_scan_start(mvm, req);
1444 if (!ret)
1445 goto out;
1446 err:
1447 mvm->scan_status = IWL_MVM_SCAN_NONE;
1448 out:
1449 mutex_unlock(&mvm->mutex);
1450 return ret;
1453 static void iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
1454 struct ieee80211_vif *vif)
1456 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1458 mutex_lock(&mvm->mutex);
1459 iwl_mvm_sched_scan_stop(mvm);
1460 mutex_unlock(&mvm->mutex);
1463 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1464 enum set_key_cmd cmd,
1465 struct ieee80211_vif *vif,
1466 struct ieee80211_sta *sta,
1467 struct ieee80211_key_conf *key)
1469 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1470 int ret;
1472 if (iwlwifi_mod_params.sw_crypto) {
1473 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1474 return -EOPNOTSUPP;
1477 switch (key->cipher) {
1478 case WLAN_CIPHER_SUITE_TKIP:
1479 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1480 /* fall-through */
1481 case WLAN_CIPHER_SUITE_CCMP:
1482 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1483 break;
1484 case WLAN_CIPHER_SUITE_AES_CMAC:
1485 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1486 break;
1487 case WLAN_CIPHER_SUITE_WEP40:
1488 case WLAN_CIPHER_SUITE_WEP104:
1490 * Support for TX only, at least for now, so accept
1491 * the key and do nothing else. Then mac80211 will
1492 * pass it for TX but we don't have to use it for RX.
1494 return 0;
1495 default:
1496 /* currently FW supports only one optional cipher scheme */
1497 if (hw->n_cipher_schemes &&
1498 hw->cipher_schemes->cipher == key->cipher)
1499 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
1500 else
1501 return -EOPNOTSUPP;
1504 mutex_lock(&mvm->mutex);
1506 switch (cmd) {
1507 case SET_KEY:
1508 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1509 vif->type == NL80211_IFTYPE_AP) && !sta) {
1511 * GTK on AP interface is a TX-only key, return 0;
1512 * on IBSS they're per-station and because we're lazy
1513 * we don't support them for RX, so do the same.
1515 ret = 0;
1516 key->hw_key_idx = STA_KEY_IDX_INVALID;
1517 break;
1520 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1521 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1522 if (ret) {
1523 IWL_WARN(mvm, "set key failed\n");
1525 * can't add key for RX, but we don't need it
1526 * in the device for TX so still return 0
1528 key->hw_key_idx = STA_KEY_IDX_INVALID;
1529 ret = 0;
1532 break;
1533 case DISABLE_KEY:
1534 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1535 ret = 0;
1536 break;
1539 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1540 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1541 break;
1542 default:
1543 ret = -EINVAL;
1546 mutex_unlock(&mvm->mutex);
1547 return ret;
1550 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1551 struct ieee80211_vif *vif,
1552 struct ieee80211_key_conf *keyconf,
1553 struct ieee80211_sta *sta,
1554 u32 iv32, u16 *phase1key)
1556 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1558 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1559 return;
1561 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1565 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1566 struct ieee80211_vif *vif,
1567 struct ieee80211_channel *channel,
1568 int duration,
1569 enum ieee80211_roc_type type)
1571 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1572 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1573 struct cfg80211_chan_def chandef;
1574 struct iwl_mvm_phy_ctxt *phy_ctxt;
1575 int ret, i;
1577 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1578 duration, type);
1580 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1581 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1582 return -EINVAL;
1585 mutex_lock(&mvm->mutex);
1587 for (i = 0; i < NUM_PHY_CTX; i++) {
1588 phy_ctxt = &mvm->phy_ctxts[i];
1589 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1590 continue;
1592 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
1594 * Unbind the P2P_DEVICE from the current PHY context,
1595 * and if the PHY context is not used remove it.
1597 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1598 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1599 goto out_unlock;
1601 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1603 /* Bind the P2P_DEVICE to the current PHY Context */
1604 mvmvif->phy_ctxt = phy_ctxt;
1606 ret = iwl_mvm_binding_add_vif(mvm, vif);
1607 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1608 goto out_unlock;
1610 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1611 goto schedule_time_event;
1615 /* Need to update the PHY context only if the ROC channel changed */
1616 if (channel == mvmvif->phy_ctxt->channel)
1617 goto schedule_time_event;
1619 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1622 * Change the PHY context configuration as it is currently referenced
1623 * only by the P2P Device MAC
1625 if (mvmvif->phy_ctxt->ref == 1) {
1626 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
1627 &chandef, 1, 1);
1628 if (ret)
1629 goto out_unlock;
1630 } else {
1632 * The PHY context is shared with other MACs. Need to remove the
1633 * P2P Device from the binding, allocate an new PHY context and
1634 * create a new binding
1636 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1637 if (!phy_ctxt) {
1638 ret = -ENOSPC;
1639 goto out_unlock;
1642 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
1643 1, 1);
1644 if (ret) {
1645 IWL_ERR(mvm, "Failed to change PHY context\n");
1646 goto out_unlock;
1649 /* Unbind the P2P_DEVICE from the current PHY context */
1650 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1651 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1652 goto out_unlock;
1654 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1656 /* Bind the P2P_DEVICE to the new allocated PHY context */
1657 mvmvif->phy_ctxt = phy_ctxt;
1659 ret = iwl_mvm_binding_add_vif(mvm, vif);
1660 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1661 goto out_unlock;
1663 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1666 schedule_time_event:
1667 /* Schedule the time events */
1668 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
1670 out_unlock:
1671 mutex_unlock(&mvm->mutex);
1672 IWL_DEBUG_MAC80211(mvm, "leave\n");
1673 return ret;
1676 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1678 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1680 IWL_DEBUG_MAC80211(mvm, "enter\n");
1682 mutex_lock(&mvm->mutex);
1683 iwl_mvm_stop_p2p_roc(mvm);
1684 mutex_unlock(&mvm->mutex);
1686 IWL_DEBUG_MAC80211(mvm, "leave\n");
1687 return 0;
1690 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1691 struct ieee80211_chanctx_conf *ctx)
1693 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1694 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1695 struct iwl_mvm_phy_ctxt *phy_ctxt;
1696 int ret;
1698 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
1700 mutex_lock(&mvm->mutex);
1701 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1702 if (!phy_ctxt) {
1703 ret = -ENOSPC;
1704 goto out;
1707 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
1708 ctx->rx_chains_static,
1709 ctx->rx_chains_dynamic);
1710 if (ret) {
1711 IWL_ERR(mvm, "Failed to add PHY context\n");
1712 goto out;
1715 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
1716 *phy_ctxt_id = phy_ctxt->id;
1717 out:
1718 mutex_unlock(&mvm->mutex);
1719 return ret;
1722 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1723 struct ieee80211_chanctx_conf *ctx)
1725 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1726 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1727 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1729 mutex_lock(&mvm->mutex);
1730 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
1731 mutex_unlock(&mvm->mutex);
1734 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1735 struct ieee80211_chanctx_conf *ctx,
1736 u32 changed)
1738 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1739 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1740 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1742 if (WARN_ONCE((phy_ctxt->ref > 1) &&
1743 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
1744 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1745 IEEE80211_CHANCTX_CHANGE_RADAR |
1746 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
1747 "Cannot change PHY. Ref=%d, changed=0x%X\n",
1748 phy_ctxt->ref, changed))
1749 return;
1751 mutex_lock(&mvm->mutex);
1752 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
1753 ctx->rx_chains_static,
1754 ctx->rx_chains_dynamic);
1755 iwl_mvm_bt_coex_vif_change(mvm);
1756 mutex_unlock(&mvm->mutex);
1759 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1760 struct ieee80211_vif *vif,
1761 struct ieee80211_chanctx_conf *ctx)
1763 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1764 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1765 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1766 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1767 int ret;
1769 mutex_lock(&mvm->mutex);
1771 mvmvif->phy_ctxt = phy_ctxt;
1773 switch (vif->type) {
1774 case NL80211_IFTYPE_AP:
1775 case NL80211_IFTYPE_ADHOC:
1777 * The AP binding flow is handled as part of the start_ap flow
1778 * (in bss_info_changed), similarly for IBSS.
1780 ret = 0;
1781 goto out_unlock;
1782 case NL80211_IFTYPE_STATION:
1783 case NL80211_IFTYPE_MONITOR:
1784 break;
1785 default:
1786 ret = -EINVAL;
1787 goto out_unlock;
1790 ret = iwl_mvm_binding_add_vif(mvm, vif);
1791 if (ret)
1792 goto out_unlock;
1795 * Power state must be updated before quotas,
1796 * otherwise fw will complain.
1798 mvm->bound_vif_cnt++;
1799 iwl_mvm_power_update_binding(mvm, vif, true);
1801 /* Setting the quota at this stage is only required for monitor
1802 * interfaces. For the other types, the bss_info changed flow
1803 * will handle quota settings.
1805 if (vif->type == NL80211_IFTYPE_MONITOR) {
1806 mvmvif->monitor_active = true;
1807 ret = iwl_mvm_update_quotas(mvm, vif);
1808 if (ret)
1809 goto out_remove_binding;
1812 goto out_unlock;
1814 out_remove_binding:
1815 iwl_mvm_binding_remove_vif(mvm, vif);
1816 mvm->bound_vif_cnt--;
1817 iwl_mvm_power_update_binding(mvm, vif, false);
1818 out_unlock:
1819 mutex_unlock(&mvm->mutex);
1820 if (ret)
1821 mvmvif->phy_ctxt = NULL;
1822 return ret;
1825 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1826 struct ieee80211_vif *vif,
1827 struct ieee80211_chanctx_conf *ctx)
1829 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1830 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1832 mutex_lock(&mvm->mutex);
1834 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1836 switch (vif->type) {
1837 case NL80211_IFTYPE_AP:
1838 case NL80211_IFTYPE_ADHOC:
1839 goto out_unlock;
1840 case NL80211_IFTYPE_MONITOR:
1841 mvmvif->monitor_active = false;
1842 iwl_mvm_update_quotas(mvm, NULL);
1843 break;
1844 default:
1845 break;
1848 iwl_mvm_binding_remove_vif(mvm, vif);
1849 mvm->bound_vif_cnt--;
1850 iwl_mvm_power_update_binding(mvm, vif, false);
1852 out_unlock:
1853 mvmvif->phy_ctxt = NULL;
1854 mutex_unlock(&mvm->mutex);
1857 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1858 struct ieee80211_sta *sta,
1859 bool set)
1861 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1862 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1864 if (!mvm_sta || !mvm_sta->vif) {
1865 IWL_ERR(mvm, "Station is not associated to a vif\n");
1866 return -EINVAL;
1869 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1872 #ifdef CONFIG_NL80211_TESTMODE
1873 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
1874 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
1875 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
1876 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
1879 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
1880 struct ieee80211_vif *vif,
1881 void *data, int len)
1883 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
1884 int err;
1885 u32 noa_duration;
1887 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
1888 if (err)
1889 return err;
1891 if (!tb[IWL_MVM_TM_ATTR_CMD])
1892 return -EINVAL;
1894 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
1895 case IWL_MVM_TM_CMD_SET_NOA:
1896 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
1897 !vif->bss_conf.enable_beacon ||
1898 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
1899 return -EINVAL;
1901 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
1902 if (noa_duration >= vif->bss_conf.beacon_int)
1903 return -EINVAL;
1905 mvm->noa_duration = noa_duration;
1906 mvm->noa_vif = vif;
1908 return iwl_mvm_update_quotas(mvm, NULL);
1909 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
1910 /* must be associated client vif - ignore authorized */
1911 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
1912 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
1913 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
1914 return -EINVAL;
1916 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
1917 return iwl_mvm_enable_beacon_filter(mvm, vif);
1918 return iwl_mvm_disable_beacon_filter(mvm, vif);
1921 return -EOPNOTSUPP;
1924 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
1925 struct ieee80211_vif *vif,
1926 void *data, int len)
1928 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1929 int err;
1931 mutex_lock(&mvm->mutex);
1932 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
1933 mutex_unlock(&mvm->mutex);
1935 return err;
1937 #endif
1939 struct ieee80211_ops iwl_mvm_hw_ops = {
1940 .tx = iwl_mvm_mac_tx,
1941 .ampdu_action = iwl_mvm_mac_ampdu_action,
1942 .start = iwl_mvm_mac_start,
1943 .restart_complete = iwl_mvm_mac_restart_complete,
1944 .stop = iwl_mvm_mac_stop,
1945 .add_interface = iwl_mvm_mac_add_interface,
1946 .remove_interface = iwl_mvm_mac_remove_interface,
1947 .config = iwl_mvm_mac_config,
1948 .prepare_multicast = iwl_mvm_prepare_multicast,
1949 .configure_filter = iwl_mvm_configure_filter,
1950 .bss_info_changed = iwl_mvm_bss_info_changed,
1951 .hw_scan = iwl_mvm_mac_hw_scan,
1952 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1953 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
1954 .sta_state = iwl_mvm_mac_sta_state,
1955 .sta_notify = iwl_mvm_mac_sta_notify,
1956 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1957 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
1958 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1959 .sta_rc_update = iwl_mvm_sta_rc_update,
1960 .conf_tx = iwl_mvm_mac_conf_tx,
1961 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1962 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
1963 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
1964 .set_key = iwl_mvm_mac_set_key,
1965 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1966 .remain_on_channel = iwl_mvm_roc,
1967 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
1968 .add_chanctx = iwl_mvm_add_chanctx,
1969 .remove_chanctx = iwl_mvm_remove_chanctx,
1970 .change_chanctx = iwl_mvm_change_chanctx,
1971 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1972 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1974 .start_ap = iwl_mvm_start_ap_ibss,
1975 .stop_ap = iwl_mvm_stop_ap_ibss,
1976 .join_ibss = iwl_mvm_start_ap_ibss,
1977 .leave_ibss = iwl_mvm_stop_ap_ibss,
1979 .set_tim = iwl_mvm_set_tim,
1981 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
1983 #ifdef CONFIG_PM_SLEEP
1984 /* look at d3.c */
1985 .suspend = iwl_mvm_suspend,
1986 .resume = iwl_mvm_resume,
1987 .set_wakeup = iwl_mvm_set_wakeup,
1988 .set_rekey_data = iwl_mvm_set_rekey_data,
1989 #if IS_ENABLED(CONFIG_IPV6)
1990 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1991 #endif
1992 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1993 #endif