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 - 2013 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 LICENSE.GPL.
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 - 2013 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>
68 #include <net/mac80211.h>
70 #include "iwl-op-mode.h"
74 #include "time-event.h"
75 #include "iwl-eeprom-parse.h"
76 #include "fw-api-scan.h"
77 #include "iwl-phy-db.h"
79 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
82 .types = BIT(NL80211_IFTYPE_STATION) |
83 BIT(NL80211_IFTYPE_AP),
87 .types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
88 BIT(NL80211_IFTYPE_P2P_GO),
92 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
96 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
98 .num_different_channels = 1,
100 .limits = iwl_mvm_limits,
101 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
105 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
107 struct ieee80211_hw *hw = mvm->hw;
110 /* Tell mac80211 our characteristics */
111 hw->flags = IEEE80211_HW_SIGNAL_DBM |
112 IEEE80211_HW_SPECTRUM_MGMT |
113 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
114 IEEE80211_HW_QUEUE_CONTROL |
115 IEEE80211_HW_WANT_MONITOR_VIF |
116 IEEE80211_HW_SUPPORTS_PS |
117 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
118 IEEE80211_HW_AMPDU_AGGREGATION |
119 IEEE80211_HW_TIMING_BEACON_ONLY;
121 hw->queues = IWL_FIRST_AMPDU_QUEUE;
122 hw->offchannel_tx_hw_queue = IWL_OFFCHANNEL_QUEUE;
123 hw->rate_control_algorithm = "iwl-mvm-rs";
126 * Enable 11w if advertised by firmware and software crypto
127 * is not enabled (as the firmware will interpret some mgmt
128 * packets, so enabling it with software crypto isn't safe)
130 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
131 !iwlwifi_mod_params.sw_crypto)
132 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
134 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
135 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
136 hw->chanctx_data_size = sizeof(struct iwl_mvm_phy_ctxt);
138 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
139 BIT(NL80211_IFTYPE_P2P_CLIENT) |
140 BIT(NL80211_IFTYPE_AP) |
141 BIT(NL80211_IFTYPE_P2P_GO) |
142 BIT(NL80211_IFTYPE_P2P_DEVICE);
144 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
145 WIPHY_FLAG_DISABLE_BEACON_HINTS |
148 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
149 hw->wiphy->n_iface_combinations =
150 ARRAY_SIZE(iwl_mvm_iface_combinations);
152 hw->wiphy->max_remain_on_channel_duration = 500;
153 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
155 /* Extract MAC address */
156 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
157 hw->wiphy->addresses = mvm->addresses;
158 hw->wiphy->n_addresses = 1;
159 num_mac = mvm->nvm_data->n_hw_addrs;
161 memcpy(mvm->addresses[1].addr, mvm->addresses[0].addr,
163 mvm->addresses[1].addr[5]++;
164 hw->wiphy->n_addresses++;
167 /* we create the 802.11 header and a max-length SSID element */
168 hw->wiphy->max_scan_ie_len =
169 mvm->fw->ucode_capa.max_probe_length - 24 - 34;
170 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
172 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
173 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
174 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
175 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
176 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
177 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
179 hw->wiphy->hw_version = mvm->trans->hw_id;
181 if (iwlwifi_mod_params.power_save)
182 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
184 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
186 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
187 NL80211_FEATURE_P2P_GO_OPPPS;
189 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
191 #ifdef CONFIG_PM_SLEEP
192 if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
193 mvm->trans->ops->d3_suspend &&
194 mvm->trans->ops->d3_resume &&
195 device_can_wakeup(mvm->trans->dev)) {
196 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
197 WIPHY_WOWLAN_DISCONNECT |
198 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
199 WIPHY_WOWLAN_RFKILL_RELEASE;
200 if (!iwlwifi_mod_params.sw_crypto)
201 hw->wiphy->wowlan.flags |=
202 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
203 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
204 WIPHY_WOWLAN_4WAY_HANDSHAKE;
206 hw->wiphy->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
207 hw->wiphy->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
208 hw->wiphy->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
212 ret = iwl_mvm_leds_init(mvm);
216 return ieee80211_register_hw(mvm->hw);
219 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
220 struct ieee80211_tx_control *control,
223 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
225 if (test_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status)) {
226 IWL_DEBUG_DROP(mvm, "Dropping - RF KILL\n");
230 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_OFFCHANNEL_QUEUE &&
231 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
235 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
240 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
244 ieee80211_free_txskb(hw, skb);
247 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
248 struct ieee80211_vif *vif,
249 enum ieee80211_ampdu_mlme_action action,
250 struct ieee80211_sta *sta, u16 tid,
251 u16 *ssn, u8 buf_size)
253 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
256 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
257 sta->addr, tid, action);
259 if (!(mvm->nvm_data->sku_cap_11n_enable))
262 mutex_lock(&mvm->mutex);
265 case IEEE80211_AMPDU_RX_START:
266 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
270 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
272 case IEEE80211_AMPDU_RX_STOP:
273 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
275 case IEEE80211_AMPDU_TX_START:
276 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
278 case IEEE80211_AMPDU_TX_STOP_CONT:
279 case IEEE80211_AMPDU_TX_STOP_FLUSH:
280 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
281 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
283 case IEEE80211_AMPDU_TX_OPERATIONAL:
284 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
291 mutex_unlock(&mvm->mutex);
296 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
297 struct ieee80211_vif *vif)
299 struct iwl_mvm *mvm = data;
300 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
302 mvmvif->uploaded = false;
303 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
305 /* does this make sense at all? */
308 spin_lock_bh(&mvm->time_event_lock);
309 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
310 spin_unlock_bh(&mvm->time_event_lock);
312 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
313 mvmvif->phy_ctxt = NULL;
316 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
318 iwl_trans_stop_device(mvm->trans);
319 iwl_trans_stop_hw(mvm->trans, false);
321 mvm->scan_status = IWL_MVM_SCAN_NONE;
323 /* just in case one was running */
324 ieee80211_remain_on_channel_expired(mvm->hw);
326 ieee80211_iterate_active_interfaces_atomic(
327 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
328 iwl_mvm_cleanup_iterator, mvm);
330 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
331 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
333 ieee80211_wake_queues(mvm->hw);
338 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
340 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
343 mutex_lock(&mvm->mutex);
345 /* Clean up some internal and mac80211 state on restart */
346 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
347 iwl_mvm_restart_cleanup(mvm);
349 ret = iwl_mvm_up(mvm);
350 mutex_unlock(&mvm->mutex);
355 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
357 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
360 mutex_lock(&mvm->mutex);
362 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
363 ret = iwl_mvm_update_quotas(mvm, NULL);
365 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
368 mutex_unlock(&mvm->mutex);
371 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
373 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
375 flush_work(&mvm->async_handlers_wk);
377 mutex_lock(&mvm->mutex);
378 /* async_handlers_wk is now blocked */
381 * The work item could be running or queued if the
382 * ROC time event stops just as we get here.
384 cancel_work_sync(&mvm->roc_done_wk);
386 iwl_trans_stop_device(mvm->trans);
387 iwl_trans_stop_hw(mvm->trans, false);
389 iwl_mvm_async_handlers_purge(mvm);
390 /* async_handlers_list is empty and will stay empty: HW is stopped */
392 /* the fw is stopped, the aux sta is dead: clean up driver state */
393 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
395 mutex_unlock(&mvm->mutex);
398 * The worker might have been waiting for the mutex, let it run and
399 * discover that its list is now empty.
401 cancel_work_sync(&mvm->async_handlers_wk);
404 static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
405 struct ieee80211_vif *vif)
407 struct iwl_mvm *mvm = data;
410 ret = iwl_mvm_power_disable(mvm, vif);
412 IWL_ERR(mvm, "failed to disable power management\n");
415 static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
416 struct ieee80211_vif *vif)
418 struct iwl_mvm *mvm = data;
420 iwl_mvm_power_update_mode(mvm, vif);
423 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
424 struct ieee80211_vif *vif)
426 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
427 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
431 * Not much to do here. The stack will not allow interface
432 * types or combinations that we didn't advertise, so we
433 * don't really have to check the types.
436 mutex_lock(&mvm->mutex);
438 /* Allocate resources for the MAC context, and add it the the fw */
439 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
444 * The AP binding flow can be done only after the beacon
445 * template is configured (which happens only in the mac80211
446 * start_ap() flow), and adding the broadcast station can happen
447 * only after the binding.
448 * In addition, since modifying the MAC before adding a bcast
449 * station is not allowed by the FW, delay the adding of MAC context to
450 * the point where we can also add the bcast station.
451 * In short: there's not much we can do at this point, other than
452 * allocating resources :)
454 if (vif->type == NL80211_IFTYPE_AP) {
455 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
456 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
459 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
467 * TODO: remove this temporary code.
468 * Currently MVM FW supports power management only on single MAC.
469 * Iterate and disable PM on all active interfaces.
470 * Note: the method below does not count the new interface being added
474 if (mvm->vif_count > 1) {
475 IWL_DEBUG_MAC80211(mvm,
476 "Disable power on existing interfaces\n");
477 ieee80211_iterate_active_interfaces_atomic(
479 IEEE80211_IFACE_ITER_NORMAL,
480 iwl_mvm_pm_disable_iterator, mvm);
483 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
488 * Update power state on the new interface. Admittedly, based on
489 * mac80211 logics this power update will disable power management
491 iwl_mvm_power_update_mode(mvm, vif);
494 * P2P_DEVICE interface does not have a channel context assigned to it,
495 * so a dedicated PHY context is allocated to it and the corresponding
496 * MAC context is bound to it at this stage.
498 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
499 struct ieee80211_channel *chan;
500 struct cfg80211_chan_def chandef;
502 mvmvif->phy_ctxt = &mvm->phy_ctxt_roc;
505 * The channel used here isn't relevant as it's
506 * going to be overwritten as part of the ROC flow.
507 * For now use the first channel we have.
509 chan = &mvm->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels[0];
510 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
511 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt,
516 ret = iwl_mvm_binding_add_vif(mvm, vif);
520 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
524 /* Save a pointer to p2p device vif, so it can later be used to
525 * update the p2p device MAC when a GO is started/stopped */
526 mvm->p2p_device_vif = vif;
532 iwl_mvm_binding_remove_vif(mvm, vif);
534 iwl_mvm_phy_ctxt_remove(mvm, mvmvif->phy_ctxt);
536 mvmvif->phy_ctxt = NULL;
537 iwl_mvm_mac_ctxt_remove(mvm, vif);
540 * TODO: remove this temporary code.
541 * Currently MVM FW supports power management only on single MAC.
542 * Check if only one additional interface remains after rereasing
543 * current one. Update power mode on the remaining interface.
546 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
548 if (mvm->vif_count == 1) {
549 ieee80211_iterate_active_interfaces(
550 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
551 iwl_mvm_power_update_iterator, mvm);
553 iwl_mvm_mac_ctxt_release(mvm, vif);
555 mutex_unlock(&mvm->mutex);
560 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
561 struct ieee80211_vif *vif)
565 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
566 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
567 tfd_msk |= BIT(vif->hw_queue[ac]);
569 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
570 tfd_msk |= BIT(vif->cab_queue);
573 mutex_lock(&mvm->mutex);
574 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
575 mutex_unlock(&mvm->mutex);
578 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
580 * Flush the ROC worker which will flush the OFFCHANNEL queue.
581 * We assume here that all the packets sent to the OFFCHANNEL
582 * queue are sent in ROC session.
584 flush_work(&mvm->roc_done_wk);
587 * By now, all the AC queues are empty. The AGG queues are
588 * empty too. We already got all the Tx responses for all the
589 * packets in the queues. The drain work can have been
590 * triggered. Flush it. This work item takes the mutex, so kill
591 * it before we take it.
593 flush_work(&mvm->sta_drained_wk);
597 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
598 struct ieee80211_vif *vif)
600 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
601 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
603 iwl_mvm_prepare_mac_removal(mvm, vif);
605 mutex_lock(&mvm->mutex);
608 * For AP/GO interface, the tear down of the resources allocated to the
609 * interface is be handled as part of the stop_ap flow.
611 if (vif->type == NL80211_IFTYPE_AP) {
612 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
616 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
617 mvm->p2p_device_vif = NULL;
618 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
619 iwl_mvm_binding_remove_vif(mvm, vif);
620 iwl_mvm_phy_ctxt_remove(mvm, mvmvif->phy_ctxt);
621 mvmvif->phy_ctxt = NULL;
625 * TODO: remove this temporary code.
626 * Currently MVM FW supports power management only on single MAC.
627 * Check if only one additional interface remains after removing
628 * current one. Update power mode on the remaining interface.
632 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
634 if (mvm->vif_count == 1) {
635 ieee80211_iterate_active_interfaces(
636 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
637 iwl_mvm_power_update_iterator, mvm);
640 iwl_mvm_mac_ctxt_remove(mvm, vif);
643 iwl_mvm_mac_ctxt_release(mvm, vif);
644 mutex_unlock(&mvm->mutex);
647 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
652 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
653 unsigned int changed_flags,
654 unsigned int *total_flags,
660 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
661 struct ieee80211_vif *vif,
662 struct ieee80211_bss_conf *bss_conf,
665 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
668 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
670 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
672 if (changes & BSS_CHANGED_ASSOC) {
673 if (bss_conf->assoc) {
674 /* add quota for this interface */
675 ret = iwl_mvm_update_quotas(mvm, vif);
677 IWL_ERR(mvm, "failed to update quotas\n");
680 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
681 /* remove AP station now that the MAC is unassoc */
682 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
684 IWL_ERR(mvm, "failed to remove AP station\n");
685 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
686 /* remove quota for this interface */
687 ret = iwl_mvm_update_quotas(mvm, NULL);
689 IWL_ERR(mvm, "failed to update quotas\n");
691 } else if (changes & BSS_CHANGED_DTIM_PERIOD) {
693 * We received a beacon _after_ association so
694 * remove the session protection.
696 iwl_mvm_remove_time_event(mvm, mvmvif,
697 &mvmvif->time_event_data);
698 } else if (changes & BSS_CHANGED_PS) {
700 * TODO: remove this temporary code.
701 * Currently MVM FW supports power management only on single
702 * MAC. Avoid power mode update if more than one interface
705 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
707 if (mvm->vif_count == 1) {
708 ret = iwl_mvm_power_update_mode(mvm, vif);
710 IWL_ERR(mvm, "failed to update power mode\n");
715 static int iwl_mvm_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
717 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
718 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
721 mutex_lock(&mvm->mutex);
723 /* Send the beacon template */
724 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
728 /* Add the mac context */
729 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
733 /* Perform the binding */
734 ret = iwl_mvm_binding_add_vif(mvm, vif);
738 mvmvif->ap_active = true;
740 /* Send the bcast station. At this stage the TBTT and DTIM time events
741 * are added and applied to the scheduler */
742 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
746 ret = iwl_mvm_update_quotas(mvm, vif);
750 /* Need to update the P2P Device MAC */
751 if (vif->p2p && mvm->p2p_device_vif)
752 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
754 mutex_unlock(&mvm->mutex);
758 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
760 iwl_mvm_binding_remove_vif(mvm, vif);
762 iwl_mvm_mac_ctxt_remove(mvm, vif);
764 mutex_unlock(&mvm->mutex);
768 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
770 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
771 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
773 iwl_mvm_prepare_mac_removal(mvm, vif);
775 mutex_lock(&mvm->mutex);
777 mvmvif->ap_active = false;
779 /* Need to update the P2P Device MAC */
780 if (vif->p2p && mvm->p2p_device_vif)
781 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
783 iwl_mvm_update_quotas(mvm, NULL);
784 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
785 iwl_mvm_binding_remove_vif(mvm, vif);
786 iwl_mvm_mac_ctxt_remove(mvm, vif);
788 mutex_unlock(&mvm->mutex);
791 static void iwl_mvm_bss_info_changed_ap(struct iwl_mvm *mvm,
792 struct ieee80211_vif *vif,
793 struct ieee80211_bss_conf *bss_conf,
796 /* Need to send a new beacon template to the FW */
797 if (changes & BSS_CHANGED_BEACON) {
798 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
799 IWL_WARN(mvm, "Failed updating beacon data\n");
803 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
804 struct ieee80211_vif *vif,
805 struct ieee80211_bss_conf *bss_conf,
808 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
810 mutex_lock(&mvm->mutex);
813 case NL80211_IFTYPE_STATION:
814 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
816 case NL80211_IFTYPE_AP:
817 iwl_mvm_bss_info_changed_ap(mvm, vif, bss_conf, changes);
820 /* shouldn't happen */
824 mutex_unlock(&mvm->mutex);
827 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
828 struct ieee80211_vif *vif,
829 struct cfg80211_scan_request *req)
831 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
834 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
837 mutex_lock(&mvm->mutex);
839 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
840 ret = iwl_mvm_scan_request(mvm, vif, req);
844 mutex_unlock(&mvm->mutex);
849 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
850 struct ieee80211_vif *vif)
852 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
854 mutex_lock(&mvm->mutex);
856 iwl_mvm_cancel_scan(mvm);
858 mutex_unlock(&mvm->mutex);
862 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
863 struct ieee80211_sta *sta, u16 tid,
865 enum ieee80211_frame_release_type reason,
868 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
870 /* TODO: how do we tell the fw to send frames for a specific TID */
873 * The fw will send EOSP notification when the last frame will be
876 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
879 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
880 struct ieee80211_vif *vif,
881 enum sta_notify_cmd cmd,
882 struct ieee80211_sta *sta)
884 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
885 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
888 case STA_NOTIFY_SLEEP:
889 if (atomic_read(&mvmsta->pending_frames) > 0)
890 ieee80211_sta_block_awake(hw, sta, true);
892 * The fw updates the STA to be asleep. Tx packets on the Tx
893 * queues to this station will not be transmitted. The fw will
894 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
897 case STA_NOTIFY_AWAKE:
898 if (WARN_ON(mvmsta->sta_id == IWL_INVALID_STATION))
900 iwl_mvm_sta_modify_ps_wake(mvm, sta);
907 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
908 struct ieee80211_vif *vif,
909 struct ieee80211_sta *sta,
910 enum ieee80211_sta_state old_state,
911 enum ieee80211_sta_state new_state)
913 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
914 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
917 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
918 sta->addr, old_state, new_state);
920 /* this would be a mac80211 bug ... but don't crash */
921 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
924 /* if a STA is being removed, reuse its ID */
925 flush_work(&mvm->sta_drained_wk);
927 mutex_lock(&mvm->mutex);
928 if (old_state == IEEE80211_STA_NOTEXIST &&
929 new_state == IEEE80211_STA_NONE) {
930 ret = iwl_mvm_add_sta(mvm, vif, sta);
931 } else if (old_state == IEEE80211_STA_NONE &&
932 new_state == IEEE80211_STA_AUTH) {
934 } else if (old_state == IEEE80211_STA_AUTH &&
935 new_state == IEEE80211_STA_ASSOC) {
936 ret = iwl_mvm_update_sta(mvm, vif, sta);
938 iwl_mvm_rs_rate_init(mvm, sta,
939 mvmvif->phy_ctxt->channel->band);
940 } else if (old_state == IEEE80211_STA_ASSOC &&
941 new_state == IEEE80211_STA_AUTHORIZED) {
943 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
944 new_state == IEEE80211_STA_ASSOC) {
946 } else if (old_state == IEEE80211_STA_ASSOC &&
947 new_state == IEEE80211_STA_AUTH) {
949 } else if (old_state == IEEE80211_STA_AUTH &&
950 new_state == IEEE80211_STA_NONE) {
952 } else if (old_state == IEEE80211_STA_NONE &&
953 new_state == IEEE80211_STA_NOTEXIST) {
954 ret = iwl_mvm_rm_sta(mvm, vif, sta);
958 mutex_unlock(&mvm->mutex);
963 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
965 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
967 mvm->rts_threshold = value;
972 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
973 struct ieee80211_vif *vif, u16 ac,
974 const struct ieee80211_tx_queue_params *params)
976 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
977 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
979 mvmvif->queue_params[ac] = *params;
982 * No need to update right away, we'll get BSS_CHANGED_QOS
983 * The exception is P2P_DEVICE interface which needs immediate update.
985 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
988 mutex_lock(&mvm->mutex);
989 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
990 mutex_unlock(&mvm->mutex);
996 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
997 struct ieee80211_vif *vif)
999 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1000 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1001 200 + vif->bss_conf.beacon_int);
1002 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1003 100 + vif->bss_conf.beacon_int);
1005 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1008 mutex_lock(&mvm->mutex);
1009 /* Try really hard to protect the session and hear a beacon */
1010 iwl_mvm_protect_session(mvm, vif, duration, min_duration);
1011 mutex_unlock(&mvm->mutex);
1014 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1015 enum set_key_cmd cmd,
1016 struct ieee80211_vif *vif,
1017 struct ieee80211_sta *sta,
1018 struct ieee80211_key_conf *key)
1020 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1023 if (iwlwifi_mod_params.sw_crypto) {
1024 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1028 switch (key->cipher) {
1029 case WLAN_CIPHER_SUITE_TKIP:
1030 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1032 case WLAN_CIPHER_SUITE_CCMP:
1033 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1035 case WLAN_CIPHER_SUITE_AES_CMAC:
1036 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1038 case WLAN_CIPHER_SUITE_WEP40:
1039 case WLAN_CIPHER_SUITE_WEP104:
1041 * Support for TX only, at least for now, so accept
1042 * the key and do nothing else. Then mac80211 will
1043 * pass it for TX but we don't have to use it for RX.
1050 mutex_lock(&mvm->mutex);
1054 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1055 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1057 IWL_WARN(mvm, "set key failed\n");
1059 * can't add key for RX, but we don't need it
1060 * in the device for TX so still return 0
1067 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1068 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1074 mutex_unlock(&mvm->mutex);
1078 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1079 struct ieee80211_vif *vif,
1080 struct ieee80211_key_conf *keyconf,
1081 struct ieee80211_sta *sta,
1082 u32 iv32, u16 *phase1key)
1084 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1086 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1090 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1091 struct ieee80211_vif *vif,
1092 struct ieee80211_channel *channel,
1095 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1096 struct cfg80211_chan_def chandef;
1099 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1100 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1104 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d)\n", channel->hw_value,
1107 mutex_lock(&mvm->mutex);
1109 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1110 ret = iwl_mvm_phy_ctxt_changed(mvm, &mvm->phy_ctxt_roc,
1113 /* Schedule the time events */
1114 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration);
1116 mutex_unlock(&mvm->mutex);
1117 IWL_DEBUG_MAC80211(mvm, "leave\n");
1122 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1124 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1126 IWL_DEBUG_MAC80211(mvm, "enter\n");
1128 mutex_lock(&mvm->mutex);
1129 iwl_mvm_stop_p2p_roc(mvm);
1130 mutex_unlock(&mvm->mutex);
1132 IWL_DEBUG_MAC80211(mvm, "leave\n");
1136 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1137 struct ieee80211_chanctx_conf *ctx)
1139 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1140 struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
1143 mutex_lock(&mvm->mutex);
1145 IWL_DEBUG_MAC80211(mvm, "Add PHY context\n");
1146 ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, &ctx->def,
1147 ctx->rx_chains_static,
1148 ctx->rx_chains_dynamic);
1149 mutex_unlock(&mvm->mutex);
1153 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1154 struct ieee80211_chanctx_conf *ctx)
1156 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1157 struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
1159 mutex_lock(&mvm->mutex);
1160 iwl_mvm_phy_ctxt_remove(mvm, phy_ctxt);
1161 mutex_unlock(&mvm->mutex);
1164 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1165 struct ieee80211_chanctx_conf *ctx,
1168 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1169 struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
1171 mutex_lock(&mvm->mutex);
1172 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1173 ctx->rx_chains_static,
1174 ctx->rx_chains_dynamic);
1175 mutex_unlock(&mvm->mutex);
1178 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1179 struct ieee80211_vif *vif,
1180 struct ieee80211_chanctx_conf *ctx)
1182 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1183 struct iwl_mvm_phy_ctxt *phyctx = (void *)ctx->drv_priv;
1184 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1187 mutex_lock(&mvm->mutex);
1189 mvmvif->phy_ctxt = phyctx;
1191 switch (vif->type) {
1192 case NL80211_IFTYPE_AP:
1194 * The AP binding flow is handled as part of the start_ap flow
1195 * (in bss_info_changed).
1199 case NL80211_IFTYPE_STATION:
1200 case NL80211_IFTYPE_ADHOC:
1201 case NL80211_IFTYPE_MONITOR:
1208 ret = iwl_mvm_binding_add_vif(mvm, vif);
1213 * Setting the quota at this stage is only required for monitor
1214 * interfaces. For the other types, the bss_info changed flow
1215 * will handle quota settings.
1217 if (vif->type == NL80211_IFTYPE_MONITOR) {
1218 ret = iwl_mvm_update_quotas(mvm, vif);
1220 goto out_remove_binding;
1226 iwl_mvm_binding_remove_vif(mvm, vif);
1228 mutex_unlock(&mvm->mutex);
1230 mvmvif->phy_ctxt = NULL;
1234 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1235 struct ieee80211_vif *vif,
1236 struct ieee80211_chanctx_conf *ctx)
1238 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1239 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1241 mutex_lock(&mvm->mutex);
1243 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1245 if (vif->type == NL80211_IFTYPE_AP)
1248 iwl_mvm_binding_remove_vif(mvm, vif);
1249 switch (vif->type) {
1250 case NL80211_IFTYPE_MONITOR:
1251 iwl_mvm_update_quotas(mvm, vif);
1258 mvmvif->phy_ctxt = NULL;
1259 mutex_unlock(&mvm->mutex);
1262 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1263 struct ieee80211_sta *sta,
1266 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1267 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1269 if (!mvm_sta || !mvm_sta->vif) {
1270 IWL_ERR(mvm, "Station is not associated to a vif\n");
1274 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1277 struct ieee80211_ops iwl_mvm_hw_ops = {
1278 .tx = iwl_mvm_mac_tx,
1279 .ampdu_action = iwl_mvm_mac_ampdu_action,
1280 .start = iwl_mvm_mac_start,
1281 .restart_complete = iwl_mvm_mac_restart_complete,
1282 .stop = iwl_mvm_mac_stop,
1283 .add_interface = iwl_mvm_mac_add_interface,
1284 .remove_interface = iwl_mvm_mac_remove_interface,
1285 .config = iwl_mvm_mac_config,
1286 .configure_filter = iwl_mvm_configure_filter,
1287 .bss_info_changed = iwl_mvm_bss_info_changed,
1288 .hw_scan = iwl_mvm_mac_hw_scan,
1289 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1290 .sta_state = iwl_mvm_mac_sta_state,
1291 .sta_notify = iwl_mvm_mac_sta_notify,
1292 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1293 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1294 .conf_tx = iwl_mvm_mac_conf_tx,
1295 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1296 .set_key = iwl_mvm_mac_set_key,
1297 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1298 .remain_on_channel = iwl_mvm_roc,
1299 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
1301 .add_chanctx = iwl_mvm_add_chanctx,
1302 .remove_chanctx = iwl_mvm_remove_chanctx,
1303 .change_chanctx = iwl_mvm_change_chanctx,
1304 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1305 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1307 .start_ap = iwl_mvm_start_ap,
1308 .stop_ap = iwl_mvm_stop_ap,
1310 .set_tim = iwl_mvm_set_tim,
1312 #ifdef CONFIG_PM_SLEEP
1314 .suspend = iwl_mvm_suspend,
1315 .resume = iwl_mvm_resume,
1316 .set_wakeup = iwl_mvm_set_wakeup,
1317 .set_rekey_data = iwl_mvm_set_rekey_data,
1318 #if IS_ENABLED(CONFIG_IPV6)
1319 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1321 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,