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