mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / wireless / iwlwifi / mvm / mac80211.c
blobc34b011769b70983709f441e4cf21977e0e5a34b
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 - 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,
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 - 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
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"
81 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
83 .max = 1,
84 .types = BIT(NL80211_IFTYPE_STATION),
87 .max = 1,
88 .types = BIT(NL80211_IFTYPE_AP) |
89 BIT(NL80211_IFTYPE_P2P_CLIENT) |
90 BIT(NL80211_IFTYPE_P2P_GO),
93 .max = 1,
94 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
98 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
100 .num_different_channels = 1,
101 .max_interfaces = 3,
102 .limits = iwl_mvm_limits,
103 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
107 #ifdef CONFIG_PM_SLEEP
108 static const struct nl80211_wowlan_tcp_data_token_feature
109 iwl_mvm_wowlan_tcp_token_feature = {
110 .min_len = 0,
111 .max_len = 255,
112 .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
115 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
116 .tok = &iwl_mvm_wowlan_tcp_token_feature,
117 .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
118 sizeof(struct ethhdr) -
119 sizeof(struct iphdr) -
120 sizeof(struct tcphdr),
121 .data_interval_max = 65535, /* __le16 in API */
122 .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
123 sizeof(struct ethhdr) -
124 sizeof(struct iphdr) -
125 sizeof(struct tcphdr),
126 .seq = true,
128 #endif
130 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
132 int i;
134 memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
135 for (i = 0; i < NUM_PHY_CTX; i++) {
136 mvm->phy_ctxts[i].id = i;
137 mvm->phy_ctxts[i].ref = 0;
141 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
143 struct ieee80211_hw *hw = mvm->hw;
144 int num_mac, ret, i;
146 /* Tell mac80211 our characteristics */
147 hw->flags = IEEE80211_HW_SIGNAL_DBM |
148 IEEE80211_HW_SPECTRUM_MGMT |
149 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
150 IEEE80211_HW_QUEUE_CONTROL |
151 IEEE80211_HW_WANT_MONITOR_VIF |
152 IEEE80211_HW_SUPPORTS_PS |
153 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
154 IEEE80211_HW_AMPDU_AGGREGATION |
155 IEEE80211_HW_TIMING_BEACON_ONLY |
156 IEEE80211_HW_CONNECTION_MONITOR |
157 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
158 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
159 IEEE80211_HW_SUPPORTS_UAPSD;
161 hw->queues = IWL_MVM_FIRST_AGG_QUEUE;
162 hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
163 hw->rate_control_algorithm = "iwl-mvm-rs";
166 * Enable 11w if advertised by firmware and software crypto
167 * is not enabled (as the firmware will interpret some mgmt
168 * packets, so enabling it with software crypto isn't safe)
170 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
171 !iwlwifi_mod_params.sw_crypto)
172 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
174 hw->sta_data_size = sizeof(struct iwl_mvm_sta);
175 hw->vif_data_size = sizeof(struct iwl_mvm_vif);
176 hw->chanctx_data_size = sizeof(u16);
178 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
179 BIT(NL80211_IFTYPE_P2P_CLIENT) |
180 BIT(NL80211_IFTYPE_AP) |
181 BIT(NL80211_IFTYPE_P2P_GO) |
182 BIT(NL80211_IFTYPE_P2P_DEVICE);
184 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
185 WIPHY_FLAG_DISABLE_BEACON_HINTS |
186 WIPHY_FLAG_IBSS_RSN;
188 hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
189 hw->wiphy->n_iface_combinations =
190 ARRAY_SIZE(iwl_mvm_iface_combinations);
192 hw->wiphy->max_remain_on_channel_duration = 10000;
193 hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
194 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
195 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
197 /* Extract MAC address */
198 memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
199 hw->wiphy->addresses = mvm->addresses;
200 hw->wiphy->n_addresses = 1;
202 /* Extract additional MAC addresses if available */
203 num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
204 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
206 for (i = 1; i < num_mac; i++) {
207 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
208 ETH_ALEN);
209 mvm->addresses[i].addr[5]++;
210 hw->wiphy->n_addresses++;
213 iwl_mvm_reset_phy_ctxts(mvm);
215 /* we create the 802.11 header and a max-length SSID element */
216 hw->wiphy->max_scan_ie_len =
217 mvm->fw->ucode_capa.max_probe_length - 24 - 34;
218 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
220 if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
221 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
222 &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
223 if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
224 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
225 &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
227 hw->wiphy->hw_version = mvm->trans->hw_id;
229 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
230 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
231 else
232 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
234 hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
235 NL80211_FEATURE_P2P_GO_OPPPS;
237 mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
239 #ifdef CONFIG_PM_SLEEP
240 if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
241 mvm->trans->ops->d3_suspend &&
242 mvm->trans->ops->d3_resume &&
243 device_can_wakeup(mvm->trans->dev)) {
244 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
245 WIPHY_WOWLAN_DISCONNECT |
246 WIPHY_WOWLAN_EAP_IDENTITY_REQ |
247 WIPHY_WOWLAN_RFKILL_RELEASE;
248 if (!iwlwifi_mod_params.sw_crypto)
249 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
250 WIPHY_WOWLAN_GTK_REKEY_FAILURE |
251 WIPHY_WOWLAN_4WAY_HANDSHAKE;
253 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
254 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
255 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
256 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
257 hw->wiphy->wowlan = &mvm->wowlan;
259 #endif
261 ret = iwl_mvm_leds_init(mvm);
262 if (ret)
263 return ret;
265 ret = ieee80211_register_hw(mvm->hw);
266 if (ret)
267 iwl_mvm_leds_exit(mvm);
269 return ret;
272 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
273 struct ieee80211_tx_control *control,
274 struct sk_buff *skb)
276 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
278 if (iwl_mvm_is_radio_killed(mvm)) {
279 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
280 goto drop;
283 if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
284 !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
285 goto drop;
287 if (control->sta) {
288 if (iwl_mvm_tx_skb(mvm, skb, control->sta))
289 goto drop;
290 return;
293 if (iwl_mvm_tx_skb_non_sta(mvm, skb))
294 goto drop;
295 return;
296 drop:
297 ieee80211_free_txskb(hw, skb);
300 static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
302 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
303 return false;
304 return true;
307 static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
309 if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
310 return false;
311 if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
312 return true;
314 /* enabled by default */
315 return true;
318 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
319 struct ieee80211_vif *vif,
320 enum ieee80211_ampdu_mlme_action action,
321 struct ieee80211_sta *sta, u16 tid,
322 u16 *ssn, u8 buf_size)
324 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
325 int ret;
327 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
328 sta->addr, tid, action);
330 if (!(mvm->nvm_data->sku_cap_11n_enable))
331 return -EACCES;
333 mutex_lock(&mvm->mutex);
335 switch (action) {
336 case IEEE80211_AMPDU_RX_START:
337 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
338 ret = -EINVAL;
339 break;
341 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
342 break;
343 case IEEE80211_AMPDU_RX_STOP:
344 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
345 break;
346 case IEEE80211_AMPDU_TX_START:
347 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
348 ret = -EINVAL;
349 break;
351 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
352 break;
353 case IEEE80211_AMPDU_TX_STOP_CONT:
354 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
355 break;
356 case IEEE80211_AMPDU_TX_STOP_FLUSH:
357 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
358 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
359 break;
360 case IEEE80211_AMPDU_TX_OPERATIONAL:
361 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
362 break;
363 default:
364 WARN_ON_ONCE(1);
365 ret = -EINVAL;
366 break;
368 mutex_unlock(&mvm->mutex);
370 return ret;
373 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
374 struct ieee80211_vif *vif)
376 struct iwl_mvm *mvm = data;
377 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
379 mvmvif->uploaded = false;
380 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
382 spin_lock_bh(&mvm->time_event_lock);
383 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
384 spin_unlock_bh(&mvm->time_event_lock);
386 mvmvif->phy_ctxt = NULL;
389 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
391 iwl_trans_stop_device(mvm->trans);
392 iwl_trans_stop_hw(mvm->trans, false);
394 mvm->scan_status = IWL_MVM_SCAN_NONE;
396 /* just in case one was running */
397 ieee80211_remain_on_channel_expired(mvm->hw);
399 ieee80211_iterate_active_interfaces_atomic(
400 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
401 iwl_mvm_cleanup_iterator, mvm);
403 mvm->p2p_device_vif = NULL;
405 iwl_mvm_reset_phy_ctxts(mvm);
406 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
407 memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
409 ieee80211_wake_queues(mvm->hw);
411 mvm->vif_count = 0;
412 mvm->rx_ba_sessions = 0;
415 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
417 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
418 int ret;
420 mutex_lock(&mvm->mutex);
422 /* Clean up some internal and mac80211 state on restart */
423 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
424 iwl_mvm_restart_cleanup(mvm);
426 ret = iwl_mvm_up(mvm);
427 mutex_unlock(&mvm->mutex);
429 return ret;
432 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
434 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
435 int ret;
437 mutex_lock(&mvm->mutex);
439 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
440 ret = iwl_mvm_update_quotas(mvm, NULL);
441 if (ret)
442 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
443 ret);
445 mutex_unlock(&mvm->mutex);
448 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
450 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
452 flush_work(&mvm->async_handlers_wk);
454 mutex_lock(&mvm->mutex);
455 /* async_handlers_wk is now blocked */
458 * The work item could be running or queued if the
459 * ROC time event stops just as we get here.
461 cancel_work_sync(&mvm->roc_done_wk);
463 iwl_trans_stop_device(mvm->trans);
464 iwl_trans_stop_hw(mvm->trans, false);
466 iwl_mvm_async_handlers_purge(mvm);
467 /* async_handlers_list is empty and will stay empty: HW is stopped */
469 /* the fw is stopped, the aux sta is dead: clean up driver state */
470 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
472 mutex_unlock(&mvm->mutex);
475 * The worker might have been waiting for the mutex, let it run and
476 * discover that its list is now empty.
478 cancel_work_sync(&mvm->async_handlers_wk);
481 static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
482 struct ieee80211_vif *vif)
484 struct iwl_mvm *mvm = data;
485 int ret;
487 ret = iwl_mvm_power_disable(mvm, vif);
488 if (ret)
489 IWL_ERR(mvm, "failed to disable power management\n");
492 static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
493 struct ieee80211_vif *vif)
495 struct iwl_mvm *mvm = data;
497 iwl_mvm_power_update_mode(mvm, vif);
500 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
502 u16 i;
504 lockdep_assert_held(&mvm->mutex);
506 for (i = 0; i < NUM_PHY_CTX; i++)
507 if (!mvm->phy_ctxts[i].ref)
508 return &mvm->phy_ctxts[i];
510 IWL_ERR(mvm, "No available PHY context\n");
511 return NULL;
514 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
515 struct ieee80211_vif *vif)
517 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
518 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
519 int ret;
522 * Not much to do here. The stack will not allow interface
523 * types or combinations that we didn't advertise, so we
524 * don't really have to check the types.
527 mutex_lock(&mvm->mutex);
529 /* Allocate resources for the MAC context, and add it to the fw */
530 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
531 if (ret)
532 goto out_unlock;
535 * TODO: remove this temporary code.
536 * Currently MVM FW supports power management only on single MAC.
537 * If new interface added, disable PM on existing interface.
538 * P2P device is a special case, since it is handled by FW similary to
539 * scan. If P2P deviced is added, PM remains enabled on existing
540 * interface.
541 * Note: the method below does not count the new interface being added
542 * at this moment.
544 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
545 mvm->vif_count++;
546 if (mvm->vif_count > 1) {
547 IWL_DEBUG_MAC80211(mvm,
548 "Disable power on existing interfaces\n");
549 ieee80211_iterate_active_interfaces_atomic(
550 mvm->hw,
551 IEEE80211_IFACE_ITER_NORMAL,
552 iwl_mvm_pm_disable_iterator, mvm);
556 * The AP binding flow can be done only after the beacon
557 * template is configured (which happens only in the mac80211
558 * start_ap() flow), and adding the broadcast station can happen
559 * only after the binding.
560 * In addition, since modifying the MAC before adding a bcast
561 * station is not allowed by the FW, delay the adding of MAC context to
562 * the point where we can also add the bcast station.
563 * In short: there's not much we can do at this point, other than
564 * allocating resources :)
566 if (vif->type == NL80211_IFTYPE_AP) {
567 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
568 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
569 qmask);
570 if (ret) {
571 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
572 goto out_release;
575 iwl_mvm_vif_dbgfs_register(mvm, vif);
576 goto out_unlock;
579 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
580 if (ret)
581 goto out_release;
584 * Update power state on the new interface. Admittedly, based on
585 * mac80211 logics this power update will disable power management
587 iwl_mvm_power_update_mode(mvm, vif);
589 /* beacon filtering */
590 ret = iwl_mvm_disable_beacon_filter(mvm, vif);
591 if (ret)
592 goto out_remove_mac;
594 if (!mvm->bf_allowed_vif &&
595 vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
596 mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
597 mvm->bf_allowed_vif = mvmvif;
598 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
599 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
603 * P2P_DEVICE interface does not have a channel context assigned to it,
604 * so a dedicated PHY context is allocated to it and the corresponding
605 * MAC context is bound to it at this stage.
607 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
609 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
610 if (!mvmvif->phy_ctxt) {
611 ret = -ENOSPC;
612 goto out_free_bf;
615 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
616 ret = iwl_mvm_binding_add_vif(mvm, vif);
617 if (ret)
618 goto out_unref_phy;
620 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
621 if (ret)
622 goto out_unbind;
624 /* Save a pointer to p2p device vif, so it can later be used to
625 * update the p2p device MAC when a GO is started/stopped */
626 mvm->p2p_device_vif = vif;
629 iwl_mvm_vif_dbgfs_register(mvm, vif);
630 goto out_unlock;
632 out_unbind:
633 iwl_mvm_binding_remove_vif(mvm, vif);
634 out_unref_phy:
635 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
636 out_free_bf:
637 if (mvm->bf_allowed_vif == mvmvif) {
638 mvm->bf_allowed_vif = NULL;
639 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
640 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
642 out_remove_mac:
643 mvmvif->phy_ctxt = NULL;
644 iwl_mvm_mac_ctxt_remove(mvm, vif);
645 out_release:
646 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
647 mvm->vif_count--;
648 ieee80211_iterate_active_interfaces(
649 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
650 iwl_mvm_power_update_iterator, mvm);
651 iwl_mvm_mac_ctxt_release(mvm, vif);
652 out_unlock:
653 mutex_unlock(&mvm->mutex);
655 return ret;
658 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
659 struct ieee80211_vif *vif)
661 u32 tfd_msk = 0, ac;
663 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
664 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
665 tfd_msk |= BIT(vif->hw_queue[ac]);
667 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
668 tfd_msk |= BIT(vif->cab_queue);
670 if (tfd_msk) {
671 mutex_lock(&mvm->mutex);
672 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
673 mutex_unlock(&mvm->mutex);
676 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
678 * Flush the ROC worker which will flush the OFFCHANNEL queue.
679 * We assume here that all the packets sent to the OFFCHANNEL
680 * queue are sent in ROC session.
682 flush_work(&mvm->roc_done_wk);
683 } else {
685 * By now, all the AC queues are empty. The AGG queues are
686 * empty too. We already got all the Tx responses for all the
687 * packets in the queues. The drain work can have been
688 * triggered. Flush it.
690 flush_work(&mvm->sta_drained_wk);
694 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
695 struct ieee80211_vif *vif)
697 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
698 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
700 iwl_mvm_prepare_mac_removal(mvm, vif);
702 mutex_lock(&mvm->mutex);
704 if (mvm->bf_allowed_vif == mvmvif) {
705 mvm->bf_allowed_vif = NULL;
706 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
707 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
710 iwl_mvm_vif_dbgfs_clean(mvm, vif);
713 * For AP/GO interface, the tear down of the resources allocated to the
714 * interface is be handled as part of the stop_ap flow.
716 if (vif->type == NL80211_IFTYPE_AP) {
717 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
718 goto out_release;
721 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
722 mvm->p2p_device_vif = NULL;
723 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
724 iwl_mvm_binding_remove_vif(mvm, vif);
725 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
726 mvmvif->phy_ctxt = NULL;
730 * TODO: remove this temporary code.
731 * Currently MVM FW supports power management only on single MAC.
732 * Check if only one additional interface remains after removing
733 * current one. Update power mode on the remaining interface.
735 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
736 mvm->vif_count--;
737 IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
738 mvm->vif_count);
739 if (mvm->vif_count == 1) {
740 ieee80211_iterate_active_interfaces(
741 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
742 iwl_mvm_power_update_iterator, mvm);
745 iwl_mvm_mac_ctxt_remove(mvm, vif);
747 out_release:
748 iwl_mvm_mac_ctxt_release(mvm, vif);
749 mutex_unlock(&mvm->mutex);
752 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
753 s8 tx_power)
755 /* FW is in charge of regulatory enforcement */
756 struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
757 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
758 .pwr_restriction = cpu_to_le16(tx_power),
761 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
762 sizeof(reduce_txpwr_cmd),
763 &reduce_txpwr_cmd);
766 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
768 return 0;
771 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
772 unsigned int changed_flags,
773 unsigned int *total_flags,
774 u64 multicast)
776 *total_flags = 0;
779 static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
780 struct ieee80211_vif *vif)
782 struct iwl_mcast_filter_cmd mcast_filter_cmd = {
783 .pass_all = 1,
786 memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
788 return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
789 sizeof(mcast_filter_cmd),
790 &mcast_filter_cmd);
793 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
794 struct ieee80211_vif *vif,
795 struct ieee80211_bss_conf *bss_conf,
796 u32 changes)
798 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
799 int ret;
801 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
802 if (ret)
803 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
805 if (changes & BSS_CHANGED_ASSOC) {
806 if (bss_conf->assoc) {
807 /* add quota for this interface */
808 ret = iwl_mvm_update_quotas(mvm, vif);
809 if (ret) {
810 IWL_ERR(mvm, "failed to update quotas\n");
811 return;
813 iwl_mvm_configure_mcast_filter(mvm, vif);
814 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
815 /* remove AP station now that the MAC is unassoc */
816 ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
817 if (ret)
818 IWL_ERR(mvm, "failed to remove AP station\n");
819 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
820 /* remove quota for this interface */
821 ret = iwl_mvm_update_quotas(mvm, NULL);
822 if (ret)
823 IWL_ERR(mvm, "failed to update quotas\n");
826 /* reset rssi values */
827 mvmvif->bf_data.ave_beacon_signal = 0;
829 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD)) {
830 /* Workaround for FW bug, otherwise FW disables device
831 * power save upon disassociation
833 ret = iwl_mvm_power_update_mode(mvm, vif);
834 if (ret)
835 IWL_ERR(mvm, "failed to update power mode\n");
837 iwl_mvm_bt_coex_vif_assoc(mvm, vif);
838 } else if (changes & BSS_CHANGED_BEACON_INFO) {
840 * We received a beacon _after_ association so
841 * remove the session protection.
843 iwl_mvm_remove_time_event(mvm, mvmvif,
844 &mvmvif->time_event_data);
845 } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_QOS)) {
846 ret = iwl_mvm_power_update_mode(mvm, vif);
847 if (ret)
848 IWL_ERR(mvm, "failed to update power mode\n");
850 if (changes & BSS_CHANGED_TXPOWER) {
851 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
852 bss_conf->txpower);
853 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
856 if (changes & BSS_CHANGED_CQM) {
857 IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
858 /* reset cqm events tracking */
859 mvmvif->bf_data.last_cqm_event = 0;
860 ret = iwl_mvm_update_beacon_filter(mvm, vif);
861 if (ret)
862 IWL_ERR(mvm, "failed to update CQM thresholds\n");
866 static int iwl_mvm_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
868 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
869 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
870 int ret;
872 mutex_lock(&mvm->mutex);
874 /* Send the beacon template */
875 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
876 if (ret)
877 goto out_unlock;
879 /* Add the mac context */
880 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
881 if (ret)
882 goto out_unlock;
884 /* Perform the binding */
885 ret = iwl_mvm_binding_add_vif(mvm, vif);
886 if (ret)
887 goto out_remove;
889 mvmvif->ap_active = true;
891 /* Send the bcast station. At this stage the TBTT and DTIM time events
892 * are added and applied to the scheduler */
893 ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
894 if (ret)
895 goto out_unbind;
897 ret = iwl_mvm_update_quotas(mvm, vif);
898 if (ret)
899 goto out_rm_bcast;
901 /* Need to update the P2P Device MAC */
902 if (vif->p2p && mvm->p2p_device_vif)
903 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
905 mutex_unlock(&mvm->mutex);
906 return 0;
908 out_rm_bcast:
909 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
910 out_unbind:
911 iwl_mvm_binding_remove_vif(mvm, vif);
912 out_remove:
913 iwl_mvm_mac_ctxt_remove(mvm, vif);
914 out_unlock:
915 mutex_unlock(&mvm->mutex);
916 return ret;
919 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
921 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
922 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
924 iwl_mvm_prepare_mac_removal(mvm, vif);
926 mutex_lock(&mvm->mutex);
928 mvmvif->ap_active = false;
930 /* Need to update the P2P Device MAC */
931 if (vif->p2p && mvm->p2p_device_vif)
932 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
934 iwl_mvm_update_quotas(mvm, NULL);
935 iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
936 iwl_mvm_binding_remove_vif(mvm, vif);
937 iwl_mvm_mac_ctxt_remove(mvm, vif);
939 mutex_unlock(&mvm->mutex);
942 static void iwl_mvm_bss_info_changed_ap(struct iwl_mvm *mvm,
943 struct ieee80211_vif *vif,
944 struct ieee80211_bss_conf *bss_conf,
945 u32 changes)
947 /* Need to send a new beacon template to the FW */
948 if (changes & BSS_CHANGED_BEACON) {
949 if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
950 IWL_WARN(mvm, "Failed updating beacon data\n");
954 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
955 struct ieee80211_vif *vif,
956 struct ieee80211_bss_conf *bss_conf,
957 u32 changes)
959 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
961 mutex_lock(&mvm->mutex);
963 switch (vif->type) {
964 case NL80211_IFTYPE_STATION:
965 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
966 break;
967 case NL80211_IFTYPE_AP:
968 iwl_mvm_bss_info_changed_ap(mvm, vif, bss_conf, changes);
969 break;
970 default:
971 /* shouldn't happen */
972 WARN_ON_ONCE(1);
975 mutex_unlock(&mvm->mutex);
978 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
979 struct ieee80211_vif *vif,
980 struct cfg80211_scan_request *req)
982 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
983 int ret;
985 if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
986 return -EINVAL;
988 mutex_lock(&mvm->mutex);
990 if (mvm->scan_status == IWL_MVM_SCAN_NONE)
991 ret = iwl_mvm_scan_request(mvm, vif, req);
992 else
993 ret = -EBUSY;
995 mutex_unlock(&mvm->mutex);
997 return ret;
1000 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1001 struct ieee80211_vif *vif)
1003 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1005 mutex_lock(&mvm->mutex);
1007 iwl_mvm_cancel_scan(mvm);
1009 mutex_unlock(&mvm->mutex);
1012 static void
1013 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1014 struct ieee80211_sta *sta, u16 tid,
1015 int num_frames,
1016 enum ieee80211_frame_release_type reason,
1017 bool more_data)
1019 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1021 /* TODO: how do we tell the fw to send frames for a specific TID */
1024 * The fw will send EOSP notification when the last frame will be
1025 * transmitted.
1027 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
1030 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1031 struct ieee80211_vif *vif,
1032 enum sta_notify_cmd cmd,
1033 struct ieee80211_sta *sta)
1035 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1036 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1038 switch (cmd) {
1039 case STA_NOTIFY_SLEEP:
1040 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1041 ieee80211_sta_block_awake(hw, sta, true);
1043 * The fw updates the STA to be asleep. Tx packets on the Tx
1044 * queues to this station will not be transmitted. The fw will
1045 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1047 break;
1048 case STA_NOTIFY_AWAKE:
1049 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1050 break;
1051 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1052 break;
1053 default:
1054 break;
1058 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1059 struct ieee80211_vif *vif,
1060 struct ieee80211_sta *sta,
1061 enum ieee80211_sta_state old_state,
1062 enum ieee80211_sta_state new_state)
1064 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1065 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1066 int ret;
1068 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1069 sta->addr, old_state, new_state);
1071 /* this would be a mac80211 bug ... but don't crash */
1072 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
1073 return -EINVAL;
1075 /* if a STA is being removed, reuse its ID */
1076 flush_work(&mvm->sta_drained_wk);
1078 mutex_lock(&mvm->mutex);
1079 if (old_state == IEEE80211_STA_NOTEXIST &&
1080 new_state == IEEE80211_STA_NONE) {
1082 * Firmware bug - it'll crash if the beacon interval is less
1083 * than 16. We can't avoid connecting at all, so refuse the
1084 * station state change, this will cause mac80211 to abandon
1085 * attempts to connect to this AP, and eventually wpa_s will
1086 * blacklist the AP...
1088 if (vif->type == NL80211_IFTYPE_STATION &&
1089 vif->bss_conf.beacon_int < 16) {
1090 IWL_ERR(mvm,
1091 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
1092 sta->addr, vif->bss_conf.beacon_int);
1093 ret = -EINVAL;
1094 goto out_unlock;
1096 ret = iwl_mvm_add_sta(mvm, vif, sta);
1097 } else if (old_state == IEEE80211_STA_NONE &&
1098 new_state == IEEE80211_STA_AUTH) {
1099 ret = 0;
1100 } else if (old_state == IEEE80211_STA_AUTH &&
1101 new_state == IEEE80211_STA_ASSOC) {
1102 ret = iwl_mvm_update_sta(mvm, vif, sta);
1103 if (ret == 0)
1104 iwl_mvm_rs_rate_init(mvm, sta,
1105 mvmvif->phy_ctxt->channel->band);
1106 } else if (old_state == IEEE80211_STA_ASSOC &&
1107 new_state == IEEE80211_STA_AUTHORIZED) {
1108 /* enable beacon filtering */
1109 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
1110 ret = 0;
1111 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
1112 new_state == IEEE80211_STA_ASSOC) {
1113 /* disable beacon filtering */
1114 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
1115 ret = 0;
1116 } else if (old_state == IEEE80211_STA_ASSOC &&
1117 new_state == IEEE80211_STA_AUTH) {
1118 ret = 0;
1119 } else if (old_state == IEEE80211_STA_AUTH &&
1120 new_state == IEEE80211_STA_NONE) {
1121 ret = 0;
1122 } else if (old_state == IEEE80211_STA_NONE &&
1123 new_state == IEEE80211_STA_NOTEXIST) {
1124 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1125 } else {
1126 ret = -EIO;
1128 out_unlock:
1129 mutex_unlock(&mvm->mutex);
1131 return ret;
1134 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1136 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1138 mvm->rts_threshold = value;
1140 return 0;
1143 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
1144 struct ieee80211_vif *vif, u16 ac,
1145 const struct ieee80211_tx_queue_params *params)
1147 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1148 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1150 mvmvif->queue_params[ac] = *params;
1153 * No need to update right away, we'll get BSS_CHANGED_QOS
1154 * The exception is P2P_DEVICE interface which needs immediate update.
1156 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1157 int ret;
1159 mutex_lock(&mvm->mutex);
1160 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
1161 mutex_unlock(&mvm->mutex);
1162 return ret;
1164 return 0;
1167 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
1168 struct ieee80211_vif *vif)
1170 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1171 u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
1172 200 + vif->bss_conf.beacon_int);
1173 u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
1174 100 + vif->bss_conf.beacon_int);
1176 if (WARN_ON_ONCE(vif->bss_conf.assoc))
1177 return;
1179 mutex_lock(&mvm->mutex);
1180 /* Try really hard to protect the session and hear a beacon */
1181 iwl_mvm_protect_session(mvm, vif, duration, min_duration);
1182 mutex_unlock(&mvm->mutex);
1185 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
1186 enum set_key_cmd cmd,
1187 struct ieee80211_vif *vif,
1188 struct ieee80211_sta *sta,
1189 struct ieee80211_key_conf *key)
1191 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1192 int ret;
1194 if (iwlwifi_mod_params.sw_crypto) {
1195 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
1196 return -EOPNOTSUPP;
1199 switch (key->cipher) {
1200 case WLAN_CIPHER_SUITE_TKIP:
1201 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1202 /* fall-through */
1203 case WLAN_CIPHER_SUITE_CCMP:
1204 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1205 break;
1206 case WLAN_CIPHER_SUITE_AES_CMAC:
1207 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
1208 break;
1209 case WLAN_CIPHER_SUITE_WEP40:
1210 case WLAN_CIPHER_SUITE_WEP104:
1212 * Support for TX only, at least for now, so accept
1213 * the key and do nothing else. Then mac80211 will
1214 * pass it for TX but we don't have to use it for RX.
1216 return 0;
1217 default:
1218 return -EOPNOTSUPP;
1221 mutex_lock(&mvm->mutex);
1223 switch (cmd) {
1224 case SET_KEY:
1225 if (vif->type == NL80211_IFTYPE_AP && !sta) {
1226 /* GTK on AP interface is a TX-only key, return 0 */
1227 ret = 0;
1228 key->hw_key_idx = STA_KEY_IDX_INVALID;
1229 break;
1232 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
1233 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
1234 if (ret) {
1235 IWL_WARN(mvm, "set key failed\n");
1237 * can't add key for RX, but we don't need it
1238 * in the device for TX so still return 0
1240 key->hw_key_idx = STA_KEY_IDX_INVALID;
1241 ret = 0;
1244 break;
1245 case DISABLE_KEY:
1246 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
1247 ret = 0;
1248 break;
1251 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
1252 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
1253 break;
1254 default:
1255 ret = -EINVAL;
1258 mutex_unlock(&mvm->mutex);
1259 return ret;
1262 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
1263 struct ieee80211_vif *vif,
1264 struct ieee80211_key_conf *keyconf,
1265 struct ieee80211_sta *sta,
1266 u32 iv32, u16 *phase1key)
1268 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1270 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
1274 static int iwl_mvm_roc(struct ieee80211_hw *hw,
1275 struct ieee80211_vif *vif,
1276 struct ieee80211_channel *channel,
1277 int duration,
1278 enum ieee80211_roc_type type)
1280 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1281 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1282 struct cfg80211_chan_def chandef;
1283 struct iwl_mvm_phy_ctxt *phy_ctxt;
1284 int ret, i;
1286 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
1287 duration, type);
1289 if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
1290 IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
1291 return -EINVAL;
1294 mutex_lock(&mvm->mutex);
1296 for (i = 0; i < NUM_PHY_CTX; i++) {
1297 phy_ctxt = &mvm->phy_ctxts[i];
1298 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
1299 continue;
1301 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
1303 * Unbind the P2P_DEVICE from the current PHY context,
1304 * and if the PHY context is not used remove it.
1306 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1307 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1308 goto out_unlock;
1310 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1312 /* Bind the P2P_DEVICE to the current PHY Context */
1313 mvmvif->phy_ctxt = phy_ctxt;
1315 ret = iwl_mvm_binding_add_vif(mvm, vif);
1316 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1317 goto out_unlock;
1319 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1320 goto schedule_time_event;
1324 /* Need to update the PHY context only if the ROC channel changed */
1325 if (channel == mvmvif->phy_ctxt->channel)
1326 goto schedule_time_event;
1328 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1331 * Change the PHY context configuration as it is currently referenced
1332 * only by the P2P Device MAC
1334 if (mvmvif->phy_ctxt->ref == 1) {
1335 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
1336 &chandef, 1, 1);
1337 if (ret)
1338 goto out_unlock;
1339 } else {
1341 * The PHY context is shared with other MACs. Need to remove the
1342 * P2P Device from the binding, allocate an new PHY context and
1343 * create a new binding
1345 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1346 if (!phy_ctxt) {
1347 ret = -ENOSPC;
1348 goto out_unlock;
1351 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
1352 1, 1);
1353 if (ret) {
1354 IWL_ERR(mvm, "Failed to change PHY context\n");
1355 goto out_unlock;
1358 /* Unbind the P2P_DEVICE from the current PHY context */
1359 ret = iwl_mvm_binding_remove_vif(mvm, vif);
1360 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
1361 goto out_unlock;
1363 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1365 /* Bind the P2P_DEVICE to the new allocated PHY context */
1366 mvmvif->phy_ctxt = phy_ctxt;
1368 ret = iwl_mvm_binding_add_vif(mvm, vif);
1369 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
1370 goto out_unlock;
1372 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1375 schedule_time_event:
1376 /* Schedule the time events */
1377 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
1379 out_unlock:
1380 mutex_unlock(&mvm->mutex);
1381 IWL_DEBUG_MAC80211(mvm, "leave\n");
1382 return ret;
1385 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
1387 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1389 IWL_DEBUG_MAC80211(mvm, "enter\n");
1391 mutex_lock(&mvm->mutex);
1392 iwl_mvm_stop_p2p_roc(mvm);
1393 mutex_unlock(&mvm->mutex);
1395 IWL_DEBUG_MAC80211(mvm, "leave\n");
1396 return 0;
1399 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
1400 struct ieee80211_chanctx_conf *ctx)
1402 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1403 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1404 struct iwl_mvm_phy_ctxt *phy_ctxt;
1405 int ret;
1407 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
1409 mutex_lock(&mvm->mutex);
1410 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1411 if (!phy_ctxt) {
1412 ret = -ENOSPC;
1413 goto out;
1416 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1417 ctx->rx_chains_static,
1418 ctx->rx_chains_dynamic);
1419 if (ret) {
1420 IWL_ERR(mvm, "Failed to add PHY context\n");
1421 goto out;
1424 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
1425 *phy_ctxt_id = phy_ctxt->id;
1426 out:
1427 mutex_unlock(&mvm->mutex);
1428 return ret;
1431 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
1432 struct ieee80211_chanctx_conf *ctx)
1434 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1435 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1436 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1438 mutex_lock(&mvm->mutex);
1439 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
1440 mutex_unlock(&mvm->mutex);
1443 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
1444 struct ieee80211_chanctx_conf *ctx,
1445 u32 changed)
1447 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1448 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1449 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1451 if (WARN_ONCE((phy_ctxt->ref > 1) &&
1452 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
1453 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1454 IEEE80211_CHANCTX_CHANGE_RADAR)),
1455 "Cannot change PHY. Ref=%d, changed=0x%X\n",
1456 phy_ctxt->ref, changed))
1457 return;
1459 mutex_lock(&mvm->mutex);
1460 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
1461 ctx->rx_chains_static,
1462 ctx->rx_chains_dynamic);
1463 mutex_unlock(&mvm->mutex);
1466 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
1467 struct ieee80211_vif *vif,
1468 struct ieee80211_chanctx_conf *ctx)
1470 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1471 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
1472 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
1473 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1474 int ret;
1476 mutex_lock(&mvm->mutex);
1478 mvmvif->phy_ctxt = phy_ctxt;
1480 switch (vif->type) {
1481 case NL80211_IFTYPE_AP:
1483 * The AP binding flow is handled as part of the start_ap flow
1484 * (in bss_info_changed).
1486 ret = 0;
1487 goto out_unlock;
1488 case NL80211_IFTYPE_STATION:
1489 case NL80211_IFTYPE_ADHOC:
1490 case NL80211_IFTYPE_MONITOR:
1491 break;
1492 default:
1493 ret = -EINVAL;
1494 goto out_unlock;
1497 ret = iwl_mvm_binding_add_vif(mvm, vif);
1498 if (ret)
1499 goto out_unlock;
1502 * Setting the quota at this stage is only required for monitor
1503 * interfaces. For the other types, the bss_info changed flow
1504 * will handle quota settings.
1506 if (vif->type == NL80211_IFTYPE_MONITOR) {
1507 mvmvif->monitor_active = true;
1508 ret = iwl_mvm_update_quotas(mvm, vif);
1509 if (ret)
1510 goto out_remove_binding;
1513 goto out_unlock;
1515 out_remove_binding:
1516 iwl_mvm_binding_remove_vif(mvm, vif);
1517 out_unlock:
1518 mutex_unlock(&mvm->mutex);
1519 if (ret)
1520 mvmvif->phy_ctxt = NULL;
1521 return ret;
1524 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
1525 struct ieee80211_vif *vif,
1526 struct ieee80211_chanctx_conf *ctx)
1528 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1529 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1531 mutex_lock(&mvm->mutex);
1533 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
1535 if (vif->type == NL80211_IFTYPE_AP)
1536 goto out_unlock;
1538 switch (vif->type) {
1539 case NL80211_IFTYPE_MONITOR:
1540 mvmvif->monitor_active = false;
1541 iwl_mvm_update_quotas(mvm, NULL);
1542 break;
1543 default:
1544 break;
1547 iwl_mvm_binding_remove_vif(mvm, vif);
1548 out_unlock:
1549 mvmvif->phy_ctxt = NULL;
1550 mutex_unlock(&mvm->mutex);
1553 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
1554 struct ieee80211_sta *sta,
1555 bool set)
1557 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1558 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1560 if (!mvm_sta || !mvm_sta->vif) {
1561 IWL_ERR(mvm, "Station is not associated to a vif\n");
1562 return -EINVAL;
1565 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
1568 static void iwl_mvm_mac_rssi_callback(struct ieee80211_hw *hw,
1569 struct ieee80211_vif *vif,
1570 enum ieee80211_rssi_event rssi_event)
1572 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1574 iwl_mvm_bt_rssi_event(mvm, vif, rssi_event);
1577 struct ieee80211_ops iwl_mvm_hw_ops = {
1578 .tx = iwl_mvm_mac_tx,
1579 .ampdu_action = iwl_mvm_mac_ampdu_action,
1580 .start = iwl_mvm_mac_start,
1581 .restart_complete = iwl_mvm_mac_restart_complete,
1582 .stop = iwl_mvm_mac_stop,
1583 .add_interface = iwl_mvm_mac_add_interface,
1584 .remove_interface = iwl_mvm_mac_remove_interface,
1585 .config = iwl_mvm_mac_config,
1586 .configure_filter = iwl_mvm_configure_filter,
1587 .bss_info_changed = iwl_mvm_bss_info_changed,
1588 .hw_scan = iwl_mvm_mac_hw_scan,
1589 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1590 .sta_state = iwl_mvm_mac_sta_state,
1591 .sta_notify = iwl_mvm_mac_sta_notify,
1592 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1593 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1594 .conf_tx = iwl_mvm_mac_conf_tx,
1595 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1596 .set_key = iwl_mvm_mac_set_key,
1597 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
1598 .remain_on_channel = iwl_mvm_roc,
1599 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
1600 .rssi_callback = iwl_mvm_mac_rssi_callback,
1602 .add_chanctx = iwl_mvm_add_chanctx,
1603 .remove_chanctx = iwl_mvm_remove_chanctx,
1604 .change_chanctx = iwl_mvm_change_chanctx,
1605 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
1606 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
1608 .start_ap = iwl_mvm_start_ap,
1609 .stop_ap = iwl_mvm_stop_ap,
1611 .set_tim = iwl_mvm_set_tim,
1613 #ifdef CONFIG_PM_SLEEP
1614 /* look at d3.c */
1615 .suspend = iwl_mvm_suspend,
1616 .resume = iwl_mvm_resume,
1617 .set_wakeup = iwl_mvm_set_wakeup,
1618 .set_rekey_data = iwl_mvm_set_rekey_data,
1619 #if IS_ENABLED(CONFIG_IPV6)
1620 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1621 #endif
1622 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1623 #endif