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