1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
4  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5  * Copyright (C) 2015-2017 Intel Deutschland GmbH
6  */
7 #include <net/mac80211.h>
8 
9 #include "iwl-debug.h"
10 #include "iwl-io.h"
11 #include "iwl-prph.h"
12 #include "iwl-csr.h"
13 #include "mvm.h"
14 #include "fw/api/rs.h"
15 #include "fw/img.h"
16 
17 /*
18  * Will return 0 even if the cmd failed when RFKILL is asserted unless
19  * CMD_WANT_SKB is set in cmd->flags.
20  */
21 int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
22 {
23 	int ret;
24 
25 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
26 	if (WARN_ON(mvm->d3_test_active))
27 		return -EIO;
28 #endif
29 
30 	/*
31 	 * Synchronous commands from this op-mode must hold
32 	 * the mutex, this ensures we don't try to send two
33 	 * (or more) synchronous commands at a time.
34 	 */
35 	if (!(cmd->flags & CMD_ASYNC))
36 		lockdep_assert_held(&mvm->mutex);
37 
38 	ret = iwl_trans_send_cmd(mvm->trans, cmd);
39 
40 	/*
41 	 * If the caller wants the SKB, then don't hide any problems, the
42 	 * caller might access the response buffer which will be NULL if
43 	 * the command failed.
44 	 */
45 	if (cmd->flags & CMD_WANT_SKB)
46 		return ret;
47 
48 	/* Silently ignore failures if RFKILL is asserted */
49 	if (!ret || ret == -ERFKILL)
50 		return 0;
51 	return ret;
52 }
53 
54 int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
55 			 u32 flags, u16 len, const void *data)
56 {
57 	struct iwl_host_cmd cmd = {
58 		.id = id,
59 		.len = { len, },
60 		.data = { data, },
61 		.flags = flags,
62 	};
63 
64 	return iwl_mvm_send_cmd(mvm, &cmd);
65 }
66 
67 /*
68  * We assume that the caller set the status to the success value
69  */
70 int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
71 			    u32 *status)
72 {
73 	struct iwl_rx_packet *pkt;
74 	struct iwl_cmd_response *resp;
75 	int ret, resp_len;
76 
77 	lockdep_assert_held(&mvm->mutex);
78 
79 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
80 	if (WARN_ON(mvm->d3_test_active))
81 		return -EIO;
82 #endif
83 
84 	/*
85 	 * Only synchronous commands can wait for status,
86 	 * we use WANT_SKB so the caller can't.
87 	 */
88 	if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
89 		      "cmd flags %x", cmd->flags))
90 		return -EINVAL;
91 
92 	cmd->flags |= CMD_WANT_SKB;
93 
94 	ret = iwl_trans_send_cmd(mvm->trans, cmd);
95 	if (ret == -ERFKILL) {
96 		/*
97 		 * The command failed because of RFKILL, don't update
98 		 * the status, leave it as success and return 0.
99 		 */
100 		return 0;
101 	} else if (ret) {
102 		return ret;
103 	}
104 
105 	pkt = cmd->resp_pkt;
106 
107 	resp_len = iwl_rx_packet_payload_len(pkt);
108 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
109 		ret = -EIO;
110 		goto out_free_resp;
111 	}
112 
113 	resp = (void *)pkt->data;
114 	*status = le32_to_cpu(resp->status);
115  out_free_resp:
116 	iwl_free_resp(cmd);
117 	return ret;
118 }
119 
120 /*
121  * We assume that the caller set the status to the sucess value
122  */
123 int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
124 				const void *data, u32 *status)
125 {
126 	struct iwl_host_cmd cmd = {
127 		.id = id,
128 		.len = { len, },
129 		.data = { data, },
130 	};
131 
132 	return iwl_mvm_send_cmd_status(mvm, &cmd, status);
133 }
134 
135 #define IWL_DECLARE_RATE_INFO(r) \
136 	[IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
137 
138 /*
139  * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
140  */
141 static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
142 	IWL_DECLARE_RATE_INFO(1),
143 	IWL_DECLARE_RATE_INFO(2),
144 	IWL_DECLARE_RATE_INFO(5),
145 	IWL_DECLARE_RATE_INFO(11),
146 	IWL_DECLARE_RATE_INFO(6),
147 	IWL_DECLARE_RATE_INFO(9),
148 	IWL_DECLARE_RATE_INFO(12),
149 	IWL_DECLARE_RATE_INFO(18),
150 	IWL_DECLARE_RATE_INFO(24),
151 	IWL_DECLARE_RATE_INFO(36),
152 	IWL_DECLARE_RATE_INFO(48),
153 	IWL_DECLARE_RATE_INFO(54),
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;
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 (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(int rate_idx)
174 {
175 	/* Get PLCP rate for tx_cmd->rate_n_flags */
176 	return fw_rate_idx_to_plcp[rate_idx];
177 }
178 
179 u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)
180 {
181 	static const u8 mac80211_ac_to_ucode_ac[] = {
182 		AC_VO,
183 		AC_VI,
184 		AC_BE,
185 		AC_BK
186 	};
187 
188 	return mac80211_ac_to_ucode_ac[ac];
189 }
190 
191 void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
192 {
193 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
194 	struct iwl_error_resp *err_resp = (void *)pkt->data;
195 
196 	IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
197 		le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
198 	IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
199 		le16_to_cpu(err_resp->bad_cmd_seq_num),
200 		le32_to_cpu(err_resp->error_service));
201 	IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",
202 		le64_to_cpu(err_resp->timestamp));
203 }
204 
205 /*
206  * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
207  * The parameter should also be a combination of ANT_[ABC].
208  */
209 u8 first_antenna(u8 mask)
210 {
211 	BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
212 	if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
213 		return BIT(0);
214 	return BIT(ffs(mask) - 1);
215 }
216 
217 /*
218  * Toggles between TX antennas to send the probe request on.
219  * Receives the bitmask of valid TX antennas and the *index* used
220  * for the last TX, and returns the next valid *index* to use.
221  * In order to set it in the tx_cmd, must do BIT(idx).
222  */
223 u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
224 {
225 	u8 ind = last_idx;
226 	int i;
227 
228 	for (i = 0; i < MAX_ANT_NUM; i++) {
229 		ind = (ind + 1) % MAX_ANT_NUM;
230 		if (valid & BIT(ind))
231 			return ind;
232 	}
233 
234 	WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
235 	return last_idx;
236 }
237 
238 /*
239  * Note: This structure is read from the device with IO accesses,
240  * and the reading already does the endian conversion. As it is
241  * read with u32-sized accesses, any members with a different size
242  * need to be ordered correctly though!
243  */
244 struct iwl_error_event_table_v1 {
245 	u32 valid;		/* (nonzero) valid, (0) log is empty */
246 	u32 error_id;		/* type of error */
247 	u32 pc;			/* program counter */
248 	u32 blink1;		/* branch link */
249 	u32 blink2;		/* branch link */
250 	u32 ilink1;		/* interrupt link */
251 	u32 ilink2;		/* interrupt link */
252 	u32 data1;		/* error-specific data */
253 	u32 data2;		/* error-specific data */
254 	u32 data3;		/* error-specific data */
255 	u32 bcon_time;		/* beacon timer */
256 	u32 tsf_low;		/* network timestamp function timer */
257 	u32 tsf_hi;		/* network timestamp function timer */
258 	u32 gp1;		/* GP1 timer register */
259 	u32 gp2;		/* GP2 timer register */
260 	u32 gp3;		/* GP3 timer register */
261 	u32 ucode_ver;		/* uCode version */
262 	u32 hw_ver;		/* HW Silicon version */
263 	u32 brd_ver;		/* HW board version */
264 	u32 log_pc;		/* log program counter */
265 	u32 frame_ptr;		/* frame pointer */
266 	u32 stack_ptr;		/* stack pointer */
267 	u32 hcmd;		/* last host command header */
268 	u32 isr0;		/* isr status register LMPM_NIC_ISR0:
269 				 * rxtx_flag */
270 	u32 isr1;		/* isr status register LMPM_NIC_ISR1:
271 				 * host_flag */
272 	u32 isr2;		/* isr status register LMPM_NIC_ISR2:
273 				 * enc_flag */
274 	u32 isr3;		/* isr status register LMPM_NIC_ISR3:
275 				 * time_flag */
276 	u32 isr4;		/* isr status register LMPM_NIC_ISR4:
277 				 * wico interrupt */
278 	u32 isr_pref;		/* isr status register LMPM_NIC_PREF_STAT */
279 	u32 wait_event;		/* wait event() caller address */
280 	u32 l2p_control;	/* L2pControlField */
281 	u32 l2p_duration;	/* L2pDurationField */
282 	u32 l2p_mhvalid;	/* L2pMhValidBits */
283 	u32 l2p_addr_match;	/* L2pAddrMatchStat */
284 	u32 lmpm_pmg_sel;	/* indicate which clocks are turned on
285 				 * (LMPM_PMG_SEL) */
286 	u32 u_timestamp;	/* indicate when the date and time of the
287 				 * compilation */
288 	u32 flow_handler;	/* FH read/write pointers, RX credit */
289 } __packed /* LOG_ERROR_TABLE_API_S_VER_1 */;
290 
291 struct iwl_error_event_table {
292 	u32 valid;		/* (nonzero) valid, (0) log is empty */
293 	u32 error_id;		/* type of error */
294 	u32 trm_hw_status0;	/* TRM HW status */
295 	u32 trm_hw_status1;	/* TRM HW status */
296 	u32 blink2;		/* branch link */
297 	u32 ilink1;		/* interrupt link */
298 	u32 ilink2;		/* interrupt link */
299 	u32 data1;		/* error-specific data */
300 	u32 data2;		/* error-specific data */
301 	u32 data3;		/* error-specific data */
302 	u32 bcon_time;		/* beacon timer */
303 	u32 tsf_low;		/* network timestamp function timer */
304 	u32 tsf_hi;		/* network timestamp function timer */
305 	u32 gp1;		/* GP1 timer register */
306 	u32 gp2;		/* GP2 timer register */
307 	u32 fw_rev_type;	/* firmware revision type */
308 	u32 major;		/* uCode version major */
309 	u32 minor;		/* uCode version minor */
310 	u32 hw_ver;		/* HW Silicon version */
311 	u32 brd_ver;		/* HW board version */
312 	u32 log_pc;		/* log program counter */
313 	u32 frame_ptr;		/* frame pointer */
314 	u32 stack_ptr;		/* stack pointer */
315 	u32 hcmd;		/* last host command header */
316 	u32 isr0;		/* isr status register LMPM_NIC_ISR0:
317 				 * rxtx_flag */
318 	u32 isr1;		/* isr status register LMPM_NIC_ISR1:
319 				 * host_flag */
320 	u32 isr2;		/* isr status register LMPM_NIC_ISR2:
321 				 * enc_flag */
322 	u32 isr3;		/* isr status register LMPM_NIC_ISR3:
323 				 * time_flag */
324 	u32 isr4;		/* isr status register LMPM_NIC_ISR4:
325 				 * wico interrupt */
326 	u32 last_cmd_id;	/* last HCMD id handled by the firmware */
327 	u32 wait_event;		/* wait event() caller address */
328 	u32 l2p_control;	/* L2pControlField */
329 	u32 l2p_duration;	/* L2pDurationField */
330 	u32 l2p_mhvalid;	/* L2pMhValidBits */
331 	u32 l2p_addr_match;	/* L2pAddrMatchStat */
332 	u32 lmpm_pmg_sel;	/* indicate which clocks are turned on
333 				 * (LMPM_PMG_SEL) */
334 	u32 u_timestamp;	/* indicate when the date and time of the
335 				 * compilation */
336 	u32 flow_handler;	/* FH read/write pointers, RX credit */
337 } __packed /* LOG_ERROR_TABLE_API_S_VER_3 */;
338 
339 /*
340  * UMAC error struct - relevant starting from family 8000 chip.
341  * Note: This structure is read from the device with IO accesses,
342  * and the reading already does the endian conversion. As it is
343  * read with u32-sized accesses, any members with a different size
344  * need to be ordered correctly though!
345  */
346 struct iwl_umac_error_event_table {
347 	u32 valid;		/* (nonzero) valid, (0) log is empty */
348 	u32 error_id;		/* type of error */
349 	u32 blink1;		/* branch link */
350 	u32 blink2;		/* branch link */
351 	u32 ilink1;		/* interrupt link */
352 	u32 ilink2;		/* interrupt link */
353 	u32 data1;		/* error-specific data */
354 	u32 data2;		/* error-specific data */
355 	u32 data3;		/* error-specific data */
356 	u32 umac_major;
357 	u32 umac_minor;
358 	u32 frame_pointer;	/* core register 27*/
359 	u32 stack_pointer;	/* core register 28 */
360 	u32 cmd_header;		/* latest host cmd sent to UMAC */
361 	u32 nic_isr_pref;	/* ISR status register */
362 } __packed;
363 
364 #define ERROR_START_OFFSET  (1 * sizeof(u32))
365 #define ERROR_ELEM_SIZE     (7 * sizeof(u32))
366 
367 static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
368 {
369 	struct iwl_trans *trans = mvm->trans;
370 	struct iwl_umac_error_event_table table = {};
371 	u32 base = mvm->trans->dbg.umac_error_event_table;
372 
373 	if (!base &&
374 	    !(mvm->trans->dbg.error_event_table_tlv_status &
375 	      IWL_ERROR_EVENT_TABLE_UMAC))
376 		return;
377 
378 	iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
379 
380 	if (table.valid)
381 		mvm->fwrt.dump.umac_err_id = table.error_id;
382 
383 	if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
384 		IWL_ERR(trans, "Start IWL Error Log Dump:\n");
385 		IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
386 			mvm->status, table.valid);
387 	}
388 
389 	IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
390 		iwl_fw_lookup_assert_desc(table.error_id));
391 	IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
392 	IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
393 	IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
394 	IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
395 	IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
396 	IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
397 	IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
398 	IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
399 	IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
400 	IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
401 	IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
402 	IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
403 	IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
404 }
405 
406 static void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u8 lmac_num)
407 {
408 	struct iwl_trans *trans = mvm->trans;
409 	struct iwl_error_event_table table = {};
410 	u32 val, base = mvm->trans->dbg.lmac_error_event_table[lmac_num];
411 
412 	if (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) {
413 		if (!base)
414 			base = mvm->fw->init_errlog_ptr;
415 	} else {
416 		if (!base)
417 			base = mvm->fw->inst_errlog_ptr;
418 	}
419 
420 	if (base < 0x400000) {
421 		IWL_ERR(mvm,
422 			"Not valid error log pointer 0x%08X for %s uCode\n",
423 			base,
424 			(mvm->fwrt.cur_fw_img == IWL_UCODE_INIT)
425 			? "Init" : "RT");
426 		return;
427 	}
428 
429 	/* check if there is a HW error */
430 	val = iwl_trans_read_mem32(trans, base);
431 	if (((val & ~0xf) == 0xa5a5a5a0) || ((val & ~0xf) == 0x5a5a5a50)) {
432 		int err;
433 
434 		IWL_ERR(trans, "HW error, resetting before reading\n");
435 
436 		/* reset the device */
437 		iwl_trans_sw_reset(trans);
438 
439 		err = iwl_finish_nic_init(trans, trans->trans_cfg);
440 		if (err)
441 			return;
442 	}
443 
444 	iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
445 
446 	if (table.valid)
447 		mvm->fwrt.dump.lmac_err_id[lmac_num] = table.error_id;
448 
449 	if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
450 		IWL_ERR(trans, "Start IWL Error Log Dump:\n");
451 		IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
452 			mvm->status, table.valid);
453 	}
454 
455 	/* Do not change this output - scripts rely on it */
456 
457 	IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
458 
459 	IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
460 		iwl_fw_lookup_assert_desc(table.error_id));
461 	IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
462 	IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
463 	IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
464 	IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
465 	IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
466 	IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
467 	IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
468 	IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
469 	IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
470 	IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
471 	IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
472 	IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
473 	IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
474 	IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
475 	IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
476 	IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
477 	IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
478 	IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
479 	IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
480 	IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
481 	IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
482 	IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
483 	IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
484 	IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
485 	IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
486 	IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
487 	IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
488 	IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
489 	IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
490 	IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
491 	IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
492 	IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
493 	IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
494 }
495 
496 static void iwl_mvm_dump_iml_error_log(struct iwl_mvm *mvm)
497 {
498 	struct iwl_trans *trans = mvm->trans;
499 	u32 error;
500 
501 	error = iwl_read_umac_prph(trans, UMAG_SB_CPU_2_STATUS);
502 
503 	IWL_ERR(trans, "IML/ROM dump:\n");
504 
505 	if (error & 0xFFFF0000)
506 		IWL_ERR(trans, "IML/ROM SYSASSERT:\n");
507 
508 	IWL_ERR(mvm, "0x%08X | IML/ROM error/state\n", error);
509 	IWL_ERR(mvm, "0x%08X | IML/ROM data1\n",
510 		iwl_read_umac_prph(trans, UMAG_SB_CPU_1_STATUS));
511 }
512 
513 void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
514 {
515 	if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
516 		IWL_ERR(mvm,
517 			"DEVICE_ENABLED bit is not set. Aborting dump.\n");
518 		return;
519 	}
520 
521 	iwl_mvm_dump_lmac_error_log(mvm, 0);
522 
523 	if (mvm->trans->dbg.lmac_error_event_table[1])
524 		iwl_mvm_dump_lmac_error_log(mvm, 1);
525 
526 	iwl_mvm_dump_umac_error_log(mvm);
527 
528 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
529 		iwl_mvm_dump_iml_error_log(mvm);
530 
531 	iwl_fw_error_print_fseq_regs(&mvm->fwrt);
532 }
533 
534 int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
535 			 int tid, int frame_limit, u16 ssn)
536 {
537 	struct iwl_scd_txq_cfg_cmd cmd = {
538 		.scd_queue = queue,
539 		.action = SCD_CFG_ENABLE_QUEUE,
540 		.window = frame_limit,
541 		.sta_id = sta_id,
542 		.ssn = cpu_to_le16(ssn),
543 		.tx_fifo = fifo,
544 		.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
545 			      queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
546 		.tid = tid,
547 	};
548 	int ret;
549 
550 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
551 		return -EINVAL;
552 
553 	if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
554 		 "Trying to reconfig unallocated queue %d\n", queue))
555 		return -ENXIO;
556 
557 	IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
558 
559 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
560 	WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
561 		  queue, fifo, ret);
562 
563 	return ret;
564 }
565 
566 /**
567  * iwl_mvm_send_lq_cmd() - Send link quality command
568  * @mvm: Driver data.
569  * @lq: Link quality command to send.
570  *
571  * The link quality command is sent as the last step of station creation.
572  * This is the special case in which init is set and we call a callback in
573  * this case to clear the state indicating that station creation is in
574  * progress.
575  */
576 int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)
577 {
578 	struct iwl_host_cmd cmd = {
579 		.id = LQ_CMD,
580 		.len = { sizeof(struct iwl_lq_cmd), },
581 		.flags = CMD_ASYNC,
582 		.data = { lq, },
583 	};
584 
585 	if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
586 		    iwl_mvm_has_tlc_offload(mvm)))
587 		return -EINVAL;
588 
589 	return iwl_mvm_send_cmd(mvm, &cmd);
590 }
591 
592 /**
593  * iwl_mvm_update_smps - Get a request to change the SMPS mode
594  * @mvm: Driver data.
595  * @vif: Pointer to the ieee80211_vif structure
596  * @req_type: The part of the driver who call for a change.
597  * @smps_request: The request to change the SMPS mode.
598  *
599  * Get a requst to change the SMPS mode,
600  * and change it according to all other requests in the driver.
601  */
602 void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
603 			 enum iwl_mvm_smps_type_request req_type,
604 			 enum ieee80211_smps_mode smps_request)
605 {
606 	struct iwl_mvm_vif *mvmvif;
607 	enum ieee80211_smps_mode smps_mode;
608 	int i;
609 
610 	lockdep_assert_held(&mvm->mutex);
611 
612 	/* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
613 	if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
614 		return;
615 
616 	if (vif->type == NL80211_IFTYPE_AP)
617 		smps_mode = IEEE80211_SMPS_OFF;
618 	else
619 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
620 
621 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
622 	mvmvif->smps_requests[req_type] = smps_request;
623 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
624 		if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
625 			smps_mode = IEEE80211_SMPS_STATIC;
626 			break;
627 		}
628 		if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
629 			smps_mode = IEEE80211_SMPS_DYNAMIC;
630 	}
631 
632 	ieee80211_request_smps(vif, smps_mode);
633 }
634 
635 int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
636 {
637 	struct iwl_statistics_cmd scmd = {
638 		.flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
639 	};
640 	struct iwl_host_cmd cmd = {
641 		.id = STATISTICS_CMD,
642 		.len[0] = sizeof(scmd),
643 		.data[0] = &scmd,
644 		.flags = CMD_WANT_SKB,
645 	};
646 	int ret;
647 
648 	ret = iwl_mvm_send_cmd(mvm, &cmd);
649 	if (ret)
650 		return ret;
651 
652 	iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
653 	iwl_free_resp(&cmd);
654 
655 	if (clear)
656 		iwl_mvm_accu_radio_stats(mvm);
657 
658 	return 0;
659 }
660 
661 void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
662 {
663 	mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
664 	mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
665 	mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
666 	mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
667 }
668 
669 static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
670 				   struct ieee80211_vif *vif)
671 {
672 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
673 	bool *result = _data;
674 	int i;
675 
676 	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
677 		if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
678 		    mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
679 			*result = false;
680 	}
681 }
682 
683 bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
684 {
685 	bool result = true;
686 
687 	lockdep_assert_held(&mvm->mutex);
688 
689 	if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
690 		return false;
691 
692 	if (mvm->cfg->rx_with_siso_diversity)
693 		return false;
694 
695 	ieee80211_iterate_active_interfaces_atomic(
696 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
697 			iwl_mvm_diversity_iter, &result);
698 
699 	return result;
700 }
701 
702 void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
703 				  bool low_latency, u16 mac_id)
704 {
705 	struct iwl_mac_low_latency_cmd cmd = {
706 		.mac_id = cpu_to_le32(mac_id)
707 	};
708 
709 	if (!fw_has_capa(&mvm->fw->ucode_capa,
710 			 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
711 		return;
712 
713 	if (low_latency) {
714 		/* currently we don't care about the direction */
715 		cmd.low_latency_rx = 1;
716 		cmd.low_latency_tx = 1;
717 	}
718 
719 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(LOW_LATENCY_CMD,
720 						 MAC_CONF_GROUP, 0),
721 				 0, sizeof(cmd), &cmd))
722 		IWL_ERR(mvm, "Failed to send low latency command\n");
723 }
724 
725 int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
726 			       bool low_latency,
727 			       enum iwl_mvm_low_latency_cause cause)
728 {
729 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
730 	int res;
731 	bool prev;
732 
733 	lockdep_assert_held(&mvm->mutex);
734 
735 	prev = iwl_mvm_vif_low_latency(mvmvif);
736 	iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
737 
738 	low_latency = iwl_mvm_vif_low_latency(mvmvif);
739 
740 	if (low_latency == prev)
741 		return 0;
742 
743 	iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
744 
745 	res = iwl_mvm_update_quotas(mvm, false, NULL);
746 	if (res)
747 		return res;
748 
749 	iwl_mvm_bt_coex_vif_change(mvm);
750 
751 	return iwl_mvm_power_update_mac(mvm);
752 }
753 
754 struct iwl_mvm_low_latency_iter {
755 	bool result;
756 	bool result_per_band[NUM_NL80211_BANDS];
757 };
758 
759 static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
760 {
761 	struct iwl_mvm_low_latency_iter *result = _data;
762 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
763 	enum nl80211_band band;
764 
765 	if (iwl_mvm_vif_low_latency(mvmvif)) {
766 		result->result = true;
767 
768 		if (!mvmvif->phy_ctxt)
769 			return;
770 
771 		band = mvmvif->phy_ctxt->channel->band;
772 		result->result_per_band[band] = true;
773 	}
774 }
775 
776 bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
777 {
778 	struct iwl_mvm_low_latency_iter data = {};
779 
780 	ieee80211_iterate_active_interfaces_atomic(
781 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
782 			iwl_mvm_ll_iter, &data);
783 
784 	return data.result;
785 }
786 
787 bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
788 {
789 	struct iwl_mvm_low_latency_iter data = {};
790 
791 	ieee80211_iterate_active_interfaces_atomic(
792 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
793 			iwl_mvm_ll_iter, &data);
794 
795 	return data.result_per_band[band];
796 }
797 
798 struct iwl_bss_iter_data {
799 	struct ieee80211_vif *vif;
800 	bool error;
801 };
802 
803 static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
804 				       struct ieee80211_vif *vif)
805 {
806 	struct iwl_bss_iter_data *data = _data;
807 
808 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
809 		return;
810 
811 	if (data->vif) {
812 		data->error = true;
813 		return;
814 	}
815 
816 	data->vif = vif;
817 }
818 
819 struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
820 {
821 	struct iwl_bss_iter_data bss_iter_data = {};
822 
823 	ieee80211_iterate_active_interfaces_atomic(
824 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
825 		iwl_mvm_bss_iface_iterator, &bss_iter_data);
826 
827 	if (bss_iter_data.error) {
828 		IWL_ERR(mvm, "More than one managed interface active!\n");
829 		return ERR_PTR(-EINVAL);
830 	}
831 
832 	return bss_iter_data.vif;
833 }
834 
835 struct iwl_sta_iter_data {
836 	bool assoc;
837 };
838 
839 static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
840 				       struct ieee80211_vif *vif)
841 {
842 	struct iwl_sta_iter_data *data = _data;
843 
844 	if (vif->type != NL80211_IFTYPE_STATION)
845 		return;
846 
847 	if (vif->bss_conf.assoc)
848 		data->assoc = true;
849 }
850 
851 bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
852 {
853 	struct iwl_sta_iter_data data = {
854 		.assoc = false,
855 	};
856 
857 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
858 						   IEEE80211_IFACE_ITER_NORMAL,
859 						   iwl_mvm_sta_iface_iterator,
860 						   &data);
861 	return data.assoc;
862 }
863 
864 unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
865 				    struct ieee80211_vif *vif,
866 				    bool tdls, bool cmd_q)
867 {
868 	struct iwl_fw_dbg_trigger_tlv *trigger;
869 	struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
870 	unsigned int default_timeout = cmd_q ?
871 		IWL_DEF_WD_TIMEOUT :
872 		mvm->trans->trans_cfg->base_params->wd_timeout;
873 
874 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
875 		/*
876 		 * We can't know when the station is asleep or awake, so we
877 		 * must disable the queue hang detection.
878 		 */
879 		if (fw_has_capa(&mvm->fw->ucode_capa,
880 				IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
881 		    vif && vif->type == NL80211_IFTYPE_AP)
882 			return IWL_WATCHDOG_DISABLED;
883 		return default_timeout;
884 	}
885 
886 	trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
887 	txq_timer = (void *)trigger->data;
888 
889 	if (tdls)
890 		return le32_to_cpu(txq_timer->tdls);
891 
892 	if (cmd_q)
893 		return le32_to_cpu(txq_timer->command_queue);
894 
895 	if (WARN_ON(!vif))
896 		return default_timeout;
897 
898 	switch (ieee80211_vif_type_p2p(vif)) {
899 	case NL80211_IFTYPE_ADHOC:
900 		return le32_to_cpu(txq_timer->ibss);
901 	case NL80211_IFTYPE_STATION:
902 		return le32_to_cpu(txq_timer->bss);
903 	case NL80211_IFTYPE_AP:
904 		return le32_to_cpu(txq_timer->softap);
905 	case NL80211_IFTYPE_P2P_CLIENT:
906 		return le32_to_cpu(txq_timer->p2p_client);
907 	case NL80211_IFTYPE_P2P_GO:
908 		return le32_to_cpu(txq_timer->p2p_go);
909 	case NL80211_IFTYPE_P2P_DEVICE:
910 		return le32_to_cpu(txq_timer->p2p_device);
911 	case NL80211_IFTYPE_MONITOR:
912 		return default_timeout;
913 	default:
914 		WARN_ON(1);
915 		return mvm->trans->trans_cfg->base_params->wd_timeout;
916 	}
917 }
918 
919 void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
920 			     const char *errmsg)
921 {
922 	struct iwl_fw_dbg_trigger_tlv *trig;
923 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
924 
925 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
926 				     FW_DBG_TRIGGER_MLME);
927 	if (!trig)
928 		goto out;
929 
930 	trig_mlme = (void *)trig->data;
931 
932 	if (trig_mlme->stop_connection_loss &&
933 	    --trig_mlme->stop_connection_loss)
934 		goto out;
935 
936 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
937 
938 out:
939 	ieee80211_connection_loss(vif);
940 }
941 
942 void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
943 					  struct ieee80211_vif *vif,
944 					  const struct ieee80211_sta *sta,
945 					  u16 tid)
946 {
947 	struct iwl_fw_dbg_trigger_tlv *trig;
948 	struct iwl_fw_dbg_trigger_ba *ba_trig;
949 
950 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
951 				     FW_DBG_TRIGGER_BA);
952 	if (!trig)
953 		return;
954 
955 	ba_trig = (void *)trig->data;
956 
957 	if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
958 		return;
959 
960 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
961 				"Frame from %pM timed out, tid %d",
962 				sta->addr, tid);
963 }
964 
965 u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
966 {
967 	if (!elapsed)
968 		return 0;
969 
970 	return (100 * airtime / elapsed) / USEC_PER_MSEC;
971 }
972 
973 static enum iwl_mvm_traffic_load
974 iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
975 {
976 	u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
977 
978 	if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
979 		return IWL_MVM_TRAFFIC_HIGH;
980 	if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
981 		return IWL_MVM_TRAFFIC_MEDIUM;
982 
983 	return IWL_MVM_TRAFFIC_LOW;
984 }
985 
986 struct iwl_mvm_tcm_iter_data {
987 	struct iwl_mvm *mvm;
988 	bool any_sent;
989 };
990 
991 static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
992 {
993 	struct iwl_mvm_tcm_iter_data *data = _data;
994 	struct iwl_mvm *mvm = data->mvm;
995 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
996 	bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
997 
998 	if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
999 		return;
1000 
1001 	low_latency = mvm->tcm.result.low_latency[mvmvif->id];
1002 
1003 	if (!mvm->tcm.result.change[mvmvif->id] &&
1004 	    prev == low_latency) {
1005 		iwl_mvm_update_quotas(mvm, false, NULL);
1006 		return;
1007 	}
1008 
1009 	if (prev != low_latency) {
1010 		/* this sends traffic load and updates quota as well */
1011 		iwl_mvm_update_low_latency(mvm, vif, low_latency,
1012 					   LOW_LATENCY_TRAFFIC);
1013 	} else {
1014 		iwl_mvm_update_quotas(mvm, false, NULL);
1015 	}
1016 
1017 	data->any_sent = true;
1018 }
1019 
1020 static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
1021 {
1022 	struct iwl_mvm_tcm_iter_data data = {
1023 		.mvm = mvm,
1024 		.any_sent = false,
1025 	};
1026 
1027 	mutex_lock(&mvm->mutex);
1028 
1029 	ieee80211_iterate_active_interfaces(
1030 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1031 		iwl_mvm_tcm_iter, &data);
1032 
1033 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1034 		iwl_mvm_config_scan(mvm);
1035 
1036 	mutex_unlock(&mvm->mutex);
1037 }
1038 
1039 static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
1040 {
1041 	struct iwl_mvm *mvm;
1042 	struct iwl_mvm_vif *mvmvif;
1043 	struct ieee80211_vif *vif;
1044 
1045 	mvmvif = container_of(wk, struct iwl_mvm_vif,
1046 			      uapsd_nonagg_detected_wk.work);
1047 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1048 	mvm = mvmvif->mvm;
1049 
1050 	if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
1051 		return;
1052 
1053 	/* remember that this AP is broken */
1054 	memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
1055 	       vif->bss_conf.bssid, ETH_ALEN);
1056 	mvm->uapsd_noagg_bssid_write_idx++;
1057 	if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
1058 		mvm->uapsd_noagg_bssid_write_idx = 0;
1059 
1060 	iwl_mvm_connection_loss(mvm, vif,
1061 				"AP isn't using AMPDU with uAPSD enabled");
1062 }
1063 
1064 static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
1065 					 struct ieee80211_vif *vif)
1066 {
1067 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1068 
1069 	if (vif->type != NL80211_IFTYPE_STATION)
1070 		return;
1071 
1072 	if (!vif->bss_conf.assoc)
1073 		return;
1074 
1075 	if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
1076 	    !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
1077 	    !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
1078 	    !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
1079 		return;
1080 
1081 	if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
1082 		return;
1083 
1084 	mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
1085 	IWL_INFO(mvm,
1086 		 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
1087 	schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
1088 }
1089 
1090 static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
1091 						 unsigned int elapsed,
1092 						 int mac)
1093 {
1094 	u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
1095 	u64 tpt;
1096 	unsigned long rate;
1097 	struct ieee80211_vif *vif;
1098 
1099 	rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
1100 
1101 	if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
1102 	    mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
1103 		return;
1104 
1105 	if (iwl_mvm_has_new_rx_api(mvm)) {
1106 		tpt = 8 * bytes; /* kbps */
1107 		do_div(tpt, elapsed);
1108 		rate *= 1000; /* kbps */
1109 		if (tpt < 22 * rate / 100)
1110 			return;
1111 	} else {
1112 		/*
1113 		 * the rate here is actually the threshold, in 100Kbps units,
1114 		 * so do the needed conversion from bytes to 100Kbps:
1115 		 * 100kb = bits / (100 * 1000),
1116 		 * 100kbps = 100kb / (msecs / 1000) ==
1117 		 *           (bits / (100 * 1000)) / (msecs / 1000) ==
1118 		 *           bits / (100 * msecs)
1119 		 */
1120 		tpt = (8 * bytes);
1121 		do_div(tpt, elapsed * 100);
1122 		if (tpt < rate)
1123 			return;
1124 	}
1125 
1126 	rcu_read_lock();
1127 	vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
1128 	if (vif)
1129 		iwl_mvm_uapsd_agg_disconnect(mvm, vif);
1130 	rcu_read_unlock();
1131 }
1132 
1133 static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
1134 				 struct ieee80211_vif *vif)
1135 {
1136 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1137 	u32 *band = _data;
1138 
1139 	if (!mvmvif->phy_ctxt)
1140 		return;
1141 
1142 	band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
1143 }
1144 
1145 static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
1146 					    unsigned long ts,
1147 					    bool handle_uapsd)
1148 {
1149 	unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
1150 	unsigned int uapsd_elapsed =
1151 		jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
1152 	u32 total_airtime = 0;
1153 	u32 band_airtime[NUM_NL80211_BANDS] = {0};
1154 	u32 band[NUM_MAC_INDEX_DRIVER] = {0};
1155 	int ac, mac, i;
1156 	bool low_latency = false;
1157 	enum iwl_mvm_traffic_load load, band_load;
1158 	bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
1159 
1160 	if (handle_ll)
1161 		mvm->tcm.ll_ts = ts;
1162 	if (handle_uapsd)
1163 		mvm->tcm.uapsd_nonagg_ts = ts;
1164 
1165 	mvm->tcm.result.elapsed = elapsed;
1166 
1167 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1168 						   IEEE80211_IFACE_ITER_NORMAL,
1169 						   iwl_mvm_tcm_iterator,
1170 						   &band);
1171 
1172 	for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1173 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1174 		u32 vo_vi_pkts = 0;
1175 		u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1176 
1177 		total_airtime += airtime;
1178 		band_airtime[band[mac]] += airtime;
1179 
1180 		load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1181 		mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1182 		mvm->tcm.result.load[mac] = load;
1183 		mvm->tcm.result.airtime[mac] = airtime;
1184 
1185 		for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1186 			vo_vi_pkts += mdata->rx.pkts[ac] +
1187 				      mdata->tx.pkts[ac];
1188 
1189 		/* enable immediately with enough packets but defer disabling */
1190 		if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1191 			mvm->tcm.result.low_latency[mac] = true;
1192 		else if (handle_ll)
1193 			mvm->tcm.result.low_latency[mac] = false;
1194 
1195 		if (handle_ll) {
1196 			/* clear old data */
1197 			memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1198 			memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1199 		}
1200 		low_latency |= mvm->tcm.result.low_latency[mac];
1201 
1202 		if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1203 			iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1204 							     mac);
1205 		/* clear old data */
1206 		if (handle_uapsd)
1207 			mdata->uapsd_nonagg_detect.rx_bytes = 0;
1208 		memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1209 		memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1210 	}
1211 
1212 	load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1213 	mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
1214 	mvm->tcm.result.global_load = load;
1215 
1216 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
1217 		band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1218 		mvm->tcm.result.band_load[i] = band_load;
1219 	}
1220 
1221 	/*
1222 	 * If the current load isn't low we need to force re-evaluation
1223 	 * in the TCM period, so that we can return to low load if there
1224 	 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
1225 	 * triggered by traffic).
1226 	 */
1227 	if (load != IWL_MVM_TRAFFIC_LOW)
1228 		return MVM_TCM_PERIOD;
1229 	/*
1230 	 * If low-latency is active we need to force re-evaluation after
1231 	 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
1232 	 * when there's no traffic at all.
1233 	 */
1234 	if (low_latency)
1235 		return MVM_LL_PERIOD;
1236 	/*
1237 	 * Otherwise, we don't need to run the work struct because we're
1238 	 * in the default "idle" state - traffic indication is low (which
1239 	 * also covers the "no traffic" case) and low-latency is disabled
1240 	 * so there's no state that may need to be disabled when there's
1241 	 * no traffic at all.
1242 	 *
1243 	 * Note that this has no impact on the regular scheduling of the
1244 	 * updates triggered by traffic - those happen whenever one of the
1245 	 * two timeouts expire (if there's traffic at all.)
1246 	 */
1247 	return 0;
1248 }
1249 
1250 void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1251 {
1252 	unsigned long ts = jiffies;
1253 	bool handle_uapsd =
1254 		time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1255 			       msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
1256 
1257 	spin_lock(&mvm->tcm.lock);
1258 	if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1259 		spin_unlock(&mvm->tcm.lock);
1260 		return;
1261 	}
1262 	spin_unlock(&mvm->tcm.lock);
1263 
1264 	if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1265 		mutex_lock(&mvm->mutex);
1266 		if (iwl_mvm_request_statistics(mvm, true))
1267 			handle_uapsd = false;
1268 		mutex_unlock(&mvm->mutex);
1269 	}
1270 
1271 	spin_lock(&mvm->tcm.lock);
1272 	/* re-check if somebody else won the recheck race */
1273 	if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1274 		/* calculate statistics */
1275 		unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1276 								  handle_uapsd);
1277 
1278 		/* the memset needs to be visible before the timestamp */
1279 		smp_mb();
1280 		mvm->tcm.ts = ts;
1281 		if (work_delay)
1282 			schedule_delayed_work(&mvm->tcm.work, work_delay);
1283 	}
1284 	spin_unlock(&mvm->tcm.lock);
1285 
1286 	iwl_mvm_tcm_results(mvm);
1287 }
1288 
1289 void iwl_mvm_tcm_work(struct work_struct *work)
1290 {
1291 	struct delayed_work *delayed_work = to_delayed_work(work);
1292 	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1293 					   tcm.work);
1294 
1295 	iwl_mvm_recalc_tcm(mvm);
1296 }
1297 
1298 void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1299 {
1300 	spin_lock_bh(&mvm->tcm.lock);
1301 	mvm->tcm.paused = true;
1302 	spin_unlock_bh(&mvm->tcm.lock);
1303 	if (with_cancel)
1304 		cancel_delayed_work_sync(&mvm->tcm.work);
1305 }
1306 
1307 void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1308 {
1309 	int mac;
1310 	bool low_latency = false;
1311 
1312 	spin_lock_bh(&mvm->tcm.lock);
1313 	mvm->tcm.ts = jiffies;
1314 	mvm->tcm.ll_ts = jiffies;
1315 	for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1316 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1317 
1318 		memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1319 		memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1320 		memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1321 		memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1322 
1323 		if (mvm->tcm.result.low_latency[mac])
1324 			low_latency = true;
1325 	}
1326 	/* The TCM data needs to be reset before "paused" flag changes */
1327 	smp_mb();
1328 	mvm->tcm.paused = false;
1329 
1330 	/*
1331 	 * if the current load is not low or low latency is active, force
1332 	 * re-evaluation to cover the case of no traffic.
1333 	 */
1334 	if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
1335 		schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
1336 	else if (low_latency)
1337 		schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
1338 
1339 	spin_unlock_bh(&mvm->tcm.lock);
1340 }
1341 
1342 void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1343 {
1344 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1345 
1346 	INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1347 			  iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1348 }
1349 
1350 void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1351 {
1352 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1353 
1354 	cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1355 }
1356 
1357 u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
1358 {
1359 	u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
1360 
1361 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
1362 	    mvm->trans->cfg->gp2_reg_addr)
1363 		reg_addr = mvm->trans->cfg->gp2_reg_addr;
1364 
1365 	return iwl_read_prph(mvm->trans, reg_addr);
1366 }
1367 
1368 void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
1369 {
1370 	bool ps_disabled;
1371 
1372 	lockdep_assert_held(&mvm->mutex);
1373 
1374 	/* Disable power save when reading GP2 */
1375 	ps_disabled = mvm->ps_disabled;
1376 	if (!ps_disabled) {
1377 		mvm->ps_disabled = true;
1378 		iwl_mvm_power_update_device(mvm);
1379 	}
1380 
1381 	*gp2 = iwl_mvm_get_systime(mvm);
1382 	*boottime = ktime_get_boottime_ns();
1383 
1384 	if (!ps_disabled) {
1385 		mvm->ps_disabled = ps_disabled;
1386 		iwl_mvm_power_update_device(mvm);
1387 	}
1388 }
1389