xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/utils.c (revision 3663e2c4bc45fcdc71931fcbfcbfbf9b71f55c83)
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 	/*
439 	 * Don't request statistics during restart, they'll not have any useful
440 	 * information right after restart, nor is clearing needed
441 	 */
442 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
443 		return 0;
444 
445 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
446 		return iwl_mvm_request_system_statistics(mvm, clear, cmd_ver);
447 
448 	/* From version 15 - STATISTICS_NOTIFICATION, the reply for
449 	 * STATISTICS_CMD is empty, and the response is with
450 	 * STATISTICS_NOTIFICATION notification
451 	 */
452 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
453 				    STATISTICS_NOTIFICATION, 0) < 15) {
454 		cmd.flags = CMD_WANT_SKB;
455 
456 		ret = iwl_mvm_send_cmd(mvm, &cmd);
457 		if (ret)
458 			return ret;
459 
460 		iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
461 		iwl_free_resp(&cmd);
462 	} else {
463 		struct iwl_notification_wait stats_wait;
464 		static const u16 stats_complete[] = {
465 			STATISTICS_NOTIFICATION,
466 		};
467 
468 		iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,
469 					   stats_complete, ARRAY_SIZE(stats_complete),
470 					   iwl_wait_stats_complete, NULL);
471 
472 		ret = iwl_mvm_send_cmd(mvm, &cmd);
473 		if (ret) {
474 			iwl_remove_notification(&mvm->notif_wait, &stats_wait);
475 			return ret;
476 		}
477 
478 		/* 200ms should be enough for FW to collect data from all
479 		 * LMACs and send STATISTICS_NOTIFICATION to host
480 		 */
481 		ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 5);
482 		if (ret)
483 			return ret;
484 	}
485 
486 	if (clear)
487 		iwl_mvm_accu_radio_stats(mvm);
488 
489 	return 0;
490 }
491 
492 void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
493 {
494 	mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
495 	mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
496 	mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
497 	mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
498 }
499 
500 struct iwl_mvm_diversity_iter_data {
501 	struct iwl_mvm_phy_ctxt *ctxt;
502 	bool result;
503 };
504 
505 static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
506 				   struct ieee80211_vif *vif)
507 {
508 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
509 	struct iwl_mvm_diversity_iter_data *data = _data;
510 	int i, link_id;
511 
512 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
513 		struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
514 
515 		if (link_info->phy_ctxt != data->ctxt)
516 			continue;
517 
518 		for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
519 			if (link_info->smps_requests[i] == IEEE80211_SMPS_STATIC ||
520 			    link_info->smps_requests[i] == IEEE80211_SMPS_DYNAMIC) {
521 				data->result = false;
522 				break;
523 			}
524 		}
525 	}
526 }
527 
528 bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm,
529 				  struct iwl_mvm_phy_ctxt *ctxt)
530 {
531 	struct iwl_mvm_diversity_iter_data data = {
532 		.ctxt = ctxt,
533 		.result = true,
534 	};
535 
536 	lockdep_assert_held(&mvm->mutex);
537 
538 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
539 		return false;
540 
541 	if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
542 		return false;
543 
544 	if (mvm->cfg->rx_with_siso_diversity)
545 		return false;
546 
547 	ieee80211_iterate_active_interfaces_atomic(
548 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
549 			iwl_mvm_diversity_iter, &data);
550 
551 	return data.result;
552 }
553 
554 void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
555 				  bool low_latency, u16 mac_id)
556 {
557 	struct iwl_mac_low_latency_cmd cmd = {
558 		.mac_id = cpu_to_le32(mac_id)
559 	};
560 
561 	if (!fw_has_capa(&mvm->fw->ucode_capa,
562 			 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
563 		return;
564 
565 	if (low_latency) {
566 		/* currently we don't care about the direction */
567 		cmd.low_latency_rx = 1;
568 		cmd.low_latency_tx = 1;
569 	}
570 
571 	if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, LOW_LATENCY_CMD),
572 				 0, sizeof(cmd), &cmd))
573 		IWL_ERR(mvm, "Failed to send low latency command\n");
574 }
575 
576 int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
577 			       bool low_latency,
578 			       enum iwl_mvm_low_latency_cause cause)
579 {
580 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
581 	int res;
582 	bool prev;
583 
584 	lockdep_assert_held(&mvm->mutex);
585 
586 	prev = iwl_mvm_vif_low_latency(mvmvif);
587 	iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
588 
589 	low_latency = iwl_mvm_vif_low_latency(mvmvif);
590 
591 	if (low_latency == prev)
592 		return 0;
593 
594 	iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
595 
596 	res = iwl_mvm_update_quotas(mvm, false, NULL);
597 	if (res)
598 		return res;
599 
600 	iwl_mvm_bt_coex_vif_change(mvm);
601 
602 	return iwl_mvm_power_update_mac(mvm);
603 }
604 
605 struct iwl_mvm_low_latency_iter {
606 	bool result;
607 	bool result_per_band[NUM_NL80211_BANDS];
608 };
609 
610 static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
611 {
612 	struct iwl_mvm_low_latency_iter *result = _data;
613 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
614 	enum nl80211_band band;
615 
616 	if (iwl_mvm_vif_low_latency(mvmvif)) {
617 		result->result = true;
618 
619 		if (!mvmvif->deflink.phy_ctxt)
620 			return;
621 
622 		band = mvmvif->deflink.phy_ctxt->channel->band;
623 		result->result_per_band[band] = true;
624 	}
625 }
626 
627 bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
628 {
629 	struct iwl_mvm_low_latency_iter data = {};
630 
631 	ieee80211_iterate_active_interfaces_atomic(
632 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
633 			iwl_mvm_ll_iter, &data);
634 
635 	return data.result;
636 }
637 
638 bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
639 {
640 	struct iwl_mvm_low_latency_iter data = {};
641 
642 	ieee80211_iterate_active_interfaces_atomic(
643 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
644 			iwl_mvm_ll_iter, &data);
645 
646 	return data.result_per_band[band];
647 }
648 
649 struct iwl_bss_iter_data {
650 	struct ieee80211_vif *vif;
651 	bool error;
652 };
653 
654 static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
655 				       struct ieee80211_vif *vif)
656 {
657 	struct iwl_bss_iter_data *data = _data;
658 
659 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
660 		return;
661 
662 	if (data->vif) {
663 		data->error = true;
664 		return;
665 	}
666 
667 	data->vif = vif;
668 }
669 
670 struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
671 {
672 	struct iwl_bss_iter_data bss_iter_data = {};
673 
674 	ieee80211_iterate_active_interfaces_atomic(
675 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
676 		iwl_mvm_bss_iface_iterator, &bss_iter_data);
677 
678 	if (bss_iter_data.error) {
679 		IWL_ERR(mvm, "More than one managed interface active!\n");
680 		return ERR_PTR(-EINVAL);
681 	}
682 
683 	return bss_iter_data.vif;
684 }
685 
686 struct iwl_bss_find_iter_data {
687 	struct ieee80211_vif *vif;
688 	u32 macid;
689 };
690 
691 static void iwl_mvm_bss_find_iface_iterator(void *_data, u8 *mac,
692 					    struct ieee80211_vif *vif)
693 {
694 	struct iwl_bss_find_iter_data *data = _data;
695 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
696 
697 	if (mvmvif->id == data->macid)
698 		data->vif = vif;
699 }
700 
701 struct ieee80211_vif *iwl_mvm_get_vif_by_macid(struct iwl_mvm *mvm, u32 macid)
702 {
703 	struct iwl_bss_find_iter_data data = {
704 		.macid = macid,
705 	};
706 
707 	lockdep_assert_held(&mvm->mutex);
708 
709 	ieee80211_iterate_active_interfaces_atomic(
710 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
711 		iwl_mvm_bss_find_iface_iterator, &data);
712 
713 	return data.vif;
714 }
715 
716 struct iwl_sta_iter_data {
717 	bool assoc;
718 };
719 
720 static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
721 				       struct ieee80211_vif *vif)
722 {
723 	struct iwl_sta_iter_data *data = _data;
724 
725 	if (vif->type != NL80211_IFTYPE_STATION)
726 		return;
727 
728 	if (vif->cfg.assoc)
729 		data->assoc = true;
730 }
731 
732 bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
733 {
734 	struct iwl_sta_iter_data data = {
735 		.assoc = false,
736 	};
737 
738 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
739 						   IEEE80211_IFACE_ITER_NORMAL,
740 						   iwl_mvm_sta_iface_iterator,
741 						   &data);
742 	return data.assoc;
743 }
744 
745 unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
746 				    struct ieee80211_vif *vif,
747 				    bool tdls, bool cmd_q)
748 {
749 	struct iwl_fw_dbg_trigger_tlv *trigger;
750 	struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
751 	unsigned int default_timeout = cmd_q ?
752 		IWL_DEF_WD_TIMEOUT :
753 		mvm->trans->trans_cfg->base_params->wd_timeout;
754 
755 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
756 		/*
757 		 * We can't know when the station is asleep or awake, so we
758 		 * must disable the queue hang detection.
759 		 */
760 		if (fw_has_capa(&mvm->fw->ucode_capa,
761 				IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
762 		    vif && vif->type == NL80211_IFTYPE_AP)
763 			return IWL_WATCHDOG_DISABLED;
764 		return default_timeout;
765 	}
766 
767 	trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
768 	txq_timer = (void *)trigger->data;
769 
770 	if (tdls)
771 		return le32_to_cpu(txq_timer->tdls);
772 
773 	if (cmd_q)
774 		return le32_to_cpu(txq_timer->command_queue);
775 
776 	if (WARN_ON(!vif))
777 		return default_timeout;
778 
779 	switch (ieee80211_vif_type_p2p(vif)) {
780 	case NL80211_IFTYPE_ADHOC:
781 		return le32_to_cpu(txq_timer->ibss);
782 	case NL80211_IFTYPE_STATION:
783 		return le32_to_cpu(txq_timer->bss);
784 	case NL80211_IFTYPE_AP:
785 		return le32_to_cpu(txq_timer->softap);
786 	case NL80211_IFTYPE_P2P_CLIENT:
787 		return le32_to_cpu(txq_timer->p2p_client);
788 	case NL80211_IFTYPE_P2P_GO:
789 		return le32_to_cpu(txq_timer->p2p_go);
790 	case NL80211_IFTYPE_P2P_DEVICE:
791 		return le32_to_cpu(txq_timer->p2p_device);
792 	case NL80211_IFTYPE_MONITOR:
793 		return default_timeout;
794 	default:
795 		WARN_ON(1);
796 		return mvm->trans->trans_cfg->base_params->wd_timeout;
797 	}
798 }
799 
800 void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
801 			     const char *errmsg)
802 {
803 	struct iwl_fw_dbg_trigger_tlv *trig;
804 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
805 
806 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
807 				     FW_DBG_TRIGGER_MLME);
808 	if (!trig)
809 		goto out;
810 
811 	trig_mlme = (void *)trig->data;
812 
813 	if (trig_mlme->stop_connection_loss &&
814 	    --trig_mlme->stop_connection_loss)
815 		goto out;
816 
817 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
818 
819 out:
820 	ieee80211_connection_loss(vif);
821 }
822 
823 void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
824 					  struct ieee80211_vif *vif,
825 					  const struct ieee80211_sta *sta,
826 					  u16 tid)
827 {
828 	struct iwl_fw_dbg_trigger_tlv *trig;
829 	struct iwl_fw_dbg_trigger_ba *ba_trig;
830 
831 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
832 				     FW_DBG_TRIGGER_BA);
833 	if (!trig)
834 		return;
835 
836 	ba_trig = (void *)trig->data;
837 
838 	if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
839 		return;
840 
841 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
842 				"Frame from %pM timed out, tid %d",
843 				sta->addr, tid);
844 }
845 
846 u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
847 {
848 	if (!elapsed)
849 		return 0;
850 
851 	return (100 * airtime / elapsed) / USEC_PER_MSEC;
852 }
853 
854 static enum iwl_mvm_traffic_load
855 iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
856 {
857 	u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
858 
859 	if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
860 		return IWL_MVM_TRAFFIC_HIGH;
861 	if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
862 		return IWL_MVM_TRAFFIC_MEDIUM;
863 
864 	return IWL_MVM_TRAFFIC_LOW;
865 }
866 
867 static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
868 {
869 	struct iwl_mvm *mvm = _data;
870 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
871 	bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
872 
873 	if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
874 		return;
875 
876 	low_latency = mvm->tcm.result.low_latency[mvmvif->id];
877 
878 	if (!mvm->tcm.result.change[mvmvif->id] &&
879 	    prev == low_latency) {
880 		iwl_mvm_update_quotas(mvm, false, NULL);
881 		return;
882 	}
883 
884 	if (prev != low_latency) {
885 		/* this sends traffic load and updates quota as well */
886 		iwl_mvm_update_low_latency(mvm, vif, low_latency,
887 					   LOW_LATENCY_TRAFFIC);
888 	} else {
889 		iwl_mvm_update_quotas(mvm, false, NULL);
890 	}
891 }
892 
893 static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
894 {
895 	guard(mvm)(mvm);
896 
897 	ieee80211_iterate_active_interfaces(
898 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
899 		iwl_mvm_tcm_iter, mvm);
900 
901 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
902 		iwl_mvm_config_scan(mvm);
903 }
904 
905 static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
906 {
907 	struct iwl_mvm *mvm;
908 	struct iwl_mvm_vif *mvmvif;
909 	struct ieee80211_vif *vif;
910 
911 	mvmvif = container_of(wk, struct iwl_mvm_vif,
912 			      uapsd_nonagg_detected_wk.work);
913 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
914 	mvm = mvmvif->mvm;
915 
916 	if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
917 		return;
918 
919 	/* remember that this AP is broken */
920 	memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
921 	       vif->bss_conf.bssid, ETH_ALEN);
922 	mvm->uapsd_noagg_bssid_write_idx++;
923 	if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
924 		mvm->uapsd_noagg_bssid_write_idx = 0;
925 
926 	iwl_mvm_connection_loss(mvm, vif,
927 				"AP isn't using AMPDU with uAPSD enabled");
928 }
929 
930 static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
931 					 struct ieee80211_vif *vif)
932 {
933 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
934 
935 	if (vif->type != NL80211_IFTYPE_STATION)
936 		return;
937 
938 	if (!vif->cfg.assoc)
939 		return;
940 
941 	if (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
942 	    !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
943 	    !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
944 	    !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd)
945 		return;
946 
947 	if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
948 		return;
949 
950 	mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
951 	IWL_INFO(mvm,
952 		 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
953 	schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk,
954 			      15 * HZ);
955 }
956 
957 static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
958 						 unsigned int elapsed,
959 						 int mac)
960 {
961 	u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
962 	u64 tpt;
963 	unsigned long rate;
964 	struct ieee80211_vif *vif;
965 
966 	rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
967 
968 	if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
969 	    mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
970 		return;
971 
972 	if (iwl_mvm_has_new_rx_api(mvm)) {
973 		tpt = 8 * bytes; /* kbps */
974 		do_div(tpt, elapsed);
975 		rate *= 1000; /* kbps */
976 		if (tpt < 22 * rate / 100)
977 			return;
978 	} else {
979 		/*
980 		 * the rate here is actually the threshold, in 100Kbps units,
981 		 * so do the needed conversion from bytes to 100Kbps:
982 		 * 100kb = bits / (100 * 1000),
983 		 * 100kbps = 100kb / (msecs / 1000) ==
984 		 *           (bits / (100 * 1000)) / (msecs / 1000) ==
985 		 *           bits / (100 * msecs)
986 		 */
987 		tpt = (8 * bytes);
988 		do_div(tpt, elapsed * 100);
989 		if (tpt < rate)
990 			return;
991 	}
992 
993 	rcu_read_lock();
994 	vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
995 	if (vif)
996 		iwl_mvm_uapsd_agg_disconnect(mvm, vif);
997 	rcu_read_unlock();
998 }
999 
1000 static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
1001 				 struct ieee80211_vif *vif)
1002 {
1003 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1004 	u32 *band = _data;
1005 
1006 	if (!mvmvif->deflink.phy_ctxt)
1007 		return;
1008 
1009 	band[mvmvif->id] = mvmvif->deflink.phy_ctxt->channel->band;
1010 }
1011 
1012 static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
1013 					    unsigned long ts,
1014 					    bool handle_uapsd)
1015 {
1016 	unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
1017 	unsigned int uapsd_elapsed =
1018 		jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
1019 	u32 total_airtime = 0;
1020 	u32 band_airtime[NUM_NL80211_BANDS] = {0};
1021 	u32 band[NUM_MAC_INDEX_DRIVER] = {0};
1022 	int ac, mac, i;
1023 	bool low_latency = false;
1024 	enum iwl_mvm_traffic_load load, band_load;
1025 	bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
1026 
1027 	if (handle_ll)
1028 		mvm->tcm.ll_ts = ts;
1029 	if (handle_uapsd)
1030 		mvm->tcm.uapsd_nonagg_ts = ts;
1031 
1032 	mvm->tcm.result.elapsed = elapsed;
1033 
1034 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1035 						   IEEE80211_IFACE_ITER_NORMAL,
1036 						   iwl_mvm_tcm_iterator,
1037 						   &band);
1038 
1039 	for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1040 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1041 		u32 vo_vi_pkts = 0;
1042 		u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1043 
1044 		total_airtime += airtime;
1045 		band_airtime[band[mac]] += airtime;
1046 
1047 		load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1048 		mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1049 		mvm->tcm.result.load[mac] = load;
1050 		mvm->tcm.result.airtime[mac] = airtime;
1051 
1052 		for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1053 			vo_vi_pkts += mdata->rx.pkts[ac] +
1054 				      mdata->tx.pkts[ac];
1055 
1056 		/* enable immediately with enough packets but defer disabling */
1057 		if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1058 			mvm->tcm.result.low_latency[mac] = true;
1059 		else if (handle_ll)
1060 			mvm->tcm.result.low_latency[mac] = false;
1061 
1062 		if (handle_ll) {
1063 			/* clear old data */
1064 			memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1065 			memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1066 		}
1067 		low_latency |= mvm->tcm.result.low_latency[mac];
1068 
1069 		if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1070 			iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1071 							     mac);
1072 		/* clear old data */
1073 		if (handle_uapsd)
1074 			mdata->uapsd_nonagg_detect.rx_bytes = 0;
1075 		memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1076 		memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1077 	}
1078 
1079 	load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1080 	mvm->tcm.result.global_load = load;
1081 
1082 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
1083 		band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1084 		mvm->tcm.result.band_load[i] = band_load;
1085 	}
1086 
1087 	/*
1088 	 * If the current load isn't low we need to force re-evaluation
1089 	 * in the TCM period, so that we can return to low load if there
1090 	 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
1091 	 * triggered by traffic).
1092 	 */
1093 	if (load != IWL_MVM_TRAFFIC_LOW)
1094 		return MVM_TCM_PERIOD;
1095 	/*
1096 	 * If low-latency is active we need to force re-evaluation after
1097 	 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
1098 	 * when there's no traffic at all.
1099 	 */
1100 	if (low_latency)
1101 		return MVM_LL_PERIOD;
1102 	/*
1103 	 * Otherwise, we don't need to run the work struct because we're
1104 	 * in the default "idle" state - traffic indication is low (which
1105 	 * also covers the "no traffic" case) and low-latency is disabled
1106 	 * so there's no state that may need to be disabled when there's
1107 	 * no traffic at all.
1108 	 *
1109 	 * Note that this has no impact on the regular scheduling of the
1110 	 * updates triggered by traffic - those happen whenever one of the
1111 	 * two timeouts expire (if there's traffic at all.)
1112 	 */
1113 	return 0;
1114 }
1115 
1116 void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1117 {
1118 	unsigned long ts = jiffies;
1119 	bool handle_uapsd =
1120 		time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1121 			       msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
1122 
1123 	spin_lock(&mvm->tcm.lock);
1124 	if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1125 		spin_unlock(&mvm->tcm.lock);
1126 		return;
1127 	}
1128 	spin_unlock(&mvm->tcm.lock);
1129 
1130 	if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1131 		guard(mvm)(mvm);
1132 		if (iwl_mvm_request_statistics(mvm, true))
1133 			handle_uapsd = false;
1134 	}
1135 
1136 	spin_lock(&mvm->tcm.lock);
1137 	/* re-check if somebody else won the recheck race */
1138 	if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1139 		/* calculate statistics */
1140 		unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1141 								  handle_uapsd);
1142 
1143 		/* the memset needs to be visible before the timestamp */
1144 		smp_mb();
1145 		mvm->tcm.ts = ts;
1146 		if (work_delay)
1147 			schedule_delayed_work(&mvm->tcm.work, work_delay);
1148 	}
1149 	spin_unlock(&mvm->tcm.lock);
1150 
1151 	iwl_mvm_tcm_results(mvm);
1152 }
1153 
1154 void iwl_mvm_tcm_work(struct work_struct *work)
1155 {
1156 	struct delayed_work *delayed_work = to_delayed_work(work);
1157 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1158 					   tcm.work);
1159 
1160 	iwl_mvm_recalc_tcm(mvm);
1161 }
1162 
1163 void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1164 {
1165 	spin_lock_bh(&mvm->tcm.lock);
1166 	mvm->tcm.paused = true;
1167 	spin_unlock_bh(&mvm->tcm.lock);
1168 	if (with_cancel)
1169 		cancel_delayed_work_sync(&mvm->tcm.work);
1170 }
1171 
1172 void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1173 {
1174 	int mac;
1175 	bool low_latency = false;
1176 
1177 	spin_lock_bh(&mvm->tcm.lock);
1178 	mvm->tcm.ts = jiffies;
1179 	mvm->tcm.ll_ts = jiffies;
1180 	for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1181 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1182 
1183 		memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1184 		memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1185 		memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1186 		memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1187 
1188 		if (mvm->tcm.result.low_latency[mac])
1189 			low_latency = true;
1190 	}
1191 	/* The TCM data needs to be reset before "paused" flag changes */
1192 	smp_mb();
1193 	mvm->tcm.paused = false;
1194 
1195 	/*
1196 	 * if the current load is not low or low latency is active, force
1197 	 * re-evaluation to cover the case of no traffic.
1198 	 */
1199 	if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
1200 		schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
1201 	else if (low_latency)
1202 		schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
1203 
1204 	spin_unlock_bh(&mvm->tcm.lock);
1205 }
1206 
1207 void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1208 {
1209 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1210 
1211 	INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1212 			  iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1213 }
1214 
1215 void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1216 {
1217 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1218 
1219 	cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1220 }
1221 
1222 u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
1223 {
1224 	u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
1225 
1226 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
1227 	    mvm->trans->cfg->gp2_reg_addr)
1228 		reg_addr = mvm->trans->cfg->gp2_reg_addr;
1229 
1230 	return iwl_read_prph(mvm->trans, reg_addr);
1231 }
1232 
1233 void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, int clock_type,
1234 			   u32 *gp2, u64 *boottime, ktime_t *realtime)
1235 {
1236 	bool ps_disabled;
1237 
1238 	lockdep_assert_held(&mvm->mutex);
1239 
1240 	/* Disable power save when reading GP2 */
1241 	ps_disabled = mvm->ps_disabled;
1242 	if (!ps_disabled) {
1243 		mvm->ps_disabled = true;
1244 		iwl_mvm_power_update_device(mvm);
1245 	}
1246 
1247 	*gp2 = iwl_mvm_get_systime(mvm);
1248 
1249 	if (clock_type == CLOCK_BOOTTIME && boottime)
1250 		*boottime = ktime_get_boottime_ns();
1251 	else if (clock_type == CLOCK_REALTIME && realtime)
1252 		*realtime = ktime_get_real();
1253 
1254 	if (!ps_disabled) {
1255 		mvm->ps_disabled = ps_disabled;
1256 		iwl_mvm_power_update_device(mvm);
1257 	}
1258 }
1259 
1260 /* Find if at least two links from different vifs use same channel
1261  * FIXME: consider having a refcount array in struct iwl_mvm_vif for
1262  * used phy_ctxt ids.
1263  */
1264 bool iwl_mvm_have_links_same_channel(struct iwl_mvm_vif *vif1,
1265 				     struct iwl_mvm_vif *vif2)
1266 {
1267 	unsigned int i, j;
1268 
1269 	for_each_mvm_vif_valid_link(vif1, i) {
1270 		for_each_mvm_vif_valid_link(vif2, j) {
1271 			if (vif1->link[i]->phy_ctxt == vif2->link[j]->phy_ctxt)
1272 				return true;
1273 		}
1274 	}
1275 
1276 	return false;
1277 }
1278 
1279 bool iwl_mvm_vif_is_active(struct iwl_mvm_vif *mvmvif)
1280 {
1281 	unsigned int i;
1282 
1283 	/* FIXME: can it fail when phy_ctxt is assigned? */
1284 	for_each_mvm_vif_valid_link(mvmvif, i) {
1285 		if (mvmvif->link[i]->phy_ctxt &&
1286 		    mvmvif->link[i]->phy_ctxt->id < NUM_PHY_CTX)
1287 			return true;
1288 	}
1289 
1290 	return false;
1291 }
1292