1
2
3
4
5
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
16const 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
23const 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
34struct 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
43static 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
51 if (vif == data->vif)
52 return;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 switch (data->vif->type) {
71 case NL80211_IFTYPE_STATION:
72
73
74
75
76
77
78
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
101
102
103
104
105
106
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
129
130
131
132
133
134 break;
135 }
136
137
138
139
140
141
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
149static 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
156 if (vif == data->vif) {
157 data->found_vif = true;
158 return;
159 }
160
161
162
163
164
165
166
167 __clear_bit(mvmvif->id, data->available_mac_ids);
168
169
170 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
171}
172
173void 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
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
196int 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 switch (vif->type) {
230 case NL80211_IFTYPE_ADHOC:
231 break;
232 case NL80211_IFTYPE_STATION:
233 if (!vif->p2p)
234 break;
235 fallthrough;
236 default:
237 __clear_bit(0, data.available_mac_ids);
238 }
239
240 ieee80211_iterate_active_interfaces_atomic(
241 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
242 iwl_mvm_mac_iface_iterator, &data);
243
244
245
246
247
248
249
250
251
252
253
254 if (data.found_vif)
255 return 0;
256
257
258 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
259 return -EBUSY;
260
261 mvmvif->id = find_first_bit(data.available_mac_ids,
262 NUM_MAC_INDEX_DRIVER);
263 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
264 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
265 ret = -EIO;
266 goto exit_fail;
267 }
268
269 if (data.preferred_tsf != NUM_TSF_IDS)
270 mvmvif->tsf_id = data.preferred_tsf;
271 else
272 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
273 NUM_TSF_IDS);
274 if (mvmvif->tsf_id == NUM_TSF_IDS) {
275 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
276 ret = -EIO;
277 goto exit_fail;
278 }
279
280 mvmvif->color = 0;
281
282 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
283 mvmvif->time_event_data.id = TE_MAX;
284
285
286 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
287 return 0;
288
289
290 if (vif->type == NL80211_IFTYPE_AP ||
291 vif->type == NL80211_IFTYPE_ADHOC) {
292
293
294
295
296 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
297 }
298
299 mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
300 mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
301 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
302
303 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
304 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
305
306 return 0;
307
308exit_fail:
309 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
310 return ret;
311}
312
313static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
314 struct ieee80211_vif *vif,
315 enum nl80211_band band,
316 u8 *cck_rates, u8 *ofdm_rates)
317{
318 struct ieee80211_supported_band *sband;
319 unsigned long basic = vif->bss_conf.basic_rates;
320 int lowest_present_ofdm = 100;
321 int lowest_present_cck = 100;
322 u8 cck = 0;
323 u8 ofdm = 0;
324 int i;
325
326 sband = mvm->hw->wiphy->bands[band];
327
328 for_each_set_bit(i, &basic, BITS_PER_LONG) {
329 int hw = sband->bitrates[i].hw_value;
330 if (hw >= IWL_FIRST_OFDM_RATE) {
331 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
332 if (lowest_present_ofdm > hw)
333 lowest_present_ofdm = hw;
334 } else {
335 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
336
337 cck |= BIT(hw);
338 if (lowest_present_cck > hw)
339 lowest_present_cck = hw;
340 }
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
367 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
368 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
369 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
370
371 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386 if (IWL_RATE_11M_INDEX < lowest_present_cck)
387 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
388 if (IWL_RATE_5M_INDEX < lowest_present_cck)
389 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
390 if (IWL_RATE_2M_INDEX < lowest_present_cck)
391 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
392
393 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
394
395 *cck_rates = cck;
396 *ofdm_rates = ofdm;
397}
398
399static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
400 struct ieee80211_vif *vif,
401 struct iwl_mac_ctx_cmd *cmd)
402{
403
404 u8 protection_mode = vif->bss_conf.ht_operation_mode &
405 IEEE80211_HT_OP_MODE_PROTECTION;
406
407 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
408
409 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
410
411
412
413
414 switch (protection_mode) {
415 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
416 break;
417 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
418 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
419 cmd->protection_flags |= cpu_to_le32(ht_flag);
420 break;
421 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
422
423 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
424 cmd->protection_flags |= cpu_to_le32(ht_flag);
425 break;
426 default:
427 IWL_ERR(mvm, "Illegal protection mode %d\n",
428 protection_mode);
429 break;
430 }
431}
432
433static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
434 struct ieee80211_vif *vif,
435 struct iwl_mac_ctx_cmd *cmd,
436 const u8 *bssid_override,
437 u32 action)
438{
439 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
440 struct ieee80211_chanctx_conf *chanctx;
441 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
442 IEEE80211_HT_OP_MODE_PROTECTION);
443 u8 cck_ack_rates, ofdm_ack_rates;
444 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
445 int i;
446
447 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
448 mvmvif->color));
449 cmd->action = cpu_to_le32(action);
450
451 switch (vif->type) {
452 case NL80211_IFTYPE_STATION:
453 if (vif->p2p)
454 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
455 else
456 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
457 break;
458 case NL80211_IFTYPE_AP:
459 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
460 break;
461 case NL80211_IFTYPE_MONITOR:
462 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
463 break;
464 case NL80211_IFTYPE_P2P_DEVICE:
465 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
466 break;
467 case NL80211_IFTYPE_ADHOC:
468 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
469 break;
470 default:
471 WARN_ON_ONCE(1);
472 }
473
474 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
475
476 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
477
478 if (bssid)
479 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
480 else
481 eth_broadcast_addr(cmd->bssid_addr);
482
483 rcu_read_lock();
484 chanctx = rcu_dereference(vif->chanctx_conf);
485 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
486 : NL80211_BAND_2GHZ,
487 &cck_ack_rates, &ofdm_ack_rates);
488 rcu_read_unlock();
489
490 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
491 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
492
493 cmd->cck_short_preamble =
494 cpu_to_le32(vif->bss_conf.use_short_preamble ?
495 MAC_FLG_SHORT_PREAMBLE : 0);
496 cmd->short_slot =
497 cpu_to_le32(vif->bss_conf.use_short_slot ?
498 MAC_FLG_SHORT_SLOT : 0);
499
500 cmd->filter_flags = 0;
501
502 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
503 u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
504 u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
505
506 cmd->ac[ucode_ac].cw_min =
507 cpu_to_le16(mvmvif->queue_params[i].cw_min);
508 cmd->ac[ucode_ac].cw_max =
509 cpu_to_le16(mvmvif->queue_params[i].cw_max);
510 cmd->ac[ucode_ac].edca_txop =
511 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
512 cmd->ac[ucode_ac].aifsn = mvmvif->queue_params[i].aifs;
513 cmd->ac[ucode_ac].fifos_mask = BIT(txf);
514 }
515
516 if (vif->bss_conf.qos)
517 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
518
519 if (vif->bss_conf.use_cts_prot)
520 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
521
522 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
523 vif->bss_conf.use_cts_prot,
524 vif->bss_conf.ht_operation_mode);
525 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
526 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
527 if (ht_enabled)
528 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
529}
530
531static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
532 struct iwl_mac_ctx_cmd *cmd)
533{
534 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
535 sizeof(*cmd), cmd);
536 if (ret)
537 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
538 le32_to_cpu(cmd->action), ret);
539 return ret;
540}
541
542static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
543 struct ieee80211_vif *vif,
544 u32 action, bool force_assoc_off,
545 const u8 *bssid_override)
546{
547 struct iwl_mac_ctx_cmd cmd = {};
548 struct iwl_mac_data_sta *ctxt_sta;
549
550 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
551
552
553 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
554
555 if (vif->p2p) {
556 struct ieee80211_p2p_noa_attr *noa =
557 &vif->bss_conf.p2p_noa_attr;
558
559 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
560 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
561 ctxt_sta = &cmd.p2p_sta.sta;
562 } else {
563 ctxt_sta = &cmd.sta;
564 }
565
566
567 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
568 !force_assoc_off) {
569 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
570 u8 ap_sta_id = mvmvif->ap_sta_id;
571 u32 dtim_offs;
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588 dtim_offs = vif->bss_conf.sync_dtim_count *
589 vif->bss_conf.beacon_int;
590
591 dtim_offs *= 1024;
592
593 ctxt_sta->dtim_tsf =
594 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
595 ctxt_sta->dtim_time =
596 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
597 ctxt_sta->assoc_beacon_arrive_time =
598 cpu_to_le32(vif->bss_conf.sync_device_ts);
599
600 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
601 le64_to_cpu(ctxt_sta->dtim_tsf),
602 le32_to_cpu(ctxt_sta->dtim_time),
603 dtim_offs);
604
605 ctxt_sta->is_assoc = cpu_to_le32(1);
606
607
608
609
610
611 if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
612 struct ieee80211_sta *sta;
613
614 rcu_read_lock();
615
616 sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
617 if (!IS_ERR_OR_NULL(sta)) {
618 struct iwl_mvm_sta *mvmsta =
619 iwl_mvm_sta_from_mac80211(sta);
620
621 if (mvmsta->sta_state ==
622 IEEE80211_STA_AUTHORIZED)
623 cmd.filter_flags |=
624 cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
625 }
626
627 rcu_read_unlock();
628 }
629 } else {
630 ctxt_sta->is_assoc = cpu_to_le32(0);
631
632
633
634
635 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
636 }
637
638 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
639 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
640 vif->bss_conf.dtim_period);
641
642 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
643 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
644
645 if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
646 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
647
648 if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
649 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
650 if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT) {
651 ctxt_sta->data_policy |= cpu_to_le32(TWT_SUPPORTED);
652 if (vif->bss_conf.twt_protected)
653 ctxt_sta->data_policy |=
654 cpu_to_le32(PROTECTED_TWT_SUPPORTED);
655 }
656 }
657
658
659 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
660}
661
662static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
663 struct ieee80211_vif *vif,
664 u32 action)
665{
666 struct iwl_mac_ctx_cmd cmd = {};
667 u32 tfd_queue_msk = BIT(mvm->snif_queue);
668 int ret;
669
670 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
671
672 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
673
674 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
675 MAC_FILTER_IN_CONTROL_AND_MGMT |
676 MAC_FILTER_IN_BEACON |
677 MAC_FILTER_IN_PROBE_REQUEST |
678 MAC_FILTER_IN_CRC32 |
679 MAC_FILTER_ACCEPT_GRP);
680 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
681
682
683 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
684 vif->type, IWL_STA_GENERAL_PURPOSE);
685 if (ret)
686 return ret;
687
688 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
689}
690
691static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
692 struct ieee80211_vif *vif,
693 u32 action)
694{
695 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
696 struct iwl_mac_ctx_cmd cmd = {};
697
698 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
699
700 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
701
702 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
703 MAC_FILTER_IN_PROBE_REQUEST |
704 MAC_FILTER_ACCEPT_GRP);
705
706
707 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
708
709
710 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
711
712 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
713}
714
715struct iwl_mvm_go_iterator_data {
716 bool go_active;
717};
718
719static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
720{
721 struct iwl_mvm_go_iterator_data *data = _data;
722 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
723
724 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
725 mvmvif->ap_ibss_active)
726 data->go_active = true;
727}
728
729static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
730 struct ieee80211_vif *vif,
731 u32 action)
732{
733 struct iwl_mac_ctx_cmd cmd = {};
734 struct iwl_mvm_go_iterator_data data = {};
735
736 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
737
738 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
739
740
741 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
742
743
744
745
746
747
748
749
750
751 ieee80211_iterate_active_interfaces_atomic(
752 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
753 iwl_mvm_go_iterator, &data);
754
755 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
756 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
757}
758
759void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
760 __le32 *tim_index, __le32 *tim_size,
761 u8 *beacon, u32 frame_size)
762{
763 u32 tim_idx;
764 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
765
766
767
768 tim_idx = mgmt->u.beacon.variable - beacon;
769
770
771 while ((tim_idx < (frame_size - 2)) &&
772 (beacon[tim_idx] != WLAN_EID_TIM))
773 tim_idx += beacon[tim_idx+1] + 2;
774
775
776 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
777 *tim_index = cpu_to_le32(tim_idx);
778 *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
779 } else {
780 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
781 }
782}
783
784static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
785{
786 struct ieee80211_mgmt *mgmt = (void *)beacon;
787 const u8 *ie;
788
789 if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
790 return 0;
791
792 frame_size -= mgmt->u.beacon.variable - beacon;
793
794 ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
795 if (!ie)
796 return 0;
797
798 return ie - beacon;
799}
800
801u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
802 struct ieee80211_vif *vif)
803{
804 u8 rate;
805 if (info->band == NL80211_BAND_2GHZ && !vif->p2p)
806 rate = IWL_FIRST_CCK_RATE;
807 else
808 rate = IWL_FIRST_OFDM_RATE;
809
810 return rate;
811}
812
813static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
814 struct ieee80211_vif *vif,
815 struct sk_buff *beacon,
816 struct iwl_tx_cmd *tx)
817{
818 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
819 struct ieee80211_tx_info *info;
820 u8 rate;
821 u32 tx_flags;
822
823 info = IEEE80211_SKB_CB(beacon);
824
825
826 tx->len = cpu_to_le16((u16)beacon->len);
827 tx->sta_id = mvmvif->bcast_sta.sta_id;
828 tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
829 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
830 tx_flags |=
831 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
832 TX_CMD_FLG_BT_PRIO_POS;
833 tx->tx_flags = cpu_to_le32(tx_flags);
834
835 if (!fw_has_capa(&mvm->fw->ucode_capa,
836 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
837 iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
838
839 tx->rate_n_flags =
840 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
841 RATE_MCS_ANT_POS);
842
843 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
844
845 tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
846 if (rate == IWL_FIRST_CCK_RATE)
847 tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
848
849}
850
851int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
852 struct sk_buff *beacon,
853 void *data, int len)
854{
855 struct iwl_host_cmd cmd = {
856 .id = BEACON_TEMPLATE_CMD,
857 .flags = CMD_ASYNC,
858 };
859
860 cmd.len[0] = len;
861 cmd.data[0] = data;
862 cmd.dataflags[0] = 0;
863 cmd.len[1] = beacon->len;
864 cmd.data[1] = beacon->data;
865 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
866
867 return iwl_mvm_send_cmd(mvm, &cmd);
868}
869
870static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
871 struct ieee80211_vif *vif,
872 struct sk_buff *beacon)
873{
874 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
875 struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
876
877 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
878
879 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
880
881 if (vif->type == NL80211_IFTYPE_AP)
882 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
883 &beacon_cmd.tim_size,
884 beacon->data, beacon->len);
885
886 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
887 sizeof(beacon_cmd));
888}
889
890static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
891 struct ieee80211_vif *vif,
892 struct sk_buff *beacon)
893{
894 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
895 struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
896
897 iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
898
899 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
900
901 if (vif->type == NL80211_IFTYPE_AP)
902 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
903 &beacon_cmd.tim_size,
904 beacon->data, beacon->len);
905
906 beacon_cmd.csa_offset =
907 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
908 WLAN_EID_CHANNEL_SWITCH,
909 beacon->len));
910 beacon_cmd.ecsa_offset =
911 cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
912 WLAN_EID_EXT_CHANSWITCH_ANN,
913 beacon->len));
914
915 return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
916 sizeof(beacon_cmd));
917}
918
919static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
920 struct ieee80211_vif *vif,
921 struct sk_buff *beacon)
922{
923 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
924 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
925 struct iwl_mac_beacon_cmd beacon_cmd = {};
926 u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
927 u16 flags;
928 struct ieee80211_chanctx_conf *ctx;
929 int channel;
930
931 flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
932
933 if (rate == IWL_FIRST_CCK_RATE)
934 flags |= IWL_MAC_BEACON_CCK;
935
936
937 rcu_read_lock();
938 ctx = rcu_dereference(vif->chanctx_conf);
939 channel = ieee80211_frequency_to_channel(ctx->def.chan->center_freq);
940 WARN_ON(channel == 0);
941 if (cfg80211_channel_is_psc(ctx->def.chan) &&
942 !IWL_MVM_DISABLE_AP_FILS) {
943 flags |= IWL_MAC_BEACON_FILS;
944 beacon_cmd.short_ssid =
945 cpu_to_le32(~crc32_le(~0, vif->bss_conf.ssid,
946 vif->bss_conf.ssid_len));
947 }
948 rcu_read_unlock();
949
950 beacon_cmd.flags = cpu_to_le16(flags);
951 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
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
972int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
973 struct ieee80211_vif *vif,
974 struct sk_buff *beacon)
975{
976 if (WARN_ON(!beacon))
977 return -EINVAL;
978
979 if (IWL_MVM_NON_TRANSMITTING_AP)
980 return 0;
981
982 if (!fw_has_capa(&mvm->fw->ucode_capa,
983 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
984 return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
985
986 if (fw_has_api(&mvm->fw->ucode_capa,
987 IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
988 return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
989
990 return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
991}
992
993
994int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
995 struct ieee80211_vif *vif)
996{
997 struct sk_buff *beacon;
998 int ret;
999
1000 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1001 vif->type != NL80211_IFTYPE_ADHOC);
1002
1003 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
1004 if (!beacon)
1005 return -ENOMEM;
1006
1007#ifdef CONFIG_IWLWIFI_DEBUGFS
1008 if (mvm->beacon_inject_active)
1009 return -EBUSY;
1010#endif
1011
1012 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1013 dev_kfree_skb(beacon);
1014 return ret;
1015}
1016
1017struct iwl_mvm_mac_ap_iterator_data {
1018 struct iwl_mvm *mvm;
1019 struct ieee80211_vif *vif;
1020 u32 beacon_device_ts;
1021 u16 beacon_int;
1022};
1023
1024
1025static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1026 struct ieee80211_vif *vif)
1027{
1028 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1029
1030 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1031 return;
1032
1033
1034 if (vif->p2p && data->beacon_device_ts)
1035 return;
1036
1037 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1038 data->beacon_int = vif->bss_conf.beacon_int;
1039}
1040
1041
1042
1043
1044static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1045 struct ieee80211_vif *vif,
1046 struct iwl_mac_ctx_cmd *cmd,
1047 struct iwl_mac_data_ap *ctxt_ap,
1048 bool add)
1049{
1050 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1051 struct iwl_mvm_mac_ap_iterator_data data = {
1052 .mvm = mvm,
1053 .vif = vif,
1054 .beacon_device_ts = 0
1055 };
1056
1057
1058 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
1059
1060
1061
1062
1063
1064
1065
1066 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1067 if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
1068 cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1069 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1070 } else {
1071 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1072 }
1073
1074 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1075 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1076 vif->bss_conf.dtim_period);
1077
1078 if (!fw_has_api(&mvm->fw->ucode_capa,
1079 IWL_UCODE_TLV_API_STA_TYPE))
1080 ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->cab_queue);
1081
1082
1083
1084
1085
1086
1087 if (add) {
1088
1089
1090
1091
1092
1093 ieee80211_iterate_active_interfaces_atomic(
1094 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1095 iwl_mvm_mac_ap_iterator, &data);
1096
1097 if (data.beacon_device_ts) {
1098 u32 rand = (prandom_u32() % (64 - 36)) + 36;
1099 mvmvif->ap_beacon_time = data.beacon_device_ts +
1100 ieee80211_tu_to_usec(data.beacon_int * rand /
1101 100);
1102 } else {
1103 mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
1104 }
1105 }
1106
1107 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
1108 ctxt_ap->beacon_tsf = 0;
1109
1110
1111 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1112}
1113
1114static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1115 struct ieee80211_vif *vif,
1116 u32 action)
1117{
1118 struct iwl_mac_ctx_cmd cmd = {};
1119
1120 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1121
1122
1123 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1124
1125
1126 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
1127 action == FW_CTXT_ACTION_ADD);
1128
1129 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1130}
1131
1132static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1133 struct ieee80211_vif *vif,
1134 u32 action)
1135{
1136 struct iwl_mac_ctx_cmd cmd = {};
1137 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
1138
1139 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1140
1141
1142 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
1143
1144
1145 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
1146 action == FW_CTXT_ACTION_ADD);
1147
1148 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1149 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1150 cmd.go.opp_ps_enabled =
1151 cpu_to_le32(!!(noa->oppps_ctwindow &
1152 IEEE80211_P2P_OPPPS_ENABLE_BIT));
1153
1154 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1155}
1156
1157static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1158 u32 action, bool force_assoc_off,
1159 const u8 *bssid_override)
1160{
1161 switch (vif->type) {
1162 case NL80211_IFTYPE_STATION:
1163 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
1164 force_assoc_off,
1165 bssid_override);
1166 case NL80211_IFTYPE_AP:
1167 if (!vif->p2p)
1168 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1169 else
1170 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1171 case NL80211_IFTYPE_MONITOR:
1172 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1173 case NL80211_IFTYPE_P2P_DEVICE:
1174 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1175 case NL80211_IFTYPE_ADHOC:
1176 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
1177 default:
1178 break;
1179 }
1180
1181 return -EOPNOTSUPP;
1182}
1183
1184int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1185{
1186 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1187 int ret;
1188
1189 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1190 vif->addr, ieee80211_vif_type_p2p(vif)))
1191 return -EIO;
1192
1193 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
1194 true, NULL);
1195 if (ret)
1196 return ret;
1197
1198
1199 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1200
1201 mvmvif->uploaded = true;
1202 return 0;
1203}
1204
1205int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1206 bool force_assoc_off, const u8 *bssid_override)
1207{
1208 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1209
1210 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1211 vif->addr, ieee80211_vif_type_p2p(vif)))
1212 return -EIO;
1213
1214 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
1215 force_assoc_off, bssid_override);
1216}
1217
1218int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1219{
1220 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1221 struct iwl_mac_ctx_cmd cmd;
1222 int ret;
1223
1224 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1225 vif->addr, ieee80211_vif_type_p2p(vif)))
1226 return -EIO;
1227
1228 memset(&cmd, 0, sizeof(cmd));
1229
1230 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1231 mvmvif->color));
1232 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1233
1234 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
1235 sizeof(cmd), &cmd);
1236 if (ret) {
1237 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1238 return ret;
1239 }
1240
1241 mvmvif->uploaded = false;
1242
1243 if (vif->type == NL80211_IFTYPE_MONITOR) {
1244 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
1245 iwl_mvm_dealloc_snif_sta(mvm);
1246 }
1247
1248 return 0;
1249}
1250
1251static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1252 struct ieee80211_vif *csa_vif, u32 gp2,
1253 bool tx_success)
1254{
1255 struct iwl_mvm_vif *mvmvif =
1256 iwl_mvm_vif_from_mac80211(csa_vif);
1257
1258
1259 if (!tx_success && !mvmvif->csa_countdown)
1260 return;
1261
1262 mvmvif->csa_countdown = true;
1263
1264 if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
1265 int c = ieee80211_beacon_update_cntdwn(csa_vif);
1266
1267 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1268 if (csa_vif->p2p &&
1269 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1270 tx_success) {
1271 u32 rel_time = (c + 1) *
1272 csa_vif->bss_conf.beacon_int -
1273 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
1274 u32 apply_time = gp2 + rel_time * 1024;
1275
1276 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1277 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1278 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1279 apply_time);
1280 }
1281 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1282
1283 ieee80211_csa_finish(csa_vif);
1284 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1285 }
1286}
1287
1288void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1289 struct iwl_rx_cmd_buffer *rxb)
1290{
1291 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1292 unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1293 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1294 struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
1295 struct ieee80211_vif *csa_vif;
1296 struct ieee80211_vif *tx_blocked_vif;
1297 struct agg_tx_status *agg_status;
1298 u16 status;
1299
1300 lockdep_assert_held(&mvm->mutex);
1301
1302 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1303
1304 if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
1305 struct iwl_mvm_tx_resp *beacon_notify_hdr =
1306 &beacon_v5->beacon_notify_hdr;
1307
1308 if (unlikely(pkt_len < sizeof(*beacon_v5)))
1309 return;
1310
1311 mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
1312 agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
1313 status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
1314 IWL_DEBUG_RX(mvm,
1315 "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
1316 status, beacon_notify_hdr->failure_frame,
1317 le64_to_cpu(beacon->tsf),
1318 mvm->ap_last_beacon_gp2,
1319 le32_to_cpu(beacon_notify_hdr->initial_rate));
1320 } else {
1321 if (unlikely(pkt_len < sizeof(*beacon)))
1322 return;
1323
1324 mvm->ibss_manager = beacon->ibss_mgr_status != 0;
1325 status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
1326 IWL_DEBUG_RX(mvm,
1327 "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
1328 status, le64_to_cpu(beacon->tsf),
1329 mvm->ap_last_beacon_gp2);
1330 }
1331
1332 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1333 lockdep_is_held(&mvm->mutex));
1334 if (unlikely(csa_vif && csa_vif->csa_active))
1335 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1336 (status == TX_STATUS_SUCCESS));
1337
1338 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1339 lockdep_is_held(&mvm->mutex));
1340 if (unlikely(tx_blocked_vif)) {
1341 struct iwl_mvm_vif *mvmvif =
1342 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1343
1344
1345
1346
1347
1348
1349 if (!mvm->csa_tx_block_bcn_timeout)
1350 mvm->csa_tx_block_bcn_timeout =
1351 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1352 else
1353 mvm->csa_tx_block_bcn_timeout--;
1354
1355
1356 if (mvm->csa_tx_block_bcn_timeout == 0) {
1357 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1358 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1359 }
1360 }
1361}
1362
1363void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1364 struct iwl_rx_cmd_buffer *rxb)
1365{
1366 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1367 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1368 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1369 struct iwl_fw_dbg_trigger_tlv *trigger;
1370 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1371 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
1372 struct ieee80211_vif *vif;
1373 u32 id = le32_to_cpu(mb->mac_id);
1374 union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
1375
1376 IWL_DEBUG_INFO(mvm,
1377 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1378 le32_to_cpu(mb->mac_id),
1379 le32_to_cpu(mb->consec_missed_beacons),
1380 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1381 le32_to_cpu(mb->num_recvd_beacons),
1382 le32_to_cpu(mb->num_expected_beacons));
1383
1384 rcu_read_lock();
1385
1386 vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1387 if (!vif)
1388 goto out;
1389
1390 rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
1391 rx_missed_bcon_since_rx =
1392 le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
1393
1394
1395
1396
1397 if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
1398 iwl_mvm_connection_loss(mvm, vif, "missed beacons");
1399 else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
1400 ieee80211_beacon_loss(vif);
1401
1402 iwl_dbg_tlv_time_point(&mvm->fwrt,
1403 IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
1404
1405 trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1406 FW_DBG_TRIGGER_MISSED_BEACONS);
1407 if (!trigger)
1408 goto out;
1409
1410 bcon_trig = (void *)trigger->data;
1411 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1412 stop_trig_missed_bcon_since_rx =
1413 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1414
1415
1416
1417 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1418 rx_missed_bcon >= stop_trig_missed_bcon)
1419 iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
1420
1421out:
1422 rcu_read_unlock();
1423}
1424
1425void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
1426 struct iwl_rx_cmd_buffer *rxb)
1427{
1428 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1429 unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
1430 struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
1431 struct ieee80211_rx_status rx_status;
1432 struct sk_buff *skb;
1433 u32 size = le32_to_cpu(sb->byte_count);
1434
1435 if (size == 0 || pkt_len < struct_size(sb, data, size))
1436 return;
1437
1438 skb = alloc_skb(size, GFP_ATOMIC);
1439 if (!skb) {
1440 IWL_ERR(mvm, "alloc_skb failed\n");
1441 return;
1442 }
1443
1444
1445 memset(&rx_status, 0, sizeof(rx_status));
1446 rx_status.mactime = le64_to_cpu(sb->tsf);
1447
1448 rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
1449 rx_status.device_timestamp = le32_to_cpu(sb->system_time);
1450 rx_status.band =
1451 (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
1452 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1453 rx_status.freq =
1454 ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
1455 rx_status.band);
1456
1457
1458 skb_put_data(skb, sb->data, size);
1459 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
1460
1461
1462 ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
1463}
1464
1465void iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
1466 struct iwl_rx_cmd_buffer *rxb)
1467{
1468 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1469 struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
1470 struct iwl_probe_resp_data *old_data, *new_data;
1471 u32 id = le32_to_cpu(notif->mac_id);
1472 struct ieee80211_vif *vif;
1473 struct iwl_mvm_vif *mvmvif;
1474
1475 IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
1476 notif->noa_active, notif->csa_counter);
1477
1478 vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
1479 if (!vif)
1480 return;
1481
1482 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1483
1484 new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
1485 if (!new_data)
1486 return;
1487
1488 memcpy(&new_data->notif, notif, sizeof(new_data->notif));
1489
1490
1491 new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
1492 sizeof(new_data->notif.noa_attr) - 1;
1493
1494
1495
1496
1497
1498 if (new_data->notif.noa_attr.len_low ==
1499 sizeof(struct ieee80211_p2p_noa_desc) + 2)
1500 new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
1501
1502 old_data = rcu_dereference_protected(mvmvif->probe_resp_data,
1503 lockdep_is_held(&mvmvif->mvm->mutex));
1504 rcu_assign_pointer(mvmvif->probe_resp_data, new_data);
1505
1506 if (old_data)
1507 kfree_rcu(old_data, rcu_head);
1508
1509 if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
1510 notif->csa_counter >= 1)
1511 ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
1512}
1513
1514void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
1515 struct iwl_rx_cmd_buffer *rxb)
1516{
1517 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1518 struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
1519 struct ieee80211_vif *csa_vif, *vif;
1520 struct iwl_mvm_vif *mvmvif;
1521 u32 id_n_color, csa_id, mac_id;
1522
1523 id_n_color = le32_to_cpu(notif->id_and_color);
1524 mac_id = id_n_color & FW_CTXT_ID_MSK;
1525
1526 if (WARN_ON_ONCE(mac_id >= NUM_MAC_INDEX_DRIVER))
1527 return;
1528
1529 rcu_read_lock();
1530 vif = rcu_dereference(mvm->vif_id_to_mac[mac_id]);
1531 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1532
1533 switch (vif->type) {
1534 case NL80211_IFTYPE_AP:
1535 csa_vif = rcu_dereference(mvm->csa_vif);
1536 if (WARN_ON(!csa_vif || !csa_vif->csa_active ||
1537 csa_vif != vif))
1538 goto out_unlock;
1539
1540 csa_id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
1541 if (WARN(csa_id != id_n_color,
1542 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
1543 csa_id, id_n_color))
1544 goto out_unlock;
1545
1546 IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
1547
1548 schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
1549 msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
1550 csa_vif->bss_conf.beacon_int));
1551
1552 ieee80211_csa_finish(csa_vif);
1553
1554 rcu_read_unlock();
1555
1556 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1557 return;
1558 case NL80211_IFTYPE_STATION:
1559 iwl_mvm_csa_client_absent(mvm, vif);
1560 cancel_delayed_work(&mvmvif->csa_work);
1561 ieee80211_chswitch_done(vif, true);
1562 break;
1563 default:
1564
1565 WARN_ON_ONCE(1);
1566 break;
1567 }
1568out_unlock:
1569 rcu_read_unlock();
1570}
1571
1572void iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
1573 struct iwl_rx_cmd_buffer *rxb)
1574{
1575 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1576 struct iwl_missed_vap_notif *mb = (void *)pkt->data;
1577 struct ieee80211_vif *vif;
1578 u32 id = le32_to_cpu(mb->mac_id);
1579
1580 IWL_DEBUG_INFO(mvm,
1581 "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
1582 le32_to_cpu(mb->mac_id),
1583 mb->num_beacon_intervals_elapsed,
1584 mb->profile_periodicity);
1585
1586 rcu_read_lock();
1587
1588 vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
1589 if (vif)
1590 iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
1591
1592 rcu_read_unlock();
1593}
1594