]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/intel/iwlwifi/mvm/mac-ctxt.c
Merge remote-tracking branches 'asoc/topic/adsp', 'asoc/topic/ak4613', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / intel / iwlwifi / mvm / mac-ctxt.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
51368bf7 8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
0db056d3 10 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
8ca151b5
JB
11 *
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.
15 *
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.
20 *
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
25 *
26 * The full GNU General Public License is included in this distribution
410dc5aa 27 * in the file called COPYING.
8ca151b5
JB
28 *
29 * Contact Information:
cb2f8277 30 * Intel Linux Wireless <linuxwifi@intel.com>
8ca151b5
JB
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
51368bf7 35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
0db056d3 37 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
8ca151b5
JB
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
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.
53 *
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.
65 *
66 *****************************************************************************/
67
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"
7f0a7c67 74#include "time-event.h"
2f89a5d7 75#include "fw-dbg.h"
8ca151b5
JB
76
77const u8 iwl_mvm_ac_to_tx_fifo[] = {
8ca151b5 78 IWL_MVM_TX_FIFO_VO,
3dd94794
EG
79 IWL_MVM_TX_FIFO_VI,
80 IWL_MVM_TX_FIFO_BE,
81 IWL_MVM_TX_FIFO_BK,
8ca151b5
JB
82};
83
84struct 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)];
8ca151b5
JB
89 enum iwl_tsf_id preferred_tsf;
90 bool found_vif;
91};
92
a74346d7
AN
93struct iwl_mvm_hw_queues_iface_iterator_data {
94 struct ieee80211_vif *exclude_vif;
95 unsigned long used_hw_queues;
96};
97
6e97b0d2
IP
98static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
99 struct ieee80211_vif *vif)
8ca151b5
JB
100{
101 struct iwl_mvm_mac_iface_iterator_data *data = _data;
102 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2c3e62a1 103 u16 min_bi;
8ca151b5 104
6e97b0d2
IP
105 /* Skip the interface for which we are trying to assign a tsf_id */
106 if (vif == data->vif)
8ca151b5 107 return;
8ca151b5
JB
108
109 /*
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.
116 *
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.
121 *
122 * Depending on the new interface type it can be slaved to
123 * or become the master of an existing interface.
124 */
125 switch (data->vif->type) {
126 case NL80211_IFTYPE_STATION:
127 /*
2c3e62a1
IP
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.
8ca151b5 134 */
2c3e62a1
IP
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;
139
140 min_bi = min(data->vif->bss_conf.beacon_int,
141 vif->bss_conf.beacon_int);
142
143 if (!min_bi)
144 break;
145
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;
8ca151b5
JB
150 }
151 break;
2c3e62a1 152
8ca151b5
JB
153 case NL80211_IFTYPE_AP:
154 /*
2c3e62a1
IP
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.
8ca151b5 162 */
2c3e62a1
IP
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;
168
169 min_bi = min(data->vif->bss_conf.beacon_int,
170 vif->bss_conf.beacon_int);
171
172 if (!min_bi)
173 break;
174
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;
8ca151b5
JB
179 }
180 break;
181 default:
182 /*
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.
188 */
189 break;
190 }
191
192 /*
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.
197 */
198 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
199
200 if (data->preferred_tsf == mvmvif->tsf_id)
201 data->preferred_tsf = NUM_TSF_IDS;
202}
203
110cf810
JB
204/*
205 * Get the mask of the queues used by the vif
206 */
d92b732e 207u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
110cf810
JB
208{
209 u32 qmask = 0, ac;
210
211 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
212 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
213
1008e442
IP
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]);
217 }
110cf810 218
4d339989
LK
219 if (vif->type == NL80211_IFTYPE_AP ||
220 vif->type == NL80211_IFTYPE_ADHOC)
110cf810
JB
221 qmask |= BIT(vif->cab_queue);
222
223 return qmask;
224}
225
a74346d7
AN
226static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
227 struct ieee80211_vif *vif)
228{
229 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
230
231 /* exclude the given vif */
232 if (vif == data->exclude_vif)
233 return;
234
235 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
236}
237
238static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
239 struct ieee80211_sta *sta)
240{
241 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
242 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
243
244 /* Mark the queues used by the sta */
245 data->used_hw_queues |= mvmsta->tfd_queue_msk;
246}
247
248unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
249 struct ieee80211_vif *exclude_vif)
250{
51ec09af 251 u8 sta_id;
a74346d7
AN
252 struct iwl_mvm_hw_queues_iface_iterator_data data = {
253 .exclude_vif = exclude_vif,
254 .used_hw_queues =
255 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
097129c9 256 BIT(mvm->aux_queue),
a74346d7
AN
257 };
258
097129c9
LK
259 if (iwl_mvm_is_dqa_supported(mvm))
260 data.used_hw_queues |= BIT(IWL_MVM_DQA_CMD_QUEUE);
261 else
262 data.used_hw_queues |= BIT(IWL_MVM_CMD_QUEUE);
263
a74346d7
AN
264 lockdep_assert_held(&mvm->mutex);
265
266 /* mark all VIF used hw queues */
267 ieee80211_iterate_active_interfaces_atomic(
268 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
269 iwl_mvm_iface_hw_queues_iter, &data);
270
271 /* don't assign the same hw queues as TDLS stations */
272 ieee80211_iterate_stations_atomic(mvm->hw,
273 iwl_mvm_mac_sta_hw_queues_iter,
274 &data);
275
51ec09af
AN
276 /*
277 * Some TDLS stations may be removed but are in the process of being
278 * drained. Don't touch their queues.
279 */
280 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
281 data.used_hw_queues |= mvm->tfd_drained[sta_id];
282
a74346d7
AN
283 return data.used_hw_queues;
284}
285
6e97b0d2
IP
286static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
287 struct ieee80211_vif *vif)
288{
289 struct iwl_mvm_mac_iface_iterator_data *data = _data;
290 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6e97b0d2
IP
291
292 /* Iterator may already find the interface being added -- skip it */
293 if (vif == data->vif) {
294 data->found_vif = true;
295 return;
296 }
297
6e97b0d2
IP
298 /* Mark MAC IDs as used by clearing the available bit, and
299 * (below) mark TSFs as used if their existing use is not
300 * compatible with the new interface type.
301 * No locking or atomic bit operations are needed since the
302 * data is on the stack of the caller function.
303 */
304 __clear_bit(mvmvif->id, data->available_mac_ids);
305
306 /* find a suitable tsf_id */
307 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
308}
309
6e97b0d2
IP
310void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
311 struct ieee80211_vif *vif)
312{
313 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
314 struct iwl_mvm_mac_iface_iterator_data data = {
315 .mvm = mvm,
316 .vif = vif,
317 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
318 /* no preference yet */
319 .preferred_tsf = NUM_TSF_IDS,
320 };
321
322 ieee80211_iterate_active_interfaces_atomic(
323 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
324 iwl_mvm_mac_tsf_id_iter, &data);
325
326 if (data.preferred_tsf != NUM_TSF_IDS)
327 mvmvif->tsf_id = data.preferred_tsf;
328 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
329 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
330 NUM_TSF_IDS);
331}
332
8ca151b5
JB
333static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
334 struct ieee80211_vif *vif)
335{
336 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
337 struct iwl_mvm_mac_iface_iterator_data data = {
338 .mvm = mvm,
339 .vif = vif,
340 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
341 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
342 /* no preference yet */
343 .preferred_tsf = NUM_TSF_IDS,
8ca151b5
JB
344 .found_vif = false,
345 };
346 u32 ac;
9ee718aa 347 int ret, i;
110cf810 348 unsigned long used_hw_queues;
8ca151b5
JB
349
350 /*
351 * Allocate a MAC ID and a TSF for this MAC, along with the queues
352 * and other resources.
353 */
354
355 /*
356 * Before the iterator, we start with all MAC IDs and TSFs available.
357 *
358 * During iteration, all MAC IDs are cleared that are in use by other
359 * virtual interfaces, and all TSF IDs are cleared that can't be used
360 * by this new virtual interface because they're used by an interface
361 * that can't share it with the new one.
362 * At the same time, we check if there's a preferred TSF in the case
363 * that we should share it with another interface.
364 */
365
5023d966
JB
366 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
367 switch (vif->type) {
368 case NL80211_IFTYPE_ADHOC:
369 break;
370 case NL80211_IFTYPE_STATION:
371 if (!vif->p2p)
372 break;
373 /* fall through */
374 default:
ec8b6885 375 __clear_bit(0, data.available_mac_ids);
5023d966 376 }
ec8b6885 377
8ca151b5
JB
378 ieee80211_iterate_active_interfaces_atomic(
379 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
380 iwl_mvm_mac_iface_iterator, &data);
381
a74346d7
AN
382 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
383
8ca151b5
JB
384 /*
385 * In the case we're getting here during resume, it's similar to
386 * firmware restart, and with RESUME_ALL the iterator will find
387 * the vif being added already.
388 * We don't want to reassign any IDs in either case since doing
389 * so would probably assign different IDs (as interfaces aren't
390 * necessarily added in the same order), but the old IDs were
391 * preserved anyway, so skip ID assignment for both resume and
392 * recovery.
393 */
394 if (data.found_vif)
395 return 0;
396
397 /* Therefore, in recovery, we can't get here */
fd11bd05
IP
398 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
399 return -EBUSY;
8ca151b5
JB
400
401 mvmvif->id = find_first_bit(data.available_mac_ids,
402 NUM_MAC_INDEX_DRIVER);
403 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
404 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
405 ret = -EIO;
406 goto exit_fail;
407 }
408
409 if (data.preferred_tsf != NUM_TSF_IDS)
410 mvmvif->tsf_id = data.preferred_tsf;
411 else
412 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
413 NUM_TSF_IDS);
414 if (mvmvif->tsf_id == NUM_TSF_IDS) {
415 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
416 ret = -EIO;
417 goto exit_fail;
418 }
419
420 mvmvif->color = 0;
421
1e849c93
IP
422 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
423 mvmvif->time_event_data.id = TE_MAX;
424
8ca151b5
JB
425 /* No need to allocate data queues to P2P Device MAC.*/
426 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
427 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
428 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
429
430 return 0;
431 }
432
24afba76
LK
433 /*
434 * Find available queues, and allocate them to the ACs. When in
435 * DQA-mode they aren't really used, and this is done only so the
436 * mac80211 ieee80211_check_queues() function won't fail
437 */
8ca151b5 438 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
110cf810 439 u8 queue = find_first_zero_bit(&used_hw_queues,
19e737c9 440 mvm->first_agg_queue);
8ca151b5 441
24afba76
LK
442 if (!iwl_mvm_is_dqa_supported(mvm) &&
443 queue >= mvm->first_agg_queue) {
8ca151b5
JB
444 IWL_ERR(mvm, "Failed to allocate queue\n");
445 ret = -EIO;
446 goto exit_fail;
447 }
448
110cf810 449 __set_bit(queue, &used_hw_queues);
8ca151b5
JB
450 vif->hw_queue[ac] = queue;
451 }
452
453 /* Allocate the CAB queue for softAP and GO interfaces */
454 if (vif->type == NL80211_IFTYPE_AP) {
0e0e4420
LK
455 u8 queue;
456
457 if (!iwl_mvm_is_dqa_supported(mvm)) {
458 queue = find_first_zero_bit(&used_hw_queues,
459 mvm->first_agg_queue);
460
461 if (queue >= mvm->first_agg_queue) {
462 IWL_ERR(mvm, "Failed to allocate cab queue\n");
463 ret = -EIO;
464 goto exit_fail;
465 }
466 } else {
467 queue = IWL_MVM_DQA_GCAST_QUEUE;
8ca151b5
JB
468 }
469
470 vif->cab_queue = queue;
471 } else {
472 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
473 }
474
475 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
476 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
477
9ee718aa
EL
478 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
479 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
480
8ca151b5
JB
481 return 0;
482
483exit_fail:
484 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
485 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
486 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
487 return ret;
488}
489
490int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
491{
5d42e7b2
EG
492 unsigned int wdg_timeout =
493 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
8ca151b5
JB
494 u32 ac;
495 int ret;
496
497 lockdep_assert_held(&mvm->mutex);
498
499 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
500 if (ret)
501 return ret;
502
8236f7db
SS
503 /* If DQA is supported - queues will be enabled when needed */
504 if (iwl_mvm_is_dqa_supported(mvm))
505 return 0;
506
8ca151b5
JB
507 switch (vif->type) {
508 case NL80211_IFTYPE_P2P_DEVICE:
8236f7db
SS
509 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
510 IWL_MVM_OFFCHANNEL_QUEUE,
511 IWL_MVM_TX_FIFO_VO, 0, wdg_timeout);
8ca151b5
JB
512 break;
513 case NL80211_IFTYPE_AP:
4ecafae9 514 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue, vif->cab_queue,
5c1156ef 515 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
8ca151b5
JB
516 /* fall through */
517 default:
518 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3edf8ff6 519 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
4ecafae9 520 vif->hw_queue[ac],
5c1156ef 521 iwl_mvm_ac_to_tx_fifo[ac], 0,
4cf677fd 522 wdg_timeout);
8ca151b5
JB
523 break;
524 }
525
526 return 0;
527}
528
529void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
530{
531 int ac;
532
533 lockdep_assert_held(&mvm->mutex);
534
df88c08d
LK
535 /*
536 * If DQA is supported - queues were already disabled, since in
537 * DQA-mode the queues are a property of the STA and not of the
538 * vif, and at this point the STA was already deleted
539 */
540 if (iwl_mvm_is_dqa_supported(mvm))
541 return;
542
8ca151b5
JB
543 switch (vif->type) {
544 case NL80211_IFTYPE_P2P_DEVICE:
df88c08d
LK
545 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
546 IWL_MVM_OFFCHANNEL_QUEUE,
547 IWL_MAX_TID_COUNT, 0);
4c965139 548
8ca151b5
JB
549 break;
550 case NL80211_IFTYPE_AP:
4ecafae9
LK
551 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
552 IWL_MAX_TID_COUNT, 0);
8ca151b5
JB
553 /* fall through */
554 default:
555 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
4ecafae9
LK
556 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac],
557 vif->hw_queue[ac],
558 IWL_MAX_TID_COUNT, 0);
8ca151b5
JB
559 }
560}
561
562static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
563 struct ieee80211_vif *vif,
57fbcce3 564 enum nl80211_band band,
8ca151b5
JB
565 u8 *cck_rates, u8 *ofdm_rates)
566{
567 struct ieee80211_supported_band *sband;
568 unsigned long basic = vif->bss_conf.basic_rates;
569 int lowest_present_ofdm = 100;
570 int lowest_present_cck = 100;
571 u8 cck = 0;
572 u8 ofdm = 0;
573 int i;
574
575 sband = mvm->hw->wiphy->bands[band];
576
577 for_each_set_bit(i, &basic, BITS_PER_LONG) {
578 int hw = sband->bitrates[i].hw_value;
579 if (hw >= IWL_FIRST_OFDM_RATE) {
580 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
581 if (lowest_present_ofdm > hw)
582 lowest_present_ofdm = hw;
583 } else {
584 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
585
586 cck |= BIT(hw);
587 if (lowest_present_cck > hw)
588 lowest_present_cck = hw;
589 }
590 }
591
592 /*
593 * Now we've got the basic rates as bitmaps in the ofdm and cck
594 * variables. This isn't sufficient though, as there might not
595 * be all the right rates in the bitmap. E.g. if the only basic
596 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
597 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
598 *
599 * [...] a STA responding to a received frame shall transmit
600 * its Control Response frame [...] at the highest rate in the
601 * BSSBasicRateSet parameter that is less than or equal to the
602 * rate of the immediately previous frame in the frame exchange
603 * sequence ([...]) and that is of the same modulation class
604 * ([...]) as the received frame. If no rate contained in the
605 * BSSBasicRateSet parameter meets these conditions, then the
606 * control frame sent in response to a received frame shall be
607 * transmitted at the highest mandatory rate of the PHY that is
608 * less than or equal to the rate of the received frame, and
609 * that is of the same modulation class as the received frame.
610 *
611 * As a consequence, we need to add all mandatory rates that are
612 * lower than all of the basic rates to these bitmaps.
613 */
614
615 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
616 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
617 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
618 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
619 /* 6M already there or needed so always add */
620 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
621
622 /*
623 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
624 * Note, however:
625 * - if no CCK rates are basic, it must be ERP since there must
626 * be some basic rates at all, so they're OFDM => ERP PHY
627 * (or we're in 5 GHz, and the cck bitmap will never be used)
628 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
629 * - if 5.5M is basic, 1M and 2M are mandatory
630 * - if 2M is basic, 1M is mandatory
631 * - if 1M is basic, that's the only valid ACK rate.
632 * As a consequence, it's not as complicated as it sounds, just add
633 * any lower rates to the ACK rate bitmap.
634 */
635 if (IWL_RATE_11M_INDEX < lowest_present_cck)
636 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
637 if (IWL_RATE_5M_INDEX < lowest_present_cck)
638 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
639 if (IWL_RATE_2M_INDEX < lowest_present_cck)
640 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
641 /* 1M already there or needed so always add */
642 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
643
644 *cck_rates = cck;
645 *ofdm_rates = ofdm;
646}
647
8a5e3660
AA
648static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
649 struct ieee80211_vif *vif,
650 struct iwl_mac_ctx_cmd *cmd)
651{
652 /* for both sta and ap, ht_operation_mode hold the protection_mode */
653 u8 protection_mode = vif->bss_conf.ht_operation_mode &
654 IEEE80211_HT_OP_MODE_PROTECTION;
655 /* The fw does not distinguish between ht and fat */
656 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
657
658 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
659 /*
660 * See section 9.23.3.1 of IEEE 80211-2012.
661 * Nongreenfield HT STAs Present is not supported.
662 */
663 switch (protection_mode) {
664 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
665 break;
666 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
667 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
668 cmd->protection_flags |= cpu_to_le32(ht_flag);
669 break;
670 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
671 /* Protect when channel wider than 20MHz */
672 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
673 cmd->protection_flags |= cpu_to_le32(ht_flag);
674 break;
675 default:
676 IWL_ERR(mvm, "Illegal protection mode %d\n",
677 protection_mode);
678 break;
679 }
680}
681
8ca151b5
JB
682static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
683 struct ieee80211_vif *vif,
684 struct iwl_mac_ctx_cmd *cmd,
3dfd3a97 685 const u8 *bssid_override,
8ca151b5
JB
686 u32 action)
687{
688 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
689 struct ieee80211_chanctx_conf *chanctx;
8a5e3660
AA
690 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
691 IEEE80211_HT_OP_MODE_PROTECTION);
8ca151b5 692 u8 cck_ack_rates, ofdm_ack_rates;
3dfd3a97 693 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
8ca151b5
JB
694 int i;
695
696 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
697 mvmvif->color));
698 cmd->action = cpu_to_le32(action);
699
700 switch (vif->type) {
701 case NL80211_IFTYPE_STATION:
702 if (vif->p2p)
703 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
704 else
705 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
706 break;
707 case NL80211_IFTYPE_AP:
708 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
709 break;
710 case NL80211_IFTYPE_MONITOR:
711 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
712 break;
713 case NL80211_IFTYPE_P2P_DEVICE:
714 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
715 break;
716 case NL80211_IFTYPE_ADHOC:
717 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
718 break;
719 default:
720 WARN_ON_ONCE(1);
721 }
722
723 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
724
725 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
3dfd3a97
JB
726
727 if (bssid)
728 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
8ca151b5
JB
729 else
730 eth_broadcast_addr(cmd->bssid_addr);
731
732 rcu_read_lock();
733 chanctx = rcu_dereference(vif->chanctx_conf);
734 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
57fbcce3 735 : NL80211_BAND_2GHZ,
8ca151b5
JB
736 &cck_ack_rates, &ofdm_ack_rates);
737 rcu_read_unlock();
738
739 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
740 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
741
742 cmd->cck_short_preamble =
743 cpu_to_le32(vif->bss_conf.use_short_preamble ?
744 MAC_FLG_SHORT_PREAMBLE : 0);
745 cmd->short_slot =
746 cpu_to_le32(vif->bss_conf.use_short_slot ?
747 MAC_FLG_SHORT_SLOT : 0);
748
bd6f5bd7
AB
749 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
750
3dd94794
EG
751 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
752 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
753
754 cmd->ac[txf].cw_min =
755 cpu_to_le16(mvmvif->queue_params[i].cw_min);
756 cmd->ac[txf].cw_max =
757 cpu_to_le16(mvmvif->queue_params[i].cw_max);
758 cmd->ac[txf].edca_txop =
8ca151b5 759 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
3dd94794
EG
760 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
761 cmd->ac[txf].fifos_mask = BIT(txf);
8ca151b5
JB
762 }
763
764 if (vif->bss_conf.qos)
765 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
766
dc271ee0 767 if (vif->bss_conf.use_cts_prot)
cc7ee2ba 768 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
dc271ee0 769
8a5e3660
AA
770 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
771 vif->bss_conf.use_cts_prot,
772 vif->bss_conf.ht_operation_mode);
773 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
8ca151b5 774 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
8a5e3660
AA
775 if (ht_enabled)
776 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
8ca151b5
JB
777}
778
779static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
780 struct iwl_mac_ctx_cmd *cmd)
781{
a1022927 782 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
8ca151b5
JB
783 sizeof(*cmd), cmd);
784 if (ret)
785 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
786 le32_to_cpu(cmd->action), ret);
787 return ret;
788}
789
cf52023c
LC
790static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
791 struct ieee80211_vif *vif,
3dfd3a97
JB
792 u32 action, bool force_assoc_off,
793 const u8 *bssid_override)
8ca151b5 794{
cf52023c
LC
795 struct iwl_mac_ctx_cmd cmd = {};
796 struct iwl_mac_data_sta *ctxt_sta;
797
798 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
799
800 /* Fill the common data for all mac context types */
3dfd3a97 801 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
cf52023c
LC
802
803 if (vif->p2p) {
804 struct ieee80211_p2p_noa_attr *noa =
805 &vif->bss_conf.p2p_noa_attr;
806
807 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
808 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
809 ctxt_sta = &cmd.p2p_sta.sta;
810 } else {
cf52023c
LC
811 ctxt_sta = &cmd.sta;
812 }
813
210a544e 814 /* We need the dtim_period to set the MAC as associated */
ba283927
AB
815 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
816 !force_assoc_off) {
d2931bbd
JB
817 u32 dtim_offs;
818
819 /*
820 * The DTIM count counts down, so when it is N that means N
821 * more beacon intervals happen until the DTIM TBTT. Therefore
822 * add this to the current time. If that ends up being in the
823 * future, the firmware will handle it.
824 *
825 * Also note that the system_timestamp (which we get here as
826 * "sync_device_ts") and TSF timestamp aren't at exactly the
827 * same offset in the frame -- the TSF is at the first symbol
828 * of the TSF, the system timestamp is at signal acquisition
829 * time. This means there's an offset between them of at most
830 * a few hundred microseconds (24 * 8 bits + PLCP time gives
831 * 384us in the longest case), this is currently not relevant
832 * as the firmware wakes up around 2ms before the TBTT.
833 */
834 dtim_offs = vif->bss_conf.sync_dtim_count *
835 vif->bss_conf.beacon_int;
836 /* convert TU to usecs */
837 dtim_offs *= 1024;
838
839 ctxt_sta->dtim_tsf =
840 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
841 ctxt_sta->dtim_time =
842 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
843
844 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
845 le64_to_cpu(ctxt_sta->dtim_tsf),
846 le32_to_cpu(ctxt_sta->dtim_time),
847 dtim_offs);
848
210a544e 849 ctxt_sta->is_assoc = cpu_to_le32(1);
d2931bbd 850 } else {
210a544e 851 ctxt_sta->is_assoc = cpu_to_le32(0);
7c8b3bc6
LC
852
853 /* Allow beacons to pass through as long as we are not
854 * associated, or we do not have dtim period information.
855 */
856 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
d2931bbd 857 }
8ca151b5
JB
858
859 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
860 ctxt_sta->bi_reciprocal =
861 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
862 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
863 vif->bss_conf.dtim_period);
864 ctxt_sta->dtim_reciprocal =
865 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
866 vif->bss_conf.dtim_period));
867
868 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
869 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
8ca151b5 870
effd1929
AO
871 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
872 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
873
8ca151b5
JB
874 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
875}
876
877static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
878 struct ieee80211_vif *vif,
879 u32 action)
880{
881 struct iwl_mac_ctx_cmd cmd = {};
0e39eb03
CRI
882 u32 tfd_queue_msk = 0;
883 int ret, i;
8ca151b5
JB
884
885 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
886
3dfd3a97 887 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
53585495 888
737847aa
LK
889 if (!iwl_mvm_is_dqa_supported(mvm)) {
890 for (i = 0; i < IEEE80211_NUM_ACS; i++)
891 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
892 tfd_queue_msk |= BIT(vif->hw_queue[i]);
893 }
0e39eb03 894
53585495
JB
895 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
896 MAC_FILTER_IN_CONTROL_AND_MGMT |
897 MAC_FILTER_IN_BEACON |
fb8b8ee1
JB
898 MAC_FILTER_IN_PROBE_REQUEST |
899 MAC_FILTER_IN_CRC32);
30686bf7 900 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
53585495 901
0e39eb03
CRI
902 /* Allocate sniffer station */
903 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
904 vif->type);
905 if (ret)
906 return ret;
907
8ca151b5
JB
908 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
909}
910
5023d966
JB
911static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
912 struct ieee80211_vif *vif,
913 u32 action)
914{
915 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
916 struct iwl_mac_ctx_cmd cmd = {};
917
918 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
919
3dfd3a97 920 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
5023d966
JB
921
922 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
923 MAC_FILTER_IN_PROBE_REQUEST);
924
925 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
926 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
927 cmd.ibss.bi_reciprocal =
928 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
929
930 /* TODO: Assumes that the beacon id == mac context id */
931 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
932
933 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
934}
935
8ca151b5
JB
936struct iwl_mvm_go_iterator_data {
937 bool go_active;
938};
939
940static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
941{
942 struct iwl_mvm_go_iterator_data *data = _data;
943 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
944
5023d966
JB
945 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
946 mvmvif->ap_ibss_active)
8ca151b5
JB
947 data->go_active = true;
948}
949
950static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
951 struct ieee80211_vif *vif,
952 u32 action)
953{
954 struct iwl_mac_ctx_cmd cmd = {};
955 struct iwl_mvm_go_iterator_data data = {};
956
957 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
958
3dfd3a97 959 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8ca151b5
JB
960
961 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
1dcd15ee
IP
962
963 /* Override the filter flags to accept only probe requests */
964 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
8ca151b5
JB
965
966 /*
967 * This flag should be set to true when the P2P Device is
968 * discoverable and there is at least another active P2P GO. Settings
969 * this flag will allow the P2P Device to be discoverable on other
970 * channels in addition to its listen channel.
971 * Note that this flag should not be set in other cases as it opens the
972 * Rx filters on all MAC and increases the number of interrupts.
973 */
974 ieee80211_iterate_active_interfaces_atomic(
975 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
976 iwl_mvm_go_iterator, &data);
977
978 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
979 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
980}
981
982static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
8364fbb4 983 __le32 *tim_index, __le32 *tim_size,
8ca151b5
JB
984 u8 *beacon, u32 frame_size)
985{
986 u32 tim_idx;
987 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
988
989 /* The index is relative to frame start but we start looking at the
990 * variable-length part of the beacon. */
991 tim_idx = mgmt->u.beacon.variable - beacon;
992
993 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
994 while ((tim_idx < (frame_size - 2)) &&
995 (beacon[tim_idx] != WLAN_EID_TIM))
996 tim_idx += beacon[tim_idx+1] + 2;
997
998 /* If TIM field was found, set variables */
999 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
8364fbb4
SS
1000 *tim_index = cpu_to_le32(tim_idx);
1001 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
8ca151b5
JB
1002 } else {
1003 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
1004 }
1005}
1006
d3a108a4
AO
1007static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
1008{
1009 struct ieee80211_mgmt *mgmt = (void *)beacon;
1010 const u8 *ie;
1011
1012 if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
1013 return 0;
1014
1015 frame_size -= mgmt->u.beacon.variable - beacon;
1016
1017 ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
1018 if (!ie)
1019 return 0;
1020
1021 return ie - beacon;
1022}
1023
8ca151b5
JB
1024static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1025 struct ieee80211_vif *vif,
1026 struct sk_buff *beacon)
1027{
1028 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1029 struct iwl_host_cmd cmd = {
1030 .id = BEACON_TEMPLATE_CMD,
1031 .flags = CMD_ASYNC,
1032 };
d3a108a4
AO
1033 union {
1034 struct iwl_mac_beacon_cmd_v6 beacon_cmd_v6;
8364fbb4 1035 struct iwl_mac_beacon_cmd_v7 beacon_cmd;
d3a108a4 1036 } u = {};
8364fbb4 1037 struct iwl_mac_beacon_cmd beacon_cmd;
8ca151b5
JB
1038 struct ieee80211_tx_info *info;
1039 u32 beacon_skb_len;
75f6b9b6 1040 u32 rate, tx_flags;
8ca151b5
JB
1041
1042 if (WARN_ON(!beacon))
1043 return -EINVAL;
1044
1045 beacon_skb_len = beacon->len;
1046
8364fbb4
SS
1047 if (fw_has_capa(&mvm->fw->ucode_capa,
1048 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD)) {
1049 u32 csa_offset, ecsa_offset;
1050
1051 csa_offset = iwl_mvm_find_ie_offset(beacon->data,
1052 WLAN_EID_CHANNEL_SWITCH,
1053 beacon_skb_len);
1054 ecsa_offset =
1055 iwl_mvm_find_ie_offset(beacon->data,
1056 WLAN_EID_EXT_CHANSWITCH_ANN,
1057 beacon_skb_len);
1058
1059 if (iwl_mvm_has_new_tx_api(mvm)) {
1060 beacon_cmd.data.template_id =
1061 cpu_to_le32((u32)mvmvif->id);
1062 beacon_cmd.data.ecsa_offset = cpu_to_le32(ecsa_offset);
1063 beacon_cmd.data.csa_offset = cpu_to_le32(csa_offset);
1064 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon_skb_len);
1065 if (vif->type == NL80211_IFTYPE_AP)
1066 iwl_mvm_mac_ctxt_set_tim(mvm,
1067 &beacon_cmd.data.tim_idx,
1068 &beacon_cmd.data.tim_size,
1069 beacon->data,
1070 beacon_skb_len);
1071 cmd.len[0] = sizeof(beacon_cmd);
1072 cmd.data[0] = &beacon_cmd;
1073 goto send;
1074
1075 } else {
1076 u.beacon_cmd.data.ecsa_offset =
1077 cpu_to_le32(ecsa_offset);
1078 u.beacon_cmd.data.csa_offset = cpu_to_le32(csa_offset);
1079 cmd.len[0] = sizeof(u.beacon_cmd);
1080 cmd.data[0] = &u;
1081 }
1082 } else {
1083 cmd.len[0] = sizeof(u.beacon_cmd_v6);
1084 cmd.data[0] = &u;
1085 }
1086
8ca151b5
JB
1087 /* TODO: for now the beacon template id is set to be the mac context id.
1088 * Might be better to handle it as another resource ... */
d3a108a4 1089 u.beacon_cmd_v6.template_id = cpu_to_le32((u32)mvmvif->id);
75f6b9b6 1090 info = IEEE80211_SKB_CB(beacon);
8ca151b5
JB
1091
1092 /* Set up TX command fields */
d3a108a4
AO
1093 u.beacon_cmd_v6.tx.len = cpu_to_le16((u16)beacon_skb_len);
1094 u.beacon_cmd_v6.tx.sta_id = mvmvif->bcast_sta.sta_id;
1095 u.beacon_cmd_v6.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
75f6b9b6
EG
1096 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
1097 tx_flags |=
1098 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
1099 TX_CMD_FLG_BT_PRIO_POS;
d3a108a4 1100 u.beacon_cmd_v6.tx.tx_flags = cpu_to_le32(tx_flags);
8ca151b5 1101
1e3c3c35
EG
1102 if (!fw_has_capa(&mvm->fw->ucode_capa,
1103 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
1104 mvm->mgmt_last_antenna_idx =
1105 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
1106 mvm->mgmt_last_antenna_idx);
1107 }
8ca151b5 1108
d3a108a4 1109 u.beacon_cmd_v6.tx.rate_n_flags =
8ca151b5
JB
1110 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1111 RATE_MCS_ANT_POS);
1112
57fbcce3 1113 if (info->band == NL80211_BAND_5GHZ || vif->p2p) {
8ca151b5
JB
1114 rate = IWL_FIRST_OFDM_RATE;
1115 } else {
1116 rate = IWL_FIRST_CCK_RATE;
d3a108a4
AO
1117 u.beacon_cmd_v6.tx.rate_n_flags |=
1118 cpu_to_le32(RATE_MCS_CCK_MSK);
8ca151b5 1119 }
d3a108a4 1120 u.beacon_cmd_v6.tx.rate_n_flags |=
8ca151b5
JB
1121 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1122
1123 /* Set up TX beacon command fields */
5023d966 1124 if (vif->type == NL80211_IFTYPE_AP)
8364fbb4
SS
1125 iwl_mvm_mac_ctxt_set_tim(mvm, &u.beacon_cmd_v6.tim_idx,
1126 &u.beacon_cmd_v6.tim_size,
5023d966
JB
1127 beacon->data,
1128 beacon_skb_len);
8ca151b5 1129
8364fbb4 1130send:
8ca151b5 1131 /* Submit command */
8ca151b5
JB
1132 cmd.dataflags[0] = 0;
1133 cmd.len[1] = beacon_skb_len;
1134 cmd.data[1] = beacon->data;
1135 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1136
1137 return iwl_mvm_send_cmd(mvm, &cmd);
1138}
1139
5023d966 1140/* The beacon template for the AP/GO/IBSS has changed and needs update */
8ca151b5
JB
1141int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1142 struct ieee80211_vif *vif)
1143{
1144 struct sk_buff *beacon;
1145 int ret;
1146
5023d966
JB
1147 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1148 vif->type != NL80211_IFTYPE_ADHOC);
8ca151b5 1149
fe887665 1150 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
8ca151b5
JB
1151 if (!beacon)
1152 return -ENOMEM;
1153
1154 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1155 dev_kfree_skb(beacon);
1156 return ret;
1157}
1158
3a3cb92e
IP
1159struct iwl_mvm_mac_ap_iterator_data {
1160 struct iwl_mvm *mvm;
1161 struct ieee80211_vif *vif;
1162 u32 beacon_device_ts;
1163 u16 beacon_int;
1164};
1165
1166/* Find the beacon_device_ts and beacon_int for a managed interface */
1167static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1168 struct ieee80211_vif *vif)
1169{
1170 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1171
1172 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1173 return;
1174
1175 /* Station client has higher priority over P2P client*/
1176 if (vif->p2p && data->beacon_device_ts)
1177 return;
1178
1179 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1180 data->beacon_int = vif->bss_conf.beacon_int;
1181}
1182
8ca151b5
JB
1183/*
1184 * Fill the specific data for mac context of type AP of P2P GO
1185 */
1186static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1187 struct ieee80211_vif *vif,
186cd49a 1188 struct iwl_mac_ctx_cmd *cmd,
506a81e6
JB
1189 struct iwl_mac_data_ap *ctxt_ap,
1190 bool add)
8ca151b5
JB
1191{
1192 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3a3cb92e
IP
1193 struct iwl_mvm_mac_ap_iterator_data data = {
1194 .mvm = mvm,
1195 .vif = vif,
1196 .beacon_device_ts = 0
1197 };
8ca151b5 1198
186cd49a
JB
1199 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1200 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1201
1202 /*
1203 * in AP mode, pass probe requests and beacons from other APs
1204 * (needed for ht protection); when there're no any associated
1205 * station don't ask FW to pass beacons to prevent unnecessary
1206 * wake-ups.
1207 */
1208 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1209 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1210 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1211 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1212 } else {
1213 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1214 }
1215
8ca151b5
JB
1216 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1217 ctxt_ap->bi_reciprocal =
1218 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1219 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1220 vif->bss_conf.dtim_period);
1221 ctxt_ap->dtim_reciprocal =
1222 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1223 vif->bss_conf.dtim_period));
1224
1225 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
8ca151b5 1226
506a81e6 1227 /*
3a3cb92e 1228 * Only set the beacon time when the MAC is being added, when we
506a81e6
JB
1229 * just modify the MAC then we should keep the time -- the firmware
1230 * can otherwise have a "jumping" TBTT.
1231 */
3a3cb92e
IP
1232 if (add) {
1233 /*
1234 * If there is a station/P2P client interface which is
1235 * associated, set the AP's TBTT far enough from the station's
1236 * TBTT. Otherwise, set it to the current system time
1237 */
1238 ieee80211_iterate_active_interfaces_atomic(
1239 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1240 iwl_mvm_mac_ap_iterator, &data);
1241
1242 if (data.beacon_device_ts) {
9f8f8ca5 1243 u32 rand = (prandom_u32() % (64 - 36)) + 36;
3a3cb92e
IP
1244 mvmvif->ap_beacon_time = data.beacon_device_ts +
1245 ieee80211_tu_to_usec(data.beacon_int * rand /
1246 100);
1247 } else {
1248 mvmvif->ap_beacon_time =
1249 iwl_read_prph(mvm->trans,
1250 DEVICE_SYSTEM_TIME_REG);
1251 }
1252 }
506a81e6
JB
1253
1254 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
506a81e6 1255 ctxt_ap->beacon_tsf = 0; /* unused */
8ca151b5
JB
1256
1257 /* TODO: Assume that the beacon id == mac context id */
1258 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1259}
1260
f82c8339
IP
1261static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1262 struct ieee80211_vif *vif,
1263 u32 action)
8ca151b5
JB
1264{
1265 struct iwl_mac_ctx_cmd cmd = {};
1266
1267 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1268
1269 /* Fill the common data for all mac context types */
3dfd3a97 1270 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8ca151b5
JB
1271
1272 /* Fill the data specific for ap mode */
186cd49a 1273 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
506a81e6 1274 action == FW_CTXT_ACTION_ADD);
8ca151b5
JB
1275
1276 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1277}
1278
1279static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1280 struct ieee80211_vif *vif,
1281 u32 action)
1282{
1283 struct iwl_mac_ctx_cmd cmd = {};
67baf663 1284 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
8ca151b5
JB
1285
1286 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1287
1288 /* Fill the common data for all mac context types */
3dfd3a97 1289 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
8ca151b5
JB
1290
1291 /* Fill the data specific for GO mode */
186cd49a 1292 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
506a81e6 1293 action == FW_CTXT_ACTION_ADD);
8ca151b5 1294
67baf663
JD
1295 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1296 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1297 cmd.go.opp_ps_enabled =
1298 cpu_to_le32(!!(noa->oppps_ctwindow &
1299 IEEE80211_P2P_OPPPS_ENABLE_BIT));
8ca151b5
JB
1300
1301 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1302}
1303
1304static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3dfd3a97
JB
1305 u32 action, bool force_assoc_off,
1306 const u8 *bssid_override)
8ca151b5
JB
1307{
1308 switch (vif->type) {
1309 case NL80211_IFTYPE_STATION:
cf52023c 1310 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
3dfd3a97
JB
1311 force_assoc_off,
1312 bssid_override);
8ca151b5
JB
1313 break;
1314 case NL80211_IFTYPE_AP:
1315 if (!vif->p2p)
1316 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1317 else
1318 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1319 break;
1320 case NL80211_IFTYPE_MONITOR:
1321 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1322 case NL80211_IFTYPE_P2P_DEVICE:
1323 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
5023d966
JB
1324 case NL80211_IFTYPE_ADHOC:
1325 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
8ca151b5
JB
1326 default:
1327 break;
1328 }
1329
1330 return -EOPNOTSUPP;
1331}
1332
1333int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1334{
1335 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1336 int ret;
1337
1338 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1339 vif->addr, ieee80211_vif_type_p2p(vif)))
1340 return -EIO;
1341
bca49d9a 1342 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
3dfd3a97 1343 true, NULL);
8ca151b5
JB
1344 if (ret)
1345 return ret;
1346
6d9d32b8
JB
1347 /* will only do anything at resume from D3 time */
1348 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1349
8ca151b5
JB
1350 mvmvif->uploaded = true;
1351 return 0;
1352}
1353
bca49d9a 1354int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3dfd3a97 1355 bool force_assoc_off, const u8 *bssid_override)
8ca151b5
JB
1356{
1357 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1358
1359 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1360 vif->addr, ieee80211_vif_type_p2p(vif)))
1361 return -EIO;
1362
bca49d9a 1363 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
3dfd3a97 1364 force_assoc_off, bssid_override);
8ca151b5
JB
1365}
1366
1367int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1368{
1369 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1370 struct iwl_mac_ctx_cmd cmd;
1371 int ret;
1372
1373 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1374 vif->addr, ieee80211_vif_type_p2p(vif)))
1375 return -EIO;
1376
1377 memset(&cmd, 0, sizeof(cmd));
1378
1379 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1380 mvmvif->color));
1381 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1382
a1022927 1383 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
8ca151b5
JB
1384 sizeof(cmd), &cmd);
1385 if (ret) {
1386 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1387 return ret;
1388 }
1389
1390 mvmvif->uploaded = false;
fb8b8ee1 1391
0e39eb03 1392 if (vif->type == NL80211_IFTYPE_MONITOR) {
30686bf7 1393 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
0e39eb03
CRI
1394 iwl_mvm_dealloc_snif_sta(mvm);
1395 }
fb8b8ee1 1396
8ca151b5
JB
1397 return 0;
1398}
571765c8 1399
7f0a7c67 1400static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
7ef0aab6
AO
1401 struct ieee80211_vif *csa_vif, u32 gp2,
1402 bool tx_success)
7f0a7c67
AO
1403{
1404 struct iwl_mvm_vif *mvmvif =
1405 iwl_mvm_vif_from_mac80211(csa_vif);
1406
7ef0aab6
AO
1407 /* Don't start to countdown from a failed beacon */
1408 if (!tx_success && !mvmvif->csa_countdown)
1409 return;
1410
1411 mvmvif->csa_countdown = true;
1412
7f0a7c67
AO
1413 if (!ieee80211_csa_is_complete(csa_vif)) {
1414 int c = ieee80211_csa_update_counter(csa_vif);
1415
1416 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1417 if (csa_vif->p2p &&
7ef0aab6
AO
1418 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1419 tx_success) {
7f0a7c67
AO
1420 u32 rel_time = (c + 1) *
1421 csa_vif->bss_conf.beacon_int -
f991e17b 1422 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
7f0a7c67
AO
1423 u32 apply_time = gp2 + rel_time * 1024;
1424
f991e17b
LC
1425 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1426 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1427 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1428 apply_time);
7f0a7c67
AO
1429 }
1430 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1431 /* we don't have CSA NoA scheduled yet, switch now */
1432 ieee80211_csa_finish(csa_vif);
1433 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1434 }
1435}
1436
0416841d
JB
1437void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1438 struct iwl_rx_cmd_buffer *rxb)
571765c8
IP
1439{
1440 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2d2c0e9c 1441 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1c87bbad 1442 struct iwl_mvm_tx_resp *beacon_notify_hdr;
664322fa 1443 struct ieee80211_vif *csa_vif;
003e5236 1444 struct ieee80211_vif *tx_blocked_vif;
7ef0aab6 1445 u16 status;
571765c8 1446
bd3398e2
AO
1447 lockdep_assert_held(&mvm->mutex);
1448
2d2c0e9c
EG
1449 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1450 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1c87bbad 1451
7ef0aab6 1452 status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
1c87bbad
DS
1453 IWL_DEBUG_RX(mvm,
1454 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
2d2c0e9c
EG
1455 status, beacon_notify_hdr->failure_frame,
1456 le64_to_cpu(beacon->tsf),
1c87bbad
DS
1457 mvm->ap_last_beacon_gp2,
1458 le32_to_cpu(beacon_notify_hdr->initial_rate));
bd3398e2 1459
664322fa
AO
1460 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1461 lockdep_is_held(&mvm->mutex));
7f0a7c67 1462 if (unlikely(csa_vif && csa_vif->csa_active))
7ef0aab6
AO
1463 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1464 (status == TX_STATUS_SUCCESS));
bd3398e2 1465
003e5236
AO
1466 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1467 lockdep_is_held(&mvm->mutex));
1468 if (unlikely(tx_blocked_vif)) {
1469 struct iwl_mvm_vif *mvmvif =
1470 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1471
1472 /*
1473 * The channel switch is started and we have blocked the
1474 * stations. If this is the first beacon (the timeout wasn't
1475 * set), set the unblock timeout, otherwise countdown
1476 */
1477 if (!mvm->csa_tx_block_bcn_timeout)
1478 mvm->csa_tx_block_bcn_timeout =
1479 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1480 else
1481 mvm->csa_tx_block_bcn_timeout--;
1482
1483 /* Check if the timeout is expired, and unblock tx */
1484 if (mvm->csa_tx_block_bcn_timeout == 0) {
1485 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1486 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1487 }
1488 }
571765c8 1489}
d64048ed
HG
1490
1491static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1492 struct ieee80211_vif *vif)
1493{
12d423e8 1494 struct iwl_missed_beacons_notif *missed_beacons = _data;
d64048ed 1495 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9d761fd8
EG
1496 struct iwl_mvm *mvm = mvmvif->mvm;
1497 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1498 struct iwl_fw_dbg_trigger_tlv *trigger;
1499 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1500 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
d64048ed 1501
12d423e8
IP
1502 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1503 return;
1504
9d761fd8
EG
1505 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1506 rx_missed_bcon_since_rx =
1507 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
12d423e8
IP
1508 /*
1509 * TODO: the threshold should be adjusted based on latency conditions,
1510 * and/or in case of a CS flow on one of the other AP vifs.
1511 */
1512 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1513 IWL_MVM_MISSED_BEACONS_THRESHOLD)
d64048ed 1514 ieee80211_beacon_loss(vif);
9d761fd8
EG
1515
1516 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1517 FW_DBG_TRIGGER_MISSED_BEACONS))
1518 return;
1519
1520 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1521 FW_DBG_TRIGGER_MISSED_BEACONS);
1522 bcon_trig = (void *)trigger->data;
1523 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1524 stop_trig_missed_bcon_since_rx =
1525 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1526
1527 /* TODO: implement start trigger */
1528
1529 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1530 return;
1531
1532 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1533 rx_missed_bcon >= stop_trig_missed_bcon)
5d4f929e 1534 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
d64048ed
HG
1535}
1536
0416841d
JB
1537void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1538 struct iwl_rx_cmd_buffer *rxb)
d64048ed
HG
1539{
1540 struct iwl_rx_packet *pkt = rxb_addr(rxb);
12d423e8
IP
1541 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1542
1543 IWL_DEBUG_INFO(mvm,
1544 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1545 le32_to_cpu(mb->mac_id),
1546 le32_to_cpu(mb->consec_missed_beacons),
1547 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1548 le32_to_cpu(mb->num_recvd_beacons),
1549 le32_to_cpu(mb->num_expected_beacons));
d64048ed
HG
1550
1551 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1552 IEEE80211_IFACE_ITER_NORMAL,
1553 iwl_mvm_beacon_loss_iterator,
12d423e8 1554 mb);
d64048ed 1555}
0db056d3
SS
1556
1557void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1558 struct iwl_rx_cmd_buffer *rxb)
1559{
1560 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1561 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1562 struct ieee80211_rx_status rx_status;
1563 struct sk_buff *skb;
1564 u32 size = le32_to_cpu(sb->byte_count);
1565
1566 if (size == 0)
1567 return;
1568
1569 skb = alloc_skb(size, GFP_ATOMIC);
1570 if (!skb) {
1571 IWL_ERR(mvm, "alloc_skb failed\n");
1572 return;
1573 }
1574
1575 /* update rx_status according to the notification's metadata */
1576 memset(&rx_status, 0, sizeof(rx_status));
1577 rx_status.mactime = le64_to_cpu(sb->tsf);
77fe7395
SS
1578 /* TSF as indicated by the firmware is at INA time */
1579 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
0db056d3
SS
1580 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1581 rx_status.band =
34118c25 1582 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
57fbcce3 1583 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
0db056d3
SS
1584 rx_status.freq =
1585 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1586 rx_status.band);
1587
1588 /* copy the data */
1589 memcpy(skb_put(skb, size), sb->data, size);
1590 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1591
1592 /* pass it as regular rx to mac80211 */
d63b548f 1593 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
0db056d3 1594}
d3a108a4
AO
1595
1596void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1597 struct iwl_rx_cmd_buffer *rxb)
1598{
1599 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1600 struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1601 struct ieee80211_vif *csa_vif;
1602 struct iwl_mvm_vif *mvmvif;
1603 int len = iwl_rx_packet_payload_len(pkt);
1604 u32 id_n_color;
1605
1606 if (WARN_ON_ONCE(len < sizeof(*notif)))
1607 return;
1608
1609 rcu_read_lock();
1610
1611 csa_vif = rcu_dereference(mvm->csa_vif);
1612 if (WARN_ON(!csa_vif || !csa_vif->csa_active))
1613 goto out_unlock;
1614
1615 id_n_color = le32_to_cpu(notif->id_and_color);
1616
1617 mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1618 if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
1619 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1620 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
1621 goto out_unlock;
1622
1623 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1624
1625 queue_delayed_work(system_wq, &mvm->cs_tx_unblock_dwork,
1626 msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1627 csa_vif->bss_conf.beacon_int));
1628
1629 ieee80211_csa_finish(csa_vif);
1630
1631 rcu_read_unlock();
1632
1633 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1634
1635 return;
1636
1637out_unlock:
1638 rcu_read_unlock();
1639}