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#include <net/mac80211.h>
63
64#include "iwl-debug.h"
65#include "iwl-io.h"
66#include "iwl-prph.h"
67#include "iwl-csr.h"
68#include "mvm.h"
69#include "fw/api/rs.h"
70
71
72
73
74
75int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
76{
77 int ret;
78
79#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
80 if (WARN_ON(mvm->d3_test_active))
81 return -EIO;
82#endif
83
84
85
86
87
88
89 if (!(cmd->flags & CMD_ASYNC))
90 lockdep_assert_held(&mvm->mutex);
91
92 ret = iwl_trans_send_cmd(mvm->trans, cmd);
93
94
95
96
97
98
99 if (cmd->flags & CMD_WANT_SKB)
100 return ret;
101
102
103 if (!ret || ret == -ERFKILL)
104 return 0;
105 return ret;
106}
107
108int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
109 u32 flags, u16 len, const void *data)
110{
111 struct iwl_host_cmd cmd = {
112 .id = id,
113 .len = { len, },
114 .data = { data, },
115 .flags = flags,
116 };
117
118 return iwl_mvm_send_cmd(mvm, &cmd);
119}
120
121
122
123
124int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
125 u32 *status)
126{
127 struct iwl_rx_packet *pkt;
128 struct iwl_cmd_response *resp;
129 int ret, resp_len;
130
131 lockdep_assert_held(&mvm->mutex);
132
133#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
134 if (WARN_ON(mvm->d3_test_active))
135 return -EIO;
136#endif
137
138
139
140
141
142 if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
143 "cmd flags %x", cmd->flags))
144 return -EINVAL;
145
146 cmd->flags |= CMD_WANT_SKB;
147
148 ret = iwl_trans_send_cmd(mvm->trans, cmd);
149 if (ret == -ERFKILL) {
150
151
152
153
154 return 0;
155 } else if (ret) {
156 return ret;
157 }
158
159 pkt = cmd->resp_pkt;
160
161 resp_len = iwl_rx_packet_payload_len(pkt);
162 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
163 ret = -EIO;
164 goto out_free_resp;
165 }
166
167 resp = (void *)pkt->data;
168 *status = le32_to_cpu(resp->status);
169 out_free_resp:
170 iwl_free_resp(cmd);
171 return ret;
172}
173
174
175
176
177int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
178 const void *data, u32 *status)
179{
180 struct iwl_host_cmd cmd = {
181 .id = id,
182 .len = { len, },
183 .data = { data, },
184 };
185
186 return iwl_mvm_send_cmd_status(mvm, &cmd, status);
187}
188
189#define IWL_DECLARE_RATE_INFO(r) \
190 [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
191
192
193
194
195static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
196 IWL_DECLARE_RATE_INFO(1),
197 IWL_DECLARE_RATE_INFO(2),
198 IWL_DECLARE_RATE_INFO(5),
199 IWL_DECLARE_RATE_INFO(11),
200 IWL_DECLARE_RATE_INFO(6),
201 IWL_DECLARE_RATE_INFO(9),
202 IWL_DECLARE_RATE_INFO(12),
203 IWL_DECLARE_RATE_INFO(18),
204 IWL_DECLARE_RATE_INFO(24),
205 IWL_DECLARE_RATE_INFO(36),
206 IWL_DECLARE_RATE_INFO(48),
207 IWL_DECLARE_RATE_INFO(54),
208};
209
210int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
211 enum nl80211_band band)
212{
213 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
214 int idx;
215 int band_offset = 0;
216
217
218 if (band != NL80211_BAND_2GHZ)
219 band_offset = IWL_FIRST_OFDM_RATE;
220 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
221 if (fw_rate_idx_to_plcp[idx] == rate)
222 return idx - band_offset;
223
224 return -1;
225}
226
227u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
228{
229
230 return fw_rate_idx_to_plcp[rate_idx];
231}
232
233u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)
234{
235 static const u8 mac80211_ac_to_ucode_ac[] = {
236 AC_VO,
237 AC_VI,
238 AC_BE,
239 AC_BK
240 };
241
242 return mac80211_ac_to_ucode_ac[ac];
243}
244
245void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
246{
247 struct iwl_rx_packet *pkt = rxb_addr(rxb);
248 struct iwl_error_resp *err_resp = (void *)pkt->data;
249
250 IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
251 le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
252 IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
253 le16_to_cpu(err_resp->bad_cmd_seq_num),
254 le32_to_cpu(err_resp->error_service));
255 IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",
256 le64_to_cpu(err_resp->timestamp));
257}
258
259
260
261
262
263u8 first_antenna(u8 mask)
264{
265 BUILD_BUG_ON(ANT_A != BIT(0));
266 if (WARN_ON_ONCE(!mask))
267 return BIT(0);
268 return BIT(ffs(mask) - 1);
269}
270
271
272
273
274
275
276
277u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
278{
279 u8 ind = last_idx;
280 int i;
281
282 for (i = 0; i < MAX_ANT_NUM; i++) {
283 ind = (ind + 1) % MAX_ANT_NUM;
284 if (valid & BIT(ind))
285 return ind;
286 }
287
288 WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
289 return last_idx;
290}
291
292#define FW_SYSASSERT_CPU_MASK 0xf0000000
293static const struct {
294 const char *name;
295 u8 num;
296} advanced_lookup[] = {
297 { "NMI_INTERRUPT_WDG", 0x34 },
298 { "SYSASSERT", 0x35 },
299 { "UCODE_VERSION_MISMATCH", 0x37 },
300 { "BAD_COMMAND", 0x38 },
301 { "BAD_COMMAND", 0x39 },
302 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
303 { "FATAL_ERROR", 0x3D },
304 { "NMI_TRM_HW_ERR", 0x46 },
305 { "NMI_INTERRUPT_TRM", 0x4C },
306 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
307 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
308 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
309 { "NMI_INTERRUPT_HOST", 0x66 },
310 { "NMI_INTERRUPT_LMAC_FATAL", 0x70 },
311 { "NMI_INTERRUPT_UMAC_FATAL", 0x71 },
312 { "NMI_INTERRUPT_OTHER_LMAC_FATAL", 0x73 },
313 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
314 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
315 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
316 { "ADVANCED_SYSASSERT", 0 },
317};
318
319static const char *desc_lookup(u32 num)
320{
321 int i;
322
323 for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
324 if (advanced_lookup[i].num == (num & ~FW_SYSASSERT_CPU_MASK))
325 return advanced_lookup[i].name;
326
327
328 return advanced_lookup[i].name;
329}
330
331
332
333
334
335
336
337struct iwl_error_event_table_v1 {
338 u32 valid;
339 u32 error_id;
340 u32 pc;
341 u32 blink1;
342 u32 blink2;
343 u32 ilink1;
344 u32 ilink2;
345 u32 data1;
346 u32 data2;
347 u32 data3;
348 u32 bcon_time;
349 u32 tsf_low;
350 u32 tsf_hi;
351 u32 gp1;
352 u32 gp2;
353 u32 gp3;
354 u32 ucode_ver;
355 u32 hw_ver;
356 u32 brd_ver;
357 u32 log_pc;
358 u32 frame_ptr;
359 u32 stack_ptr;
360 u32 hcmd;
361 u32 isr0;
362
363 u32 isr1;
364
365 u32 isr2;
366
367 u32 isr3;
368
369 u32 isr4;
370
371 u32 isr_pref;
372 u32 wait_event;
373 u32 l2p_control;
374 u32 l2p_duration;
375 u32 l2p_mhvalid;
376 u32 l2p_addr_match;
377 u32 lmpm_pmg_sel;
378
379 u32 u_timestamp;
380
381 u32 flow_handler;
382} __packed ;
383
384struct iwl_error_event_table {
385 u32 valid;
386 u32 error_id;
387 u32 trm_hw_status0;
388 u32 trm_hw_status1;
389 u32 blink2;
390 u32 ilink1;
391 u32 ilink2;
392 u32 data1;
393 u32 data2;
394 u32 data3;
395 u32 bcon_time;
396 u32 tsf_low;
397 u32 tsf_hi;
398 u32 gp1;
399 u32 gp2;
400 u32 fw_rev_type;
401 u32 major;
402 u32 minor;
403 u32 hw_ver;
404 u32 brd_ver;
405 u32 log_pc;
406 u32 frame_ptr;
407 u32 stack_ptr;
408 u32 hcmd;
409 u32 isr0;
410
411 u32 isr1;
412
413 u32 isr2;
414
415 u32 isr3;
416
417 u32 isr4;
418
419 u32 last_cmd_id;
420 u32 wait_event;
421 u32 l2p_control;
422 u32 l2p_duration;
423 u32 l2p_mhvalid;
424 u32 l2p_addr_match;
425 u32 lmpm_pmg_sel;
426
427 u32 u_timestamp;
428
429 u32 flow_handler;
430} __packed ;
431
432
433
434
435
436
437
438
439struct iwl_umac_error_event_table {
440 u32 valid;
441 u32 error_id;
442 u32 blink1;
443 u32 blink2;
444 u32 ilink1;
445 u32 ilink2;
446 u32 data1;
447 u32 data2;
448 u32 data3;
449 u32 umac_major;
450 u32 umac_minor;
451 u32 frame_pointer;
452 u32 stack_pointer;
453 u32 cmd_header;
454 u32 nic_isr_pref;
455} __packed;
456
457#define ERROR_START_OFFSET (1 * sizeof(u32))
458#define ERROR_ELEM_SIZE (7 * sizeof(u32))
459
460static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
461{
462 struct iwl_trans *trans = mvm->trans;
463 struct iwl_umac_error_event_table table;
464 u32 base = mvm->trans->dbg.umac_error_event_table;
465
466 if (!mvm->support_umac_log &&
467 !(mvm->trans->dbg.error_event_table_tlv_status &
468 IWL_ERROR_EVENT_TABLE_UMAC))
469 return;
470
471 iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
472
473 if (table.valid)
474 mvm->fwrt.dump.umac_err_id = table.error_id;
475
476 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
477 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
478 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
479 mvm->status, table.valid);
480 }
481
482 IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
483 desc_lookup(table.error_id));
484 IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
485 IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
486 IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
487 IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
488 IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
489 IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
490 IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
491 IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
492 IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
493 IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
494 IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
495 IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
496 IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
497}
498
499static void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u8 lmac_num)
500{
501 struct iwl_trans *trans = mvm->trans;
502 struct iwl_error_event_table table;
503 u32 val, base = mvm->trans->dbg.lmac_error_event_table[lmac_num];
504
505 if (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) {
506 if (!base)
507 base = mvm->fw->init_errlog_ptr;
508 } else {
509 if (!base)
510 base = mvm->fw->inst_errlog_ptr;
511 }
512
513 if (base < 0x400000) {
514 IWL_ERR(mvm,
515 "Not valid error log pointer 0x%08X for %s uCode\n",
516 base,
517 (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT)
518 ? "Init" : "RT");
519 return;
520 }
521
522
523 val = iwl_trans_read_mem32(trans, base);
524 if (((val & ~0xf) == 0xa5a5a5a0) || ((val & ~0xf) == 0x5a5a5a50)) {
525 int err;
526
527 IWL_ERR(trans, "HW error, resetting before reading\n");
528
529
530 iwl_trans_sw_reset(trans);
531
532 err = iwl_finish_nic_init(trans, trans->trans_cfg);
533 if (err)
534 return;
535 }
536
537 iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
538
539 if (table.valid)
540 mvm->fwrt.dump.lmac_err_id[lmac_num] = table.error_id;
541
542 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
543 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
544 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
545 mvm->status, table.valid);
546 }
547
548
549
550 IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
551
552 IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
553 desc_lookup(table.error_id));
554 IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
555 IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
556 IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
557 IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
558 IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
559 IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
560 IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
561 IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
562 IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
563 IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
564 IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
565 IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
566 IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
567 IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
568 IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
569 IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
570 IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
571 IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
572 IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
573 IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
574 IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
575 IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
576 IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
577 IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
578 IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
579 IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
580 IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
581 IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
582 IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
583 IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
584 IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
585 IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
586 IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
587}
588
589static void iwl_mvm_dump_iml_error_log(struct iwl_mvm *mvm)
590{
591 struct iwl_trans *trans = mvm->trans;
592 u32 error;
593
594 error = iwl_read_umac_prph(trans, UMAG_SB_CPU_2_STATUS);
595
596 IWL_ERR(trans, "IML/ROM dump:\n");
597
598 if (error & 0xFFFF0000)
599 IWL_ERR(trans, "IML/ROM SYSASSERT:\n");
600
601 IWL_ERR(mvm, "0x%08X | IML/ROM error/state\n", error);
602 IWL_ERR(mvm, "0x%08X | IML/ROM data1\n",
603 iwl_read_umac_prph(trans, UMAG_SB_CPU_1_STATUS));
604}
605
606void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
607{
608 if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
609 IWL_ERR(mvm,
610 "DEVICE_ENABLED bit is not set. Aborting dump.\n");
611 return;
612 }
613
614 iwl_mvm_dump_lmac_error_log(mvm, 0);
615
616 if (mvm->trans->dbg.lmac_error_event_table[1])
617 iwl_mvm_dump_lmac_error_log(mvm, 1);
618
619 iwl_mvm_dump_umac_error_log(mvm);
620
621 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
622 iwl_mvm_dump_iml_error_log(mvm);
623
624 iwl_fw_error_print_fseq_regs(&mvm->fwrt);
625}
626
627int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
628 int tid, int frame_limit, u16 ssn)
629{
630 struct iwl_scd_txq_cfg_cmd cmd = {
631 .scd_queue = queue,
632 .action = SCD_CFG_ENABLE_QUEUE,
633 .window = frame_limit,
634 .sta_id = sta_id,
635 .ssn = cpu_to_le16(ssn),
636 .tx_fifo = fifo,
637 .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
638 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
639 .tid = tid,
640 };
641 int ret;
642
643 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
644 return -EINVAL;
645
646 if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
647 "Trying to reconfig unallocated queue %d\n", queue))
648 return -ENXIO;
649
650 IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
651
652 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
653 WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
654 queue, fifo, ret);
655
656 return ret;
657}
658
659
660
661
662
663
664
665
666
667
668int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)
669{
670 struct iwl_host_cmd cmd = {
671 .id = LQ_CMD,
672 .len = { sizeof(struct iwl_lq_cmd), },
673 .flags = CMD_ASYNC,
674 .data = { lq, },
675 };
676
677 if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
678 iwl_mvm_has_tlc_offload(mvm)))
679 return -EINVAL;
680
681 return iwl_mvm_send_cmd(mvm, &cmd);
682}
683
684
685
686
687
688
689
690
691
692void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
693 enum iwl_mvm_smps_type_request req_type,
694 enum ieee80211_smps_mode smps_request)
695{
696 struct iwl_mvm_vif *mvmvif;
697 enum ieee80211_smps_mode smps_mode;
698 int i;
699
700 lockdep_assert_held(&mvm->mutex);
701
702
703 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
704 return;
705
706 if (vif->type == NL80211_IFTYPE_AP)
707 smps_mode = IEEE80211_SMPS_OFF;
708 else
709 smps_mode = IEEE80211_SMPS_AUTOMATIC;
710
711 mvmvif = iwl_mvm_vif_from_mac80211(vif);
712 mvmvif->smps_requests[req_type] = smps_request;
713 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
714 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
715 smps_mode = IEEE80211_SMPS_STATIC;
716 break;
717 }
718 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
719 smps_mode = IEEE80211_SMPS_DYNAMIC;
720 }
721
722 ieee80211_request_smps(vif, smps_mode);
723}
724
725int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
726{
727 struct iwl_statistics_cmd scmd = {
728 .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
729 };
730 struct iwl_host_cmd cmd = {
731 .id = STATISTICS_CMD,
732 .len[0] = sizeof(scmd),
733 .data[0] = &scmd,
734 .flags = CMD_WANT_SKB,
735 };
736 int ret;
737
738 ret = iwl_mvm_send_cmd(mvm, &cmd);
739 if (ret)
740 return ret;
741
742 iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
743 iwl_free_resp(&cmd);
744
745 if (clear)
746 iwl_mvm_accu_radio_stats(mvm);
747
748 return 0;
749}
750
751void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
752{
753 mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
754 mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
755 mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
756 mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
757}
758
759static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
760 struct ieee80211_vif *vif)
761{
762 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
763 bool *result = _data;
764 int i;
765
766 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
767 if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
768 mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
769 *result = false;
770 }
771}
772
773bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
774{
775 bool result = true;
776
777 lockdep_assert_held(&mvm->mutex);
778
779 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
780 return false;
781
782 if (mvm->cfg->rx_with_siso_diversity)
783 return false;
784
785 ieee80211_iterate_active_interfaces_atomic(
786 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
787 iwl_mvm_diversity_iter, &result);
788
789 return result;
790}
791
792void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
793 bool low_latency, u16 mac_id)
794{
795 struct iwl_mac_low_latency_cmd cmd = {
796 .mac_id = cpu_to_le32(mac_id)
797 };
798
799 if (!fw_has_capa(&mvm->fw->ucode_capa,
800 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
801 return;
802
803 if (low_latency) {
804
805 cmd.low_latency_rx = 1;
806 cmd.low_latency_tx = 1;
807 }
808
809 if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(LOW_LATENCY_CMD,
810 MAC_CONF_GROUP, 0),
811 0, sizeof(cmd), &cmd))
812 IWL_ERR(mvm, "Failed to send low latency command\n");
813}
814
815int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
816 bool low_latency,
817 enum iwl_mvm_low_latency_cause cause)
818{
819 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
820 int res;
821 bool prev;
822
823 lockdep_assert_held(&mvm->mutex);
824
825 prev = iwl_mvm_vif_low_latency(mvmvif);
826 iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
827
828 low_latency = iwl_mvm_vif_low_latency(mvmvif);
829
830 if (low_latency == prev)
831 return 0;
832
833 iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
834
835 res = iwl_mvm_update_quotas(mvm, false, NULL);
836 if (res)
837 return res;
838
839 iwl_mvm_bt_coex_vif_change(mvm);
840
841 return iwl_mvm_power_update_mac(mvm);
842}
843
844struct iwl_mvm_low_latency_iter {
845 bool result;
846 bool result_per_band[NUM_NL80211_BANDS];
847};
848
849static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
850{
851 struct iwl_mvm_low_latency_iter *result = _data;
852 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
853 enum nl80211_band band;
854
855 if (iwl_mvm_vif_low_latency(mvmvif)) {
856 result->result = true;
857
858 if (!mvmvif->phy_ctxt)
859 return;
860
861 band = mvmvif->phy_ctxt->channel->band;
862 result->result_per_band[band] = true;
863 }
864}
865
866bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
867{
868 struct iwl_mvm_low_latency_iter data = {};
869
870 ieee80211_iterate_active_interfaces_atomic(
871 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
872 iwl_mvm_ll_iter, &data);
873
874 return data.result;
875}
876
877bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
878{
879 struct iwl_mvm_low_latency_iter data = {};
880
881 ieee80211_iterate_active_interfaces_atomic(
882 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
883 iwl_mvm_ll_iter, &data);
884
885 return data.result_per_band[band];
886}
887
888struct iwl_bss_iter_data {
889 struct ieee80211_vif *vif;
890 bool error;
891};
892
893static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
894 struct ieee80211_vif *vif)
895{
896 struct iwl_bss_iter_data *data = _data;
897
898 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
899 return;
900
901 if (data->vif) {
902 data->error = true;
903 return;
904 }
905
906 data->vif = vif;
907}
908
909struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
910{
911 struct iwl_bss_iter_data bss_iter_data = {};
912
913 ieee80211_iterate_active_interfaces_atomic(
914 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
915 iwl_mvm_bss_iface_iterator, &bss_iter_data);
916
917 if (bss_iter_data.error) {
918 IWL_ERR(mvm, "More than one managed interface active!\n");
919 return ERR_PTR(-EINVAL);
920 }
921
922 return bss_iter_data.vif;
923}
924
925struct iwl_sta_iter_data {
926 bool assoc;
927};
928
929static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
930 struct ieee80211_vif *vif)
931{
932 struct iwl_sta_iter_data *data = _data;
933
934 if (vif->type != NL80211_IFTYPE_STATION)
935 return;
936
937 if (vif->bss_conf.assoc)
938 data->assoc = true;
939}
940
941bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
942{
943 struct iwl_sta_iter_data data = {
944 .assoc = false,
945 };
946
947 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
948 IEEE80211_IFACE_ITER_NORMAL,
949 iwl_mvm_sta_iface_iterator,
950 &data);
951 return data.assoc;
952}
953
954unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
955 struct ieee80211_vif *vif,
956 bool tdls, bool cmd_q)
957{
958 struct iwl_fw_dbg_trigger_tlv *trigger;
959 struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
960 unsigned int default_timeout = cmd_q ?
961 IWL_DEF_WD_TIMEOUT :
962 mvm->trans->trans_cfg->base_params->wd_timeout;
963
964 if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
965
966
967
968
969 if (fw_has_capa(&mvm->fw->ucode_capa,
970 IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
971 vif && vif->type == NL80211_IFTYPE_AP)
972 return IWL_WATCHDOG_DISABLED;
973 return default_timeout;
974 }
975
976 trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
977 txq_timer = (void *)trigger->data;
978
979 if (tdls)
980 return le32_to_cpu(txq_timer->tdls);
981
982 if (cmd_q)
983 return le32_to_cpu(txq_timer->command_queue);
984
985 if (WARN_ON(!vif))
986 return default_timeout;
987
988 switch (ieee80211_vif_type_p2p(vif)) {
989 case NL80211_IFTYPE_ADHOC:
990 return le32_to_cpu(txq_timer->ibss);
991 case NL80211_IFTYPE_STATION:
992 return le32_to_cpu(txq_timer->bss);
993 case NL80211_IFTYPE_AP:
994 return le32_to_cpu(txq_timer->softap);
995 case NL80211_IFTYPE_P2P_CLIENT:
996 return le32_to_cpu(txq_timer->p2p_client);
997 case NL80211_IFTYPE_P2P_GO:
998 return le32_to_cpu(txq_timer->p2p_go);
999 case NL80211_IFTYPE_P2P_DEVICE:
1000 return le32_to_cpu(txq_timer->p2p_device);
1001 case NL80211_IFTYPE_MONITOR:
1002 return default_timeout;
1003 default:
1004 WARN_ON(1);
1005 return mvm->trans->trans_cfg->base_params->wd_timeout;
1006 }
1007}
1008
1009void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1010 const char *errmsg)
1011{
1012 struct iwl_fw_dbg_trigger_tlv *trig;
1013 struct iwl_fw_dbg_trigger_mlme *trig_mlme;
1014
1015 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1016 FW_DBG_TRIGGER_MLME);
1017 if (!trig)
1018 goto out;
1019
1020 trig_mlme = (void *)trig->data;
1021
1022 if (trig_mlme->stop_connection_loss &&
1023 --trig_mlme->stop_connection_loss)
1024 goto out;
1025
1026 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
1027
1028out:
1029 ieee80211_connection_loss(vif);
1030}
1031
1032void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
1033 struct ieee80211_vif *vif,
1034 const struct ieee80211_sta *sta,
1035 u16 tid)
1036{
1037 struct iwl_fw_dbg_trigger_tlv *trig;
1038 struct iwl_fw_dbg_trigger_ba *ba_trig;
1039
1040 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
1041 FW_DBG_TRIGGER_BA);
1042 if (!trig)
1043 return;
1044
1045 ba_trig = (void *)trig->data;
1046
1047 if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
1048 return;
1049
1050 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1051 "Frame from %pM timed out, tid %d",
1052 sta->addr, tid);
1053}
1054
1055u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
1056{
1057 if (!elapsed)
1058 return 0;
1059
1060 return (100 * airtime / elapsed) / USEC_PER_MSEC;
1061}
1062
1063static enum iwl_mvm_traffic_load
1064iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
1065{
1066 u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
1067
1068 if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
1069 return IWL_MVM_TRAFFIC_HIGH;
1070 if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
1071 return IWL_MVM_TRAFFIC_MEDIUM;
1072
1073 return IWL_MVM_TRAFFIC_LOW;
1074}
1075
1076struct iwl_mvm_tcm_iter_data {
1077 struct iwl_mvm *mvm;
1078 bool any_sent;
1079};
1080
1081static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
1082{
1083 struct iwl_mvm_tcm_iter_data *data = _data;
1084 struct iwl_mvm *mvm = data->mvm;
1085 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1086 bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
1087
1088 if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
1089 return;
1090
1091 low_latency = mvm->tcm.result.low_latency[mvmvif->id];
1092
1093 if (!mvm->tcm.result.change[mvmvif->id] &&
1094 prev == low_latency) {
1095 iwl_mvm_update_quotas(mvm, false, NULL);
1096 return;
1097 }
1098
1099 if (prev != low_latency) {
1100
1101 iwl_mvm_update_low_latency(mvm, vif, low_latency,
1102 LOW_LATENCY_TRAFFIC);
1103 } else {
1104 iwl_mvm_update_quotas(mvm, false, NULL);
1105 }
1106
1107 data->any_sent = true;
1108}
1109
1110static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
1111{
1112 struct iwl_mvm_tcm_iter_data data = {
1113 .mvm = mvm,
1114 .any_sent = false,
1115 };
1116
1117 mutex_lock(&mvm->mutex);
1118
1119 ieee80211_iterate_active_interfaces(
1120 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1121 iwl_mvm_tcm_iter, &data);
1122
1123 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1124 iwl_mvm_config_scan(mvm);
1125
1126 mutex_unlock(&mvm->mutex);
1127}
1128
1129static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
1130{
1131 struct iwl_mvm *mvm;
1132 struct iwl_mvm_vif *mvmvif;
1133 struct ieee80211_vif *vif;
1134
1135 mvmvif = container_of(wk, struct iwl_mvm_vif,
1136 uapsd_nonagg_detected_wk.work);
1137 vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1138 mvm = mvmvif->mvm;
1139
1140 if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
1141 return;
1142
1143
1144 memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
1145 vif->bss_conf.bssid, ETH_ALEN);
1146 mvm->uapsd_noagg_bssid_write_idx++;
1147 if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
1148 mvm->uapsd_noagg_bssid_write_idx = 0;
1149
1150 iwl_mvm_connection_loss(mvm, vif,
1151 "AP isn't using AMPDU with uAPSD enabled");
1152}
1153
1154static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
1155 struct ieee80211_vif *vif)
1156{
1157 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1158
1159 if (vif->type != NL80211_IFTYPE_STATION)
1160 return;
1161
1162 if (!vif->bss_conf.assoc)
1163 return;
1164
1165 if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
1166 !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
1167 !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
1168 !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
1169 return;
1170
1171 if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
1172 return;
1173
1174 mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
1175 IWL_INFO(mvm,
1176 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
1177 schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
1178}
1179
1180static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
1181 unsigned int elapsed,
1182 int mac)
1183{
1184 u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
1185 u64 tpt;
1186 unsigned long rate;
1187 struct ieee80211_vif *vif;
1188
1189 rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
1190
1191 if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
1192 mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
1193 return;
1194
1195 if (iwl_mvm_has_new_rx_api(mvm)) {
1196 tpt = 8 * bytes;
1197 do_div(tpt, elapsed);
1198 rate *= 1000;
1199 if (tpt < 22 * rate / 100)
1200 return;
1201 } else {
1202
1203
1204
1205
1206
1207
1208
1209
1210 tpt = (8 * bytes);
1211 do_div(tpt, elapsed * 100);
1212 if (tpt < rate)
1213 return;
1214 }
1215
1216 rcu_read_lock();
1217 vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
1218 if (vif)
1219 iwl_mvm_uapsd_agg_disconnect(mvm, vif);
1220 rcu_read_unlock();
1221}
1222
1223static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
1224 struct ieee80211_vif *vif)
1225{
1226 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1227 u32 *band = _data;
1228
1229 if (!mvmvif->phy_ctxt)
1230 return;
1231
1232 band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
1233}
1234
1235static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
1236 unsigned long ts,
1237 bool handle_uapsd)
1238{
1239 unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
1240 unsigned int uapsd_elapsed =
1241 jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
1242 u32 total_airtime = 0;
1243 u32 band_airtime[NUM_NL80211_BANDS] = {0};
1244 u32 band[NUM_MAC_INDEX_DRIVER] = {0};
1245 int ac, mac, i;
1246 bool low_latency = false;
1247 enum iwl_mvm_traffic_load load, band_load;
1248 bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
1249
1250 if (handle_ll)
1251 mvm->tcm.ll_ts = ts;
1252 if (handle_uapsd)
1253 mvm->tcm.uapsd_nonagg_ts = ts;
1254
1255 mvm->tcm.result.elapsed = elapsed;
1256
1257 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1258 IEEE80211_IFACE_ITER_NORMAL,
1259 iwl_mvm_tcm_iterator,
1260 &band);
1261
1262 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1263 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1264 u32 vo_vi_pkts = 0;
1265 u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1266
1267 total_airtime += airtime;
1268 band_airtime[band[mac]] += airtime;
1269
1270 load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1271 mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1272 mvm->tcm.result.load[mac] = load;
1273 mvm->tcm.result.airtime[mac] = airtime;
1274
1275 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1276 vo_vi_pkts += mdata->rx.pkts[ac] +
1277 mdata->tx.pkts[ac];
1278
1279
1280 if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1281 mvm->tcm.result.low_latency[mac] = true;
1282 else if (handle_ll)
1283 mvm->tcm.result.low_latency[mac] = false;
1284
1285 if (handle_ll) {
1286
1287 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1288 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1289 }
1290 low_latency |= mvm->tcm.result.low_latency[mac];
1291
1292 if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1293 iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1294 mac);
1295
1296 if (handle_uapsd)
1297 mdata->uapsd_nonagg_detect.rx_bytes = 0;
1298 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1299 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1300 }
1301
1302 load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1303 mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
1304 mvm->tcm.result.global_load = load;
1305
1306 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1307 band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1308 mvm->tcm.result.band_load[i] = band_load;
1309 }
1310
1311
1312
1313
1314
1315
1316
1317 if (load != IWL_MVM_TRAFFIC_LOW)
1318 return MVM_TCM_PERIOD;
1319
1320
1321
1322
1323
1324 if (low_latency)
1325 return MVM_LL_PERIOD;
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337 return 0;
1338}
1339
1340void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1341{
1342 unsigned long ts = jiffies;
1343 bool handle_uapsd =
1344 time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1345 msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
1346
1347 spin_lock(&mvm->tcm.lock);
1348 if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1349 spin_unlock(&mvm->tcm.lock);
1350 return;
1351 }
1352 spin_unlock(&mvm->tcm.lock);
1353
1354 if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1355 mutex_lock(&mvm->mutex);
1356 if (iwl_mvm_request_statistics(mvm, true))
1357 handle_uapsd = false;
1358 mutex_unlock(&mvm->mutex);
1359 }
1360
1361 spin_lock(&mvm->tcm.lock);
1362
1363 if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1364
1365 unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1366 handle_uapsd);
1367
1368
1369 smp_mb();
1370 mvm->tcm.ts = ts;
1371 if (work_delay)
1372 schedule_delayed_work(&mvm->tcm.work, work_delay);
1373 }
1374 spin_unlock(&mvm->tcm.lock);
1375
1376 iwl_mvm_tcm_results(mvm);
1377}
1378
1379void iwl_mvm_tcm_work(struct work_struct *work)
1380{
1381 struct delayed_work *delayed_work = to_delayed_work(work);
1382 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1383 tcm.work);
1384
1385 iwl_mvm_recalc_tcm(mvm);
1386}
1387
1388void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1389{
1390 spin_lock_bh(&mvm->tcm.lock);
1391 mvm->tcm.paused = true;
1392 spin_unlock_bh(&mvm->tcm.lock);
1393 if (with_cancel)
1394 cancel_delayed_work_sync(&mvm->tcm.work);
1395}
1396
1397void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1398{
1399 int mac;
1400 bool low_latency = false;
1401
1402 spin_lock_bh(&mvm->tcm.lock);
1403 mvm->tcm.ts = jiffies;
1404 mvm->tcm.ll_ts = jiffies;
1405 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1406 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1407
1408 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1409 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1410 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1411 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1412
1413 if (mvm->tcm.result.low_latency[mac])
1414 low_latency = true;
1415 }
1416
1417 smp_mb();
1418 mvm->tcm.paused = false;
1419
1420
1421
1422
1423
1424 if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
1425 schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
1426 else if (low_latency)
1427 schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
1428
1429 spin_unlock_bh(&mvm->tcm.lock);
1430}
1431
1432void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1433{
1434 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1435
1436 INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1437 iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1438}
1439
1440void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1441{
1442 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1443
1444 cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1445}
1446
1447u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
1448{
1449 u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
1450
1451 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
1452 mvm->trans->cfg->gp2_reg_addr)
1453 reg_addr = mvm->trans->cfg->gp2_reg_addr;
1454
1455 return iwl_read_prph(mvm->trans, reg_addr);
1456}
1457
1458void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
1459{
1460 bool ps_disabled;
1461
1462 lockdep_assert_held(&mvm->mutex);
1463
1464
1465 ps_disabled = mvm->ps_disabled;
1466 if (!ps_disabled) {
1467 mvm->ps_disabled = true;
1468 iwl_mvm_power_update_device(mvm);
1469 }
1470
1471 *gp2 = iwl_mvm_get_systime(mvm);
1472 *boottime = ktime_get_boottime_ns();
1473
1474 if (!ps_disabled) {
1475 mvm->ps_disabled = ps_disabled;
1476 iwl_mvm_power_update_device(mvm);
1477 }
1478}
1479