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