net/mlx4_en: Move filters cleanup to a proper location
[linux/fpc-iii.git] / drivers / net / wireless / intel / iwlwifi / mvm / mac-ctxt.c
blob7aae068c02e54a1eb2050c0780bf907358502a7b
1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
6 * GPL LICENSE SUMMARY
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10 * Copyright(c) 2015 - 2016 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,
24 * USA
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 <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * BSD LICENSE
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
37 * Copyright(c) 2015 - 2016 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
42 * are met:
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
49 * distribution.
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>
70 #include "iwl-io.h"
71 #include "iwl-prph.h"
72 #include "fw-api.h"
73 #include "mvm.h"
74 #include "time-event.h"
75 #include "fw-dbg.h"
77 const u8 iwl_mvm_ac_to_tx_fifo[] = {
78 IWL_MVM_TX_FIFO_VO,
79 IWL_MVM_TX_FIFO_VI,
80 IWL_MVM_TX_FIFO_BE,
81 IWL_MVM_TX_FIFO_BK,
84 struct iwl_mvm_mac_iface_iterator_data {
85 struct iwl_mvm *mvm;
86 struct ieee80211_vif *vif;
87 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
88 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
89 enum iwl_tsf_id preferred_tsf;
90 bool found_vif;
93 struct iwl_mvm_hw_queues_iface_iterator_data {
94 struct ieee80211_vif *exclude_vif;
95 unsigned long used_hw_queues;
98 static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
99 struct ieee80211_vif *vif)
101 struct iwl_mvm_mac_iface_iterator_data *data = _data;
102 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
103 u16 min_bi;
105 /* Skip the interface for which we are trying to assign a tsf_id */
106 if (vif == data->vif)
107 return;
110 * The TSF is a hardware/firmware resource, there are 4 and
111 * the driver should assign and free them as needed. However,
112 * there are cases where 2 MACs should share the same TSF ID
113 * for the purpose of clock sync, an optimization to avoid
114 * clock drift causing overlapping TBTTs/DTIMs for a GO and
115 * client in the system.
117 * The firmware will decide according to the MAC type which
118 * will be the master and slave. Clients that need to sync
119 * with a remote station will be the master, and an AP or GO
120 * will be the slave.
122 * Depending on the new interface type it can be slaved to
123 * or become the master of an existing interface.
125 switch (data->vif->type) {
126 case NL80211_IFTYPE_STATION:
128 * The new interface is a client, so if the one we're iterating
129 * is an AP, and the beacon interval of the AP is a multiple or
130 * divisor of the beacon interval of the client, the same TSF
131 * should be used to avoid drift between the new client and
132 * existing AP. The existing AP will get drift updates from the
133 * new client context in this case.
135 if (vif->type != NL80211_IFTYPE_AP ||
136 data->preferred_tsf != NUM_TSF_IDS ||
137 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
138 break;
140 min_bi = min(data->vif->bss_conf.beacon_int,
141 vif->bss_conf.beacon_int);
143 if (!min_bi)
144 break;
146 if ((data->vif->bss_conf.beacon_int -
147 vif->bss_conf.beacon_int) % min_bi == 0) {
148 data->preferred_tsf = mvmvif->tsf_id;
149 return;
151 break;
153 case NL80211_IFTYPE_AP:
155 * The new interface is AP/GO, so if its beacon interval is a
156 * multiple or a divisor of the beacon interval of an existing
157 * interface, it should get drift updates from an existing
158 * client or use the same TSF as an existing GO. There's no
159 * drift between TSFs internally but if they used different
160 * TSFs then a new client MAC could update one of them and
161 * cause drift that way.
163 if ((vif->type != NL80211_IFTYPE_AP &&
164 vif->type != NL80211_IFTYPE_STATION) ||
165 data->preferred_tsf != NUM_TSF_IDS ||
166 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
167 break;
169 min_bi = min(data->vif->bss_conf.beacon_int,
170 vif->bss_conf.beacon_int);
172 if (!min_bi)
173 break;
175 if ((data->vif->bss_conf.beacon_int -
176 vif->bss_conf.beacon_int) % min_bi == 0) {
177 data->preferred_tsf = mvmvif->tsf_id;
178 return;
180 break;
181 default:
183 * For all other interface types there's no need to
184 * take drift into account. Either they're exclusive
185 * like IBSS and monitor, or we don't care much about
186 * their TSF (like P2P Device), but we won't be able
187 * to share the TSF resource.
189 break;
193 * Unless we exited above, we can't share the TSF resource
194 * that the virtual interface we're iterating over is using
195 * with the new one, so clear the available bit and if this
196 * was the preferred one, reset that as well.
198 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
200 if (data->preferred_tsf == mvmvif->tsf_id)
201 data->preferred_tsf = NUM_TSF_IDS;
205 * Get the mask of the queues used by the vif
207 u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
209 u32 qmask = 0, ac;
211 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
212 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
214 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
215 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
216 qmask |= BIT(vif->hw_queue[ac]);
219 if (vif->type == NL80211_IFTYPE_AP)
220 qmask |= BIT(vif->cab_queue);
222 return qmask;
225 static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
226 struct ieee80211_vif *vif)
228 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
230 /* exclude the given vif */
231 if (vif == data->exclude_vif)
232 return;
234 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
237 static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
238 struct ieee80211_sta *sta)
240 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
241 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
243 /* Mark the queues used by the sta */
244 data->used_hw_queues |= mvmsta->tfd_queue_msk;
247 unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
248 struct ieee80211_vif *exclude_vif)
250 u8 sta_id;
251 struct iwl_mvm_hw_queues_iface_iterator_data data = {
252 .exclude_vif = exclude_vif,
253 .used_hw_queues =
254 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
255 BIT(mvm->aux_queue),
258 if (iwl_mvm_is_dqa_supported(mvm))
259 data.used_hw_queues |= BIT(IWL_MVM_DQA_CMD_QUEUE);
260 else
261 data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
263 lockdep_assert_held(&mvm->mutex);
265 /* mark all VIF used hw queues */
266 ieee80211_iterate_active_interfaces_atomic(
267 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
268 iwl_mvm_iface_hw_queues_iter, &data);
270 /* don't assign the same hw queues as TDLS stations */
271 ieee80211_iterate_stations_atomic(mvm->hw,
272 iwl_mvm_mac_sta_hw_queues_iter,
273 &data);
276 * Some TDLS stations may be removed but are in the process of being
277 * drained. Don't touch their queues.
279 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
280 data.used_hw_queues |= mvm->tfd_drained[sta_id];
282 return data.used_hw_queues;
285 static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
286 struct ieee80211_vif *vif)
288 struct iwl_mvm_mac_iface_iterator_data *data = _data;
289 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
291 /* Iterator may already find the interface being added -- skip it */
292 if (vif == data->vif) {
293 data->found_vif = true;
294 return;
297 /* Mark MAC IDs as used by clearing the available bit, and
298 * (below) mark TSFs as used if their existing use is not
299 * compatible with the new interface type.
300 * No locking or atomic bit operations are needed since the
301 * data is on the stack of the caller function.
303 __clear_bit(mvmvif->id, data->available_mac_ids);
305 /* find a suitable tsf_id */
306 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
309 void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
310 struct ieee80211_vif *vif)
312 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
313 struct iwl_mvm_mac_iface_iterator_data data = {
314 .mvm = mvm,
315 .vif = vif,
316 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
317 /* no preference yet */
318 .preferred_tsf = NUM_TSF_IDS,
321 ieee80211_iterate_active_interfaces_atomic(
322 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
323 iwl_mvm_mac_tsf_id_iter, &data);
325 if (data.preferred_tsf != NUM_TSF_IDS)
326 mvmvif->tsf_id = data.preferred_tsf;
327 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
328 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
329 NUM_TSF_IDS);
332 static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
333 struct ieee80211_vif *vif)
335 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
336 struct iwl_mvm_mac_iface_iterator_data data = {
337 .mvm = mvm,
338 .vif = vif,
339 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
340 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
341 /* no preference yet */
342 .preferred_tsf = NUM_TSF_IDS,
343 .found_vif = false,
345 u32 ac;
346 int ret, i;
347 unsigned long used_hw_queues;
350 * Allocate a MAC ID and a TSF for this MAC, along with the queues
351 * and other resources.
355 * Before the iterator, we start with all MAC IDs and TSFs available.
357 * During iteration, all MAC IDs are cleared that are in use by other
358 * virtual interfaces, and all TSF IDs are cleared that can't be used
359 * by this new virtual interface because they're used by an interface
360 * that can't share it with the new one.
361 * At the same time, we check if there's a preferred TSF in the case
362 * that we should share it with another interface.
365 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
366 switch (vif->type) {
367 case NL80211_IFTYPE_ADHOC:
368 break;
369 case NL80211_IFTYPE_STATION:
370 if (!vif->p2p)
371 break;
372 /* fall through */
373 default:
374 __clear_bit(0, data.available_mac_ids);
377 ieee80211_iterate_active_interfaces_atomic(
378 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
379 iwl_mvm_mac_iface_iterator, &data);
381 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
384 * In the case we're getting here during resume, it's similar to
385 * firmware restart, and with RESUME_ALL the iterator will find
386 * the vif being added already.
387 * We don't want to reassign any IDs in either case since doing
388 * so would probably assign different IDs (as interfaces aren't
389 * necessarily added in the same order), but the old IDs were
390 * preserved anyway, so skip ID assignment for both resume and
391 * recovery.
393 if (data.found_vif)
394 return 0;
396 /* Therefore, in recovery, we can't get here */
397 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
398 return -EBUSY;
400 mvmvif->id = find_first_bit(data.available_mac_ids,
401 NUM_MAC_INDEX_DRIVER);
402 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
403 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
404 ret = -EIO;
405 goto exit_fail;
408 if (data.preferred_tsf != NUM_TSF_IDS)
409 mvmvif->tsf_id = data.preferred_tsf;
410 else
411 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
412 NUM_TSF_IDS);
413 if (mvmvif->tsf_id == NUM_TSF_IDS) {
414 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
415 ret = -EIO;
416 goto exit_fail;
419 mvmvif->color = 0;
421 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
422 mvmvif->time_event_data.id = TE_MAX;
424 /* No need to allocate data queues to P2P Device MAC.*/
425 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
426 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
427 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
429 return 0;
433 * Find available queues, and allocate them to the ACs. When in
434 * DQA-mode they aren't really used, and this is done only so the
435 * mac80211 ieee80211_check_queues() function won't fail
437 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
438 u8 queue = find_first_zero_bit(&used_hw_queues,
439 mvm->first_agg_queue);
441 if (!iwl_mvm_is_dqa_supported(mvm) &&
442 queue >= mvm->first_agg_queue) {
443 IWL_ERR(mvm, "Failed to allocate queue\n");
444 ret = -EIO;
445 goto exit_fail;
448 __set_bit(queue, &used_hw_queues);
449 vif->hw_queue[ac] = queue;
452 /* Allocate the CAB queue for softAP and GO interfaces */
453 if (vif->type == NL80211_IFTYPE_AP) {
454 u8 queue;
456 if (!iwl_mvm_is_dqa_supported(mvm)) {
457 queue = find_first_zero_bit(&used_hw_queues,
458 mvm->first_agg_queue);
460 if (queue >= mvm->first_agg_queue) {
461 IWL_ERR(mvm, "Failed to allocate cab queue\n");
462 ret = -EIO;
463 goto exit_fail;
465 } else {
466 queue = IWL_MVM_DQA_GCAST_QUEUE;
469 vif->cab_queue = queue;
470 } else {
471 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
474 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
475 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
477 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
478 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
480 return 0;
482 exit_fail:
483 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
484 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
485 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
486 return ret;
489 int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
491 unsigned int wdg_timeout =
492 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
493 u32 ac;
494 int ret;
496 lockdep_assert_held(&mvm->mutex);
498 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
499 if (ret)
500 return ret;
502 switch (vif->type) {
503 case NL80211_IFTYPE_P2P_DEVICE:
504 if (!iwl_mvm_is_dqa_supported(mvm))
505 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
506 IWL_MVM_OFFCHANNEL_QUEUE,
507 IWL_MVM_TX_FIFO_VO, 0,
508 wdg_timeout);
509 break;
510 case NL80211_IFTYPE_AP:
511 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
512 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
513 /* fall through */
514 default:
515 /* If DQA is supported - queues will be enabled when needed */
516 if (iwl_mvm_is_dqa_supported(mvm))
517 break;
519 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
520 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
521 vif->hw_queue[ac],
522 iwl_mvm_ac_to_tx_fifo[ac], 0,
523 wdg_timeout);
524 break;
527 return 0;
530 void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
532 int ac;
534 lockdep_assert_held(&mvm->mutex);
536 switch (vif->type) {
537 case NL80211_IFTYPE_P2P_DEVICE:
538 if (!iwl_mvm_is_dqa_supported(mvm))
539 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
540 IWL_MVM_OFFCHANNEL_QUEUE,
541 IWL_MAX_TID_COUNT, 0);
543 break;
544 case NL80211_IFTYPE_AP:
545 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
546 IWL_MAX_TID_COUNT, 0);
548 if (iwl_mvm_is_dqa_supported(mvm))
549 iwl_mvm_disable_txq(mvm,
550 IWL_MVM_DQA_AP_PROBE_RESP_QUEUE,
551 vif->hw_queue[0], IWL_MAX_TID_COUNT,
553 /* fall through */
554 default:
556 * If DQA is supported - queues were already disabled, since in
557 * DQA-mode the queues are a property of the STA and not of the
558 * vif, and at this point the STA was already deleted
560 if (iwl_mvm_is_dqa_supported(mvm))
561 break;
563 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
564 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
565 vif->hw_queue[ac],
566 IWL_MAX_TID_COUNT, 0);
570 static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
571 struct ieee80211_vif *vif,
572 enum nl80211_band band,
573 u8 *cck_rates, u8 *ofdm_rates)
575 struct ieee80211_supported_band *sband;
576 unsigned long basic = vif->bss_conf.basic_rates;
577 int lowest_present_ofdm = 100;
578 int lowest_present_cck = 100;
579 u8 cck = 0;
580 u8 ofdm = 0;
581 int i;
583 sband = mvm->hw->wiphy->bands[band];
585 for_each_set_bit(i, &basic, BITS_PER_LONG) {
586 int hw = sband->bitrates[i].hw_value;
587 if (hw >= IWL_FIRST_OFDM_RATE) {
588 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
589 if (lowest_present_ofdm > hw)
590 lowest_present_ofdm = hw;
591 } else {
592 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
594 cck |= BIT(hw);
595 if (lowest_present_cck > hw)
596 lowest_present_cck = hw;
601 * Now we've got the basic rates as bitmaps in the ofdm and cck
602 * variables. This isn't sufficient though, as there might not
603 * be all the right rates in the bitmap. E.g. if the only basic
604 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
605 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
607 * [...] a STA responding to a received frame shall transmit
608 * its Control Response frame [...] at the highest rate in the
609 * BSSBasicRateSet parameter that is less than or equal to the
610 * rate of the immediately previous frame in the frame exchange
611 * sequence ([...]) and that is of the same modulation class
612 * ([...]) as the received frame. If no rate contained in the
613 * BSSBasicRateSet parameter meets these conditions, then the
614 * control frame sent in response to a received frame shall be
615 * transmitted at the highest mandatory rate of the PHY that is
616 * less than or equal to the rate of the received frame, and
617 * that is of the same modulation class as the received frame.
619 * As a consequence, we need to add all mandatory rates that are
620 * lower than all of the basic rates to these bitmaps.
623 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
624 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
625 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
626 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
627 /* 6M already there or needed so always add */
628 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
631 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
632 * Note, however:
633 * - if no CCK rates are basic, it must be ERP since there must
634 * be some basic rates at all, so they're OFDM => ERP PHY
635 * (or we're in 5 GHz, and the cck bitmap will never be used)
636 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
637 * - if 5.5M is basic, 1M and 2M are mandatory
638 * - if 2M is basic, 1M is mandatory
639 * - if 1M is basic, that's the only valid ACK rate.
640 * As a consequence, it's not as complicated as it sounds, just add
641 * any lower rates to the ACK rate bitmap.
643 if (IWL_RATE_11M_INDEX < lowest_present_cck)
644 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
645 if (IWL_RATE_5M_INDEX < lowest_present_cck)
646 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
647 if (IWL_RATE_2M_INDEX < lowest_present_cck)
648 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
649 /* 1M already there or needed so always add */
650 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
652 *cck_rates = cck;
653 *ofdm_rates = ofdm;
656 static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
657 struct ieee80211_vif *vif,
658 struct iwl_mac_ctx_cmd *cmd)
660 /* for both sta and ap, ht_operation_mode hold the protection_mode */
661 u8 protection_mode = vif->bss_conf.ht_operation_mode &
662 IEEE80211_HT_OP_MODE_PROTECTION;
663 /* The fw does not distinguish between ht and fat */
664 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
666 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
668 * See section 9.23.3.1 of IEEE 80211-2012.
669 * Nongreenfield HT STAs Present is not supported.
671 switch (protection_mode) {
672 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
673 break;
674 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
675 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
676 cmd->protection_flags |= cpu_to_le32(ht_flag);
677 break;
678 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
679 /* Protect when channel wider than 20MHz */
680 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
681 cmd->protection_flags |= cpu_to_le32(ht_flag);
682 break;
683 default:
684 IWL_ERR(mvm, "Illegal protection mode %d\n",
685 protection_mode);
686 break;
690 static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
691 struct ieee80211_vif *vif,
692 struct iwl_mac_ctx_cmd *cmd,
693 const u8 *bssid_override,
694 u32 action)
696 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
697 struct ieee80211_chanctx_conf *chanctx;
698 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
699 IEEE80211_HT_OP_MODE_PROTECTION);
700 u8 cck_ack_rates, ofdm_ack_rates;
701 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
702 int i;
704 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
705 mvmvif->color));
706 cmd->action = cpu_to_le32(action);
708 switch (vif->type) {
709 case NL80211_IFTYPE_STATION:
710 if (vif->p2p)
711 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
712 else
713 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
714 break;
715 case NL80211_IFTYPE_AP:
716 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
717 break;
718 case NL80211_IFTYPE_MONITOR:
719 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
720 break;
721 case NL80211_IFTYPE_P2P_DEVICE:
722 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
723 break;
724 case NL80211_IFTYPE_ADHOC:
725 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
726 break;
727 default:
728 WARN_ON_ONCE(1);
731 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
733 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
735 if (bssid)
736 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
737 else
738 eth_broadcast_addr(cmd->bssid_addr);
740 rcu_read_lock();
741 chanctx = rcu_dereference(vif->chanctx_conf);
742 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
743 : NL80211_BAND_2GHZ,
744 &cck_ack_rates, &ofdm_ack_rates);
745 rcu_read_unlock();
747 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
748 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
750 cmd->cck_short_preamble =
751 cpu_to_le32(vif->bss_conf.use_short_preamble ?
752 MAC_FLG_SHORT_PREAMBLE : 0);
753 cmd->short_slot =
754 cpu_to_le32(vif->bss_conf.use_short_slot ?
755 MAC_FLG_SHORT_SLOT : 0);
757 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
759 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
760 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
762 cmd->ac[txf].cw_min =
763 cpu_to_le16(mvmvif->queue_params[i].cw_min);
764 cmd->ac[txf].cw_max =
765 cpu_to_le16(mvmvif->queue_params[i].cw_max);
766 cmd->ac[txf].edca_txop =
767 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
768 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
769 cmd->ac[txf].fifos_mask = BIT(txf);
772 if (vif->type == NL80211_IFTYPE_AP) {
773 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
774 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
775 BIT(IWL_MVM_TX_FIFO_MCAST);
778 * in AP mode, pass probe requests and beacons from other APs
779 * (needed for ht protection); when there're no any associated
780 * station don't ask FW to pass beacons to prevent unnecessary
781 * wake-ups.
783 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
784 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
785 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
786 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
787 } else {
788 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
792 if (vif->bss_conf.qos)
793 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
795 if (vif->bss_conf.use_cts_prot)
796 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
798 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
799 vif->bss_conf.use_cts_prot,
800 vif->bss_conf.ht_operation_mode);
801 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
802 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
803 if (ht_enabled)
804 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
807 static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
808 struct iwl_mac_ctx_cmd *cmd)
810 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
811 sizeof(*cmd), cmd);
812 if (ret)
813 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
814 le32_to_cpu(cmd->action), ret);
815 return ret;
818 static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
819 struct ieee80211_vif *vif,
820 u32 action, bool force_assoc_off,
821 const u8 *bssid_override)
823 struct iwl_mac_ctx_cmd cmd = {};
824 struct iwl_mac_data_sta *ctxt_sta;
826 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
828 /* Fill the common data for all mac context types */
829 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
831 if (vif->p2p) {
832 struct ieee80211_p2p_noa_attr *noa =
833 &vif->bss_conf.p2p_noa_attr;
835 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
836 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
837 ctxt_sta = &cmd.p2p_sta.sta;
838 } else {
839 ctxt_sta = &cmd.sta;
842 /* We need the dtim_period to set the MAC as associated */
843 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
844 !force_assoc_off) {
845 u32 dtim_offs;
848 * The DTIM count counts down, so when it is N that means N
849 * more beacon intervals happen until the DTIM TBTT. Therefore
850 * add this to the current time. If that ends up being in the
851 * future, the firmware will handle it.
853 * Also note that the system_timestamp (which we get here as
854 * "sync_device_ts") and TSF timestamp aren't at exactly the
855 * same offset in the frame -- the TSF is at the first symbol
856 * of the TSF, the system timestamp is at signal acquisition
857 * time. This means there's an offset between them of at most
858 * a few hundred microseconds (24 * 8 bits + PLCP time gives
859 * 384us in the longest case), this is currently not relevant
860 * as the firmware wakes up around 2ms before the TBTT.
862 dtim_offs = vif->bss_conf.sync_dtim_count *
863 vif->bss_conf.beacon_int;
864 /* convert TU to usecs */
865 dtim_offs *= 1024;
867 ctxt_sta->dtim_tsf =
868 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
869 ctxt_sta->dtim_time =
870 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
872 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
873 le64_to_cpu(ctxt_sta->dtim_tsf),
874 le32_to_cpu(ctxt_sta->dtim_time),
875 dtim_offs);
877 ctxt_sta->is_assoc = cpu_to_le32(1);
878 } else {
879 ctxt_sta->is_assoc = cpu_to_le32(0);
881 /* Allow beacons to pass through as long as we are not
882 * associated, or we do not have dtim period information.
884 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
887 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
888 ctxt_sta->bi_reciprocal =
889 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
890 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
891 vif->bss_conf.dtim_period);
892 ctxt_sta->dtim_reciprocal =
893 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
894 vif->bss_conf.dtim_period));
896 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
897 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
899 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
900 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
902 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
905 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
906 struct ieee80211_vif *vif,
907 u32 action)
909 struct iwl_mac_ctx_cmd cmd = {};
910 u32 tfd_queue_msk = 0;
911 int ret, i;
913 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
915 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
917 for (i = 0; i < IEEE80211_NUM_ACS; i++)
918 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
919 tfd_queue_msk |= BIT(vif->hw_queue[i]);
921 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
922 MAC_FILTER_IN_CONTROL_AND_MGMT |
923 MAC_FILTER_IN_BEACON |
924 MAC_FILTER_IN_PROBE_REQUEST |
925 MAC_FILTER_IN_CRC32);
926 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
928 /* Allocate sniffer station */
929 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
930 vif->type);
931 if (ret)
932 return ret;
934 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
937 static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
938 struct ieee80211_vif *vif,
939 u32 action)
941 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
942 struct iwl_mac_ctx_cmd cmd = {};
944 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
946 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
948 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
949 MAC_FILTER_IN_PROBE_REQUEST);
951 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
952 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
953 cmd.ibss.bi_reciprocal =
954 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
956 /* TODO: Assumes that the beacon id == mac context id */
957 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
959 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
962 struct iwl_mvm_go_iterator_data {
963 bool go_active;
966 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
968 struct iwl_mvm_go_iterator_data *data = _data;
969 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
971 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
972 mvmvif->ap_ibss_active)
973 data->go_active = true;
976 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
977 struct ieee80211_vif *vif,
978 u32 action)
980 struct iwl_mac_ctx_cmd cmd = {};
981 struct iwl_mvm_go_iterator_data data = {};
983 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
985 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
987 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
989 /* Override the filter flags to accept only probe requests */
990 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
993 * This flag should be set to true when the P2P Device is
994 * discoverable and there is at least another active P2P GO. Settings
995 * this flag will allow the P2P Device to be discoverable on other
996 * channels in addition to its listen channel.
997 * Note that this flag should not be set in other cases as it opens the
998 * Rx filters on all MAC and increases the number of interrupts.
1000 ieee80211_iterate_active_interfaces_atomic(
1001 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1002 iwl_mvm_go_iterator, &data);
1004 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
1005 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1008 static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
1009 struct iwl_mac_beacon_cmd *beacon_cmd,
1010 u8 *beacon, u32 frame_size)
1012 u32 tim_idx;
1013 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
1015 /* The index is relative to frame start but we start looking at the
1016 * variable-length part of the beacon. */
1017 tim_idx = mgmt->u.beacon.variable - beacon;
1019 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
1020 while ((tim_idx < (frame_size - 2)) &&
1021 (beacon[tim_idx] != WLAN_EID_TIM))
1022 tim_idx += beacon[tim_idx+1] + 2;
1024 /* If TIM field was found, set variables */
1025 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
1026 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
1027 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
1028 } else {
1029 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1033 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1034 struct ieee80211_vif *vif,
1035 struct sk_buff *beacon)
1037 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1038 struct iwl_host_cmd cmd = {
1039 .id = BEACON_TEMPLATE_CMD,
1040 .flags = CMD_ASYNC,
1042 struct iwl_mac_beacon_cmd beacon_cmd = {};
1043 struct ieee80211_tx_info *info;
1044 u32 beacon_skb_len;
1045 u32 rate, tx_flags;
1047 if (WARN_ON(!beacon))
1048 return -EINVAL;
1050 beacon_skb_len = beacon->len;
1052 /* TODO: for now the beacon template id is set to be the mac context id.
1053 * Might be better to handle it as another resource ... */
1054 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1055 info = IEEE80211_SKB_CB(beacon);
1057 /* Set up TX command fields */
1058 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
1059 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
1060 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
1061 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1062 tx_flags |=
1063 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1064 TX_CMD_FLG_BT_PRIO_POS;
1065 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
1067 if (!fw_has_capa(&mvm->fw->ucode_capa,
1068 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1069 mvm->mgmt_last_antenna_idx =
1070 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1071 mvm->mgmt_last_antenna_idx);
1074 beacon_cmd.tx.rate_n_flags =
1075 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1076 RATE_MCS_ANT_POS);
1078 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
1079 rate = IWL_FIRST_OFDM_RATE;
1080 } else {
1081 rate = IWL_FIRST_CCK_RATE;
1082 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1084 beacon_cmd.tx.rate_n_flags |=
1085 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1087 /* Set up TX beacon command fields */
1088 if (vif->type == NL80211_IFTYPE_AP)
1089 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1090 beacon->data,
1091 beacon_skb_len);
1093 /* Submit command */
1094 cmd.len[0] = sizeof(beacon_cmd);
1095 cmd.data[0] = &beacon_cmd;
1096 cmd.dataflags[0] = 0;
1097 cmd.len[1] = beacon_skb_len;
1098 cmd.data[1] = beacon->data;
1099 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1101 return iwl_mvm_send_cmd(mvm, &cmd);
1104 /* The beacon template for the AP/GO/IBSS has changed and needs update */
1105 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1106 struct ieee80211_vif *vif)
1108 struct sk_buff *beacon;
1109 int ret;
1111 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1112 vif->type != NL80211_IFTYPE_ADHOC);
1114 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1115 if (!beacon)
1116 return -ENOMEM;
1118 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1119 dev_kfree_skb(beacon);
1120 return ret;
1123 struct iwl_mvm_mac_ap_iterator_data {
1124 struct iwl_mvm *mvm;
1125 struct ieee80211_vif *vif;
1126 u32 beacon_device_ts;
1127 u16 beacon_int;
1130 /* Find the beacon_device_ts and beacon_int for a managed interface */
1131 static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1132 struct ieee80211_vif *vif)
1134 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1136 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1137 return;
1139 /* Station client has higher priority over P2P client*/
1140 if (vif->p2p && data->beacon_device_ts)
1141 return;
1143 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1144 data->beacon_int = vif->bss_conf.beacon_int;
1148 * Fill the specific data for mac context of type AP of P2P GO
1150 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1151 struct ieee80211_vif *vif,
1152 struct iwl_mac_data_ap *ctxt_ap,
1153 bool add)
1155 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1156 struct iwl_mvm_mac_ap_iterator_data data = {
1157 .mvm = mvm,
1158 .vif = vif,
1159 .beacon_device_ts = 0
1162 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1163 ctxt_ap->bi_reciprocal =
1164 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1165 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1166 vif->bss_conf.dtim_period);
1167 ctxt_ap->dtim_reciprocal =
1168 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1169 vif->bss_conf.dtim_period));
1171 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
1174 * Only set the beacon time when the MAC is being added, when we
1175 * just modify the MAC then we should keep the time -- the firmware
1176 * can otherwise have a "jumping" TBTT.
1178 if (add) {
1180 * If there is a station/P2P client interface which is
1181 * associated, set the AP's TBTT far enough from the station's
1182 * TBTT. Otherwise, set it to the current system time
1184 ieee80211_iterate_active_interfaces_atomic(
1185 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1186 iwl_mvm_mac_ap_iterator, &data);
1188 if (data.beacon_device_ts) {
1189 u32 rand = (prandom_u32() % (64 - 36)) + 36;
1190 mvmvif->ap_beacon_time = data.beacon_device_ts +
1191 ieee80211_tu_to_usec(data.beacon_int * rand /
1192 100);
1193 } else {
1194 mvmvif->ap_beacon_time =
1195 iwl_read_prph(mvm->trans,
1196 DEVICE_SYSTEM_TIME_REG);
1200 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1201 ctxt_ap->beacon_tsf = 0; /* unused */
1203 /* TODO: Assume that the beacon id == mac context id */
1204 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1207 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1208 struct ieee80211_vif *vif,
1209 u32 action)
1211 struct iwl_mac_ctx_cmd cmd = {};
1213 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1215 /* Fill the common data for all mac context types */
1216 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1218 /* Fill the data specific for ap mode */
1219 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1220 action == FW_CTXT_ACTION_ADD);
1222 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1225 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1226 struct ieee80211_vif *vif,
1227 u32 action)
1229 struct iwl_mac_ctx_cmd cmd = {};
1230 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1232 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1234 /* Fill the common data for all mac context types */
1235 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1237 /* Fill the data specific for GO mode */
1238 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1239 action == FW_CTXT_ACTION_ADD);
1241 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1242 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1243 cmd.go.opp_ps_enabled =
1244 cpu_to_le32(!!(noa->oppps_ctwindow &
1245 IEEE80211_P2P_OPPPS_ENABLE_BIT));
1247 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1250 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1251 u32 action, bool force_assoc_off,
1252 const u8 *bssid_override)
1254 switch (vif->type) {
1255 case NL80211_IFTYPE_STATION:
1256 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1257 force_assoc_off,
1258 bssid_override);
1259 break;
1260 case NL80211_IFTYPE_AP:
1261 if (!vif->p2p)
1262 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1263 else
1264 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1265 break;
1266 case NL80211_IFTYPE_MONITOR:
1267 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1268 case NL80211_IFTYPE_P2P_DEVICE:
1269 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1270 case NL80211_IFTYPE_ADHOC:
1271 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1272 default:
1273 break;
1276 return -EOPNOTSUPP;
1279 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1281 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1282 int ret;
1284 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1285 vif->addr, ieee80211_vif_type_p2p(vif)))
1286 return -EIO;
1288 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1289 true, NULL);
1290 if (ret)
1291 return ret;
1293 /* will only do anything at resume from D3 time */
1294 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1296 mvmvif->uploaded = true;
1297 return 0;
1300 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1301 bool force_assoc_off, const u8 *bssid_override)
1303 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1305 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1306 vif->addr, ieee80211_vif_type_p2p(vif)))
1307 return -EIO;
1309 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1310 force_assoc_off, bssid_override);
1313 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1315 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1316 struct iwl_mac_ctx_cmd cmd;
1317 int ret;
1319 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1320 vif->addr, ieee80211_vif_type_p2p(vif)))
1321 return -EIO;
1323 memset(&cmd, 0, sizeof(cmd));
1325 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1326 mvmvif->color));
1327 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1329 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1330 sizeof(cmd), &cmd);
1331 if (ret) {
1332 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1333 return ret;
1336 mvmvif->uploaded = false;
1338 if (vif->type == NL80211_IFTYPE_MONITOR) {
1339 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1340 iwl_mvm_dealloc_snif_sta(mvm);
1343 return 0;
1346 static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1347 struct ieee80211_vif *csa_vif, u32 gp2,
1348 bool tx_success)
1350 struct iwl_mvm_vif *mvmvif =
1351 iwl_mvm_vif_from_mac80211(csa_vif);
1353 /* Don't start to countdown from a failed beacon */
1354 if (!tx_success && !mvmvif->csa_countdown)
1355 return;
1357 mvmvif->csa_countdown = true;
1359 if (!ieee80211_csa_is_complete(csa_vif)) {
1360 int c = ieee80211_csa_update_counter(csa_vif);
1362 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1363 if (csa_vif->p2p &&
1364 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1365 tx_success) {
1366 u32 rel_time = (c + 1) *
1367 csa_vif->bss_conf.beacon_int -
1368 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1369 u32 apply_time = gp2 + rel_time * 1024;
1371 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1372 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1373 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1374 apply_time);
1376 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1377 /* we don't have CSA NoA scheduled yet, switch now */
1378 ieee80211_csa_finish(csa_vif);
1379 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1383 void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1384 struct iwl_rx_cmd_buffer *rxb)
1386 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1387 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1388 struct iwl_mvm_tx_resp *beacon_notify_hdr;
1389 struct ieee80211_vif *csa_vif;
1390 struct ieee80211_vif *tx_blocked_vif;
1391 u16 status;
1393 lockdep_assert_held(&mvm->mutex);
1395 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1396 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1398 status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
1399 IWL_DEBUG_RX(mvm,
1400 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1401 status, beacon_notify_hdr->failure_frame,
1402 le64_to_cpu(beacon->tsf),
1403 mvm->ap_last_beacon_gp2,
1404 le32_to_cpu(beacon_notify_hdr->initial_rate));
1406 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1407 lockdep_is_held(&mvm->mutex));
1408 if (unlikely(csa_vif && csa_vif->csa_active))
1409 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1410 (status == TX_STATUS_SUCCESS));
1412 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1413 lockdep_is_held(&mvm->mutex));
1414 if (unlikely(tx_blocked_vif)) {
1415 struct iwl_mvm_vif *mvmvif =
1416 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1419 * The channel switch is started and we have blocked the
1420 * stations. If this is the first beacon (the timeout wasn't
1421 * set), set the unblock timeout, otherwise countdown
1423 if (!mvm->csa_tx_block_bcn_timeout)
1424 mvm->csa_tx_block_bcn_timeout =
1425 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1426 else
1427 mvm->csa_tx_block_bcn_timeout--;
1429 /* Check if the timeout is expired, and unblock tx */
1430 if (mvm->csa_tx_block_bcn_timeout == 0) {
1431 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1432 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1437 static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1438 struct ieee80211_vif *vif)
1440 struct iwl_missed_beacons_notif *missed_beacons = _data;
1441 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1442 struct iwl_mvm *mvm = mvmvif->mvm;
1443 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1444 struct iwl_fw_dbg_trigger_tlv *trigger;
1445 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1446 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1448 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1449 return;
1451 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1452 rx_missed_bcon_since_rx =
1453 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
1455 * TODO: the threshold should be adjusted based on latency conditions,
1456 * and/or in case of a CS flow on one of the other AP vifs.
1458 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1459 IWL_MVM_MISSED_BEACONS_THRESHOLD)
1460 ieee80211_beacon_loss(vif);
1462 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1463 FW_DBG_TRIGGER_MISSED_BEACONS))
1464 return;
1466 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1467 FW_DBG_TRIGGER_MISSED_BEACONS);
1468 bcon_trig = (void *)trigger->data;
1469 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1470 stop_trig_missed_bcon_since_rx =
1471 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1473 /* TODO: implement start trigger */
1475 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1476 return;
1478 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1479 rx_missed_bcon >= stop_trig_missed_bcon)
1480 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
1483 void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1484 struct iwl_rx_cmd_buffer *rxb)
1486 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1487 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1489 IWL_DEBUG_INFO(mvm,
1490 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1491 le32_to_cpu(mb->mac_id),
1492 le32_to_cpu(mb->consec_missed_beacons),
1493 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1494 le32_to_cpu(mb->num_recvd_beacons),
1495 le32_to_cpu(mb->num_expected_beacons));
1497 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1498 IEEE80211_IFACE_ITER_NORMAL,
1499 iwl_mvm_beacon_loss_iterator,
1500 mb);
1503 void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1504 struct iwl_rx_cmd_buffer *rxb)
1506 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1507 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1508 struct ieee80211_rx_status rx_status;
1509 struct sk_buff *skb;
1510 u32 size = le32_to_cpu(sb->byte_count);
1512 if (size == 0)
1513 return;
1515 skb = alloc_skb(size, GFP_ATOMIC);
1516 if (!skb) {
1517 IWL_ERR(mvm, "alloc_skb failed\n");
1518 return;
1521 /* update rx_status according to the notification's metadata */
1522 memset(&rx_status, 0, sizeof(rx_status));
1523 rx_status.mactime = le64_to_cpu(sb->tsf);
1524 /* TSF as indicated by the firmware is at INA time */
1525 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1526 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1527 rx_status.band =
1528 (sb->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1529 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1530 rx_status.freq =
1531 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1532 rx_status.band);
1534 /* copy the data */
1535 memcpy(skb_put(skb, size), sb->data, size);
1536 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1538 /* pass it as regular rx to mac80211 */
1539 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);