1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10 * Copyright(c) 2015 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <ilw@linux.intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
37 * Copyright(c) 2015 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
68 #include <linux/etherdevice.h>
69 #include <net/mac80211.h>
74 #include "time-event.h"
76 const u8 iwl_mvm_ac_to_tx_fifo[] = {
83 struct iwl_mvm_mac_iface_iterator_data {
85 struct ieee80211_vif *vif;
86 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
87 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
88 enum iwl_tsf_id preferred_tsf;
92 struct iwl_mvm_hw_queues_iface_iterator_data {
93 struct ieee80211_vif *exclude_vif;
94 unsigned long used_hw_queues;
97 static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
98 struct ieee80211_vif *vif)
100 struct iwl_mvm_mac_iface_iterator_data *data = _data;
101 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
104 /* Skip the interface for which we are trying to assign a tsf_id */
105 if (vif == data->vif)
109 * The TSF is a hardware/firmware resource, there are 4 and
110 * the driver should assign and free them as needed. However,
111 * there are cases where 2 MACs should share the same TSF ID
112 * for the purpose of clock sync, an optimization to avoid
113 * clock drift causing overlapping TBTTs/DTIMs for a GO and
114 * client in the system.
116 * The firmware will decide according to the MAC type which
117 * will be the master and slave. Clients that need to sync
118 * with a remote station will be the master, and an AP or GO
121 * Depending on the new interface type it can be slaved to
122 * or become the master of an existing interface.
124 switch (data->vif->type) {
125 case NL80211_IFTYPE_STATION:
127 * The new interface is a client, so if the one we're iterating
128 * is an AP, and the beacon interval of the AP is a multiple or
129 * divisor of the beacon interval of the client, the same TSF
130 * should be used to avoid drift between the new client and
131 * existing AP. The existing AP will get drift updates from the
132 * new client context in this case.
134 if (vif->type != NL80211_IFTYPE_AP ||
135 data->preferred_tsf != NUM_TSF_IDS ||
136 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
139 min_bi = min(data->vif->bss_conf.beacon_int,
140 vif->bss_conf.beacon_int);
145 if ((data->vif->bss_conf.beacon_int -
146 vif->bss_conf.beacon_int) % min_bi == 0) {
147 data->preferred_tsf = mvmvif->tsf_id;
152 case NL80211_IFTYPE_AP:
154 * The new interface is AP/GO, so if its beacon interval is a
155 * multiple or a divisor of the beacon interval of an existing
156 * interface, it should get drift updates from an existing
157 * client or use the same TSF as an existing GO. There's no
158 * drift between TSFs internally but if they used different
159 * TSFs then a new client MAC could update one of them and
160 * cause drift that way.
162 if ((vif->type != NL80211_IFTYPE_AP &&
163 vif->type != NL80211_IFTYPE_STATION) ||
164 data->preferred_tsf != NUM_TSF_IDS ||
165 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
168 min_bi = min(data->vif->bss_conf.beacon_int,
169 vif->bss_conf.beacon_int);
174 if ((data->vif->bss_conf.beacon_int -
175 vif->bss_conf.beacon_int) % min_bi == 0) {
176 data->preferred_tsf = mvmvif->tsf_id;
182 * For all other interface types there's no need to
183 * take drift into account. Either they're exclusive
184 * like IBSS and monitor, or we don't care much about
185 * their TSF (like P2P Device), but we won't be able
186 * to share the TSF resource.
192 * Unless we exited above, we can't share the TSF resource
193 * that the virtual interface we're iterating over is using
194 * with the new one, so clear the available bit and if this
195 * was the preferred one, reset that as well.
197 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
199 if (data->preferred_tsf == mvmvif->tsf_id)
200 data->preferred_tsf = NUM_TSF_IDS;
204 * Get the mask of the queues used by the vif
206 u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
210 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
211 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
213 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
214 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
215 qmask |= BIT(vif->hw_queue[ac]);
218 if (vif->type == NL80211_IFTYPE_AP)
219 qmask |= BIT(vif->cab_queue);
224 static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
225 struct ieee80211_vif *vif)
227 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
229 /* exclude the given vif */
230 if (vif == data->exclude_vif)
233 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
236 static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
237 struct ieee80211_sta *sta)
239 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
240 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
242 /* Mark the queues used by the sta */
243 data->used_hw_queues |= mvmsta->tfd_queue_msk;
246 unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
247 struct ieee80211_vif *exclude_vif)
250 struct iwl_mvm_hw_queues_iface_iterator_data data = {
251 .exclude_vif = exclude_vif,
253 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
254 BIT(mvm->aux_queue) |
255 BIT(IWL_MVM_CMD_QUEUE),
258 lockdep_assert_held(&mvm->mutex);
260 /* mark all VIF used hw queues */
261 ieee80211_iterate_active_interfaces_atomic(
262 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
263 iwl_mvm_iface_hw_queues_iter, &data);
265 /* don't assign the same hw queues as TDLS stations */
266 ieee80211_iterate_stations_atomic(mvm->hw,
267 iwl_mvm_mac_sta_hw_queues_iter,
271 * Some TDLS stations may be removed but are in the process of being
272 * drained. Don't touch their queues.
274 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
275 data.used_hw_queues |= mvm->tfd_drained[sta_id];
277 return data.used_hw_queues;
280 static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
281 struct ieee80211_vif *vif)
283 struct iwl_mvm_mac_iface_iterator_data *data = _data;
284 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
286 /* Iterator may already find the interface being added -- skip it */
287 if (vif == data->vif) {
288 data->found_vif = true;
292 /* Mark MAC IDs as used by clearing the available bit, and
293 * (below) mark TSFs as used if their existing use is not
294 * compatible with the new interface type.
295 * No locking or atomic bit operations are needed since the
296 * data is on the stack of the caller function.
298 __clear_bit(mvmvif->id, data->available_mac_ids);
300 /* find a suitable tsf_id */
301 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
304 void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
305 struct ieee80211_vif *vif)
307 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
308 struct iwl_mvm_mac_iface_iterator_data data = {
311 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
312 /* no preference yet */
313 .preferred_tsf = NUM_TSF_IDS,
316 ieee80211_iterate_active_interfaces_atomic(
317 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
318 iwl_mvm_mac_tsf_id_iter, &data);
320 if (data.preferred_tsf != NUM_TSF_IDS)
321 mvmvif->tsf_id = data.preferred_tsf;
322 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
323 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
327 static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
328 struct ieee80211_vif *vif)
330 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
331 struct iwl_mvm_mac_iface_iterator_data data = {
334 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
335 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
336 /* no preference yet */
337 .preferred_tsf = NUM_TSF_IDS,
342 unsigned long used_hw_queues;
345 * Allocate a MAC ID and a TSF for this MAC, along with the queues
346 * and other resources.
350 * Before the iterator, we start with all MAC IDs and TSFs available.
352 * During iteration, all MAC IDs are cleared that are in use by other
353 * virtual interfaces, and all TSF IDs are cleared that can't be used
354 * by this new virtual interface because they're used by an interface
355 * that can't share it with the new one.
356 * At the same time, we check if there's a preferred TSF in the case
357 * that we should share it with another interface.
360 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
362 case NL80211_IFTYPE_ADHOC:
364 case NL80211_IFTYPE_STATION:
369 __clear_bit(0, data.available_mac_ids);
372 ieee80211_iterate_active_interfaces_atomic(
373 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
374 iwl_mvm_mac_iface_iterator, &data);
376 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
379 * In the case we're getting here during resume, it's similar to
380 * firmware restart, and with RESUME_ALL the iterator will find
381 * the vif being added already.
382 * We don't want to reassign any IDs in either case since doing
383 * so would probably assign different IDs (as interfaces aren't
384 * necessarily added in the same order), but the old IDs were
385 * preserved anyway, so skip ID assignment for both resume and
391 /* Therefore, in recovery, we can't get here */
392 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
395 mvmvif->id = find_first_bit(data.available_mac_ids,
396 NUM_MAC_INDEX_DRIVER);
397 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
398 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
403 if (data.preferred_tsf != NUM_TSF_IDS)
404 mvmvif->tsf_id = data.preferred_tsf;
406 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
408 if (mvmvif->tsf_id == NUM_TSF_IDS) {
409 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
416 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
417 mvmvif->time_event_data.id = TE_MAX;
419 /* No need to allocate data queues to P2P Device MAC.*/
420 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
421 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
422 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
427 /* Find available queues, and allocate them to the ACs */
428 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
429 u8 queue = find_first_zero_bit(&used_hw_queues,
430 mvm->first_agg_queue);
432 if (queue >= mvm->first_agg_queue) {
433 IWL_ERR(mvm, "Failed to allocate queue\n");
438 __set_bit(queue, &used_hw_queues);
439 vif->hw_queue[ac] = queue;
442 /* Allocate the CAB queue for softAP and GO interfaces */
443 if (vif->type == NL80211_IFTYPE_AP) {
444 u8 queue = find_first_zero_bit(&used_hw_queues,
445 mvm->first_agg_queue);
447 if (queue >= mvm->first_agg_queue) {
448 IWL_ERR(mvm, "Failed to allocate cab queue\n");
453 vif->cab_queue = queue;
455 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
458 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
459 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
461 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
462 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
467 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
468 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
469 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
473 int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
475 unsigned int wdg_timeout =
476 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
480 lockdep_assert_held(&mvm->mutex);
482 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
487 case NL80211_IFTYPE_P2P_DEVICE:
488 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
489 IWL_MVM_TX_FIFO_VO, wdg_timeout);
491 case NL80211_IFTYPE_AP:
492 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue,
493 IWL_MVM_TX_FIFO_MCAST, wdg_timeout);
496 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
497 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
498 iwl_mvm_ac_to_tx_fifo[ac],
506 void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
510 lockdep_assert_held(&mvm->mutex);
513 case NL80211_IFTYPE_P2P_DEVICE:
514 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE, 0);
516 case NL80211_IFTYPE_AP:
517 iwl_mvm_disable_txq(mvm, vif->cab_queue, 0);
520 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
521 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac], 0);
525 static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
526 struct ieee80211_vif *vif,
527 enum ieee80211_band band,
528 u8 *cck_rates, u8 *ofdm_rates)
530 struct ieee80211_supported_band *sband;
531 unsigned long basic = vif->bss_conf.basic_rates;
532 int lowest_present_ofdm = 100;
533 int lowest_present_cck = 100;
538 sband = mvm->hw->wiphy->bands[band];
540 for_each_set_bit(i, &basic, BITS_PER_LONG) {
541 int hw = sband->bitrates[i].hw_value;
542 if (hw >= IWL_FIRST_OFDM_RATE) {
543 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
544 if (lowest_present_ofdm > hw)
545 lowest_present_ofdm = hw;
547 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
550 if (lowest_present_cck > hw)
551 lowest_present_cck = hw;
556 * Now we've got the basic rates as bitmaps in the ofdm and cck
557 * variables. This isn't sufficient though, as there might not
558 * be all the right rates in the bitmap. E.g. if the only basic
559 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
560 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
562 * [...] a STA responding to a received frame shall transmit
563 * its Control Response frame [...] at the highest rate in the
564 * BSSBasicRateSet parameter that is less than or equal to the
565 * rate of the immediately previous frame in the frame exchange
566 * sequence ([...]) and that is of the same modulation class
567 * ([...]) as the received frame. If no rate contained in the
568 * BSSBasicRateSet parameter meets these conditions, then the
569 * control frame sent in response to a received frame shall be
570 * transmitted at the highest mandatory rate of the PHY that is
571 * less than or equal to the rate of the received frame, and
572 * that is of the same modulation class as the received frame.
574 * As a consequence, we need to add all mandatory rates that are
575 * lower than all of the basic rates to these bitmaps.
578 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
579 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
580 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
581 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
582 /* 6M already there or needed so always add */
583 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
586 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
588 * - if no CCK rates are basic, it must be ERP since there must
589 * be some basic rates at all, so they're OFDM => ERP PHY
590 * (or we're in 5 GHz, and the cck bitmap will never be used)
591 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
592 * - if 5.5M is basic, 1M and 2M are mandatory
593 * - if 2M is basic, 1M is mandatory
594 * - if 1M is basic, that's the only valid ACK rate.
595 * As a consequence, it's not as complicated as it sounds, just add
596 * any lower rates to the ACK rate bitmap.
598 if (IWL_RATE_11M_INDEX < lowest_present_cck)
599 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
600 if (IWL_RATE_5M_INDEX < lowest_present_cck)
601 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
602 if (IWL_RATE_2M_INDEX < lowest_present_cck)
603 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
604 /* 1M already there or needed so always add */
605 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
611 static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
612 struct ieee80211_vif *vif,
613 struct iwl_mac_ctx_cmd *cmd)
615 /* for both sta and ap, ht_operation_mode hold the protection_mode */
616 u8 protection_mode = vif->bss_conf.ht_operation_mode &
617 IEEE80211_HT_OP_MODE_PROTECTION;
618 /* The fw does not distinguish between ht and fat */
619 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
621 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
623 * See section 9.23.3.1 of IEEE 80211-2012.
624 * Nongreenfield HT STAs Present is not supported.
626 switch (protection_mode) {
627 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
629 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
630 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
631 cmd->protection_flags |= cpu_to_le32(ht_flag);
633 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
634 /* Protect when channel wider than 20MHz */
635 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
636 cmd->protection_flags |= cpu_to_le32(ht_flag);
639 IWL_ERR(mvm, "Illegal protection mode %d\n",
645 static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
646 struct ieee80211_vif *vif,
647 struct iwl_mac_ctx_cmd *cmd,
648 const u8 *bssid_override,
651 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
652 struct ieee80211_chanctx_conf *chanctx;
653 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
654 IEEE80211_HT_OP_MODE_PROTECTION);
655 u8 cck_ack_rates, ofdm_ack_rates;
656 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
659 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
661 cmd->action = cpu_to_le32(action);
664 case NL80211_IFTYPE_STATION:
666 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
668 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
670 case NL80211_IFTYPE_AP:
671 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
673 case NL80211_IFTYPE_MONITOR:
674 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
676 case NL80211_IFTYPE_P2P_DEVICE:
677 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
679 case NL80211_IFTYPE_ADHOC:
680 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
686 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
688 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
691 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
693 eth_broadcast_addr(cmd->bssid_addr);
696 chanctx = rcu_dereference(vif->chanctx_conf);
697 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
698 : IEEE80211_BAND_2GHZ,
699 &cck_ack_rates, &ofdm_ack_rates);
702 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
703 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
705 cmd->cck_short_preamble =
706 cpu_to_le32(vif->bss_conf.use_short_preamble ?
707 MAC_FLG_SHORT_PREAMBLE : 0);
709 cpu_to_le32(vif->bss_conf.use_short_slot ?
710 MAC_FLG_SHORT_SLOT : 0);
712 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
713 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
715 cmd->ac[txf].cw_min =
716 cpu_to_le16(mvmvif->queue_params[i].cw_min);
717 cmd->ac[txf].cw_max =
718 cpu_to_le16(mvmvif->queue_params[i].cw_max);
719 cmd->ac[txf].edca_txop =
720 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
721 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
722 cmd->ac[txf].fifos_mask = BIT(txf);
725 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
726 if (vif->type == NL80211_IFTYPE_AP)
727 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
728 BIT(IWL_MVM_TX_FIFO_MCAST);
730 if (vif->bss_conf.qos)
731 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
733 if (vif->bss_conf.use_cts_prot)
734 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
736 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
737 vif->bss_conf.use_cts_prot,
738 vif->bss_conf.ht_operation_mode);
739 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
740 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
742 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
744 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
747 static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
748 struct iwl_mac_ctx_cmd *cmd)
750 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
753 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
754 le32_to_cpu(cmd->action), ret);
758 static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
759 struct ieee80211_vif *vif,
760 u32 action, bool force_assoc_off,
761 const u8 *bssid_override)
763 struct iwl_mac_ctx_cmd cmd = {};
764 struct iwl_mac_data_sta *ctxt_sta;
766 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
768 /* Fill the common data for all mac context types */
769 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
772 struct ieee80211_p2p_noa_attr *noa =
773 &vif->bss_conf.p2p_noa_attr;
775 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
776 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
777 ctxt_sta = &cmd.p2p_sta.sta;
782 /* We need the dtim_period to set the MAC as associated */
783 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
788 * The DTIM count counts down, so when it is N that means N
789 * more beacon intervals happen until the DTIM TBTT. Therefore
790 * add this to the current time. If that ends up being in the
791 * future, the firmware will handle it.
793 * Also note that the system_timestamp (which we get here as
794 * "sync_device_ts") and TSF timestamp aren't at exactly the
795 * same offset in the frame -- the TSF is at the first symbol
796 * of the TSF, the system timestamp is at signal acquisition
797 * time. This means there's an offset between them of at most
798 * a few hundred microseconds (24 * 8 bits + PLCP time gives
799 * 384us in the longest case), this is currently not relevant
800 * as the firmware wakes up around 2ms before the TBTT.
802 dtim_offs = vif->bss_conf.sync_dtim_count *
803 vif->bss_conf.beacon_int;
804 /* convert TU to usecs */
808 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
809 ctxt_sta->dtim_time =
810 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
812 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
813 le64_to_cpu(ctxt_sta->dtim_tsf),
814 le32_to_cpu(ctxt_sta->dtim_time),
817 ctxt_sta->is_assoc = cpu_to_le32(1);
819 ctxt_sta->is_assoc = cpu_to_le32(0);
821 /* Allow beacons to pass through as long as we are not
822 * associated, or we do not have dtim period information.
824 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
827 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
828 ctxt_sta->bi_reciprocal =
829 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
830 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
831 vif->bss_conf.dtim_period);
832 ctxt_sta->dtim_reciprocal =
833 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
834 vif->bss_conf.dtim_period));
836 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
837 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
839 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
842 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
843 struct ieee80211_vif *vif,
846 struct iwl_mac_ctx_cmd cmd = {};
848 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
850 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
852 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
853 MAC_FILTER_IN_CONTROL_AND_MGMT |
854 MAC_FILTER_IN_BEACON |
855 MAC_FILTER_IN_PROBE_REQUEST |
856 MAC_FILTER_IN_CRC32);
857 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
859 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
862 static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
863 struct ieee80211_vif *vif,
866 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
867 struct iwl_mac_ctx_cmd cmd = {};
869 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
871 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
873 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
874 MAC_FILTER_IN_PROBE_REQUEST);
876 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
877 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
878 cmd.ibss.bi_reciprocal =
879 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
881 /* TODO: Assumes that the beacon id == mac context id */
882 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
884 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
887 struct iwl_mvm_go_iterator_data {
891 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
893 struct iwl_mvm_go_iterator_data *data = _data;
894 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
896 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
897 mvmvif->ap_ibss_active)
898 data->go_active = true;
901 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
902 struct ieee80211_vif *vif,
905 struct iwl_mac_ctx_cmd cmd = {};
906 struct iwl_mvm_go_iterator_data data = {};
908 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
910 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
912 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
914 /* Override the filter flags to accept only probe requests */
915 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
918 * This flag should be set to true when the P2P Device is
919 * discoverable and there is at least another active P2P GO. Settings
920 * this flag will allow the P2P Device to be discoverable on other
921 * channels in addition to its listen channel.
922 * Note that this flag should not be set in other cases as it opens the
923 * Rx filters on all MAC and increases the number of interrupts.
925 ieee80211_iterate_active_interfaces_atomic(
926 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
927 iwl_mvm_go_iterator, &data);
929 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
930 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
933 static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
934 struct iwl_mac_beacon_cmd *beacon_cmd,
935 u8 *beacon, u32 frame_size)
938 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
940 /* The index is relative to frame start but we start looking at the
941 * variable-length part of the beacon. */
942 tim_idx = mgmt->u.beacon.variable - beacon;
944 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
945 while ((tim_idx < (frame_size - 2)) &&
946 (beacon[tim_idx] != WLAN_EID_TIM))
947 tim_idx += beacon[tim_idx+1] + 2;
949 /* If TIM field was found, set variables */
950 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
951 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
952 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
954 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
958 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
959 struct ieee80211_vif *vif,
960 struct sk_buff *beacon)
962 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
963 struct iwl_host_cmd cmd = {
964 .id = BEACON_TEMPLATE_CMD,
967 struct iwl_mac_beacon_cmd beacon_cmd = {};
968 struct ieee80211_tx_info *info;
972 if (WARN_ON(!beacon))
975 beacon_skb_len = beacon->len;
977 /* TODO: for now the beacon template id is set to be the mac context id.
978 * Might be better to handle it as another resource ... */
979 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
980 info = IEEE80211_SKB_CB(beacon);
982 /* Set up TX command fields */
983 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
984 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
985 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
986 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
988 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
989 TX_CMD_FLG_BT_PRIO_POS;
990 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
992 mvm->mgmt_last_antenna_idx =
993 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
994 mvm->mgmt_last_antenna_idx);
996 beacon_cmd.tx.rate_n_flags =
997 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1000 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
1001 rate = IWL_FIRST_OFDM_RATE;
1003 rate = IWL_FIRST_CCK_RATE;
1004 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1006 beacon_cmd.tx.rate_n_flags |=
1007 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1009 /* Set up TX beacon command fields */
1010 if (vif->type == NL80211_IFTYPE_AP)
1011 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1015 /* Submit command */
1016 cmd.len[0] = sizeof(beacon_cmd);
1017 cmd.data[0] = &beacon_cmd;
1018 cmd.dataflags[0] = 0;
1019 cmd.len[1] = beacon_skb_len;
1020 cmd.data[1] = beacon->data;
1021 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1023 return iwl_mvm_send_cmd(mvm, &cmd);
1026 /* The beacon template for the AP/GO/IBSS has changed and needs update */
1027 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1028 struct ieee80211_vif *vif)
1030 struct sk_buff *beacon;
1033 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1034 vif->type != NL80211_IFTYPE_ADHOC);
1036 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1040 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1041 dev_kfree_skb(beacon);
1045 struct iwl_mvm_mac_ap_iterator_data {
1046 struct iwl_mvm *mvm;
1047 struct ieee80211_vif *vif;
1048 u32 beacon_device_ts;
1052 /* Find the beacon_device_ts and beacon_int for a managed interface */
1053 static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1054 struct ieee80211_vif *vif)
1056 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1058 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1061 /* Station client has higher priority over P2P client*/
1062 if (vif->p2p && data->beacon_device_ts)
1065 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1066 data->beacon_int = vif->bss_conf.beacon_int;
1070 * Fill the specific data for mac context of type AP of P2P GO
1072 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1073 struct ieee80211_vif *vif,
1074 struct iwl_mac_data_ap *ctxt_ap,
1077 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1078 struct iwl_mvm_mac_ap_iterator_data data = {
1081 .beacon_device_ts = 0
1084 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1085 ctxt_ap->bi_reciprocal =
1086 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1087 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1088 vif->bss_conf.dtim_period);
1089 ctxt_ap->dtim_reciprocal =
1090 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1091 vif->bss_conf.dtim_period));
1093 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1096 * Only set the beacon time when the MAC is being added, when we
1097 * just modify the MAC then we should keep the time -- the firmware
1098 * can otherwise have a "jumping" TBTT.
1102 * If there is a station/P2P client interface which is
1103 * associated, set the AP's TBTT far enough from the station's
1104 * TBTT. Otherwise, set it to the current system time
1106 ieee80211_iterate_active_interfaces_atomic(
1107 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1108 iwl_mvm_mac_ap_iterator, &data);
1110 if (data.beacon_device_ts) {
1111 u32 rand = (prandom_u32() % (64 - 36)) + 36;
1112 mvmvif->ap_beacon_time = data.beacon_device_ts +
1113 ieee80211_tu_to_usec(data.beacon_int * rand /
1116 mvmvif->ap_beacon_time =
1117 iwl_read_prph(mvm->trans,
1118 DEVICE_SYSTEM_TIME_REG);
1122 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1123 ctxt_ap->beacon_tsf = 0; /* unused */
1125 /* TODO: Assume that the beacon id == mac context id */
1126 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1129 int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1130 struct ieee80211_vif *vif,
1133 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1134 struct iwl_mac_ctx_cmd cmd = {};
1136 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1138 /* Fill the common data for all mac context types */
1139 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1142 * pass probe requests and beacons from other APs (needed
1143 * for ht protection); when there're no any associated station
1144 * don't ask FW to pass beacons to prevent unnecessary wake-ups.
1146 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1147 if (mvmvif->ap_assoc_sta_count) {
1148 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1149 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1151 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1154 /* Fill the data specific for ap mode */
1155 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1156 action == FW_CTXT_ACTION_ADD);
1158 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1161 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1162 struct ieee80211_vif *vif,
1165 struct iwl_mac_ctx_cmd cmd = {};
1166 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1168 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1170 /* Fill the common data for all mac context types */
1171 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1174 * pass probe requests and beacons from other APs (needed
1175 * for ht protection)
1177 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1178 MAC_FILTER_IN_BEACON);
1180 /* Fill the data specific for GO mode */
1181 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1182 action == FW_CTXT_ACTION_ADD);
1184 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1185 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1186 cmd.go.opp_ps_enabled =
1187 cpu_to_le32(!!(noa->oppps_ctwindow &
1188 IEEE80211_P2P_OPPPS_ENABLE_BIT));
1190 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1193 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1194 u32 action, bool force_assoc_off,
1195 const u8 *bssid_override)
1197 switch (vif->type) {
1198 case NL80211_IFTYPE_STATION:
1199 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1203 case NL80211_IFTYPE_AP:
1205 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1207 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1209 case NL80211_IFTYPE_MONITOR:
1210 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1211 case NL80211_IFTYPE_P2P_DEVICE:
1212 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1213 case NL80211_IFTYPE_ADHOC:
1214 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1222 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1224 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1227 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1228 vif->addr, ieee80211_vif_type_p2p(vif)))
1231 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1236 /* will only do anything at resume from D3 time */
1237 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1239 mvmvif->uploaded = true;
1243 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1244 bool force_assoc_off, const u8 *bssid_override)
1246 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1248 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1249 vif->addr, ieee80211_vif_type_p2p(vif)))
1252 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1253 force_assoc_off, bssid_override);
1256 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1258 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1259 struct iwl_mac_ctx_cmd cmd;
1262 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1263 vif->addr, ieee80211_vif_type_p2p(vif)))
1266 memset(&cmd, 0, sizeof(cmd));
1268 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1270 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1272 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1275 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1279 mvmvif->uploaded = false;
1281 if (vif->type == NL80211_IFTYPE_MONITOR)
1282 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1287 static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1288 struct ieee80211_vif *csa_vif, u32 gp2,
1291 struct iwl_mvm_vif *mvmvif =
1292 iwl_mvm_vif_from_mac80211(csa_vif);
1294 /* Don't start to countdown from a failed beacon */
1295 if (!tx_success && !mvmvif->csa_countdown)
1298 mvmvif->csa_countdown = true;
1300 if (!ieee80211_csa_is_complete(csa_vif)) {
1301 int c = ieee80211_csa_update_counter(csa_vif);
1303 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1305 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1307 u32 rel_time = (c + 1) *
1308 csa_vif->bss_conf.beacon_int -
1309 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1310 u32 apply_time = gp2 + rel_time * 1024;
1312 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1313 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1314 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1317 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1318 /* we don't have CSA NoA scheduled yet, switch now */
1319 ieee80211_csa_finish(csa_vif);
1320 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1324 void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1325 struct iwl_rx_cmd_buffer *rxb)
1327 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1328 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1329 struct iwl_mvm_tx_resp *beacon_notify_hdr;
1330 struct ieee80211_vif *csa_vif;
1331 struct ieee80211_vif *tx_blocked_vif;
1334 lockdep_assert_held(&mvm->mutex);
1336 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1337 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1339 status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
1341 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1342 status, beacon_notify_hdr->failure_frame,
1343 le64_to_cpu(beacon->tsf),
1344 mvm->ap_last_beacon_gp2,
1345 le32_to_cpu(beacon_notify_hdr->initial_rate));
1347 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1348 lockdep_is_held(&mvm->mutex));
1349 if (unlikely(csa_vif && csa_vif->csa_active))
1350 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1351 (status == TX_STATUS_SUCCESS));
1353 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1354 lockdep_is_held(&mvm->mutex));
1355 if (unlikely(tx_blocked_vif)) {
1356 struct iwl_mvm_vif *mvmvif =
1357 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1360 * The channel switch is started and we have blocked the
1361 * stations. If this is the first beacon (the timeout wasn't
1362 * set), set the unblock timeout, otherwise countdown
1364 if (!mvm->csa_tx_block_bcn_timeout)
1365 mvm->csa_tx_block_bcn_timeout =
1366 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1368 mvm->csa_tx_block_bcn_timeout--;
1370 /* Check if the timeout is expired, and unblock tx */
1371 if (mvm->csa_tx_block_bcn_timeout == 0) {
1372 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1373 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1378 static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1379 struct ieee80211_vif *vif)
1381 struct iwl_missed_beacons_notif *missed_beacons = _data;
1382 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1383 struct iwl_mvm *mvm = mvmvif->mvm;
1384 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1385 struct iwl_fw_dbg_trigger_tlv *trigger;
1386 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1387 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1389 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1392 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1393 rx_missed_bcon_since_rx =
1394 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1396 * TODO: the threshold should be adjusted based on latency conditions,
1397 * and/or in case of a CS flow on one of the other AP vifs.
1399 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1400 IWL_MVM_MISSED_BEACONS_THRESHOLD)
1401 ieee80211_beacon_loss(vif);
1403 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1404 FW_DBG_TRIGGER_MISSED_BEACONS))
1407 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1408 FW_DBG_TRIGGER_MISSED_BEACONS);
1409 bcon_trig = (void *)trigger->data;
1410 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1411 stop_trig_missed_bcon_since_rx =
1412 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1414 /* TODO: implement start trigger */
1416 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1419 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1420 rx_missed_bcon >= stop_trig_missed_bcon)
1421 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
1424 void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1425 struct iwl_rx_cmd_buffer *rxb)
1427 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1428 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1431 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1432 le32_to_cpu(mb->mac_id),
1433 le32_to_cpu(mb->consec_missed_beacons),
1434 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1435 le32_to_cpu(mb->num_recvd_beacons),
1436 le32_to_cpu(mb->num_expected_beacons));
1438 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1439 IEEE80211_IFACE_ITER_NORMAL,
1440 iwl_mvm_beacon_loss_iterator,