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