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