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