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