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