954ea9ac8e5bf78cd8022c45cc0bdc85dfab2d7c
[platform/kernel/linux-rpi.git] / drivers / net / wireless / intel / iwlwifi / mvm / mac-ctxt.c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
4  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5  * Copyright (C) 2015-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/crc32.h>
9 #include <net/mac80211.h>
10 #include "iwl-io.h"
11 #include "iwl-prph.h"
12 #include "fw-api.h"
13 #include "mvm.h"
14 #include "time-event.h"
15
16 const u8 iwl_mvm_ac_to_tx_fifo[] = {
17         IWL_MVM_TX_FIFO_VO,
18         IWL_MVM_TX_FIFO_VI,
19         IWL_MVM_TX_FIFO_BE,
20         IWL_MVM_TX_FIFO_BK,
21 };
22
23 const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
24         IWL_GEN2_EDCA_TX_FIFO_VO,
25         IWL_GEN2_EDCA_TX_FIFO_VI,
26         IWL_GEN2_EDCA_TX_FIFO_BE,
27         IWL_GEN2_EDCA_TX_FIFO_BK,
28         IWL_GEN2_TRIG_TX_FIFO_VO,
29         IWL_GEN2_TRIG_TX_FIFO_VI,
30         IWL_GEN2_TRIG_TX_FIFO_BE,
31         IWL_GEN2_TRIG_TX_FIFO_BK,
32 };
33
34 struct iwl_mvm_mac_iface_iterator_data {
35         struct iwl_mvm *mvm;
36         struct ieee80211_vif *vif;
37         unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
38         unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
39         enum iwl_tsf_id preferred_tsf;
40         bool found_vif;
41 };
42
43 static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
44                                     struct ieee80211_vif *vif)
45 {
46         struct iwl_mvm_mac_iface_iterator_data *data = _data;
47         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
48         u16 min_bi;
49
50         /* Skip the interface for which we are trying to assign a tsf_id  */
51         if (vif == data->vif)
52                 return;
53
54         /*
55          * The TSF is a hardware/firmware resource, there are 4 and
56          * the driver should assign and free them as needed. However,
57          * there are cases where 2 MACs should share the same TSF ID
58          * for the purpose of clock sync, an optimization to avoid
59          * clock drift causing overlapping TBTTs/DTIMs for a GO and
60          * client in the system.
61          *
62          * The firmware will decide according to the MAC type which
63          * will be the leader and follower. Clients that need to sync
64          * with a remote station will be the leader, and an AP or GO
65          * will be the follower.
66          *
67          * Depending on the new interface type it can be following
68          * or become the leader of an existing interface.
69          */
70         switch (data->vif->type) {
71         case NL80211_IFTYPE_STATION:
72                 /*
73                  * The new interface is a client, so if the one we're iterating
74                  * is an AP, and the beacon interval of the AP is a multiple or
75                  * divisor of the beacon interval of the client, the same TSF
76                  * should be used to avoid drift between the new client and
77                  * existing AP. The existing AP will get drift updates from the
78                  * new client context in this case.
79                  */
80                 if (vif->type != NL80211_IFTYPE_AP ||
81                     data->preferred_tsf != NUM_TSF_IDS ||
82                     !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
83                         break;
84
85                 min_bi = min(data->vif->bss_conf.beacon_int,
86                              vif->bss_conf.beacon_int);
87
88                 if (!min_bi)
89                         break;
90
91                 if ((data->vif->bss_conf.beacon_int -
92                      vif->bss_conf.beacon_int) % min_bi == 0) {
93                         data->preferred_tsf = mvmvif->tsf_id;
94                         return;
95                 }
96                 break;
97
98         case NL80211_IFTYPE_AP:
99                 /*
100                  * The new interface is AP/GO, so if its beacon interval is a
101                  * multiple or a divisor of the beacon interval of an existing
102                  * interface, it should get drift updates from an existing
103                  * client or use the same TSF as an existing GO. There's no
104                  * drift between TSFs internally but if they used different
105                  * TSFs then a new client MAC could update one of them and
106                  * cause drift that way.
107                  */
108                 if ((vif->type != NL80211_IFTYPE_AP &&
109                      vif->type != NL80211_IFTYPE_STATION) ||
110                     data->preferred_tsf != NUM_TSF_IDS ||
111                     !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
112                         break;
113
114                 min_bi = min(data->vif->bss_conf.beacon_int,
115                              vif->bss_conf.beacon_int);
116
117                 if (!min_bi)
118                         break;
119
120                 if ((data->vif->bss_conf.beacon_int -
121                      vif->bss_conf.beacon_int) % min_bi == 0) {
122                         data->preferred_tsf = mvmvif->tsf_id;
123                         return;
124                 }
125                 break;
126         default:
127                 /*
128                  * For all other interface types there's no need to
129                  * take drift into account. Either they're exclusive
130                  * like IBSS and monitor, or we don't care much about
131                  * their TSF (like P2P Device), but we won't be able
132                  * to share the TSF resource.
133                  */
134                 break;
135         }
136
137         /*
138          * Unless we exited above, we can't share the TSF resource
139          * that the virtual interface we're iterating over is using
140          * with the new one, so clear the available bit and if this
141          * was the preferred one, reset that as well.
142          */
143         __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
144
145         if (data->preferred_tsf == mvmvif->tsf_id)
146                 data->preferred_tsf = NUM_TSF_IDS;
147 }
148
149 static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
150                                        struct ieee80211_vif *vif)
151 {
152         struct iwl_mvm_mac_iface_iterator_data *data = _data;
153         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
154
155         /* Iterator may already find the interface being added -- skip it */
156         if (vif == data->vif) {
157                 data->found_vif = true;
158                 return;
159         }
160
161         /* Mark MAC IDs as used by clearing the available bit, and
162          * (below) mark TSFs as used if their existing use is not
163          * compatible with the new interface type.
164          * No locking or atomic bit operations are needed since the
165          * data is on the stack of the caller function.
166          */
167         __clear_bit(mvmvif->id, data->available_mac_ids);
168
169         /* find a suitable tsf_id */
170         iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
171 }
172
173 void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
174                                     struct ieee80211_vif *vif)
175 {
176         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
177         struct iwl_mvm_mac_iface_iterator_data data = {
178                 .mvm = mvm,
179                 .vif = vif,
180                 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
181                 /* no preference yet */
182                 .preferred_tsf = NUM_TSF_IDS,
183         };
184
185         ieee80211_iterate_active_interfaces_atomic(
186                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
187                 iwl_mvm_mac_tsf_id_iter, &data);
188
189         if (data.preferred_tsf != NUM_TSF_IDS)
190                 mvmvif->tsf_id = data.preferred_tsf;
191         else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
192                 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
193                                                 NUM_TSF_IDS);
194 }
195
196 int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
197 {
198         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
199         struct iwl_mvm_mac_iface_iterator_data data = {
200                 .mvm = mvm,
201                 .vif = vif,
202                 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
203                 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
204                 /* no preference yet */
205                 .preferred_tsf = NUM_TSF_IDS,
206                 .found_vif = false,
207         };
208         int ret, i;
209
210         lockdep_assert_held(&mvm->mutex);
211
212         /*
213          * Allocate a MAC ID and a TSF for this MAC, along with the queues
214          * and other resources.
215          */
216
217         /*
218          * Before the iterator, we start with all MAC IDs and TSFs available.
219          *
220          * During iteration, all MAC IDs are cleared that are in use by other
221          * virtual interfaces, and all TSF IDs are cleared that can't be used
222          * by this new virtual interface because they're used by an interface
223          * that can't share it with the new one.
224          * At the same time, we check if there's a preferred TSF in the case
225          * that we should share it with another interface.
226          */
227
228         /* MAC ID 0 should be used only for the managed/IBSS vif with non-MLO
229          * FW API
230          */
231         if (!mvm->mld_api_is_used) {
232                 switch (vif->type) {
233                 case NL80211_IFTYPE_ADHOC:
234                         break;
235                 case NL80211_IFTYPE_STATION:
236                         if (!vif->p2p)
237                                 break;
238                         fallthrough;
239                 default:
240                         __clear_bit(0, data.available_mac_ids);
241                 }
242         }
243
244         ieee80211_iterate_active_interfaces_atomic(
245                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
246                 iwl_mvm_mac_iface_iterator, &data);
247
248         /*
249          * In the case we're getting here during resume, it's similar to
250          * firmware restart, and with RESUME_ALL the iterator will find
251          * the vif being added already.
252          * We don't want to reassign any IDs in either case since doing
253          * so would probably assign different IDs (as interfaces aren't
254          * necessarily added in the same order), but the old IDs were
255          * preserved anyway, so skip ID assignment for both resume and
256          * recovery.
257          */
258         if (data.found_vif)
259                 return 0;
260
261         /* Therefore, in recovery, we can't get here */
262         if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
263                 return -EBUSY;
264
265         mvmvif->id = find_first_bit(data.available_mac_ids,
266                                     NUM_MAC_INDEX_DRIVER);
267         if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
268                 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
269                 ret = -EIO;
270                 goto exit_fail;
271         }
272
273         if (data.preferred_tsf != NUM_TSF_IDS)
274                 mvmvif->tsf_id = data.preferred_tsf;
275         else
276                 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
277                                                 NUM_TSF_IDS);
278         if (mvmvif->tsf_id == NUM_TSF_IDS) {
279                 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
280                 ret = -EIO;
281                 goto exit_fail;
282         }
283
284         mvmvif->color = 0;
285
286         INIT_LIST_HEAD(&mvmvif->time_event_data.list);
287         mvmvif->time_event_data.id = TE_MAX;
288
289         /* No need to allocate data queues to P2P Device MAC and NAN.*/
290         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
291                 return 0;
292
293         /* Allocate the CAB queue for softAP and GO interfaces */
294         if (vif->type == NL80211_IFTYPE_AP ||
295             vif->type == NL80211_IFTYPE_ADHOC) {
296                 /*
297                  * For TVQM this will be overwritten later with the FW assigned
298                  * queue value (when queue is enabled).
299                  */
300                 mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
301         }
302
303         mvmvif->deflink.bcast_sta.sta_id = IWL_MVM_INVALID_STA;
304         mvmvif->deflink.mcast_sta.sta_id = IWL_MVM_INVALID_STA;
305         mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
306
307         for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
308                 mvmvif->deflink.smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
309
310         return 0;
311
312 exit_fail:
313         memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
314         return ret;
315 }
316
317 static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
318                               struct ieee80211_vif *vif,
319                               enum nl80211_band band,
320                               u8 *cck_rates, u8 *ofdm_rates)
321 {
322         struct ieee80211_supported_band *sband;
323         unsigned long basic = vif->bss_conf.basic_rates;
324         int lowest_present_ofdm = 100;
325         int lowest_present_cck = 100;
326         u8 cck = 0;
327         u8 ofdm = 0;
328         int i;
329
330         sband = mvm->hw->wiphy->bands[band];
331
332         for_each_set_bit(i, &basic, BITS_PER_LONG) {
333                 int hw = sband->bitrates[i].hw_value;
334                 if (hw >= IWL_FIRST_OFDM_RATE) {
335                         ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
336                         if (lowest_present_ofdm > hw)
337                                 lowest_present_ofdm = hw;
338                 } else {
339                         BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
340
341                         cck |= BIT(hw);
342                         if (lowest_present_cck > hw)
343                                 lowest_present_cck = hw;
344                 }
345         }
346
347         /*
348          * Now we've got the basic rates as bitmaps in the ofdm and cck
349          * variables. This isn't sufficient though, as there might not
350          * be all the right rates in the bitmap. E.g. if the only basic
351          * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
352          * and 6 Mbps because the 802.11-2007 standard says in 9.6:
353          *
354          *    [...] a STA responding to a received frame shall transmit
355          *    its Control Response frame [...] at the highest rate in the
356          *    BSSBasicRateSet parameter that is less than or equal to the
357          *    rate of the immediately previous frame in the frame exchange
358          *    sequence ([...]) and that is of the same modulation class
359          *    ([...]) as the received frame. If no rate contained in the
360          *    BSSBasicRateSet parameter meets these conditions, then the
361          *    control frame sent in response to a received frame shall be
362          *    transmitted at the highest mandatory rate of the PHY that is
363          *    less than or equal to the rate of the received frame, and
364          *    that is of the same modulation class as the received frame.
365          *
366          * As a consequence, we need to add all mandatory rates that are
367          * lower than all of the basic rates to these bitmaps.
368          */
369
370         if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
371                 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
372         if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
373                 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
374         /* 6M already there or needed so always add */
375         ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
376
377         /*
378          * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
379          * Note, however:
380          *  - if no CCK rates are basic, it must be ERP since there must
381          *    be some basic rates at all, so they're OFDM => ERP PHY
382          *    (or we're in 5 GHz, and the cck bitmap will never be used)
383          *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
384          *  - if 5.5M is basic, 1M and 2M are mandatory
385          *  - if 2M is basic, 1M is mandatory
386          *  - if 1M is basic, that's the only valid ACK rate.
387          * As a consequence, it's not as complicated as it sounds, just add
388          * any lower rates to the ACK rate bitmap.
389          */
390         if (IWL_RATE_11M_INDEX < lowest_present_cck)
391                 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
392         if (IWL_RATE_5M_INDEX < lowest_present_cck)
393                 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
394         if (IWL_RATE_2M_INDEX < lowest_present_cck)
395                 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
396         /* 1M already there or needed so always add */
397         cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
398
399         *cck_rates = cck;
400         *ofdm_rates = ofdm;
401 }
402
403 void iwl_mvm_set_fw_basic_rates(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
404                                 struct ieee80211_bss_conf *link_conf,
405                                 __le32 *cck_rates, __le32 *ofdm_rates)
406 {
407         struct ieee80211_chanctx_conf *chanctx;
408         u8 cck_ack_rates = 0, ofdm_ack_rates = 0;
409
410         rcu_read_lock();
411         chanctx = rcu_dereference(link_conf->chanctx_conf);
412         iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
413                                             : NL80211_BAND_2GHZ,
414                           &cck_ack_rates, &ofdm_ack_rates);
415
416         rcu_read_unlock();
417
418         *cck_rates = cpu_to_le32((u32)cck_ack_rates);
419         *ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
420 }
421
422 void iwl_mvm_set_fw_protection_flags(struct iwl_mvm *mvm,
423                                      struct ieee80211_vif *vif,
424                                      struct ieee80211_bss_conf *link_conf,
425                                      __le32 *protection_flags, u32 ht_flag,
426                                      u32 tgg_flag)
427 {
428         /* for both sta and ap, ht_operation_mode hold the protection_mode */
429         u8 protection_mode = link_conf->ht_operation_mode &
430                                  IEEE80211_HT_OP_MODE_PROTECTION;
431         bool ht_enabled = !!(link_conf->ht_operation_mode &
432                              IEEE80211_HT_OP_MODE_PROTECTION);
433
434         if (link_conf->use_cts_prot)
435                 *protection_flags |= cpu_to_le32(tgg_flag);
436
437         IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
438                        link_conf->use_cts_prot,
439                        link_conf->ht_operation_mode);
440
441         if (!ht_enabled)
442                 return;
443
444         IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
445         /*
446          * See section 9.23.3.1 of IEEE 80211-2012.
447          * Nongreenfield HT STAs Present is not supported.
448          */
449         switch (protection_mode) {
450         case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
451                 break;
452         case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
453         case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
454                 *protection_flags |= cpu_to_le32(ht_flag);
455                 break;
456         case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
457                 /* Protect when channel wider than 20MHz */
458                 if (link_conf->chandef.width > NL80211_CHAN_WIDTH_20)
459                         *protection_flags |= cpu_to_le32(ht_flag);
460                 break;
461         default:
462                 IWL_ERR(mvm, "Illegal protection mode %d\n",
463                         protection_mode);
464                 break;
465         }
466 }
467
468 void iwl_mvm_set_fw_qos_params(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
469                                struct ieee80211_bss_conf *link_conf,
470                                struct iwl_ac_qos *ac, __le32 *qos_flags)
471 {
472         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
473         int i;
474
475         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
476                 u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
477                 u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
478
479                 ac[ucode_ac].cw_min =
480                         cpu_to_le16(mvmvif->deflink.queue_params[i].cw_min);
481                 ac[ucode_ac].cw_max =
482                         cpu_to_le16(mvmvif->deflink.queue_params[i].cw_max);
483                 ac[ucode_ac].edca_txop =
484                         cpu_to_le16(mvmvif->deflink.queue_params[i].txop * 32);
485                 ac[ucode_ac].aifsn = mvmvif->deflink.queue_params[i].aifs;
486                 ac[ucode_ac].fifos_mask = BIT(txf);
487         }
488
489         if (link_conf->qos)
490                 *qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
491
492         if (link_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
493                 *qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
494 }
495
496 int iwl_mvm_get_mac_type(struct ieee80211_vif *vif)
497 {
498         u32 mac_type = FW_MAC_TYPE_BSS_STA;
499
500         switch (vif->type) {
501         case NL80211_IFTYPE_STATION:
502                 if (vif->p2p)
503                         mac_type = FW_MAC_TYPE_P2P_STA;
504                 else
505                         mac_type = FW_MAC_TYPE_BSS_STA;
506                 break;
507         case NL80211_IFTYPE_AP:
508                 mac_type = FW_MAC_TYPE_GO;
509                 break;
510         case NL80211_IFTYPE_MONITOR:
511                 mac_type = FW_MAC_TYPE_LISTENER;
512                 break;
513         case NL80211_IFTYPE_P2P_DEVICE:
514                 mac_type = FW_MAC_TYPE_P2P_DEVICE;
515                 break;
516         case NL80211_IFTYPE_ADHOC:
517                 mac_type = FW_MAC_TYPE_IBSS;
518                 break;
519         default:
520                 WARN_ON_ONCE(1);
521         }
522         return mac_type;
523 }
524
525 static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
526                                         struct ieee80211_vif *vif,
527                                         struct iwl_mac_ctx_cmd *cmd,
528                                         const u8 *bssid_override,
529                                         u32 action)
530 {
531         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
532         const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
533         u32 ht_flag;
534
535         cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
536                                                             mvmvif->color));
537         cmd->action = cpu_to_le32(action);
538         cmd->mac_type = cpu_to_le32(iwl_mvm_get_mac_type(vif));
539
540         cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
541
542         memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
543
544         if (bssid)
545                 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
546         else
547                 eth_broadcast_addr(cmd->bssid_addr);
548
549         iwl_mvm_set_fw_basic_rates(mvm, vif, &vif->bss_conf, &cmd->cck_rates,
550                                    &cmd->ofdm_rates);
551
552         cmd->cck_short_preamble =
553                 cpu_to_le32(vif->bss_conf.use_short_preamble ?
554                             MAC_FLG_SHORT_PREAMBLE : 0);
555         cmd->short_slot =
556                 cpu_to_le32(vif->bss_conf.use_short_slot ?
557                             MAC_FLG_SHORT_SLOT : 0);
558
559         cmd->filter_flags = 0;
560
561         iwl_mvm_set_fw_qos_params(mvm, vif, &vif->bss_conf, &cmd->ac[0],
562                                   &cmd->qos_flags);
563
564         /* The fw does not distinguish between ht and fat */
565         ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
566         iwl_mvm_set_fw_protection_flags(mvm, vif, &vif->bss_conf,
567                                         &cmd->protection_flags,
568                                         ht_flag, MAC_PROT_FLG_TGG_PROTECT);
569 }
570
571 static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
572                                      struct iwl_mac_ctx_cmd *cmd)
573 {
574         int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
575                                        sizeof(*cmd), cmd);
576         if (ret)
577                 IWL_ERR(mvm, "Failed to send MAC_CONTEXT_CMD (action:%d): %d\n",
578                         le32_to_cpu(cmd->action), ret);
579         return ret;
580 }
581
582 void iwl_mvm_set_fw_dtim_tbtt(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
583                               struct ieee80211_bss_conf *link_conf,
584                               __le64 *dtim_tsf, __le32 *dtim_time,
585                               __le32 *assoc_beacon_arrive_time)
586 {
587         u32 dtim_offs;
588
589         /*
590          * The DTIM count counts down, so when it is N that means N
591          * more beacon intervals happen until the DTIM TBTT. Therefore
592          * add this to the current time. If that ends up being in the
593          * future, the firmware will handle it.
594          *
595          * Also note that the system_timestamp (which we get here as
596          * "sync_device_ts") and TSF timestamp aren't at exactly the
597          * same offset in the frame -- the TSF is at the first symbol
598          * of the TSF, the system timestamp is at signal acquisition
599          * time. This means there's an offset between them of at most
600          * a few hundred microseconds (24 * 8 bits + PLCP time gives
601          * 384us in the longest case), this is currently not relevant
602          * as the firmware wakes up around 2ms before the TBTT.
603          */
604         dtim_offs = link_conf->sync_dtim_count *
605                         link_conf->beacon_int;
606         /* convert TU to usecs */
607         dtim_offs *= 1024;
608
609         *dtim_tsf =
610                 cpu_to_le64(link_conf->sync_tsf + dtim_offs);
611         *dtim_time =
612                 cpu_to_le32(link_conf->sync_device_ts + dtim_offs);
613         *assoc_beacon_arrive_time =
614                 cpu_to_le32(link_conf->sync_device_ts);
615
616         IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
617                        le64_to_cpu(*dtim_tsf),
618                        le32_to_cpu(*dtim_time),
619                        dtim_offs);
620 }
621
622 __le32 iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(struct iwl_mvm *mvm,
623                                                     struct ieee80211_vif *vif)
624 {
625         struct ieee80211_p2p_noa_attr *noa =
626                 &vif->bss_conf.p2p_noa_attr;
627
628         return cpu_to_le32(noa->oppps_ctwindow &
629                         IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
630 }
631
632 u32 iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(struct iwl_mvm *mvm,
633                                             struct ieee80211_vif *vif)
634 {
635         u32 twt_policy = 0;
636
637         if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT)
638                 twt_policy |= TWT_SUPPORTED;
639         if (vif->bss_conf.twt_protected)
640                 twt_policy |= PROTECTED_TWT_SUPPORTED;
641         if (vif->bss_conf.twt_broadcast)
642                 twt_policy |= BROADCAST_TWT_SUPPORTED;
643
644         return twt_policy;
645 }
646
647 static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
648                                     struct ieee80211_vif *vif,
649                                     u32 action, bool force_assoc_off,
650                                     const u8 *bssid_override)
651 {
652         struct iwl_mac_ctx_cmd cmd = {};
653         struct iwl_mac_data_sta *ctxt_sta;
654
655         WARN_ON(vif->type != NL80211_IFTYPE_STATION);
656
657         /* Fill the common data for all mac context types */
658         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
659
660         /*
661          * We always want to hear MCAST frames, if we're not authorized yet,
662          * we'll drop them.
663          */
664         cmd.filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
665
666         if (vif->p2p) {
667                 cmd.p2p_sta.ctwin =
668                         iwl_mvm_mac_ctxt_cmd_p2p_sta_get_oppps_ctwin(mvm, vif);
669
670                 ctxt_sta = &cmd.p2p_sta.sta;
671         } else {
672                 ctxt_sta = &cmd.sta;
673         }
674
675         /* We need the dtim_period to set the MAC as associated */
676         if (vif->cfg.assoc && vif->bss_conf.dtim_period &&
677             !force_assoc_off) {
678                 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
679
680                 iwl_mvm_set_fw_dtim_tbtt(mvm, vif, &vif->bss_conf,
681                                          &ctxt_sta->dtim_tsf,
682                                          &ctxt_sta->dtim_time,
683                                          &ctxt_sta->assoc_beacon_arrive_time);
684
685                 ctxt_sta->is_assoc = cpu_to_le32(1);
686
687                 if (!mvmvif->authorized &&
688                     fw_has_capa(&mvm->fw->ucode_capa,
689                                 IWL_UCODE_TLV_CAPA_COEX_HIGH_PRIO))
690                         ctxt_sta->data_policy |=
691                                 cpu_to_le32(COEX_HIGH_PRIORITY_ENABLE);
692         } else {
693                 ctxt_sta->is_assoc = cpu_to_le32(0);
694
695                 /* Allow beacons to pass through as long as we are not
696                  * associated, or we do not have dtim period information.
697                  */
698                 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
699         }
700
701         ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
702         ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
703                                               vif->bss_conf.dtim_period);
704
705         ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
706         ctxt_sta->assoc_id = cpu_to_le32(vif->cfg.aid);
707
708         if (vif->probe_req_reg && vif->cfg.assoc && vif->p2p)
709                 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
710
711         if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
712                 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
713                 ctxt_sta->data_policy |=
714                         cpu_to_le32(iwl_mvm_mac_ctxt_cmd_sta_get_twt_policy(mvm, vif));
715         }
716
717
718         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
719 }
720
721 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
722                                          struct ieee80211_vif *vif,
723                                          u32 action)
724 {
725         struct iwl_mac_ctx_cmd cmd = {};
726         u32 tfd_queue_msk = 0;
727         int ret;
728
729         WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
730
731         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
732
733         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
734                                        MAC_FILTER_IN_CONTROL_AND_MGMT |
735                                        MAC_FILTER_IN_BEACON |
736                                        MAC_FILTER_IN_PROBE_REQUEST |
737                                        MAC_FILTER_IN_CRC32 |
738                                        MAC_FILTER_ACCEPT_GRP);
739         ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
740
741         /*
742          * the queue mask is only relevant for old TX API, and
743          * mvm->snif_queue isn't set here (it's still set to
744          * IWL_MVM_INVALID_QUEUE so the BIT() of it is UB)
745          */
746         if (!iwl_mvm_has_new_tx_api(mvm))
747                 tfd_queue_msk = BIT(mvm->snif_queue);
748
749         /* Allocate sniffer station */
750         ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
751                                        vif->type, IWL_STA_GENERAL_PURPOSE);
752         if (ret)
753                 return ret;
754
755         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
756 }
757
758 static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
759                                      struct ieee80211_vif *vif,
760                                      u32 action)
761 {
762         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
763         struct iwl_mac_ctx_cmd cmd = {};
764
765         WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
766
767         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
768
769         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
770                                        MAC_FILTER_IN_PROBE_REQUEST |
771                                        MAC_FILTER_ACCEPT_GRP);
772
773         /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
774         cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
775
776         /* TODO: Assumes that the beacon id == mac context id */
777         cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
778
779         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
780 }
781
782 struct iwl_mvm_go_iterator_data {
783         bool go_active;
784 };
785
786 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
787 {
788         struct iwl_mvm_go_iterator_data *data = _data;
789         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
790
791         if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
792             mvmvif->ap_ibss_active)
793                 data->go_active = true;
794 }
795
796 __le32 iwl_mac_ctxt_p2p_dev_has_extended_disc(struct iwl_mvm *mvm,
797                                               struct ieee80211_vif *vif)
798 {
799         struct iwl_mvm_go_iterator_data data = {};
800
801         /*
802          * This flag should be set to true when the P2P Device is
803          * discoverable and there is at least another active P2P GO. Settings
804          * this flag will allow the P2P Device to be discoverable on other
805          * channels in addition to its listen channel.
806          * Note that this flag should not be set in other cases as it opens the
807          * Rx filters on all MAC and increases the number of interrupts.
808          */
809         ieee80211_iterate_active_interfaces_atomic(
810                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
811                 iwl_mvm_go_iterator, &data);
812
813         return cpu_to_le32(data.go_active ? 1 : 0);
814 }
815
816 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
817                                            struct ieee80211_vif *vif,
818                                            u32 action)
819 {
820         struct iwl_mac_ctx_cmd cmd = {};
821
822         WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
823
824         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
825
826         cmd.p2p_dev.is_disc_extended =
827                 iwl_mac_ctxt_p2p_dev_has_extended_disc(mvm, vif);
828
829         /* Override the filter flags to accept only probe requests */
830         cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
831
832         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
833 }
834
835 void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
836                               __le32 *tim_index, __le32 *tim_size,
837                               u8 *beacon, u32 frame_size)
838 {
839         u32 tim_idx;
840         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
841
842         /* The index is relative to frame start but we start looking at the
843          * variable-length part of the beacon. */
844         tim_idx = mgmt->u.beacon.variable - beacon;
845
846         /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
847         while ((tim_idx < (frame_size - 2)) &&
848                         (beacon[tim_idx] != WLAN_EID_TIM))
849                 tim_idx += beacon[tim_idx+1] + 2;
850
851         /* If TIM field was found, set variables */
852         if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
853                 *tim_index = cpu_to_le32(tim_idx);
854                 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
855         } else {
856                 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
857         }
858 }
859
860 static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
861 {
862         struct ieee80211_mgmt *mgmt = (void *)beacon;
863         const u8 *ie;
864
865         if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
866                 return 0;
867
868         frame_size -= mgmt->u.beacon.variable - beacon;
869
870         ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
871         if (!ie)
872                 return 0;
873
874         return ie - beacon;
875 }
876
877 u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct iwl_mvm *mvm,
878                                     struct ieee80211_tx_info *info,
879                                     struct ieee80211_vif *vif)
880 {
881         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
882         struct ieee80211_supported_band *sband;
883         unsigned long basic = vif->bss_conf.basic_rates;
884         u16 lowest_cck = IWL_RATE_COUNT, lowest_ofdm = IWL_RATE_COUNT;
885         u32 link_id = u32_get_bits(info->control.flags,
886                                    IEEE80211_TX_CTRL_MLO_LINK);
887         u8 band = info->band;
888         u8 rate;
889         u32 i;
890
891         if (link_id == IEEE80211_LINK_UNSPECIFIED && vif->valid_links) {
892                 for (i = 0; i < ARRAY_SIZE(mvmvif->link); i++) {
893                         if (!mvmvif->link[i])
894                                 continue;
895                         /* shouldn't do this when >1 link is active */
896                         WARN_ON_ONCE(link_id != IEEE80211_LINK_UNSPECIFIED);
897                         link_id = i;
898                 }
899         }
900
901         if (link_id < IEEE80211_LINK_UNSPECIFIED) {
902                 struct ieee80211_bss_conf *link_conf;
903
904                 rcu_read_lock();
905                 link_conf = rcu_dereference(vif->link_conf[link_id]);
906                 if (link_conf) {
907                         basic = link_conf->basic_rates;
908                         if (link_conf->chandef.chan)
909                                 band = link_conf->chandef.chan->band;
910                 }
911                 rcu_read_unlock();
912         }
913
914         sband = mvm->hw->wiphy->bands[band];
915         for_each_set_bit(i, &basic, BITS_PER_LONG) {
916                 u16 hw = sband->bitrates[i].hw_value;
917
918                 if (hw >= IWL_FIRST_OFDM_RATE) {
919                         if (lowest_ofdm > hw)
920                                 lowest_ofdm = hw;
921                 } else if (lowest_cck > hw) {
922                         lowest_cck = hw;
923                 }
924         }
925
926         if (band == NL80211_BAND_2GHZ && !vif->p2p &&
927             vif->type != NL80211_IFTYPE_P2P_DEVICE &&
928             !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) {
929                 if (lowest_cck != IWL_RATE_COUNT)
930                         rate = lowest_cck;
931                 else if (lowest_ofdm != IWL_RATE_COUNT)
932                         rate = lowest_ofdm;
933                 else
934                         rate = IWL_RATE_1M_INDEX;
935         } else if (lowest_ofdm != IWL_RATE_COUNT) {
936                 rate = lowest_ofdm;
937         } else {
938                 rate = IWL_RATE_6M_INDEX;
939         }
940
941         return rate;
942 }
943
944 u16 iwl_mvm_mac_ctxt_get_beacon_flags(const struct iwl_fw *fw, u8 rate_idx)
945 {
946         u16 flags = iwl_mvm_mac80211_idx_to_hwrate(fw, rate_idx);
947         bool is_new_rate = iwl_fw_lookup_cmd_ver(fw, BEACON_TEMPLATE_CMD, 0) > 10;
948
949         if (rate_idx <= IWL_FIRST_CCK_RATE)
950                 flags |= is_new_rate ? IWL_MAC_BEACON_CCK
951                           : IWL_MAC_BEACON_CCK_V1;
952
953         return flags;
954 }
955
956 u8 iwl_mvm_mac_ctxt_get_beacon_rate(struct iwl_mvm *mvm,
957                                     struct ieee80211_tx_info *info,
958                                     struct ieee80211_vif *vif)
959 {
960         struct ieee80211_supported_band *sband =
961                 mvm->hw->wiphy->bands[info->band];
962         u32 legacy = vif->bss_conf.beacon_tx_rate.control[info->band].legacy;
963
964         /* if beacon rate was configured try using it */
965         if (hweight32(legacy) == 1) {
966                 u32 rate = ffs(legacy) - 1;
967
968                 return sband->bitrates[rate].hw_value;
969         }
970
971         return iwl_mvm_mac_ctxt_get_lowest_rate(mvm, info, vif);
972 }
973
974 static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
975                                     struct ieee80211_vif *vif,
976                                     struct sk_buff *beacon,
977                                     struct iwl_tx_cmd *tx)
978 {
979         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
980         struct ieee80211_tx_info *info;
981         u8 rate;
982         u32 tx_flags;
983
984         info = IEEE80211_SKB_CB(beacon);
985
986         /* Set up TX command fields */
987         tx->len = cpu_to_le16((u16)beacon->len);
988         tx->sta_id = mvmvif->deflink.bcast_sta.sta_id;
989         tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
990         tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
991         tx_flags |=
992                 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
993                                                 TX_CMD_FLG_BT_PRIO_POS;
994         tx->tx_flags = cpu_to_le32(tx_flags);
995
996         if (!fw_has_capa(&mvm->fw->ucode_capa,
997                          IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
998                 iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
999
1000         tx->rate_n_flags =
1001                 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
1002                             RATE_MCS_ANT_POS);
1003
1004         rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1005
1006         tx->rate_n_flags |=
1007                 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(mvm->fw, rate));
1008         if (rate == IWL_FIRST_CCK_RATE)
1009                 tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK_V1);
1010
1011 }
1012
1013 int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
1014                                      struct sk_buff *beacon,
1015                                      void *data, int len)
1016 {
1017         struct iwl_host_cmd cmd = {
1018                 .id = BEACON_TEMPLATE_CMD,
1019                 .flags = CMD_ASYNC,
1020         };
1021
1022         cmd.len[0] = len;
1023         cmd.data[0] = data;
1024         cmd.dataflags[0] = 0;
1025         cmd.len[1] = beacon->len;
1026         cmd.data[1] = beacon->data;
1027         cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1028
1029         return iwl_mvm_send_cmd(mvm, &cmd);
1030 }
1031
1032 static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
1033                                            struct ieee80211_vif *vif,
1034                                            struct sk_buff *beacon)
1035 {
1036         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1037         struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
1038
1039         iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1040
1041         beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1042
1043         if (vif->type == NL80211_IFTYPE_AP)
1044                 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1045                                          &beacon_cmd.tim_size,
1046                                          beacon->data, beacon->len);
1047
1048         return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1049                                                 sizeof(beacon_cmd));
1050 }
1051
1052 static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
1053                                            struct ieee80211_vif *vif,
1054                                            struct sk_buff *beacon)
1055 {
1056         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1057         struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
1058
1059         iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
1060
1061         beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
1062
1063         if (vif->type == NL80211_IFTYPE_AP)
1064                 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1065                                          &beacon_cmd.tim_size,
1066                                          beacon->data, beacon->len);
1067
1068         beacon_cmd.csa_offset =
1069                 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1070                                                    WLAN_EID_CHANNEL_SWITCH,
1071                                                    beacon->len));
1072         beacon_cmd.ecsa_offset =
1073                 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1074                                                    WLAN_EID_EXT_CHANSWITCH_ANN,
1075                                                    beacon->len));
1076
1077         return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1078                                                 sizeof(beacon_cmd));
1079 }
1080
1081 static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
1082                                            struct ieee80211_vif *vif,
1083                                            struct sk_buff *beacon,
1084                                            struct ieee80211_bss_conf *link_conf)
1085 {
1086         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1087         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
1088         struct iwl_mac_beacon_cmd beacon_cmd = {};
1089         u8 rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1090         u16 flags;
1091         struct ieee80211_chanctx_conf *ctx;
1092         int channel;
1093         flags = iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw, rate);
1094
1095         /* Enable FILS on PSC channels only */
1096         rcu_read_lock();
1097         ctx = rcu_dereference(link_conf->chanctx_conf);
1098         channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
1099         WARN_ON(channel == 0);
1100         if (cfg80211_channel_is_psc(ctx->def.chan) &&
1101             !IWL_MVM_DISABLE_AP_FILS) {
1102                 flags |= iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD,
1103                                                0) > 10 ?
1104                         IWL_MAC_BEACON_FILS :
1105                         IWL_MAC_BEACON_FILS_V1;
1106                 beacon_cmd.short_ssid =
1107                         cpu_to_le32(~crc32_le(~0, vif->cfg.ssid,
1108                                               vif->cfg.ssid_len));
1109         }
1110         rcu_read_unlock();
1111
1112         beacon_cmd.flags = cpu_to_le16(flags);
1113         beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1114
1115         if (WARN_ON(!mvmvif->link[link_conf->link_id]))
1116                 return -EINVAL;
1117
1118         if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1119                 beacon_cmd.link_id =
1120                         cpu_to_le32(mvmvif->link[link_conf->link_id]->fw_link_id);
1121         else
1122                 beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1123
1124         if (vif->type == NL80211_IFTYPE_AP)
1125                 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1126                                          &beacon_cmd.tim_size,
1127                                          beacon->data, beacon->len);
1128
1129         beacon_cmd.csa_offset =
1130                 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1131                                                    WLAN_EID_CHANNEL_SWITCH,
1132                                                    beacon->len));
1133         beacon_cmd.ecsa_offset =
1134                 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
1135                                                    WLAN_EID_EXT_CHANSWITCH_ANN,
1136                                                    beacon->len));
1137
1138         return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1139                                                 sizeof(beacon_cmd));
1140 }
1141
1142 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
1143                                         struct ieee80211_vif *vif,
1144                                         struct sk_buff *beacon,
1145                                         struct ieee80211_bss_conf *link_conf)
1146 {
1147         if (WARN_ON(!beacon))
1148                 return -EINVAL;
1149
1150         if (IWL_MVM_NON_TRANSMITTING_AP)
1151                 return 0;
1152
1153         if (!fw_has_capa(&mvm->fw->ucode_capa,
1154                          IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
1155                 return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
1156
1157         if (fw_has_api(&mvm->fw->ucode_capa,
1158                        IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1159                 return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon,
1160                                                        link_conf);
1161
1162         return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
1163 }
1164
1165 /* The beacon template for the AP/GO/IBSS has changed and needs update */
1166 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1167                                     struct ieee80211_vif *vif,
1168                                     struct ieee80211_bss_conf *link_conf)
1169 {
1170         struct sk_buff *beacon;
1171         int ret;
1172
1173         WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1174                 vif->type != NL80211_IFTYPE_ADHOC);
1175
1176         beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL,
1177                                                link_conf->link_id);
1178         if (!beacon)
1179                 return -ENOMEM;
1180
1181 #ifdef CONFIG_IWLWIFI_DEBUGFS
1182         if (mvm->beacon_inject_active) {
1183                 dev_kfree_skb(beacon);
1184                 return -EBUSY;
1185         }
1186 #endif
1187
1188         ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon, link_conf);
1189         dev_kfree_skb(beacon);
1190         return ret;
1191 }
1192
1193 struct iwl_mvm_mac_ap_iterator_data {
1194         struct iwl_mvm *mvm;
1195         struct ieee80211_vif *vif;
1196         u32 beacon_device_ts;
1197         u16 beacon_int;
1198 };
1199
1200 /* Find the beacon_device_ts and beacon_int for a managed interface */
1201 static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1202                                     struct ieee80211_vif *vif)
1203 {
1204         struct iwl_mvm_mac_ap_iterator_data *data = _data;
1205
1206         if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
1207                 return;
1208
1209         /* Station client has higher priority over P2P client*/
1210         if (vif->p2p && data->beacon_device_ts)
1211                 return;
1212
1213         data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1214         data->beacon_int = vif->bss_conf.beacon_int;
1215 }
1216
1217 /*
1218  * Fill the filter flags for mac context of type AP or P2P GO.
1219  */
1220 void iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(struct iwl_mvm *mvm,
1221                                               struct iwl_mvm_vif *mvmvif,
1222                                               __le32 *filter_flags,
1223                                               int accept_probe_req_flag,
1224                                               int accept_beacon_flag)
1225 {
1226         /*
1227          * in AP mode, pass probe requests and beacons from other APs
1228          * (needed for ht protection); when there're no any associated
1229          * station don't ask FW to pass beacons to prevent unnecessary
1230          * wake-ups.
1231          */
1232         *filter_flags |= cpu_to_le32(accept_probe_req_flag);
1233         if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1234                 *filter_flags |= cpu_to_le32(accept_beacon_flag);
1235                 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1236         } else {
1237                 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1238         }
1239 }
1240
1241 /*
1242  * Fill the specific data for mac context of type AP of P2P GO
1243  */
1244 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1245                                          struct ieee80211_vif *vif,
1246                                          struct iwl_mac_ctx_cmd *cmd,
1247                                          struct iwl_mac_data_ap *ctxt_ap,
1248                                          bool add)
1249 {
1250         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1251         struct iwl_mvm_mac_ap_iterator_data data = {
1252                 .mvm = mvm,
1253                 .vif = vif,
1254                 .beacon_device_ts = 0
1255         };
1256
1257         /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
1258         cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1259
1260         iwl_mvm_mac_ctxt_cmd_ap_set_filter_flags(mvm, mvmvif,
1261                                                  &cmd->filter_flags,
1262                                                  MAC_FILTER_IN_PROBE_REQUEST,
1263                                                  MAC_FILTER_IN_BEACON);
1264
1265         ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1266         ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1267                                              vif->bss_conf.dtim_period);
1268
1269         if (!fw_has_api(&mvm->fw->ucode_capa,
1270                         IWL_UCODE_TLV_API_STA_TYPE))
1271                 ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->deflink.cab_queue);
1272
1273         /*
1274          * Only set the beacon time when the MAC is being added, when we
1275          * just modify the MAC then we should keep the time -- the firmware
1276          * can otherwise have a "jumping" TBTT.
1277          */
1278         if (add) {
1279                 /*
1280                  * If there is a station/P2P client interface which is
1281                  * associated, set the AP's TBTT far enough from the station's
1282                  * TBTT. Otherwise, set it to the current system time
1283                  */
1284                 ieee80211_iterate_active_interfaces_atomic(
1285                         mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1286                         iwl_mvm_mac_ap_iterator, &data);
1287
1288                 if (data.beacon_device_ts) {
1289                         u32 rand = get_random_u32_inclusive(36, 63);
1290                         mvmvif->ap_beacon_time = data.beacon_device_ts +
1291                                 ieee80211_tu_to_usec(data.beacon_int * rand /
1292                                                      100);
1293                 } else {
1294                         mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1295                 }
1296         }
1297
1298         ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1299         ctxt_ap->beacon_tsf = 0; /* unused */
1300
1301         /* TODO: Assume that the beacon id == mac context id */
1302         ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1303 }
1304
1305 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1306                                    struct ieee80211_vif *vif,
1307                                    u32 action)
1308 {
1309         struct iwl_mac_ctx_cmd cmd = {};
1310
1311         WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1312
1313         /* Fill the common data for all mac context types */
1314         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1315
1316         /* Fill the data specific for ap mode */
1317         iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1318                                      action == FW_CTXT_ACTION_ADD);
1319
1320         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1321 }
1322
1323 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1324                                    struct ieee80211_vif *vif,
1325                                    u32 action)
1326 {
1327         struct iwl_mac_ctx_cmd cmd = {};
1328         struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1329
1330         WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1331
1332         /* Fill the common data for all mac context types */
1333         iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1334
1335         /* Fill the data specific for GO mode */
1336         iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1337                                      action == FW_CTXT_ACTION_ADD);
1338
1339         cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1340                                         IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1341         cmd.go.opp_ps_enabled =
1342                         cpu_to_le32(!!(noa->oppps_ctwindow &
1343                                         IEEE80211_P2P_OPPPS_ENABLE_BIT));
1344
1345         return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1346 }
1347
1348 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1349                                 u32 action, bool force_assoc_off,
1350                                 const u8 *bssid_override)
1351 {
1352         switch (vif->type) {
1353         case NL80211_IFTYPE_STATION:
1354                 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1355                                                 force_assoc_off,
1356                                                 bssid_override);
1357         case NL80211_IFTYPE_AP:
1358                 if (!vif->p2p)
1359                         return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1360                 else
1361                         return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1362         case NL80211_IFTYPE_MONITOR:
1363                 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1364         case NL80211_IFTYPE_P2P_DEVICE:
1365                 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1366         case NL80211_IFTYPE_ADHOC:
1367                 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1368         default:
1369                 break;
1370         }
1371
1372         return -EOPNOTSUPP;
1373 }
1374
1375 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1376 {
1377         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1378         int ret;
1379
1380         if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1381                       vif->addr, ieee80211_vif_type_p2p(vif)))
1382                 return -EIO;
1383
1384         ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1385                                    true, NULL);
1386         if (ret)
1387                 return ret;
1388
1389         /* will only do anything at resume from D3 time */
1390         iwl_mvm_set_last_nonqos_seq(mvm, vif);
1391
1392         mvmvif->uploaded = true;
1393         return 0;
1394 }
1395
1396 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1397                              bool force_assoc_off, const u8 *bssid_override)
1398 {
1399         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1400
1401         if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1402                       vif->addr, ieee80211_vif_type_p2p(vif)))
1403                 return -EIO;
1404
1405         return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1406                                     force_assoc_off, bssid_override);
1407 }
1408
1409 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1410 {
1411         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1412         struct iwl_mac_ctx_cmd cmd;
1413         int ret;
1414
1415         if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1416                       vif->addr, ieee80211_vif_type_p2p(vif)))
1417                 return -EIO;
1418
1419         memset(&cmd, 0, sizeof(cmd));
1420
1421         cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1422                                                            mvmvif->color));
1423         cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1424
1425         ret = iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1426         if (ret)
1427                 return ret;
1428
1429         mvmvif->uploaded = false;
1430
1431         if (vif->type == NL80211_IFTYPE_MONITOR) {
1432                 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1433                 iwl_mvm_dealloc_snif_sta(mvm);
1434         }
1435
1436         return 0;
1437 }
1438
1439 static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1440                                    struct ieee80211_vif *csa_vif, u32 gp2,
1441                                    bool tx_success)
1442 {
1443         struct iwl_mvm_vif *mvmvif =
1444                         iwl_mvm_vif_from_mac80211(csa_vif);
1445
1446         /* Don't start to countdown from a failed beacon */
1447         if (!tx_success && !mvmvif->csa_countdown)
1448                 return;
1449
1450         mvmvif->csa_countdown = true;
1451
1452         if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
1453                 int c = ieee80211_beacon_update_cntdwn(csa_vif);
1454
1455                 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif,
1456                                                 &csa_vif->bss_conf);
1457                 if (csa_vif->p2p &&
1458                     !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1459                     tx_success) {
1460                         u32 rel_time = (c + 1) *
1461                                        csa_vif->bss_conf.beacon_int -
1462                                        IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1463                         u32 apply_time = gp2 + rel_time * 1024;
1464
1465                         iwl_mvm_schedule_csa_period(mvm, csa_vif,
1466                                          IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1467                                          IWL_MVM_CHANNEL_SWITCH_MARGIN,
1468                                          apply_time);
1469                 }
1470         } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1471                 /* we don't have CSA NoA scheduled yet, switch now */
1472                 ieee80211_csa_finish(csa_vif);
1473                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1474         }
1475 }
1476
1477 void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1478                              struct iwl_rx_cmd_buffer *rxb)
1479 {
1480         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1481         unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1482         struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1483         struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1484         struct ieee80211_vif *csa_vif;
1485         struct ieee80211_vif *tx_blocked_vif;
1486         struct agg_tx_status *agg_status;
1487         u16 status;
1488
1489         lockdep_assert_held(&mvm->mutex);
1490
1491         mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1492
1493         if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
1494                 struct iwl_mvm_tx_resp *beacon_notify_hdr =
1495                         &beacon_v5->beacon_notify_hdr;
1496
1497                 if (unlikely(pkt_len < sizeof(*beacon_v5)))
1498                         return;
1499
1500                 mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
1501                 agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1502                 status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1503                 IWL_DEBUG_RX(mvm,
1504                              "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1505                              status, beacon_notify_hdr->failure_frame,
1506                              le64_to_cpu(beacon->tsf),
1507                              mvm->ap_last_beacon_gp2,
1508                              le32_to_cpu(beacon_notify_hdr->initial_rate));
1509         } else {
1510                 if (unlikely(pkt_len < sizeof(*beacon)))
1511                         return;
1512
1513                 mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1514                 status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
1515                 IWL_DEBUG_RX(mvm,
1516                              "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
1517                              status, le64_to_cpu(beacon->tsf),
1518                              mvm->ap_last_beacon_gp2);
1519         }
1520
1521         csa_vif = rcu_dereference_protected(mvm->csa_vif,
1522                                             lockdep_is_held(&mvm->mutex));
1523         if (unlikely(csa_vif && csa_vif->bss_conf.csa_active))
1524                 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1525                                        (status == TX_STATUS_SUCCESS));
1526
1527         tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1528                                                 lockdep_is_held(&mvm->mutex));
1529         if (unlikely(tx_blocked_vif)) {
1530                 struct iwl_mvm_vif *mvmvif =
1531                         iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1532
1533                 /*
1534                  * The channel switch is started and we have blocked the
1535                  * stations. If this is the first beacon (the timeout wasn't
1536                  * set), set the unblock timeout, otherwise countdown
1537                  */
1538                 if (!mvm->csa_tx_block_bcn_timeout)
1539                         mvm->csa_tx_block_bcn_timeout =
1540                                 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1541                 else
1542                         mvm->csa_tx_block_bcn_timeout--;
1543
1544                 /* Check if the timeout is expired, and unblock tx */
1545                 if (mvm->csa_tx_block_bcn_timeout == 0) {
1546                         iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1547                         RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1548                 }
1549         }
1550 }
1551
1552 void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1553                                      struct iwl_rx_cmd_buffer *rxb)
1554 {
1555         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1556         struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1557         struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1558         struct iwl_fw_dbg_trigger_tlv *trigger;
1559         u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1560         u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1561         struct ieee80211_vif *vif;
1562         /* Id can be mac/link id depending on the notification version */
1563         u32 id = le32_to_cpu(mb->link_id);
1564         union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1565         u32 mac_type;
1566         u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1567                                                MISSED_BEACONS_NOTIFICATION,
1568                                                0);
1569
1570         rcu_read_lock();
1571
1572         /* before version four the ID in the notification refers to mac ID */
1573         if (notif_ver < 4) {
1574                 vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1575         } else {
1576                 struct ieee80211_bss_conf *bss_conf =
1577                         iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, id, true);
1578
1579                 if (!bss_conf)
1580                         goto out;
1581
1582                 vif = bss_conf->vif;
1583         }
1584
1585         IWL_DEBUG_INFO(mvm,
1586                        "missed bcn %s_id=%u, consecutive=%u (%u, %u, %u)\n",
1587                        notif_ver < 4 ? "mac" : "link",
1588                        id,
1589                        le32_to_cpu(mb->consec_missed_beacons),
1590                        le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1591                        le32_to_cpu(mb->num_recvd_beacons),
1592                        le32_to_cpu(mb->num_expected_beacons));
1593
1594         if (!vif)
1595                 goto out;
1596
1597         mac_type = iwl_mvm_get_mac_type(vif);
1598
1599         IWL_DEBUG_INFO(mvm, "missed beacon mac_type=%u,\n", mac_type);
1600
1601         mvm->trans->dbg.dump_file_name_ext_valid = true;
1602         snprintf(mvm->trans->dbg.dump_file_name_ext, IWL_FW_INI_MAX_NAME,
1603                  "MacId_%d_MacType_%d", id, mac_type);
1604
1605         rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1606         rx_missed_bcon_since_rx =
1607                 le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1608         /*
1609          * TODO: the threshold should be adjusted based on latency conditions,
1610          * and/or in case of a CS flow on one of the other AP vifs.
1611          */
1612         if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1613                 iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1614         else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1615                 ieee80211_beacon_loss(vif);
1616
1617         iwl_dbg_tlv_time_point(&mvm->fwrt,
1618                                IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1619
1620         trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1621                                         FW_DBG_TRIGGER_MISSED_BEACONS);
1622         if (!trigger)
1623                 goto out;
1624
1625         bcon_trig = (void *)trigger->data;
1626         stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1627         stop_trig_missed_bcon_since_rx =
1628                 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1629
1630         /* TODO: implement start trigger */
1631
1632         if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1633             rx_missed_bcon >= stop_trig_missed_bcon)
1634                 iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1635
1636 out:
1637         rcu_read_unlock();
1638 }
1639
1640 void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1641                                     struct iwl_rx_cmd_buffer *rxb)
1642 {
1643         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1644         unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1645         struct iwl_stored_beacon_notif_common *sb = (void *)pkt->data;
1646         struct ieee80211_rx_status rx_status;
1647         struct sk_buff *skb;
1648         u8 *data;
1649         u32 size = le32_to_cpu(sb->byte_count);
1650         int ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1651                                         WIDE_ID(PROT_OFFLOAD_GROUP, STORED_BEACON_NTF),
1652                                         0);
1653
1654         if (size == 0)
1655                 return;
1656
1657         /* handle per-version differences */
1658         if (ver <= 2) {
1659                 struct iwl_stored_beacon_notif_v2 *sb_v2 = (void *)pkt->data;
1660
1661                 if (pkt_len < struct_size(sb_v2, data, size))
1662                         return;
1663
1664                 data = sb_v2->data;
1665         } else {
1666                 struct iwl_stored_beacon_notif_v3 *sb_v3 = (void *)pkt->data;
1667
1668                 if (pkt_len < struct_size(sb_v3, data, size))
1669                         return;
1670
1671                 data = sb_v3->data;
1672         }
1673
1674         skb = alloc_skb(size, GFP_ATOMIC);
1675         if (!skb) {
1676                 IWL_ERR(mvm, "alloc_skb failed\n");
1677                 return;
1678         }
1679
1680         /* update rx_status according to the notification's metadata */
1681         memset(&rx_status, 0, sizeof(rx_status));
1682         rx_status.mactime = le64_to_cpu(sb->tsf);
1683         /* TSF as indicated by the firmware  is at INA time */
1684         rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1685         rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1686         rx_status.band =
1687                 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1688                                 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1689         rx_status.freq =
1690                 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1691                                                rx_status.band);
1692
1693         /* copy the data */
1694         skb_put_data(skb, data, size);
1695         memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1696
1697         /* pass it as regular rx to mac80211 */
1698         ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1699 }
1700
1701 void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1702                                    struct iwl_rx_cmd_buffer *rxb)
1703 {
1704         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1705         struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
1706         struct iwl_probe_resp_data *old_data, *new_data;
1707         u32 id = le32_to_cpu(notif->mac_id);
1708         struct ieee80211_vif *vif;
1709         struct iwl_mvm_vif *mvmvif;
1710
1711         IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1712                        notif->noa_active, notif->csa_counter);
1713
1714         vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1715         if (!vif)
1716                 return;
1717
1718         mvmvif = iwl_mvm_vif_from_mac80211(vif);
1719
1720         new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
1721         if (!new_data)
1722                 return;
1723
1724         memcpy(&new_data->notif, notif, sizeof(new_data->notif));
1725
1726         /* noa_attr contains 1 reserved byte, need to substruct it */
1727         new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
1728                             sizeof(new_data->notif.noa_attr) - 1;
1729
1730         /*
1731          * If it's a one time NoA, only one descriptor is needed,
1732          * adjust the length according to len_low.
1733          */
1734         if (new_data->notif.noa_attr.len_low ==
1735             sizeof(struct ieee80211_p2p_noa_desc) + 2)
1736                 new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
1737
1738         old_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1739                                              lockdep_is_held(&mvmvif->mvm->mutex));
1740         rcu_assign_pointer(mvmvif->deflink.probe_resp_data, new_data);
1741
1742         if (old_data)
1743                 kfree_rcu(old_data, rcu_head);
1744
1745         if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
1746             notif->csa_counter >= 1)
1747                 ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
1748 }
1749
1750 void iwl_mvm_channel_switch_start_notif(struct iwl_mvm *mvm,
1751                                         struct iwl_rx_cmd_buffer *rxb)
1752 {
1753         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1754         struct ieee80211_vif *csa_vif, *vif;
1755         struct iwl_mvm_vif *mvmvif, *csa_mvmvif;
1756         u32 id_n_color, csa_id;
1757         /* save mac_id or link_id to use later to cancel csa if needed */
1758         u32 id;
1759         u8 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1760                                                CHANNEL_SWITCH_START_NOTIF, 0);
1761
1762         rcu_read_lock();
1763
1764         if (notif_ver < 3) {
1765                 struct iwl_channel_switch_start_notif_v1 *notif = (void *)pkt->data;
1766                 u32 mac_id;
1767
1768                 id_n_color = le32_to_cpu(notif->id_and_color);
1769                 mac_id = id_n_color & FW_CTXT_ID_MSK;
1770
1771                 vif = iwl_mvm_rcu_dereference_vif_id(mvm, mac_id, true);
1772                 if (!vif)
1773                         goto out_unlock;
1774
1775                 id = mac_id;
1776         } else {
1777                 struct iwl_channel_switch_start_notif *notif = (void *)pkt->data;
1778                 u32 link_id = le32_to_cpu(notif->link_id);
1779                 struct ieee80211_bss_conf *bss_conf =
1780                         iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, link_id, true);
1781
1782                 if (!bss_conf)
1783                         goto out_unlock;
1784
1785                 id = link_id;
1786                 vif = bss_conf->vif;
1787         }
1788
1789         mvmvif = iwl_mvm_vif_from_mac80211(vif);
1790         if (notif_ver >= 3)
1791                 id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
1792
1793         switch (vif->type) {
1794         case NL80211_IFTYPE_AP:
1795                 csa_vif = rcu_dereference(mvm->csa_vif);
1796                 if (WARN_ON(!csa_vif || !csa_vif->bss_conf.csa_active ||
1797                             csa_vif != vif))
1798                         goto out_unlock;
1799
1800                 csa_mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
1801                 csa_id = FW_CMD_ID_AND_COLOR(csa_mvmvif->id, csa_mvmvif->color);
1802                 if (WARN(csa_id != id_n_color,
1803                          "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1804                          csa_id, id_n_color))
1805                         goto out_unlock;
1806
1807                 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1808
1809                 schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1810                                       msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1811                                                        csa_vif->bss_conf.beacon_int));
1812
1813                 ieee80211_csa_finish(csa_vif);
1814
1815                 rcu_read_unlock();
1816
1817                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1818                 return;
1819         case NL80211_IFTYPE_STATION:
1820                 /*
1821                  * if we don't know about an ongoing channel switch,
1822                  * make sure FW cancels it
1823                  */
1824                 if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1825                                             CHANNEL_SWITCH_ERROR_NOTIF,
1826                                             0) && !vif->bss_conf.csa_active) {
1827                         IWL_DEBUG_INFO(mvm, "Channel Switch was canceled\n");
1828                         iwl_mvm_cancel_channel_switch(mvm, vif, id);
1829                         break;
1830                 }
1831
1832                 iwl_mvm_csa_client_absent(mvm, vif);
1833                 cancel_delayed_work(&mvmvif->csa_work);
1834                 ieee80211_chswitch_done(vif, true);
1835                 break;
1836         default:
1837                 /* should never happen */
1838                 WARN_ON_ONCE(1);
1839                 break;
1840         }
1841 out_unlock:
1842         rcu_read_unlock();
1843 }
1844
1845 void iwl_mvm_channel_switch_error_notif(struct iwl_mvm *mvm,
1846                                         struct iwl_rx_cmd_buffer *rxb)
1847 {
1848         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1849         struct iwl_channel_switch_error_notif *notif = (void *)pkt->data;
1850         struct ieee80211_vif *vif;
1851         u32 id = le32_to_cpu(notif->link_id);
1852         u32 csa_err_mask = le32_to_cpu(notif->csa_err_mask);
1853
1854         rcu_read_lock();
1855         vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1856         if (!vif) {
1857                 rcu_read_unlock();
1858                 return;
1859         }
1860
1861         IWL_DEBUG_INFO(mvm, "FW reports CSA error: id=%u, csa_err_mask=%u\n",
1862                        id, csa_err_mask);
1863         if (csa_err_mask & (CS_ERR_COUNT_ERROR |
1864                             CS_ERR_LONG_DELAY_AFTER_CS |
1865                             CS_ERR_TX_BLOCK_TIMER_EXPIRED))
1866                 ieee80211_channel_switch_disconnect(vif, true);
1867         rcu_read_unlock();
1868 }
1869
1870 void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1871                                  struct iwl_rx_cmd_buffer *rxb)
1872 {
1873         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1874         struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1875         struct ieee80211_vif *vif;
1876         u32 id = le32_to_cpu(mb->mac_id);
1877
1878         IWL_DEBUG_INFO(mvm,
1879                        "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1880                        le32_to_cpu(mb->mac_id),
1881                        mb->num_beacon_intervals_elapsed,
1882                        mb->profile_periodicity);
1883
1884         rcu_read_lock();
1885
1886         vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1887         if (vif)
1888                 iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1889
1890         rcu_read_unlock();
1891 }