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#include <linux/etherdevice.h>
65#include <net/mac80211.h>
66#include "iwl-io.h"
67#include "iwl-prph.h"
68#include "fw-api.h"
69#include "mvm.h"
70
71const u8 iwl_mvm_ac_to_tx_fifo[] = {
72 IWL_MVM_TX_FIFO_BK,
73 IWL_MVM_TX_FIFO_BE,
74 IWL_MVM_TX_FIFO_VI,
75 IWL_MVM_TX_FIFO_VO,
76};
77
78struct iwl_mvm_mac_iface_iterator_data {
79 struct iwl_mvm *mvm;
80 struct ieee80211_vif *vif;
81 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
82 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
83 unsigned long used_hw_queues[BITS_TO_LONGS(IWL_MVM_FIRST_AGG_QUEUE)];
84 enum iwl_tsf_id preferred_tsf;
85 bool found_vif;
86};
87
88static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
89 struct ieee80211_vif *vif)
90{
91 struct iwl_mvm_mac_iface_iterator_data *data = _data;
92 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
93 u32 ac;
94
95
96 if (vif == data->vif) {
97 data->found_vif = true;
98 return;
99 }
100
101
102 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
103 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
104 __set_bit(vif->hw_queue[ac], data->used_hw_queues);
105
106 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
107 __set_bit(vif->cab_queue, data->used_hw_queues);
108
109
110
111
112
113
114
115
116 __clear_bit(mvmvif->id, data->available_mac_ids);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 switch (data->vif->type) {
135 case NL80211_IFTYPE_STATION:
136
137
138
139
140
141
142
143 if (vif->type == NL80211_IFTYPE_AP) {
144 if (data->preferred_tsf == NUM_TSF_IDS &&
145 test_bit(mvmvif->tsf_id, data->available_tsf_ids))
146 data->preferred_tsf = mvmvif->tsf_id;
147 return;
148 }
149 break;
150 case NL80211_IFTYPE_AP:
151
152
153
154
155
156
157
158
159 if (vif->type == NL80211_IFTYPE_STATION ||
160 vif->type == NL80211_IFTYPE_AP) {
161 if (data->preferred_tsf == NUM_TSF_IDS &&
162 test_bit(mvmvif->tsf_id, data->available_tsf_ids))
163 data->preferred_tsf = mvmvif->tsf_id;
164 return;
165 }
166 break;
167 default:
168
169
170
171
172
173
174
175 break;
176 }
177
178
179
180
181
182
183
184 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
185
186 if (data->preferred_tsf == mvmvif->tsf_id)
187 data->preferred_tsf = NUM_TSF_IDS;
188}
189
190
191
192
193u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
194 struct ieee80211_vif *vif)
195{
196 u32 qmask = 0, ac;
197
198 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
199 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
200
201 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
202 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
203 qmask |= BIT(vif->hw_queue[ac]);
204
205 return qmask;
206}
207
208static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
209 struct ieee80211_vif *vif)
210{
211 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
212 struct iwl_mvm_mac_iface_iterator_data data = {
213 .mvm = mvm,
214 .vif = vif,
215 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
216 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
217
218 .preferred_tsf = NUM_TSF_IDS,
219 .used_hw_queues = {
220 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
221 BIT(IWL_MVM_AUX_QUEUE) |
222 BIT(IWL_MVM_CMD_QUEUE)
223 },
224 .found_vif = false,
225 };
226 u32 ac;
227 int ret, i;
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
247 __clear_bit(0, data.available_mac_ids);
248
249 ieee80211_iterate_active_interfaces_atomic(
250 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
251 iwl_mvm_mac_iface_iterator, &data);
252
253
254
255
256
257
258
259
260
261
262
263 if (data.found_vif)
264 return 0;
265
266
267 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
268
269 mvmvif->id = find_first_bit(data.available_mac_ids,
270 NUM_MAC_INDEX_DRIVER);
271 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
272 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
273 ret = -EIO;
274 goto exit_fail;
275 }
276
277 if (data.preferred_tsf != NUM_TSF_IDS)
278 mvmvif->tsf_id = data.preferred_tsf;
279 else
280 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
281 NUM_TSF_IDS);
282 if (mvmvif->tsf_id == NUM_TSF_IDS) {
283 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
284 ret = -EIO;
285 goto exit_fail;
286 }
287
288 mvmvif->color = 0;
289
290 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
291 mvmvif->time_event_data.id = TE_MAX;
292
293
294 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
295 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
296 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
297
298 return 0;
299 }
300
301
302 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
303 u8 queue = find_first_zero_bit(data.used_hw_queues,
304 IWL_MVM_FIRST_AGG_QUEUE);
305
306 if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
307 IWL_ERR(mvm, "Failed to allocate queue\n");
308 ret = -EIO;
309 goto exit_fail;
310 }
311
312 __set_bit(queue, data.used_hw_queues);
313 vif->hw_queue[ac] = queue;
314 }
315
316
317 if (vif->type == NL80211_IFTYPE_AP) {
318 u8 queue = find_first_zero_bit(data.used_hw_queues,
319 IWL_MVM_FIRST_AGG_QUEUE);
320
321 if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
322 IWL_ERR(mvm, "Failed to allocate cab queue\n");
323 ret = -EIO;
324 goto exit_fail;
325 }
326
327 vif->cab_queue = queue;
328 } else {
329 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
330 }
331
332 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
333 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
334
335 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
336 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
337
338 return 0;
339
340exit_fail:
341 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
342 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
343 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
344 return ret;
345}
346
347int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
348{
349 u32 ac;
350 int ret;
351
352 lockdep_assert_held(&mvm->mutex);
353
354 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
355 if (ret)
356 return ret;
357
358 switch (vif->type) {
359 case NL80211_IFTYPE_P2P_DEVICE:
360 iwl_trans_ac_txq_enable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
361 IWL_MVM_TX_FIFO_VO);
362 break;
363 case NL80211_IFTYPE_AP:
364 iwl_trans_ac_txq_enable(mvm->trans, vif->cab_queue,
365 IWL_MVM_TX_FIFO_MCAST);
366
367 default:
368 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
369 iwl_trans_ac_txq_enable(mvm->trans, vif->hw_queue[ac],
370 iwl_mvm_ac_to_tx_fifo[ac]);
371 break;
372 }
373
374 return 0;
375}
376
377void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
378{
379 int ac;
380
381 lockdep_assert_held(&mvm->mutex);
382
383 switch (vif->type) {
384 case NL80211_IFTYPE_P2P_DEVICE:
385 iwl_trans_txq_disable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE);
386 break;
387 case NL80211_IFTYPE_AP:
388 iwl_trans_txq_disable(mvm->trans, vif->cab_queue);
389
390 default:
391 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
392 iwl_trans_txq_disable(mvm->trans, vif->hw_queue[ac]);
393 }
394}
395
396static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
397 struct ieee80211_vif *vif,
398 enum ieee80211_band band,
399 u8 *cck_rates, u8 *ofdm_rates)
400{
401 struct ieee80211_supported_band *sband;
402 unsigned long basic = vif->bss_conf.basic_rates;
403 int lowest_present_ofdm = 100;
404 int lowest_present_cck = 100;
405 u8 cck = 0;
406 u8 ofdm = 0;
407 int i;
408
409 sband = mvm->hw->wiphy->bands[band];
410
411 for_each_set_bit(i, &basic, BITS_PER_LONG) {
412 int hw = sband->bitrates[i].hw_value;
413 if (hw >= IWL_FIRST_OFDM_RATE) {
414 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
415 if (lowest_present_ofdm > hw)
416 lowest_present_ofdm = hw;
417 } else {
418 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
419
420 cck |= BIT(hw);
421 if (lowest_present_cck > hw)
422 lowest_present_cck = hw;
423 }
424 }
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
450 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
451 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
452 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
453
454 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 if (IWL_RATE_11M_INDEX < lowest_present_cck)
470 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
471 if (IWL_RATE_5M_INDEX < lowest_present_cck)
472 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
473 if (IWL_RATE_2M_INDEX < lowest_present_cck)
474 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
475
476 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
477
478 *cck_rates = cck;
479 *ofdm_rates = ofdm;
480}
481
482static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
483 struct ieee80211_vif *vif,
484 struct iwl_mac_ctx_cmd *cmd,
485 u32 action)
486{
487 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
488 struct ieee80211_chanctx_conf *chanctx;
489 u8 cck_ack_rates, ofdm_ack_rates;
490 int i;
491
492 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
493 mvmvif->color));
494 cmd->action = cpu_to_le32(action);
495
496 switch (vif->type) {
497 case NL80211_IFTYPE_STATION:
498 if (vif->p2p)
499 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
500 else
501 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
502 break;
503 case NL80211_IFTYPE_AP:
504 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
505 break;
506 case NL80211_IFTYPE_MONITOR:
507 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
508 break;
509 case NL80211_IFTYPE_P2P_DEVICE:
510 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
511 break;
512 case NL80211_IFTYPE_ADHOC:
513 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
514 break;
515 default:
516 WARN_ON_ONCE(1);
517 }
518
519 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
520
521 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
522 if (vif->bss_conf.bssid)
523 memcpy(cmd->bssid_addr, vif->bss_conf.bssid, ETH_ALEN);
524 else
525 eth_broadcast_addr(cmd->bssid_addr);
526
527 rcu_read_lock();
528 chanctx = rcu_dereference(vif->chanctx_conf);
529 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
530 : IEEE80211_BAND_2GHZ,
531 &cck_ack_rates, &ofdm_ack_rates);
532 rcu_read_unlock();
533
534 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
535 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
536
537 cmd->cck_short_preamble =
538 cpu_to_le32(vif->bss_conf.use_short_preamble ?
539 MAC_FLG_SHORT_PREAMBLE : 0);
540 cmd->short_slot =
541 cpu_to_le32(vif->bss_conf.use_short_slot ?
542 MAC_FLG_SHORT_SLOT : 0);
543
544 for (i = 0; i < AC_NUM; i++) {
545 cmd->ac[i].cw_min = cpu_to_le16(mvmvif->queue_params[i].cw_min);
546 cmd->ac[i].cw_max = cpu_to_le16(mvmvif->queue_params[i].cw_max);
547 cmd->ac[i].aifsn = mvmvif->queue_params[i].aifs;
548 cmd->ac[i].edca_txop =
549 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
550 cmd->ac[i].fifos_mask = BIT(iwl_mvm_ac_to_tx_fifo[i]);
551 }
552
553
554 if (vif->type == NL80211_IFTYPE_AP)
555 cmd->ac[AC_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
556
557 if (vif->bss_conf.qos)
558 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
559
560
561 if (vif->bss_conf.use_cts_prot)
562 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
563
564
565
566
567
568 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
569 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
570 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_HT_PROT |
571 MAC_PROT_FLG_FAT_PROT);
572 }
573
574 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
575}
576
577static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
578 struct iwl_mac_ctx_cmd *cmd)
579{
580 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
581 sizeof(*cmd), cmd);
582 if (ret)
583 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
584 le32_to_cpu(cmd->action), ret);
585 return ret;
586}
587
588
589
590
591static void iwl_mvm_mac_ctxt_cmd_fill_sta(struct iwl_mvm *mvm,
592 struct ieee80211_vif *vif,
593 struct iwl_mac_data_sta *ctxt_sta,
594 bool force_assoc_off)
595{
596
597 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
598 !force_assoc_off) {
599 u32 dtim_offs;
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616 dtim_offs = vif->bss_conf.sync_dtim_count *
617 vif->bss_conf.beacon_int;
618
619 dtim_offs *= 1024;
620
621 ctxt_sta->dtim_tsf =
622 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
623 ctxt_sta->dtim_time =
624 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
625
626 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
627 le64_to_cpu(ctxt_sta->dtim_tsf),
628 le32_to_cpu(ctxt_sta->dtim_time),
629 dtim_offs);
630
631 ctxt_sta->is_assoc = cpu_to_le32(1);
632 } else {
633 ctxt_sta->is_assoc = cpu_to_le32(0);
634 }
635
636 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
637 ctxt_sta->bi_reciprocal =
638 cpu_to_le32(iwl_mvm_reciprocal(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 ctxt_sta->dtim_reciprocal =
642 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
643 vif->bss_conf.dtim_period));
644
645 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
646 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
647}
648
649static int iwl_mvm_mac_ctxt_cmd_station(struct iwl_mvm *mvm,
650 struct ieee80211_vif *vif,
651 u32 action)
652{
653 struct iwl_mac_ctx_cmd cmd = {};
654
655 WARN_ON(vif->type != NL80211_IFTYPE_STATION || vif->p2p);
656
657
658 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
659
660
661
662 if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period)
663 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
664 else
665 cmd.filter_flags &= ~cpu_to_le32(MAC_FILTER_IN_BEACON);
666
667
668 iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.sta,
669 action == FW_CTXT_ACTION_ADD);
670
671 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
672}
673
674static int iwl_mvm_mac_ctxt_cmd_p2p_client(struct iwl_mvm *mvm,
675 struct ieee80211_vif *vif,
676 u32 action)
677{
678 struct iwl_mac_ctx_cmd cmd = {};
679 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
680
681 WARN_ON(vif->type != NL80211_IFTYPE_STATION || !vif->p2p);
682
683
684 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
685
686
687 iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.p2p_sta.sta,
688 action == FW_CTXT_ACTION_ADD);
689
690 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
691 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
692
693 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
694}
695
696static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
697 struct ieee80211_vif *vif,
698 u32 action)
699{
700 struct iwl_mac_ctx_cmd cmd = {};
701
702 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
703
704 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
705
706 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
707 MAC_FILTER_IN_CONTROL_AND_MGMT |
708 MAC_FILTER_IN_BEACON |
709 MAC_FILTER_IN_PROBE_REQUEST);
710
711 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
712}
713
714struct iwl_mvm_go_iterator_data {
715 bool go_active;
716};
717
718static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
719{
720 struct iwl_mvm_go_iterator_data *data = _data;
721 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
722
723 if (vif->type == NL80211_IFTYPE_AP && vif->p2p && mvmvif->ap_active)
724 data->go_active = true;
725}
726
727static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
728 struct ieee80211_vif *vif,
729 u32 action)
730{
731 struct iwl_mac_ctx_cmd cmd = {};
732 struct iwl_mvm_go_iterator_data data = {};
733
734 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
735
736 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
737
738 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
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
759static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
760 struct iwl_mac_beacon_cmd *beacon_cmd,
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 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
778 beacon_cmd->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 int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
785 struct ieee80211_vif *vif,
786 struct sk_buff *beacon)
787{
788 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
789 struct iwl_host_cmd cmd = {
790 .id = BEACON_TEMPLATE_CMD,
791 .flags = CMD_ASYNC,
792 };
793 struct iwl_mac_beacon_cmd beacon_cmd = {};
794 struct ieee80211_tx_info *info;
795 u32 beacon_skb_len;
796 u32 rate;
797
798 if (WARN_ON(!beacon))
799 return -EINVAL;
800
801 beacon_skb_len = beacon->len;
802
803
804
805 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
806
807
808 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
809 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
810 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
811 beacon_cmd.tx.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
812 TX_CMD_FLG_BT_DIS |
813 TX_CMD_FLG_TSF);
814
815 mvm->mgmt_last_antenna_idx =
816 iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
817 mvm->mgmt_last_antenna_idx);
818
819 beacon_cmd.tx.rate_n_flags =
820 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
821 RATE_MCS_ANT_POS);
822
823 info = IEEE80211_SKB_CB(beacon);
824
825 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
826 rate = IWL_FIRST_OFDM_RATE;
827 } else {
828 rate = IWL_FIRST_CCK_RATE;
829 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
830 }
831 beacon_cmd.tx.rate_n_flags |=
832 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
833
834
835 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
836 beacon->data,
837 beacon_skb_len);
838
839
840 cmd.len[0] = sizeof(beacon_cmd);
841 cmd.data[0] = &beacon_cmd;
842 cmd.dataflags[0] = 0;
843 cmd.len[1] = beacon_skb_len;
844 cmd.data[1] = beacon->data;
845 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
846
847 return iwl_mvm_send_cmd(mvm, &cmd);
848}
849
850
851int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
852 struct ieee80211_vif *vif)
853{
854 struct sk_buff *beacon;
855 int ret;
856
857 WARN_ON(vif->type != NL80211_IFTYPE_AP);
858
859 beacon = ieee80211_beacon_get(mvm->hw, vif);
860 if (!beacon)
861 return -ENOMEM;
862
863 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
864 dev_kfree_skb(beacon);
865 return ret;
866}
867
868struct iwl_mvm_mac_ap_iterator_data {
869 struct iwl_mvm *mvm;
870 struct ieee80211_vif *vif;
871 u32 beacon_device_ts;
872 u16 beacon_int;
873};
874
875
876static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
877 struct ieee80211_vif *vif)
878{
879 struct iwl_mvm_mac_ap_iterator_data *data = _data;
880
881 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
882 return;
883
884
885 if (vif->p2p && data->beacon_device_ts)
886 return;
887
888 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
889 data->beacon_int = vif->bss_conf.beacon_int;
890}
891
892
893
894
895static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
896 struct ieee80211_vif *vif,
897 struct iwl_mac_data_ap *ctxt_ap,
898 bool add)
899{
900 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
901 struct iwl_mvm_mac_ap_iterator_data data = {
902 .mvm = mvm,
903 .vif = vif,
904 .beacon_device_ts = 0
905 };
906
907 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
908 ctxt_ap->bi_reciprocal =
909 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
910 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
911 vif->bss_conf.dtim_period);
912 ctxt_ap->dtim_reciprocal =
913 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
914 vif->bss_conf.dtim_period));
915
916 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
917
918
919
920
921
922
923 if (add) {
924
925
926
927
928
929 ieee80211_iterate_active_interfaces_atomic(
930 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
931 iwl_mvm_mac_ap_iterator, &data);
932
933 if (data.beacon_device_ts) {
934 u32 rand = (prandom_u32() % (80 - 20)) + 20;
935 mvmvif->ap_beacon_time = data.beacon_device_ts +
936 ieee80211_tu_to_usec(data.beacon_int * rand /
937 100);
938 } else {
939 mvmvif->ap_beacon_time =
940 iwl_read_prph(mvm->trans,
941 DEVICE_SYSTEM_TIME_REG);
942 }
943 }
944
945 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
946 ctxt_ap->beacon_tsf = 0;
947
948
949 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
950}
951
952static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
953 struct ieee80211_vif *vif,
954 u32 action)
955{
956 struct iwl_mac_ctx_cmd cmd = {};
957
958 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
959
960
961 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
962
963
964 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
965
966
967 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
968 action == FW_CTXT_ACTION_ADD);
969
970 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
971}
972
973static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
974 struct ieee80211_vif *vif,
975 u32 action)
976{
977 struct iwl_mac_ctx_cmd cmd = {};
978 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
979
980 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
981
982
983 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
984
985
986 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
987 action == FW_CTXT_ACTION_ADD);
988
989 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
990 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
991 cmd.go.opp_ps_enabled =
992 cpu_to_le32(!!(noa->oppps_ctwindow &
993 IEEE80211_P2P_OPPPS_ENABLE_BIT));
994
995 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
996}
997
998static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
999 u32 action)
1000{
1001 switch (vif->type) {
1002 case NL80211_IFTYPE_STATION:
1003 if (!vif->p2p)
1004 return iwl_mvm_mac_ctxt_cmd_station(mvm, vif,
1005 action);
1006 else
1007 return iwl_mvm_mac_ctxt_cmd_p2p_client(mvm, vif,
1008 action);
1009 break;
1010 case NL80211_IFTYPE_AP:
1011 if (!vif->p2p)
1012 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1013 else
1014 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1015 break;
1016 case NL80211_IFTYPE_MONITOR:
1017 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1018 case NL80211_IFTYPE_P2P_DEVICE:
1019 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
1020 default:
1021 break;
1022 }
1023
1024 return -EOPNOTSUPP;
1025}
1026
1027int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1028{
1029 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1030 int ret;
1031
1032 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1033 vif->addr, ieee80211_vif_type_p2p(vif)))
1034 return -EIO;
1035
1036 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD);
1037 if (ret)
1038 return ret;
1039
1040 mvmvif->uploaded = true;
1041 return 0;
1042}
1043
1044int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1045{
1046 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1047
1048 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1049 vif->addr, ieee80211_vif_type_p2p(vif)))
1050 return -EIO;
1051
1052 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY);
1053}
1054
1055int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1056{
1057 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1058 struct iwl_mac_ctx_cmd cmd;
1059 int ret;
1060
1061 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1062 vif->addr, ieee80211_vif_type_p2p(vif)))
1063 return -EIO;
1064
1065 memset(&cmd, 0, sizeof(cmd));
1066
1067 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1068 mvmvif->color));
1069 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1070
1071 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
1072 sizeof(cmd), &cmd);
1073 if (ret) {
1074 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1075 return ret;
1076 }
1077
1078 mvmvif->uploaded = false;
1079 return 0;
1080}
1081
1082int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1083 struct iwl_rx_cmd_buffer *rxb,
1084 struct iwl_device_cmd *cmd)
1085{
1086 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1087 struct iwl_beacon_notif *beacon = (void *)pkt->data;
1088 u16 status __maybe_unused =
1089 le16_to_cpu(beacon->beacon_notify_hdr.status.status);
1090 u32 rate __maybe_unused =
1091 le32_to_cpu(beacon->beacon_notify_hdr.initial_rate);
1092
1093 IWL_DEBUG_RX(mvm, "beacon status %#x retries:%d tsf:0x%16llX rate:%d\n",
1094 status & TX_STATUS_MSK,
1095 beacon->beacon_notify_hdr.failure_frame,
1096 le64_to_cpu(beacon->tsf),
1097 rate);
1098 return 0;
1099}
1100
1101static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1102 struct ieee80211_vif *vif)
1103{
1104 u16 *id = _data;
1105 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1106
1107 if (mvmvif->id == *id)
1108 ieee80211_beacon_loss(vif);
1109}
1110
1111int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1112 struct iwl_rx_cmd_buffer *rxb,
1113 struct iwl_device_cmd *cmd)
1114{
1115 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1116 struct iwl_missed_beacons_notif *missed_beacons = (void *)pkt->data;
1117 u16 id = (u16)le32_to_cpu(missed_beacons->mac_id);
1118
1119 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1120 IEEE80211_IFACE_ITER_NORMAL,
1121 iwl_mvm_beacon_loss_iterator,
1122 &id);
1123 return 0;
1124}
1125