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