1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2025 Intel Corporation
4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5 * Copyright (C) 2015-2017 Intel Deutschland GmbH
6 */
7 #include <net/mac80211.h>
8
9 #include "iwl-debug.h"
10 #include "iwl-io.h"
11 #include "iwl-prph.h"
12 #include "iwl-csr.h"
13 #include "mvm.h"
14 #include "fw/api/rs.h"
15 #include "fw/img.h"
16
17 /*
18 * Will return 0 even if the cmd failed when RFKILL is asserted unless
19 * CMD_WANT_SKB is set in cmd->flags.
20 */
iwl_mvm_send_cmd(struct iwl_mvm * mvm,struct iwl_host_cmd * cmd)21 int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
22 {
23 int ret;
24
25 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
26 if (WARN_ON(mvm->d3_test_active))
27 return -EIO;
28 #endif
29
30 /*
31 * Synchronous commands from this op-mode must hold
32 * the mutex, this ensures we don't try to send two
33 * (or more) synchronous commands at a time.
34 */
35 if (!(cmd->flags & CMD_ASYNC))
36 lockdep_assert_held(&mvm->mutex);
37
38 ret = iwl_trans_send_cmd(mvm->trans, cmd);
39
40 /*
41 * If the caller wants the SKB, then don't hide any problems, the
42 * caller might access the response buffer which will be NULL if
43 * the command failed.
44 */
45 if (cmd->flags & CMD_WANT_SKB)
46 return ret;
47
48 /*
49 * Silently ignore failures if RFKILL is asserted or
50 * we are in suspend\resume process
51 */
52 if (!ret || ret == -ERFKILL || ret == -EHOSTDOWN)
53 return 0;
54 return ret;
55 }
56
iwl_mvm_send_cmd_pdu(struct iwl_mvm * mvm,u32 id,u32 flags,u16 len,const void * data)57 int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
58 u32 flags, u16 len, const void *data)
59 {
60 struct iwl_host_cmd cmd = {
61 .id = id,
62 .len = { len, },
63 .data = { data, },
64 .flags = flags,
65 };
66
67 return iwl_mvm_send_cmd(mvm, &cmd);
68 }
69
70 /*
71 * We assume that the caller set the status to the success value
72 */
iwl_mvm_send_cmd_status(struct iwl_mvm * mvm,struct iwl_host_cmd * cmd,u32 * status)73 int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
74 u32 *status)
75 {
76 struct iwl_rx_packet *pkt;
77 struct iwl_cmd_response *resp;
78 int ret, resp_len;
79
80 lockdep_assert_held(&mvm->mutex);
81
82 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
83 if (WARN_ON(mvm->d3_test_active))
84 return -EIO;
85 #endif
86
87 /*
88 * Only synchronous commands can wait for status,
89 * we use WANT_SKB so the caller can't.
90 */
91 if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
92 "cmd flags %x", cmd->flags))
93 return -EINVAL;
94
95 cmd->flags |= CMD_WANT_SKB;
96
97 ret = iwl_trans_send_cmd(mvm->trans, cmd);
98 if (ret == -ERFKILL) {
99 /*
100 * The command failed because of RFKILL, don't update
101 * the status, leave it as success and return 0.
102 */
103 return 0;
104 } else if (ret) {
105 return ret;
106 }
107
108 pkt = cmd->resp_pkt;
109
110 resp_len = iwl_rx_packet_payload_len(pkt);
111 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
112 ret = -EIO;
113 goto out_free_resp;
114 }
115
116 resp = (void *)pkt->data;
117 *status = le32_to_cpu(resp->status);
118 out_free_resp:
119 iwl_free_resp(cmd);
120 return ret;
121 }
122
123 /*
124 * We assume that the caller set the status to the sucess value
125 */
iwl_mvm_send_cmd_pdu_status(struct iwl_mvm * mvm,u32 id,u16 len,const void * data,u32 * status)126 int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
127 const void *data, u32 *status)
128 {
129 struct iwl_host_cmd cmd = {
130 .id = id,
131 .len = { len, },
132 .data = { data, },
133 };
134
135 return iwl_mvm_send_cmd_status(mvm, &cmd, status);
136 }
137
iwl_mvm_legacy_hw_idx_to_mac80211_idx(u32 rate_n_flags,enum nl80211_band band)138 int iwl_mvm_legacy_hw_idx_to_mac80211_idx(u32 rate_n_flags,
139 enum nl80211_band band)
140 {
141 int format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
142 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
143 bool is_LB = band == NL80211_BAND_2GHZ;
144
145 if (format == RATE_MCS_MOD_TYPE_LEGACY_OFDM)
146 return is_LB ? rate + IWL_FIRST_OFDM_RATE :
147 rate;
148
149 /* CCK is not allowed in HB */
150 return is_LB ? rate : -1;
151 }
152
iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,enum nl80211_band band)153 int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
154 enum nl80211_band band)
155 {
156 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1;
157 int idx;
158 int band_offset = 0;
159
160 /* Legacy rate format, search for match in table */
161 if (band != NL80211_BAND_2GHZ)
162 band_offset = IWL_FIRST_OFDM_RATE;
163 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
164 if (iwl_fw_rate_idx_to_plcp(idx) == rate)
165 return idx - band_offset;
166
167 return -1;
168 }
169
iwl_mvm_mac80211_idx_to_hwrate(const struct iwl_fw * fw,int rate_idx)170 u8 iwl_mvm_mac80211_idx_to_hwrate(const struct iwl_fw *fw, int rate_idx)
171 {
172 return (rate_idx >= IWL_FIRST_OFDM_RATE ?
173 rate_idx - IWL_FIRST_OFDM_RATE :
174 rate_idx);
175 }
176
iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)177 u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)
178 {
179 static const u8 mac80211_ac_to_ucode_ac[] = {
180 AC_VO,
181 AC_VI,
182 AC_BE,
183 AC_BK
184 };
185
186 return mac80211_ac_to_ucode_ac[ac];
187 }
188
iwl_mvm_rx_fw_error(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)189 void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
190 {
191 struct iwl_rx_packet *pkt = rxb_addr(rxb);
192 struct iwl_error_resp *err_resp = (void *)pkt->data;
193
194 IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
195 le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
196 IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
197 le16_to_cpu(err_resp->bad_cmd_seq_num),
198 le32_to_cpu(err_resp->error_service));
199 IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",
200 le64_to_cpu(err_resp->timestamp));
201 }
202
203 /*
204 * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
205 * The parameter should also be a combination of ANT_[ABC].
206 */
first_antenna(u8 mask)207 u8 first_antenna(u8 mask)
208 {
209 BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
210 if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
211 return BIT(0);
212 return BIT(ffs(mask) - 1);
213 }
214
215 #define MAX_ANT_NUM 2
216 /*
217 * Toggles between TX antennas to send the probe request on.
218 * Receives the bitmask of valid TX antennas and the *index* used
219 * for the last TX, and returns the next valid *index* to use.
220 * In order to set it in the tx_cmd, must do BIT(idx).
221 */
iwl_mvm_next_antenna(struct iwl_mvm * mvm,u8 valid,u8 last_idx)222 u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
223 {
224 u8 ind = last_idx;
225 int i;
226
227 for (i = 0; i < MAX_ANT_NUM; i++) {
228 ind = (ind + 1) % MAX_ANT_NUM;
229 if (valid & BIT(ind))
230 return ind;
231 }
232
233 WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
234 return last_idx;
235 }
236
237 /**
238 * iwl_mvm_send_lq_cmd() - Send link quality command
239 * @mvm: Driver data.
240 * @lq: Link quality command to send.
241 *
242 * The link quality command is sent as the last step of station creation.
243 * This is the special case in which init is set and we call a callback in
244 * this case to clear the state indicating that station creation is in
245 * progress.
246 *
247 * Returns: an error code indicating success or failure
248 */
iwl_mvm_send_lq_cmd(struct iwl_mvm * mvm,struct iwl_lq_cmd * lq)249 int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)
250 {
251 struct iwl_host_cmd cmd = {
252 .id = LQ_CMD,
253 .len = { sizeof(struct iwl_lq_cmd), },
254 .flags = CMD_ASYNC,
255 .data = { lq, },
256 };
257
258 if (WARN_ON(lq->sta_id == IWL_INVALID_STA ||
259 iwl_mvm_has_tlc_offload(mvm)))
260 return -EINVAL;
261
262 return iwl_mvm_send_cmd(mvm, &cmd);
263 }
264
265 /**
266 * iwl_mvm_update_smps - Get a request to change the SMPS mode
267 * @mvm: Driver data.
268 * @vif: Pointer to the ieee80211_vif structure
269 * @req_type: The part of the driver who call for a change.
270 * @smps_request: The request to change the SMPS mode.
271 * @link_id: for MLO link_id, otherwise 0 (deflink)
272 *
273 * Get a requst to change the SMPS mode,
274 * and change it according to all other requests in the driver.
275 */
iwl_mvm_update_smps(struct iwl_mvm * mvm,struct ieee80211_vif * vif,enum iwl_mvm_smps_type_request req_type,enum ieee80211_smps_mode smps_request,unsigned int link_id)276 void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
277 enum iwl_mvm_smps_type_request req_type,
278 enum ieee80211_smps_mode smps_request,
279 unsigned int link_id)
280 {
281 struct iwl_mvm_vif *mvmvif;
282 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
283 int i;
284
285 lockdep_assert_held(&mvm->mutex);
286
287 /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
288 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
289 return;
290
291 if (vif->type != NL80211_IFTYPE_STATION)
292 return;
293
294 /* SMPS is handled by firmware */
295 if (iwl_mvm_has_rlc_offload(mvm))
296 return;
297
298 mvmvif = iwl_mvm_vif_from_mac80211(vif);
299
300 if (WARN_ON_ONCE(!mvmvif->link[link_id]))
301 return;
302
303 mvmvif->link[link_id]->smps_requests[req_type] = smps_request;
304 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
305 if (mvmvif->link[link_id]->smps_requests[i] ==
306 IEEE80211_SMPS_STATIC) {
307 smps_mode = IEEE80211_SMPS_STATIC;
308 break;
309 }
310 if (mvmvif->link[link_id]->smps_requests[i] ==
311 IEEE80211_SMPS_DYNAMIC)
312 smps_mode = IEEE80211_SMPS_DYNAMIC;
313 }
314
315 /* SMPS is disabled in eSR */
316 if (mvmvif->esr_active)
317 smps_mode = IEEE80211_SMPS_OFF;
318
319 ieee80211_request_smps(vif, link_id, smps_mode);
320 }
321
iwl_mvm_update_smps_on_active_links(struct iwl_mvm * mvm,struct ieee80211_vif * vif,enum iwl_mvm_smps_type_request req_type,enum ieee80211_smps_mode smps_request)322 void iwl_mvm_update_smps_on_active_links(struct iwl_mvm *mvm,
323 struct ieee80211_vif *vif,
324 enum iwl_mvm_smps_type_request req_type,
325 enum ieee80211_smps_mode smps_request)
326 {
327 struct ieee80211_bss_conf *link_conf;
328 unsigned int link_id;
329
330 rcu_read_lock();
331 for_each_vif_active_link(vif, link_conf, link_id)
332 iwl_mvm_update_smps(mvm, vif, req_type, smps_request,
333 link_id);
334 rcu_read_unlock();
335 }
336
iwl_wait_stats_complete(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)337 static bool iwl_wait_stats_complete(struct iwl_notif_wait_data *notif_wait,
338 struct iwl_rx_packet *pkt, void *data)
339 {
340 WARN_ON(pkt->hdr.cmd != STATISTICS_NOTIFICATION);
341
342 return true;
343 }
344
345 #define PERIODIC_STAT_RATE 5
346
iwl_mvm_request_periodic_system_statistics(struct iwl_mvm * mvm,bool enable)347 int iwl_mvm_request_periodic_system_statistics(struct iwl_mvm *mvm, bool enable)
348 {
349 u32 flags = enable ? 0 : IWL_STATS_CFG_FLG_DISABLE_NTFY_MSK;
350 u32 type = enable ? (IWL_STATS_NTFY_TYPE_ID_OPER |
351 IWL_STATS_NTFY_TYPE_ID_OPER_PART1) : 0;
352 struct iwl_system_statistics_cmd system_cmd = {
353 .cfg_mask = cpu_to_le32(flags),
354 .config_time_sec = cpu_to_le32(enable ?
355 PERIODIC_STAT_RATE : 0),
356 .type_id_mask = cpu_to_le32(type),
357 };
358
359 return iwl_mvm_send_cmd_pdu(mvm,
360 WIDE_ID(SYSTEM_GROUP,
361 SYSTEM_STATISTICS_CMD),
362 0, sizeof(system_cmd), &system_cmd);
363 }
364
iwl_mvm_request_system_statistics(struct iwl_mvm * mvm,bool clear,u8 cmd_ver)365 static int iwl_mvm_request_system_statistics(struct iwl_mvm *mvm, bool clear,
366 u8 cmd_ver)
367 {
368 struct iwl_system_statistics_cmd system_cmd = {
369 .cfg_mask = clear ?
370 cpu_to_le32(IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK) :
371 cpu_to_le32(IWL_STATS_CFG_FLG_RESET_MSK |
372 IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK),
373 .type_id_mask = cpu_to_le32(IWL_STATS_NTFY_TYPE_ID_OPER |
374 IWL_STATS_NTFY_TYPE_ID_OPER_PART1),
375 };
376 struct iwl_host_cmd cmd = {
377 .id = WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_CMD),
378 .len[0] = sizeof(system_cmd),
379 .data[0] = &system_cmd,
380 };
381 struct iwl_notification_wait stats_wait;
382 static const u16 stats_complete[] = {
383 WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_END_NOTIF),
384 };
385 int ret;
386
387 if (cmd_ver != 1) {
388 IWL_FW_CHECK_FAILED(mvm,
389 "Invalid system statistics command version:%d\n",
390 cmd_ver);
391 return -EOPNOTSUPP;
392 }
393
394 iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,
395 stats_complete, ARRAY_SIZE(stats_complete),
396 NULL, NULL);
397
398 mvm->statistics_clear = clear;
399 ret = iwl_mvm_send_cmd(mvm, &cmd);
400 if (ret) {
401 iwl_remove_notification(&mvm->notif_wait, &stats_wait);
402 return ret;
403 }
404
405 /* 500ms for OPERATIONAL, PART1 and END notification should be enough
406 * for FW to collect data from all LMACs and send
407 * STATISTICS_NOTIFICATION to host
408 */
409 ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 2);
410 if (ret)
411 return ret;
412
413 if (clear)
414 iwl_mvm_accu_radio_stats(mvm);
415
416 return ret;
417 }
418
iwl_mvm_request_statistics(struct iwl_mvm * mvm,bool clear)419 int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
420 {
421 struct iwl_statistics_cmd scmd = {
422 .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
423 };
424
425 struct iwl_host_cmd cmd = {
426 .id = STATISTICS_CMD,
427 .len[0] = sizeof(scmd),
428 .data[0] = &scmd,
429 };
430 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
431 WIDE_ID(SYSTEM_GROUP,
432 SYSTEM_STATISTICS_CMD),
433 IWL_FW_CMD_VER_UNKNOWN);
434 int ret;
435
436 /*
437 * Don't request statistics during restart, they'll not have any useful
438 * information right after restart, nor is clearing needed
439 */
440 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
441 return 0;
442
443 if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
444 return iwl_mvm_request_system_statistics(mvm, clear, cmd_ver);
445
446 /* From version 15 - STATISTICS_NOTIFICATION, the reply for
447 * STATISTICS_CMD is empty, and the response is with
448 * STATISTICS_NOTIFICATION notification
449 */
450 if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
451 STATISTICS_NOTIFICATION, 0) < 15) {
452 cmd.flags = CMD_WANT_SKB;
453
454 ret = iwl_mvm_send_cmd(mvm, &cmd);
455 if (ret)
456 return ret;
457
458 iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
459 iwl_free_resp(&cmd);
460 } else {
461 struct iwl_notification_wait stats_wait;
462 static const u16 stats_complete[] = {
463 STATISTICS_NOTIFICATION,
464 };
465
466 iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,
467 stats_complete, ARRAY_SIZE(stats_complete),
468 iwl_wait_stats_complete, NULL);
469
470 ret = iwl_mvm_send_cmd(mvm, &cmd);
471 if (ret) {
472 iwl_remove_notification(&mvm->notif_wait, &stats_wait);
473 return ret;
474 }
475
476 /* 200ms should be enough for FW to collect data from all
477 * LMACs and send STATISTICS_NOTIFICATION to host
478 */
479 ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 5);
480 if (ret)
481 return ret;
482 }
483
484 if (clear)
485 iwl_mvm_accu_radio_stats(mvm);
486
487 return 0;
488 }
489
iwl_mvm_accu_radio_stats(struct iwl_mvm * mvm)490 void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
491 {
492 mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
493 mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
494 mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
495 mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
496 }
497
498 struct iwl_mvm_diversity_iter_data {
499 struct iwl_mvm_phy_ctxt *ctxt;
500 bool result;
501 };
502
iwl_mvm_diversity_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)503 static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
504 struct ieee80211_vif *vif)
505 {
506 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
507 struct iwl_mvm_diversity_iter_data *data = _data;
508 int i, link_id;
509
510 for_each_mvm_vif_valid_link(mvmvif, link_id) {
511 struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
512
513 if (link_info->phy_ctxt != data->ctxt)
514 continue;
515
516 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
517 if (link_info->smps_requests[i] == IEEE80211_SMPS_STATIC ||
518 link_info->smps_requests[i] == IEEE80211_SMPS_DYNAMIC) {
519 data->result = false;
520 break;
521 }
522 }
523 }
524 }
525
iwl_mvm_rx_diversity_allowed(struct iwl_mvm * mvm,struct iwl_mvm_phy_ctxt * ctxt)526 bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm,
527 struct iwl_mvm_phy_ctxt *ctxt)
528 {
529 struct iwl_mvm_diversity_iter_data data = {
530 .ctxt = ctxt,
531 .result = true,
532 };
533
534 lockdep_assert_held(&mvm->mutex);
535
536 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
537 return false;
538
539 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
540 return false;
541
542 if (mvm->cfg->rx_with_siso_diversity)
543 return false;
544
545 ieee80211_iterate_active_interfaces_atomic(
546 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
547 iwl_mvm_diversity_iter, &data);
548
549 return data.result;
550 }
551
iwl_mvm_send_low_latency_cmd(struct iwl_mvm * mvm,bool low_latency,u16 mac_id)552 void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
553 bool low_latency, u16 mac_id)
554 {
555 struct iwl_mac_low_latency_cmd cmd = {
556 .mac_id = cpu_to_le32(mac_id)
557 };
558
559 if (!fw_has_capa(&mvm->fw->ucode_capa,
560 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
561 return;
562
563 if (low_latency) {
564 /* currently we don't care about the direction */
565 cmd.low_latency_rx = 1;
566 cmd.low_latency_tx = 1;
567 }
568
569 if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, LOW_LATENCY_CMD),
570 0, sizeof(cmd), &cmd))
571 IWL_ERR(mvm, "Failed to send low latency command\n");
572 }
573
iwl_mvm_update_low_latency(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool low_latency,enum iwl_mvm_low_latency_cause cause)574 int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
575 bool low_latency,
576 enum iwl_mvm_low_latency_cause cause)
577 {
578 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
579 int res;
580 bool prev;
581
582 lockdep_assert_held(&mvm->mutex);
583
584 prev = iwl_mvm_vif_low_latency(mvmvif);
585 iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
586
587 low_latency = iwl_mvm_vif_low_latency(mvmvif);
588
589 if (low_latency == prev)
590 return 0;
591
592 iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
593
594 res = iwl_mvm_update_quotas(mvm, false, NULL);
595 if (res)
596 return res;
597
598 iwl_mvm_bt_coex_vif_change(mvm);
599
600 return iwl_mvm_power_update_mac(mvm);
601 }
602
603 struct iwl_mvm_low_latency_iter {
604 bool result;
605 bool result_per_band[NUM_NL80211_BANDS];
606 };
607
iwl_mvm_ll_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)608 static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
609 {
610 struct iwl_mvm_low_latency_iter *result = _data;
611 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
612 enum nl80211_band band;
613
614 if (iwl_mvm_vif_low_latency(mvmvif)) {
615 result->result = true;
616
617 if (!mvmvif->deflink.phy_ctxt)
618 return;
619
620 band = mvmvif->deflink.phy_ctxt->channel->band;
621 result->result_per_band[band] = true;
622 }
623 }
624
iwl_mvm_low_latency(struct iwl_mvm * mvm)625 bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
626 {
627 struct iwl_mvm_low_latency_iter data = {};
628
629 ieee80211_iterate_active_interfaces_atomic(
630 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
631 iwl_mvm_ll_iter, &data);
632
633 return data.result;
634 }
635
iwl_mvm_low_latency_band(struct iwl_mvm * mvm,enum nl80211_band band)636 bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
637 {
638 struct iwl_mvm_low_latency_iter data = {};
639
640 ieee80211_iterate_active_interfaces_atomic(
641 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
642 iwl_mvm_ll_iter, &data);
643
644 return data.result_per_band[band];
645 }
646
647 struct iwl_bss_iter_data {
648 struct ieee80211_vif *vif;
649 bool error;
650 };
651
iwl_mvm_bss_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)652 static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
653 struct ieee80211_vif *vif)
654 {
655 struct iwl_bss_iter_data *data = _data;
656
657 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
658 return;
659
660 if (data->vif) {
661 data->error = true;
662 return;
663 }
664
665 data->vif = vif;
666 }
667
iwl_mvm_get_bss_vif(struct iwl_mvm * mvm)668 struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
669 {
670 struct iwl_bss_iter_data bss_iter_data = {};
671
672 ieee80211_iterate_active_interfaces_atomic(
673 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
674 iwl_mvm_bss_iface_iterator, &bss_iter_data);
675
676 if (bss_iter_data.error)
677 return ERR_PTR(-EINVAL);
678
679 return bss_iter_data.vif;
680 }
681
682 struct iwl_bss_find_iter_data {
683 struct ieee80211_vif *vif;
684 u32 macid;
685 };
686
iwl_mvm_bss_find_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)687 static void iwl_mvm_bss_find_iface_iterator(void *_data, u8 *mac,
688 struct ieee80211_vif *vif)
689 {
690 struct iwl_bss_find_iter_data *data = _data;
691 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
692
693 if (mvmvif->id == data->macid)
694 data->vif = vif;
695 }
696
iwl_mvm_get_vif_by_macid(struct iwl_mvm * mvm,u32 macid)697 struct ieee80211_vif *iwl_mvm_get_vif_by_macid(struct iwl_mvm *mvm, u32 macid)
698 {
699 struct iwl_bss_find_iter_data data = {
700 .macid = macid,
701 };
702
703 lockdep_assert_held(&mvm->mutex);
704
705 ieee80211_iterate_active_interfaces_atomic(
706 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
707 iwl_mvm_bss_find_iface_iterator, &data);
708
709 return data.vif;
710 }
711
712 struct iwl_sta_iter_data {
713 bool assoc;
714 };
715
iwl_mvm_sta_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)716 static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
717 struct ieee80211_vif *vif)
718 {
719 struct iwl_sta_iter_data *data = _data;
720
721 if (vif->type != NL80211_IFTYPE_STATION)
722 return;
723
724 if (vif->cfg.assoc)
725 data->assoc = true;
726 }
727
iwl_mvm_is_vif_assoc(struct iwl_mvm * mvm)728 bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
729 {
730 struct iwl_sta_iter_data data = {
731 .assoc = false,
732 };
733
734 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
735 IEEE80211_IFACE_ITER_NORMAL,
736 iwl_mvm_sta_iface_iterator,
737 &data);
738 return data.assoc;
739 }
740
iwl_mvm_get_wd_timeout(struct iwl_mvm * mvm,struct ieee80211_vif * vif)741 unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
742 struct ieee80211_vif *vif)
743 {
744 unsigned int default_timeout =
745 mvm->trans->mac_cfg->base->wd_timeout;
746
747 /*
748 * We can't know when the station is asleep or awake, so we
749 * must disable the queue hang detection.
750 */
751 if (fw_has_capa(&mvm->fw->ucode_capa,
752 IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
753 vif->type == NL80211_IFTYPE_AP)
754 return IWL_WATCHDOG_DISABLED;
755 return default_timeout;
756 }
757
iwl_mvm_connection_loss(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const char * errmsg)758 void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
759 const char *errmsg)
760 {
761 struct iwl_fw_dbg_trigger_tlv *trig;
762 struct iwl_fw_dbg_trigger_mlme *trig_mlme;
763
764 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
765 FW_DBG_TRIGGER_MLME);
766 if (!trig)
767 goto out;
768
769 trig_mlme = (void *)trig->data;
770
771 if (trig_mlme->stop_connection_loss &&
772 --trig_mlme->stop_connection_loss)
773 goto out;
774
775 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
776
777 out:
778 ieee80211_connection_loss(vif);
779 }
780
iwl_mvm_event_frame_timeout_callback(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_sta * sta,u16 tid)781 void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
782 struct ieee80211_vif *vif,
783 const struct ieee80211_sta *sta,
784 u16 tid)
785 {
786 struct iwl_fw_dbg_trigger_tlv *trig;
787 struct iwl_fw_dbg_trigger_ba *ba_trig;
788
789 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
790 FW_DBG_TRIGGER_BA);
791 if (!trig)
792 return;
793
794 ba_trig = (void *)trig->data;
795
796 if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
797 return;
798
799 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
800 "Frame from %pM timed out, tid %d",
801 sta->addr, tid);
802 }
803
iwl_mvm_tcm_load_percentage(u32 airtime,u32 elapsed)804 u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
805 {
806 if (!elapsed)
807 return 0;
808
809 return (100 * airtime / elapsed) / USEC_PER_MSEC;
810 }
811
812 static enum iwl_mvm_traffic_load
iwl_mvm_tcm_load(struct iwl_mvm * mvm,u32 airtime,unsigned long elapsed)813 iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
814 {
815 u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
816
817 if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
818 return IWL_MVM_TRAFFIC_HIGH;
819 if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
820 return IWL_MVM_TRAFFIC_MEDIUM;
821
822 return IWL_MVM_TRAFFIC_LOW;
823 }
824
iwl_mvm_tcm_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)825 static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
826 {
827 struct iwl_mvm *mvm = _data;
828 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
829 bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
830
831 if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
832 return;
833
834 low_latency = mvm->tcm.result.low_latency[mvmvif->id];
835
836 if (!mvm->tcm.result.change[mvmvif->id] &&
837 prev == low_latency) {
838 iwl_mvm_update_quotas(mvm, false, NULL);
839 return;
840 }
841
842 if (prev != low_latency) {
843 /* this sends traffic load and updates quota as well */
844 iwl_mvm_update_low_latency(mvm, vif, low_latency,
845 LOW_LATENCY_TRAFFIC);
846 } else {
847 iwl_mvm_update_quotas(mvm, false, NULL);
848 }
849 }
850
iwl_mvm_tcm_results(struct iwl_mvm * mvm)851 static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
852 {
853 guard(mvm)(mvm);
854
855 ieee80211_iterate_active_interfaces(
856 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
857 iwl_mvm_tcm_iter, mvm);
858
859 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
860 iwl_mvm_config_scan(mvm);
861 }
862
iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct * wk)863 static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
864 {
865 struct iwl_mvm *mvm;
866 struct iwl_mvm_vif *mvmvif;
867 struct ieee80211_vif *vif;
868
869 mvmvif = container_of(wk, struct iwl_mvm_vif,
870 uapsd_nonagg_detected_wk.work);
871 vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
872 mvm = mvmvif->mvm;
873
874 if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
875 return;
876
877 /* remember that this AP is broken */
878 memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
879 vif->bss_conf.bssid, ETH_ALEN);
880 mvm->uapsd_noagg_bssid_write_idx++;
881 if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
882 mvm->uapsd_noagg_bssid_write_idx = 0;
883
884 iwl_mvm_connection_loss(mvm, vif,
885 "AP isn't using AMPDU with uAPSD enabled");
886 }
887
iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm * mvm,struct ieee80211_vif * vif)888 static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
889 struct ieee80211_vif *vif)
890 {
891 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
892
893 if (vif->type != NL80211_IFTYPE_STATION)
894 return;
895
896 if (!vif->cfg.assoc)
897 return;
898
899 if (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
900 !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
901 !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
902 !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd)
903 return;
904
905 if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
906 return;
907
908 mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
909 IWL_INFO(mvm,
910 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
911 schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk,
912 15 * HZ);
913 }
914
iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm * mvm,unsigned int elapsed,int mac)915 static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
916 unsigned int elapsed,
917 int mac)
918 {
919 u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
920 u64 tpt;
921 unsigned long rate;
922 struct ieee80211_vif *vif;
923
924 rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
925
926 if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
927 mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
928 return;
929
930 if (iwl_mvm_has_new_rx_api(mvm)) {
931 tpt = 8 * bytes; /* kbps */
932 do_div(tpt, elapsed);
933 rate *= 1000; /* kbps */
934 if (tpt < 22 * rate / 100)
935 return;
936 } else {
937 /*
938 * the rate here is actually the threshold, in 100Kbps units,
939 * so do the needed conversion from bytes to 100Kbps:
940 * 100kb = bits / (100 * 1000),
941 * 100kbps = 100kb / (msecs / 1000) ==
942 * (bits / (100 * 1000)) / (msecs / 1000) ==
943 * bits / (100 * msecs)
944 */
945 tpt = (8 * bytes);
946 do_div(tpt, elapsed * 100);
947 if (tpt < rate)
948 return;
949 }
950
951 rcu_read_lock();
952 vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
953 if (vif)
954 iwl_mvm_uapsd_agg_disconnect(mvm, vif);
955 rcu_read_unlock();
956 }
957
iwl_mvm_tcm_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)958 static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
959 struct ieee80211_vif *vif)
960 {
961 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
962 u32 *band = _data;
963
964 if (!mvmvif->deflink.phy_ctxt)
965 return;
966
967 band[mvmvif->id] = mvmvif->deflink.phy_ctxt->channel->band;
968 }
969
iwl_mvm_calc_tcm_stats(struct iwl_mvm * mvm,unsigned long ts,bool handle_uapsd)970 static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
971 unsigned long ts,
972 bool handle_uapsd)
973 {
974 unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
975 unsigned int uapsd_elapsed =
976 jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
977 u32 total_airtime = 0;
978 u32 band_airtime[NUM_NL80211_BANDS] = {0};
979 u32 band[NUM_MAC_INDEX_DRIVER] = {0};
980 int ac, mac, i;
981 bool low_latency = false;
982 enum iwl_mvm_traffic_load load, band_load;
983 bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
984
985 if (handle_ll)
986 mvm->tcm.ll_ts = ts;
987 if (handle_uapsd)
988 mvm->tcm.uapsd_nonagg_ts = ts;
989
990 mvm->tcm.result.elapsed = elapsed;
991
992 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
993 IEEE80211_IFACE_ITER_NORMAL,
994 iwl_mvm_tcm_iterator,
995 &band);
996
997 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
998 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
999 u32 vo_vi_pkts = 0;
1000 u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1001
1002 total_airtime += airtime;
1003 band_airtime[band[mac]] += airtime;
1004
1005 load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1006 mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1007 mvm->tcm.result.load[mac] = load;
1008 mvm->tcm.result.airtime[mac] = airtime;
1009
1010 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1011 vo_vi_pkts += mdata->rx.pkts[ac] +
1012 mdata->tx.pkts[ac];
1013
1014 /* enable immediately with enough packets but defer disabling */
1015 if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1016 mvm->tcm.result.low_latency[mac] = true;
1017 else if (handle_ll)
1018 mvm->tcm.result.low_latency[mac] = false;
1019
1020 if (handle_ll) {
1021 /* clear old data */
1022 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1023 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1024 }
1025 low_latency |= mvm->tcm.result.low_latency[mac];
1026
1027 if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1028 iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1029 mac);
1030 /* clear old data */
1031 if (handle_uapsd)
1032 mdata->uapsd_nonagg_detect.rx_bytes = 0;
1033 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1034 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1035 }
1036
1037 load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1038 mvm->tcm.result.global_load = load;
1039
1040 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1041 band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1042 mvm->tcm.result.band_load[i] = band_load;
1043 }
1044
1045 /*
1046 * If the current load isn't low we need to force re-evaluation
1047 * in the TCM period, so that we can return to low load if there
1048 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
1049 * triggered by traffic).
1050 */
1051 if (load != IWL_MVM_TRAFFIC_LOW)
1052 return MVM_TCM_PERIOD;
1053 /*
1054 * If low-latency is active we need to force re-evaluation after
1055 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
1056 * when there's no traffic at all.
1057 */
1058 if (low_latency)
1059 return MVM_LL_PERIOD;
1060 /*
1061 * Otherwise, we don't need to run the work struct because we're
1062 * in the default "idle" state - traffic indication is low (which
1063 * also covers the "no traffic" case) and low-latency is disabled
1064 * so there's no state that may need to be disabled when there's
1065 * no traffic at all.
1066 *
1067 * Note that this has no impact on the regular scheduling of the
1068 * updates triggered by traffic - those happen whenever one of the
1069 * two timeouts expire (if there's traffic at all.)
1070 */
1071 return 0;
1072 }
1073
iwl_mvm_recalc_tcm(struct iwl_mvm * mvm)1074 void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1075 {
1076 unsigned long ts = jiffies;
1077 bool handle_uapsd =
1078 time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1079 msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
1080
1081 spin_lock(&mvm->tcm.lock);
1082 if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1083 spin_unlock(&mvm->tcm.lock);
1084 return;
1085 }
1086 spin_unlock(&mvm->tcm.lock);
1087
1088 if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1089 guard(mvm)(mvm);
1090 if (iwl_mvm_request_statistics(mvm, true))
1091 handle_uapsd = false;
1092 }
1093
1094 spin_lock(&mvm->tcm.lock);
1095 /* re-check if somebody else won the recheck race */
1096 if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1097 /* calculate statistics */
1098 unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1099 handle_uapsd);
1100
1101 /* the memset needs to be visible before the timestamp */
1102 smp_mb();
1103 mvm->tcm.ts = ts;
1104 if (work_delay)
1105 schedule_delayed_work(&mvm->tcm.work, work_delay);
1106 }
1107 spin_unlock(&mvm->tcm.lock);
1108
1109 iwl_mvm_tcm_results(mvm);
1110 }
1111
iwl_mvm_tcm_work(struct work_struct * work)1112 void iwl_mvm_tcm_work(struct work_struct *work)
1113 {
1114 struct delayed_work *delayed_work = to_delayed_work(work);
1115 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1116 tcm.work);
1117
1118 iwl_mvm_recalc_tcm(mvm);
1119 }
1120
iwl_mvm_pause_tcm(struct iwl_mvm * mvm,bool with_cancel)1121 void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1122 {
1123 spin_lock_bh(&mvm->tcm.lock);
1124 mvm->tcm.paused = true;
1125 spin_unlock_bh(&mvm->tcm.lock);
1126 if (with_cancel)
1127 cancel_delayed_work_sync(&mvm->tcm.work);
1128 }
1129
iwl_mvm_resume_tcm(struct iwl_mvm * mvm)1130 void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1131 {
1132 int mac;
1133 bool low_latency = false;
1134
1135 spin_lock_bh(&mvm->tcm.lock);
1136 mvm->tcm.ts = jiffies;
1137 mvm->tcm.ll_ts = jiffies;
1138 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1139 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1140
1141 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1142 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1143 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1144 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1145
1146 if (mvm->tcm.result.low_latency[mac])
1147 low_latency = true;
1148 }
1149 /* The TCM data needs to be reset before "paused" flag changes */
1150 smp_mb();
1151 mvm->tcm.paused = false;
1152
1153 /*
1154 * if the current load is not low or low latency is active, force
1155 * re-evaluation to cover the case of no traffic.
1156 */
1157 if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
1158 schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
1159 else if (low_latency)
1160 schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
1161
1162 spin_unlock_bh(&mvm->tcm.lock);
1163 }
1164
iwl_mvm_tcm_add_vif(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1165 void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1166 {
1167 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1168
1169 INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1170 iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1171 }
1172
iwl_mvm_tcm_rm_vif(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1173 void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1174 {
1175 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1176
1177 cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1178 }
1179
iwl_mvm_get_systime(struct iwl_mvm * mvm)1180 u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
1181 {
1182 u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
1183
1184 if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
1185 mvm->trans->mac_cfg->base->gp2_reg_addr)
1186 reg_addr = mvm->trans->mac_cfg->base->gp2_reg_addr;
1187
1188 return iwl_read_prph(mvm->trans, reg_addr);
1189 }
1190
iwl_mvm_get_sync_time(struct iwl_mvm * mvm,int clock_type,u32 * gp2,u64 * boottime,ktime_t * realtime)1191 void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, int clock_type,
1192 u32 *gp2, u64 *boottime, ktime_t *realtime)
1193 {
1194 bool ps_disabled;
1195
1196 lockdep_assert_held(&mvm->mutex);
1197
1198 /* Disable power save when reading GP2 */
1199 ps_disabled = mvm->ps_disabled;
1200 if (!ps_disabled) {
1201 mvm->ps_disabled = true;
1202 iwl_mvm_power_update_device(mvm);
1203 }
1204
1205 *gp2 = iwl_mvm_get_systime(mvm);
1206
1207 if (clock_type == CLOCK_BOOTTIME && boottime)
1208 *boottime = ktime_get_boottime_ns();
1209 else if (clock_type == CLOCK_REALTIME && realtime)
1210 *realtime = ktime_get_real();
1211
1212 if (!ps_disabled) {
1213 mvm->ps_disabled = ps_disabled;
1214 iwl_mvm_power_update_device(mvm);
1215 }
1216 }
1217
1218 /* Find if at least two links from different vifs use same channel
1219 * FIXME: consider having a refcount array in struct iwl_mvm_vif for
1220 * used phy_ctxt ids.
1221 */
iwl_mvm_have_links_same_channel(struct iwl_mvm_vif * vif1,struct iwl_mvm_vif * vif2)1222 bool iwl_mvm_have_links_same_channel(struct iwl_mvm_vif *vif1,
1223 struct iwl_mvm_vif *vif2)
1224 {
1225 unsigned int i, j;
1226
1227 for_each_mvm_vif_valid_link(vif1, i) {
1228 for_each_mvm_vif_valid_link(vif2, j) {
1229 if (vif1->link[i]->phy_ctxt == vif2->link[j]->phy_ctxt)
1230 return true;
1231 }
1232 }
1233
1234 return false;
1235 }
1236
iwl_mvm_vif_is_active(struct iwl_mvm_vif * mvmvif)1237 bool iwl_mvm_vif_is_active(struct iwl_mvm_vif *mvmvif)
1238 {
1239 unsigned int i;
1240
1241 /* FIXME: can it fail when phy_ctxt is assigned? */
1242 for_each_mvm_vif_valid_link(mvmvif, i) {
1243 if (mvmvif->link[i]->phy_ctxt &&
1244 mvmvif->link[i]->phy_ctxt->id < NUM_PHY_CTX)
1245 return true;
1246 }
1247
1248 return false;
1249 }
1250