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