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