xref: /freebsd/sys/contrib/dev/iwlwifi/pcie/gen1_2/trans.c (revision b2bd08185e4984c70179c195f712cef5a136d21b)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2007-2015, 2018-2024 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/pci.h>
8 #include <linux/interrupt.h>
9 #include <linux/debugfs.h>
10 #include <linux/sched.h>
11 #include <linux/bitops.h>
12 #include <linux/gfp.h>
13 #include <linux/vmalloc.h>
14 #include <linux/module.h>
15 #include <linux/wait.h>
16 #include <linux/seq_file.h>
17 #if defined(__FreeBSD__)
18 #include <sys/rman.h>
19 #include <linux/delay.h>
20 #endif
21 
22 #include "iwl-drv.h"
23 #include "iwl-trans.h"
24 #include "iwl-csr.h"
25 #include "iwl-prph.h"
26 #include "iwl-scd.h"
27 #include "iwl-agn-hw.h"
28 #include "fw/error-dump.h"
29 #include "fw/dbg.h"
30 #include "fw/api/tx.h"
31 #include "fw/acpi.h"
32 #include "mei/iwl-mei.h"
33 #include "internal.h"
34 #include "iwl-fh.h"
35 #include "pcie/iwl-context-info-v2.h"
36 #include "pcie/utils.h"
37 
38 #define IWL_HOST_MON_BLOCK_PEMON	0x00
39 #define IWL_HOST_MON_BLOCK_HIPM		0x22
40 
41 #define IWL_HOST_MON_BLOCK_PEMON_VEC0	0x00
42 #define IWL_HOST_MON_BLOCK_PEMON_VEC1	0x01
43 #define IWL_HOST_MON_BLOCK_PEMON_WFPM	0x06
44 
iwl_dump_host_monitor_block(struct iwl_trans * trans,u32 block,u32 vec,u32 iter)45 static void iwl_dump_host_monitor_block(struct iwl_trans *trans,
46 					u32 block, u32 vec, u32 iter)
47 {
48 	int i;
49 
50 	IWL_ERR(trans, "Host monitor block 0x%x vector 0x%x\n", block, vec);
51 	iwl_write32(trans, CSR_MONITOR_CFG_REG, (block << 8) | vec);
52 	for (i = 0; i < iter; i++)
53 		IWL_ERR(trans, "    value [iter %d]: 0x%08x\n",
54 			i, iwl_read32(trans, CSR_MONITOR_STATUS_REG));
55 }
56 
iwl_pcie_dump_host_monitor(struct iwl_trans * trans)57 static void iwl_pcie_dump_host_monitor(struct iwl_trans *trans)
58 {
59 	switch (trans->mac_cfg->device_family) {
60 	case IWL_DEVICE_FAMILY_22000:
61 	case IWL_DEVICE_FAMILY_AX210:
62 		IWL_ERR(trans, "CSR_RESET = 0x%x\n",
63 			iwl_read32(trans, CSR_RESET));
64 		iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_PEMON,
65 					    IWL_HOST_MON_BLOCK_PEMON_VEC0, 15);
66 		iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_PEMON,
67 					    IWL_HOST_MON_BLOCK_PEMON_VEC1, 15);
68 		iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_PEMON,
69 					    IWL_HOST_MON_BLOCK_PEMON_WFPM, 15);
70 		iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_HIPM,
71 					    IWL_HOST_MON_BLOCK_PEMON_VEC0, 1);
72 		break;
73 	default:
74 		return;
75 	}
76 }
77 
78 /* extended range in FW SRAM */
79 #define IWL_FW_MEM_EXTENDED_START	0x40000
80 #define IWL_FW_MEM_EXTENDED_END		0x57FFF
81 
iwl_trans_pcie_sw_reset(struct iwl_trans * trans,bool retake_ownership)82 int iwl_trans_pcie_sw_reset(struct iwl_trans *trans, bool retake_ownership)
83 {
84 	/* Reset entire device - do controller reset (results in SHRD_HW_RST) */
85 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
86 		iwl_set_bit(trans, CSR_GP_CNTRL,
87 			    CSR_GP_CNTRL_REG_FLAG_SW_RESET);
88 		usleep_range(10000, 20000);
89 	} else {
90 		iwl_set_bit(trans, CSR_RESET,
91 			    CSR_RESET_REG_FLAG_SW_RESET);
92 		usleep_range(5000, 6000);
93 	}
94 
95 	if (retake_ownership)
96 		return iwl_pcie_prepare_card_hw(trans);
97 
98 	return 0;
99 }
100 
iwl_pcie_free_fw_monitor(struct iwl_trans * trans)101 static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans)
102 {
103 	struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon;
104 
105 	if (!fw_mon->size)
106 		return;
107 
108 	dma_free_coherent(trans->dev, fw_mon->size, fw_mon->block,
109 			  fw_mon->physical);
110 
111 	fw_mon->block = NULL;
112 	fw_mon->physical = 0;
113 	fw_mon->size = 0;
114 }
115 
iwl_pcie_alloc_fw_monitor_block(struct iwl_trans * trans,u8 max_power)116 static void iwl_pcie_alloc_fw_monitor_block(struct iwl_trans *trans,
117 					    u8 max_power)
118 {
119 	struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon;
120 	void *block = NULL;
121 	dma_addr_t physical = 0;
122 	u32 size = 0;
123 	u8 power;
124 
125 	if (fw_mon->size) {
126 		memset(fw_mon->block, 0, fw_mon->size);
127 		return;
128 	}
129 
130 	/* need at least 2 KiB, so stop at 11 */
131 	for (power = max_power; power >= 11; power--) {
132 		size = BIT(power);
133 		block = dma_alloc_coherent(trans->dev, size, &physical,
134 					   GFP_KERNEL | __GFP_NOWARN);
135 		if (!block)
136 			continue;
137 
138 		IWL_INFO(trans,
139 			 "Allocated 0x%08x bytes for firmware monitor.\n",
140 			 size);
141 		break;
142 	}
143 
144 	if (WARN_ON_ONCE(!block))
145 		return;
146 
147 	if (power != max_power)
148 		IWL_ERR(trans,
149 			"Sorry - debug buffer is only %luK while you requested %luK\n",
150 			(unsigned long)BIT(power - 10),
151 			(unsigned long)BIT(max_power - 10));
152 
153 	fw_mon->block = block;
154 	fw_mon->physical = physical;
155 	fw_mon->size = size;
156 }
157 
iwl_pcie_alloc_fw_monitor(struct iwl_trans * trans,u8 max_power)158 void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power)
159 {
160 	if (!max_power) {
161 		/* default max_power is maximum */
162 		max_power = 26;
163 	} else {
164 		max_power += 11;
165 	}
166 
167 	if (WARN(max_power > 26,
168 		 "External buffer size for monitor is too big %d, check the FW TLV\n",
169 		 max_power))
170 		return;
171 
172 	iwl_pcie_alloc_fw_monitor_block(trans, max_power);
173 }
174 
iwl_trans_pcie_read_shr(struct iwl_trans * trans,u32 reg)175 static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
176 {
177 	iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
178 		    ((reg & 0x0000ffff) | (2 << 28)));
179 	return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
180 }
181 
iwl_trans_pcie_write_shr(struct iwl_trans * trans,u32 reg,u32 val)182 static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
183 {
184 	iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
185 	iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
186 		    ((reg & 0x0000ffff) | (3 << 28)));
187 }
188 
iwl_pcie_set_pwr(struct iwl_trans * trans,bool vaux)189 static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
190 {
191 	if (trans->mac_cfg->base->apmg_not_supported)
192 		return;
193 
194 	if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
195 		iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
196 				       APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
197 				       ~APMG_PS_CTRL_MSK_PWR_SRC);
198 	else
199 		iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
200 				       APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
201 				       ~APMG_PS_CTRL_MSK_PWR_SRC);
202 }
203 
204 /* PCI registers */
205 #define PCI_CFG_RETRY_TIMEOUT	0x041
206 
iwl_pcie_apm_config(struct iwl_trans * trans)207 void iwl_pcie_apm_config(struct iwl_trans *trans)
208 {
209 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
210 	u16 lctl;
211 	u16 cap;
212 
213 	/*
214 	 * L0S states have been found to be unstable with our devices
215 	 * and in newer hardware they are not officially supported at
216 	 * all, so we must always set the L0S_DISABLED bit.
217 	 */
218 	iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_DISABLED);
219 
220 	pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
221 	trans_pcie->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
222 
223 	pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap);
224 	trans_pcie->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN;
225 	IWL_DEBUG_POWER(trans, "L1 %sabled - LTR %sabled\n",
226 			(lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis",
227 			trans_pcie->ltr_enabled ? "En" : "Dis");
228 }
229 
230 /*
231  * Start up NIC's basic functionality after it has been reset
232  * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
233  * NOTE:  This does not load uCode nor start the embedded processor
234  */
iwl_pcie_apm_init(struct iwl_trans * trans)235 static int iwl_pcie_apm_init(struct iwl_trans *trans)
236 {
237 	int ret;
238 
239 	IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
240 
241 	/*
242 	 * Use "set_bit" below rather than "write", to preserve any hardware
243 	 * bits already set by default after reset.
244 	 */
245 
246 	/* Disable L0S exit timer (platform NMI Work/Around) */
247 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_8000)
248 		iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
249 			    CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
250 
251 	/*
252 	 * Disable L0s without affecting L1;
253 	 *  don't wait for ICH L0s (ICH bug W/A)
254 	 */
255 	iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
256 		    CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
257 
258 	/* Set FH wait threshold to maximum (HW error during stress W/A) */
259 	iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
260 
261 	/*
262 	 * Enable HAP INTA (interrupt from management bus) to
263 	 * wake device's PCI Express link L1a -> L0s
264 	 */
265 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
266 		    CSR_HW_IF_CONFIG_REG_HAP_WAKE);
267 
268 	iwl_pcie_apm_config(trans);
269 
270 	/* Configure analog phase-lock-loop before activating to D0A */
271 	if (trans->mac_cfg->base->pll_cfg)
272 		iwl_set_bit(trans, CSR_ANA_PLL_CFG, CSR50_ANA_PLL_CFG_VAL);
273 
274 	ret = iwl_trans_activate_nic(trans);
275 	if (ret)
276 		return ret;
277 
278 	if (trans->cfg->host_interrupt_operation_mode) {
279 		/*
280 		 * This is a bit of an abuse - This is needed for 7260 / 3160
281 		 * only check host_interrupt_operation_mode even if this is
282 		 * not related to host_interrupt_operation_mode.
283 		 *
284 		 * Enable the oscillator to count wake up time for L1 exit. This
285 		 * consumes slightly more power (100uA) - but allows to be sure
286 		 * that we wake up from L1 on time.
287 		 *
288 		 * This looks weird: read twice the same register, discard the
289 		 * value, set a bit, and yet again, read that same register
290 		 * just to discard the value. But that's the way the hardware
291 		 * seems to like it.
292 		 */
293 		iwl_read_prph(trans, OSC_CLK);
294 		iwl_read_prph(trans, OSC_CLK);
295 		iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
296 		iwl_read_prph(trans, OSC_CLK);
297 		iwl_read_prph(trans, OSC_CLK);
298 	}
299 
300 	/*
301 	 * Enable DMA clock and wait for it to stabilize.
302 	 *
303 	 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
304 	 * bits do not disable clocks.  This preserves any hardware
305 	 * bits already set by default in "CLK_CTRL_REG" after reset.
306 	 */
307 	if (!trans->mac_cfg->base->apmg_not_supported) {
308 		iwl_write_prph(trans, APMG_CLK_EN_REG,
309 			       APMG_CLK_VAL_DMA_CLK_RQT);
310 		udelay(20);
311 
312 		/* Disable L1-Active */
313 		iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
314 				  APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
315 
316 		/* Clear the interrupt in APMG if the NIC is in RFKILL */
317 		iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
318 			       APMG_RTC_INT_STT_RFKILL);
319 	}
320 
321 	set_bit(STATUS_DEVICE_ENABLED, &trans->status);
322 
323 	return 0;
324 }
325 
326 /*
327  * Enable LP XTAL to avoid HW bug where device may consume much power if
328  * FW is not loaded after device reset. LP XTAL is disabled by default
329  * after device HW reset. Do it only if XTAL is fed by internal source.
330  * Configure device's "persistence" mode to avoid resetting XTAL again when
331  * SHRD_HW_RST occurs in S3.
332  */
iwl_pcie_apm_lp_xtal_enable(struct iwl_trans * trans)333 static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
334 {
335 	int ret;
336 	u32 apmg_gp1_reg;
337 	u32 apmg_xtal_cfg_reg;
338 	u32 dl_cfg_reg;
339 
340 	/* Force XTAL ON */
341 	iwl_trans_set_bit(trans, CSR_GP_CNTRL,
342 			  CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
343 
344 	ret = iwl_trans_pcie_sw_reset(trans, true);
345 
346 	if (!ret)
347 		ret = iwl_trans_activate_nic(trans);
348 
349 	if (WARN_ON(ret)) {
350 		/* Release XTAL ON request */
351 		iwl_trans_clear_bit(trans, CSR_GP_CNTRL,
352 				    CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
353 		return;
354 	}
355 
356 	/*
357 	 * Clear "disable persistence" to avoid LP XTAL resetting when
358 	 * SHRD_HW_RST is applied in S3.
359 	 */
360 	iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
361 				    APMG_PCIDEV_STT_VAL_PERSIST_DIS);
362 
363 	/*
364 	 * Force APMG XTAL to be active to prevent its disabling by HW
365 	 * caused by APMG idle state.
366 	 */
367 	apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
368 						    SHR_APMG_XTAL_CFG_REG);
369 	iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
370 				 apmg_xtal_cfg_reg |
371 				 SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
372 
373 	ret = iwl_trans_pcie_sw_reset(trans, true);
374 	if (ret)
375 		IWL_ERR(trans,
376 			"iwl_pcie_apm_lp_xtal_enable: failed to retake NIC ownership\n");
377 
378 	/* Enable LP XTAL by indirect access through CSR */
379 	apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
380 	iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
381 				 SHR_APMG_GP1_WF_XTAL_LP_EN |
382 				 SHR_APMG_GP1_CHICKEN_BIT_SELECT);
383 
384 	/* Clear delay line clock power up */
385 	dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
386 	iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
387 				 ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
388 
389 	/*
390 	 * Enable persistence mode to avoid LP XTAL resetting when
391 	 * SHRD_HW_RST is applied in S3.
392 	 */
393 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
394 		    CSR_HW_IF_CONFIG_REG_PERSISTENCE);
395 
396 	/*
397 	 * Clear "initialization complete" bit to move adapter from
398 	 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
399 	 */
400 	iwl_clear_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
401 
402 	/* Activates XTAL resources monitor */
403 	iwl_trans_set_bit(trans, CSR_MONITOR_CFG_REG,
404 			  CSR_MONITOR_XTAL_RESOURCES);
405 
406 	/* Release XTAL ON request */
407 	iwl_trans_clear_bit(trans, CSR_GP_CNTRL,
408 			    CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
409 	udelay(10);
410 
411 	/* Release APMG XTAL */
412 	iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
413 				 apmg_xtal_cfg_reg &
414 				 ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
415 }
416 
iwl_pcie_apm_stop_master(struct iwl_trans * trans)417 void iwl_pcie_apm_stop_master(struct iwl_trans *trans)
418 {
419 	int ret;
420 
421 	/* stop device's busmaster DMA activity */
422 
423 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
424 		iwl_set_bit(trans, CSR_GP_CNTRL,
425 			    CSR_GP_CNTRL_REG_FLAG_BUS_MASTER_DISABLE_REQ);
426 
427 		ret = iwl_poll_bits(trans, CSR_GP_CNTRL,
428 				    CSR_GP_CNTRL_REG_FLAG_BUS_MASTER_DISABLE_STATUS,
429 				    100);
430 		usleep_range(10000, 20000);
431 	} else {
432 		iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
433 
434 		ret = iwl_poll_bits(trans, CSR_RESET,
435 				    CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
436 	}
437 
438 	if (ret)
439 		IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
440 
441 	IWL_DEBUG_INFO(trans, "stop master\n");
442 }
443 
iwl_pcie_apm_stop(struct iwl_trans * trans,bool op_mode_leave)444 static void iwl_pcie_apm_stop(struct iwl_trans *trans, bool op_mode_leave)
445 {
446 	IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
447 
448 	if (op_mode_leave) {
449 		if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
450 			iwl_pcie_apm_init(trans);
451 
452 		/* inform ME that we are leaving */
453 		if (trans->mac_cfg->device_family == IWL_DEVICE_FAMILY_7000)
454 			iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
455 					  APMG_PCIDEV_STT_VAL_WAKE_ME);
456 		else if (trans->mac_cfg->device_family >=
457 			 IWL_DEVICE_FAMILY_8000) {
458 			iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
459 				    CSR_RESET_LINK_PWR_MGMT_DISABLED);
460 			iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
461 				    CSR_HW_IF_CONFIG_REG_WAKE_ME |
462 				    CSR_HW_IF_CONFIG_REG_WAKE_ME_PCIE_OWNER_EN);
463 			mdelay(1);
464 			iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
465 				      CSR_RESET_LINK_PWR_MGMT_DISABLED);
466 		}
467 		mdelay(5);
468 	}
469 
470 	clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
471 
472 	/* Stop device's DMA activity */
473 	iwl_pcie_apm_stop_master(trans);
474 
475 	if (trans->cfg->lp_xtal_workaround) {
476 		iwl_pcie_apm_lp_xtal_enable(trans);
477 		return;
478 	}
479 
480 	iwl_trans_pcie_sw_reset(trans, false);
481 
482 	/*
483 	 * Clear "initialization complete" bit to move adapter from
484 	 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
485 	 */
486 	iwl_clear_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
487 }
488 
iwl_pcie_nic_init(struct iwl_trans * trans)489 static int iwl_pcie_nic_init(struct iwl_trans *trans)
490 {
491 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
492 	int ret;
493 
494 	/* nic_init */
495 	spin_lock_bh(&trans_pcie->irq_lock);
496 	ret = iwl_pcie_apm_init(trans);
497 	spin_unlock_bh(&trans_pcie->irq_lock);
498 
499 	if (ret)
500 		return ret;
501 
502 	iwl_pcie_set_pwr(trans, false);
503 
504 	iwl_op_mode_nic_config(trans->op_mode);
505 
506 	/* Allocate the RX queue, or reset if it is already allocated */
507 	ret = iwl_pcie_rx_init(trans);
508 	if (ret)
509 		return ret;
510 
511 	/* Allocate or reset and init all Tx and Command queues */
512 	if (iwl_pcie_tx_init(trans)) {
513 		iwl_pcie_rx_free(trans);
514 		return -ENOMEM;
515 	}
516 
517 	if (trans->mac_cfg->base->shadow_reg_enable) {
518 		/* enable shadow regs in HW */
519 		iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
520 		IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
521 	}
522 
523 	return 0;
524 }
525 
526 #define HW_READY_TIMEOUT (50)
527 
528 /* Note: returns poll_bit return value, which is >= 0 if success */
iwl_pcie_set_hw_ready(struct iwl_trans * trans)529 static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
530 {
531 	int ret;
532 
533 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
534 		    CSR_HW_IF_CONFIG_REG_PCI_OWN_SET);
535 
536 	/* See if we got it */
537 	ret = iwl_poll_bits(trans, CSR_HW_IF_CONFIG_REG,
538 			    CSR_HW_IF_CONFIG_REG_PCI_OWN_SET,
539 			    HW_READY_TIMEOUT);
540 
541 	if (!ret)
542 		iwl_set_bit(trans, CSR_MBOX_SET_REG, CSR_MBOX_SET_REG_OS_ALIVE);
543 
544 	IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret ? " not" : "");
545 	return ret;
546 }
547 
548 /* Note: returns standard 0/-ERROR code */
iwl_pcie_prepare_card_hw(struct iwl_trans * trans)549 int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
550 {
551 	int ret;
552 	int iter;
553 
554 	IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
555 
556 	ret = iwl_pcie_set_hw_ready(trans);
557 	/* If the card is ready, exit 0 */
558 	if (!ret) {
559 		trans->csme_own = false;
560 		return 0;
561 	}
562 
563 	iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
564 		    CSR_RESET_LINK_PWR_MGMT_DISABLED);
565 	usleep_range(1000, 2000);
566 
567 	for (iter = 0; iter < 10; iter++) {
568 		int t = 0;
569 
570 		/* If HW is not ready, prepare the conditions to check again */
571 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
572 			    CSR_HW_IF_CONFIG_REG_WAKE_ME);
573 
574 		do {
575 			ret = iwl_pcie_set_hw_ready(trans);
576 			if (!ret) {
577 				trans->csme_own = false;
578 				return 0;
579 			}
580 
581 			if (iwl_mei_is_connected()) {
582 				IWL_DEBUG_INFO(trans,
583 					       "Couldn't prepare the card but SAP is connected\n");
584 				trans->csme_own = true;
585 				if (trans->mac_cfg->device_family !=
586 				    IWL_DEVICE_FAMILY_9000)
587 					IWL_ERR(trans,
588 						"SAP not supported for this NIC family\n");
589 
590 				return -EBUSY;
591 			}
592 
593 			usleep_range(200, 1000);
594 			t += 200;
595 		} while (t < 150000);
596 #if defined(__linux__)
597 		msleep(25);
598 #elif defined(__FreeBSD__)
599 		linux_msleep(25);
600 #endif
601 	}
602 
603 	IWL_ERR(trans, "Couldn't prepare the card\n");
604 
605 	return ret;
606 }
607 
608 /*
609  * ucode
610  */
iwl_pcie_load_firmware_chunk_fh(struct iwl_trans * trans,u32 dst_addr,dma_addr_t phy_addr,u32 byte_cnt)611 static void iwl_pcie_load_firmware_chunk_fh(struct iwl_trans *trans,
612 					    u32 dst_addr, dma_addr_t phy_addr,
613 					    u32 byte_cnt)
614 {
615 	iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
616 		    FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
617 
618 	iwl_write32(trans, FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
619 		    dst_addr);
620 
621 	iwl_write32(trans, FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
622 		    phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
623 
624 	iwl_write32(trans, FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
625 		    (iwl_get_dma_hi_addr(phy_addr)
626 			<< FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
627 
628 	iwl_write32(trans, FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
629 		    BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM) |
630 		    BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX) |
631 		    FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
632 
633 	iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
634 		    FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
635 		    FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
636 		    FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
637 }
638 
iwl_pcie_load_firmware_chunk(struct iwl_trans * trans,u32 dst_addr,dma_addr_t phy_addr,u32 byte_cnt)639 static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans,
640 					u32 dst_addr, dma_addr_t phy_addr,
641 					u32 byte_cnt)
642 {
643 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
644 	int ret;
645 
646 	trans_pcie->ucode_write_complete = false;
647 
648 	if (!iwl_trans_grab_nic_access(trans))
649 		return -EIO;
650 
651 	iwl_pcie_load_firmware_chunk_fh(trans, dst_addr, phy_addr,
652 					byte_cnt);
653 	iwl_trans_release_nic_access(trans);
654 
655 	ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
656 				 trans_pcie->ucode_write_complete, 5 * HZ);
657 	if (!ret) {
658 		IWL_ERR(trans, "Failed to load firmware chunk!\n");
659 		iwl_trans_pcie_dump_regs(trans, trans_pcie->pci_dev);
660 		return -ETIMEDOUT;
661 	}
662 
663 	return 0;
664 }
665 
iwl_pcie_load_section(struct iwl_trans * trans,u8 section_num,const struct fw_desc * section)666 static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
667 			    const struct fw_desc *section)
668 {
669 	u8 *v_addr;
670 	dma_addr_t p_addr;
671 	u32 offset, chunk_sz = min_t(u32, FH_MEM_TB_MAX_LENGTH, section->len);
672 	int ret = 0;
673 
674 	IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
675 		     section_num);
676 
677 	v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
678 				    GFP_KERNEL | __GFP_NOWARN);
679 	if (!v_addr) {
680 		IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
681 		chunk_sz = PAGE_SIZE;
682 		v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
683 					    &p_addr, GFP_KERNEL);
684 		if (!v_addr)
685 			return -ENOMEM;
686 	}
687 
688 	for (offset = 0; offset < section->len; offset += chunk_sz) {
689 		u32 copy_size, dst_addr;
690 		bool extended_addr = false;
691 
692 		copy_size = min_t(u32, chunk_sz, section->len - offset);
693 		dst_addr = section->offset + offset;
694 
695 		if (dst_addr >= IWL_FW_MEM_EXTENDED_START &&
696 		    dst_addr <= IWL_FW_MEM_EXTENDED_END)
697 			extended_addr = true;
698 
699 		if (extended_addr)
700 			iwl_set_bits_prph(trans, LMPM_CHICK,
701 					  LMPM_CHICK_EXTENDED_ADDR_SPACE);
702 
703 		memcpy(v_addr, (const u8 *)section->data + offset, copy_size);
704 		ret = iwl_pcie_load_firmware_chunk(trans, dst_addr, p_addr,
705 						   copy_size);
706 
707 		if (extended_addr)
708 			iwl_clear_bits_prph(trans, LMPM_CHICK,
709 					    LMPM_CHICK_EXTENDED_ADDR_SPACE);
710 
711 		if (ret) {
712 			IWL_ERR(trans,
713 				"Could not load the [%d] uCode section\n",
714 				section_num);
715 			break;
716 		}
717 	}
718 
719 	dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
720 	return ret;
721 }
722 
iwl_pcie_load_cpu_sections_8000(struct iwl_trans * trans,const struct fw_img * image,int cpu,int * first_ucode_section)723 static int iwl_pcie_load_cpu_sections_8000(struct iwl_trans *trans,
724 					   const struct fw_img *image,
725 					   int cpu,
726 					   int *first_ucode_section)
727 {
728 	int shift_param;
729 	int i, ret = 0, sec_num = 0x1;
730 	u32 val, last_read_idx = 0;
731 
732 	if (cpu == 1) {
733 		shift_param = 0;
734 		*first_ucode_section = 0;
735 	} else {
736 		shift_param = 16;
737 		(*first_ucode_section)++;
738 	}
739 
740 	for (i = *first_ucode_section; i < image->num_sec; i++) {
741 		last_read_idx = i;
742 
743 		/*
744 		 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
745 		 * CPU1 to CPU2.
746 		 * PAGING_SEPARATOR_SECTION delimiter - separate between
747 		 * CPU2 non paged to CPU2 paging sec.
748 		 */
749 		if (!image->sec[i].data ||
750 		    image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
751 		    image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
752 			IWL_DEBUG_FW(trans,
753 				     "Break since Data not valid or Empty section, sec = %d\n",
754 				     i);
755 			break;
756 		}
757 
758 		ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
759 		if (ret)
760 			return ret;
761 
762 		/* Notify ucode of loaded section number and status */
763 		val = iwl_read_direct32(trans, FH_UCODE_LOAD_STATUS);
764 		val = val | (sec_num << shift_param);
765 		iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, val);
766 
767 		sec_num = (sec_num << 1) | 0x1;
768 	}
769 
770 	*first_ucode_section = last_read_idx;
771 
772 	iwl_enable_interrupts(trans);
773 
774 	if (trans->mac_cfg->gen2) {
775 		if (cpu == 1)
776 			iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS,
777 				       0xFFFF);
778 		else
779 			iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS,
780 				       0xFFFFFFFF);
781 	} else {
782 		if (cpu == 1)
783 			iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS,
784 					   0xFFFF);
785 		else
786 			iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS,
787 					   0xFFFFFFFF);
788 	}
789 
790 	return 0;
791 }
792 
iwl_pcie_load_cpu_sections(struct iwl_trans * trans,const struct fw_img * image,int cpu,int * first_ucode_section)793 static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
794 				      const struct fw_img *image,
795 				      int cpu,
796 				      int *first_ucode_section)
797 {
798 	int i, ret = 0;
799 	u32 last_read_idx = 0;
800 
801 	if (cpu == 1)
802 		*first_ucode_section = 0;
803 	else
804 		(*first_ucode_section)++;
805 
806 	for (i = *first_ucode_section; i < image->num_sec; i++) {
807 		last_read_idx = i;
808 
809 		/*
810 		 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
811 		 * CPU1 to CPU2.
812 		 * PAGING_SEPARATOR_SECTION delimiter - separate between
813 		 * CPU2 non paged to CPU2 paging sec.
814 		 */
815 		if (!image->sec[i].data ||
816 		    image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
817 		    image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
818 			IWL_DEBUG_FW(trans,
819 				     "Break since Data not valid or Empty section, sec = %d\n",
820 				     i);
821 			break;
822 		}
823 
824 		ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
825 		if (ret)
826 			return ret;
827 	}
828 
829 	*first_ucode_section = last_read_idx;
830 
831 	return 0;
832 }
833 
iwl_pcie_apply_destination_ini(struct iwl_trans * trans)834 static void iwl_pcie_apply_destination_ini(struct iwl_trans *trans)
835 {
836 	enum iwl_fw_ini_allocation_id alloc_id = IWL_FW_INI_ALLOCATION_ID_DBGC1;
837 	struct iwl_fw_ini_allocation_tlv *fw_mon_cfg =
838 		&trans->dbg.fw_mon_cfg[alloc_id];
839 	struct iwl_dram_data *frag;
840 
841 	if (!iwl_trans_dbg_ini_valid(trans))
842 		return;
843 
844 	if (le32_to_cpu(fw_mon_cfg->buf_location) ==
845 	    IWL_FW_INI_LOCATION_SRAM_PATH) {
846 		IWL_DEBUG_FW(trans, "WRT: Applying SMEM buffer destination\n");
847 		/* set sram monitor by enabling bit 7 */
848 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
849 			    CSR_HW_IF_CONFIG_REG_BIT_MONITOR_SRAM);
850 
851 		return;
852 	}
853 
854 	if (le32_to_cpu(fw_mon_cfg->buf_location) !=
855 	    IWL_FW_INI_LOCATION_DRAM_PATH ||
856 	    !trans->dbg.fw_mon_ini[alloc_id].num_frags)
857 		return;
858 
859 	frag = &trans->dbg.fw_mon_ini[alloc_id].frags[0];
860 
861 	IWL_DEBUG_FW(trans, "WRT: Applying DRAM destination (alloc_id=%u)\n",
862 		     alloc_id);
863 
864 	iwl_write_umac_prph(trans, MON_BUFF_BASE_ADDR_VER2,
865 			    frag->physical >> MON_BUFF_SHIFT_VER2);
866 	iwl_write_umac_prph(trans, MON_BUFF_END_ADDR_VER2,
867 			    (frag->physical + frag->size - 256) >>
868 			    MON_BUFF_SHIFT_VER2);
869 }
870 
iwl_pcie_apply_destination(struct iwl_trans * trans)871 void iwl_pcie_apply_destination(struct iwl_trans *trans)
872 {
873 	const struct iwl_fw_dbg_dest_tlv_v1 *dest = trans->dbg.dest_tlv;
874 	const struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon;
875 	int i;
876 
877 	if (iwl_trans_dbg_ini_valid(trans)) {
878 		iwl_pcie_apply_destination_ini(trans);
879 		return;
880 	}
881 
882 	IWL_INFO(trans, "Applying debug destination %s\n",
883 		 get_fw_dbg_mode_string(dest->monitor_mode));
884 
885 	if (dest->monitor_mode == EXTERNAL_MODE)
886 		iwl_pcie_alloc_fw_monitor(trans, dest->size_power);
887 	else
888 		IWL_WARN(trans, "PCI should have external buffer debug\n");
889 
890 	for (i = 0; i < trans->dbg.n_dest_reg; i++) {
891 		u32 addr = le32_to_cpu(dest->reg_ops[i].addr);
892 		u32 val = le32_to_cpu(dest->reg_ops[i].val);
893 
894 		switch (dest->reg_ops[i].op) {
895 		case CSR_ASSIGN:
896 			iwl_write32(trans, addr, val);
897 			break;
898 		case CSR_SETBIT:
899 			iwl_set_bit(trans, addr, BIT(val));
900 			break;
901 		case CSR_CLEARBIT:
902 			iwl_clear_bit(trans, addr, BIT(val));
903 			break;
904 		case PRPH_ASSIGN:
905 			iwl_write_prph(trans, addr, val);
906 			break;
907 		case PRPH_SETBIT:
908 			iwl_set_bits_prph(trans, addr, BIT(val));
909 			break;
910 		case PRPH_CLEARBIT:
911 			iwl_clear_bits_prph(trans, addr, BIT(val));
912 			break;
913 		case PRPH_BLOCKBIT:
914 			if (iwl_read_prph(trans, addr) & BIT(val)) {
915 				IWL_ERR(trans,
916 					"BIT(%u) in address 0x%x is 1, stopping FW configuration\n",
917 					val, addr);
918 				goto monitor;
919 			}
920 			break;
921 		default:
922 			IWL_ERR(trans, "FW debug - unknown OP %d\n",
923 				dest->reg_ops[i].op);
924 			break;
925 		}
926 	}
927 
928 monitor:
929 	if (dest->monitor_mode == EXTERNAL_MODE && fw_mon->size) {
930 		iwl_write_prph(trans, le32_to_cpu(dest->base_reg),
931 			       fw_mon->physical >> dest->base_shift);
932 		if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_8000)
933 			iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
934 				       (fw_mon->physical + fw_mon->size -
935 					256) >> dest->end_shift);
936 		else
937 			iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
938 				       (fw_mon->physical + fw_mon->size) >>
939 				       dest->end_shift);
940 	}
941 }
942 
iwl_pcie_load_given_ucode(struct iwl_trans * trans,const struct fw_img * image)943 static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
944 				const struct fw_img *image)
945 {
946 	int ret = 0;
947 	int first_ucode_section;
948 
949 	IWL_DEBUG_FW(trans, "working with %s CPU\n",
950 		     image->is_dual_cpus ? "Dual" : "Single");
951 
952 	/* load to FW the binary non secured sections of CPU1 */
953 	ret = iwl_pcie_load_cpu_sections(trans, image, 1, &first_ucode_section);
954 	if (ret)
955 		return ret;
956 
957 	if (image->is_dual_cpus) {
958 		/* set CPU2 header address */
959 		iwl_write_prph(trans,
960 			       LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
961 			       LMPM_SECURE_CPU2_HDR_MEM_SPACE);
962 
963 		/* load to FW the binary sections of CPU2 */
964 		ret = iwl_pcie_load_cpu_sections(trans, image, 2,
965 						 &first_ucode_section);
966 		if (ret)
967 			return ret;
968 	}
969 
970 	if (iwl_pcie_dbg_on(trans))
971 		iwl_pcie_apply_destination(trans);
972 
973 	iwl_enable_interrupts(trans);
974 
975 	/* release CPU reset */
976 	iwl_write32(trans, CSR_RESET, 0);
977 
978 	return 0;
979 }
980 
iwl_pcie_load_given_ucode_8000(struct iwl_trans * trans,const struct fw_img * image)981 static int iwl_pcie_load_given_ucode_8000(struct iwl_trans *trans,
982 					  const struct fw_img *image)
983 {
984 	int ret = 0;
985 	int first_ucode_section;
986 
987 	IWL_DEBUG_FW(trans, "working with %s CPU\n",
988 		     image->is_dual_cpus ? "Dual" : "Single");
989 
990 	if (iwl_pcie_dbg_on(trans))
991 		iwl_pcie_apply_destination(trans);
992 
993 	IWL_DEBUG_POWER(trans, "Original WFPM value = 0x%08X\n",
994 			iwl_read_prph(trans, WFPM_GP2));
995 
996 	/*
997 	 * Set default value. On resume reading the values that were
998 	 * zeored can provide debug data on the resume flow.
999 	 * This is for debugging only and has no functional impact.
1000 	 */
1001 	iwl_write_prph(trans, WFPM_GP2, 0x01010101);
1002 
1003 	/* configure the ucode to be ready to get the secured image */
1004 	/* release CPU reset */
1005 	iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
1006 
1007 	/* load to FW the binary Secured sections of CPU1 */
1008 	ret = iwl_pcie_load_cpu_sections_8000(trans, image, 1,
1009 					      &first_ucode_section);
1010 	if (ret)
1011 		return ret;
1012 
1013 	/* load to FW the binary sections of CPU2 */
1014 	return iwl_pcie_load_cpu_sections_8000(trans, image, 2,
1015 					       &first_ucode_section);
1016 }
1017 
iwl_pcie_check_hw_rf_kill(struct iwl_trans * trans)1018 bool iwl_pcie_check_hw_rf_kill(struct iwl_trans *trans)
1019 {
1020 	struct iwl_trans_pcie *trans_pcie =  IWL_TRANS_GET_PCIE_TRANS(trans);
1021 	bool hw_rfkill = iwl_is_rfkill_set(trans);
1022 	bool prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1023 	bool report;
1024 
1025 	if (hw_rfkill) {
1026 		set_bit(STATUS_RFKILL_HW, &trans->status);
1027 		set_bit(STATUS_RFKILL_OPMODE, &trans->status);
1028 	} else {
1029 		clear_bit(STATUS_RFKILL_HW, &trans->status);
1030 		if (trans_pcie->opmode_down)
1031 			clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
1032 	}
1033 
1034 	report = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1035 
1036 	if (prev != report)
1037 		iwl_trans_pcie_rf_kill(trans, report, false);
1038 
1039 	return hw_rfkill;
1040 }
1041 
1042 struct iwl_causes_list {
1043 	u16 mask_reg;
1044 	u8 bit;
1045 	u8 addr;
1046 };
1047 
1048 #define IWL_CAUSE(reg, mask)						\
1049 	{								\
1050 		.mask_reg = reg,					\
1051 		.bit = ilog2(mask),					\
1052 		.addr = ilog2(mask) +					\
1053 			((reg) == CSR_MSIX_FH_INT_MASK_AD ? -16 :	\
1054 			 (reg) == CSR_MSIX_HW_INT_MASK_AD ? 16 :	\
1055 			 0xffff),	/* causes overflow warning */	\
1056 	}
1057 
1058 static const struct iwl_causes_list causes_list_common[] = {
1059 	IWL_CAUSE(CSR_MSIX_FH_INT_MASK_AD, MSIX_FH_INT_CAUSES_D2S_CH0_NUM),
1060 	IWL_CAUSE(CSR_MSIX_FH_INT_MASK_AD, MSIX_FH_INT_CAUSES_D2S_CH1_NUM),
1061 	IWL_CAUSE(CSR_MSIX_FH_INT_MASK_AD, MSIX_FH_INT_CAUSES_S2D),
1062 	IWL_CAUSE(CSR_MSIX_FH_INT_MASK_AD, MSIX_FH_INT_CAUSES_FH_ERR),
1063 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_ALIVE),
1064 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_WAKEUP),
1065 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_RESET_DONE),
1066 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_TOP_FATAL_ERR),
1067 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_CT_KILL),
1068 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_RF_KILL),
1069 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_PERIODIC),
1070 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_SCD),
1071 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_FH_TX),
1072 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_HW_ERR),
1073 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_HAP),
1074 };
1075 
1076 static const struct iwl_causes_list causes_list_pre_bz[] = {
1077 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_SW_ERR),
1078 };
1079 
1080 static const struct iwl_causes_list causes_list_bz[] = {
1081 	IWL_CAUSE(CSR_MSIX_HW_INT_MASK_AD, MSIX_HW_INT_CAUSES_REG_SW_ERR_BZ),
1082 };
1083 
iwl_pcie_map_list(struct iwl_trans * trans,const struct iwl_causes_list * causes,int arr_size,int val)1084 static void iwl_pcie_map_list(struct iwl_trans *trans,
1085 			      const struct iwl_causes_list *causes,
1086 			      int arr_size, int val)
1087 {
1088 	int i;
1089 
1090 	for (i = 0; i < arr_size; i++) {
1091 		iwl_write8(trans, CSR_MSIX_IVAR(causes[i].addr), val);
1092 		iwl_clear_bit(trans, causes[i].mask_reg,
1093 			      BIT(causes[i].bit));
1094 	}
1095 }
1096 
iwl_pcie_map_non_rx_causes(struct iwl_trans * trans)1097 static void iwl_pcie_map_non_rx_causes(struct iwl_trans *trans)
1098 {
1099 	struct iwl_trans_pcie *trans_pcie =  IWL_TRANS_GET_PCIE_TRANS(trans);
1100 	int val = trans_pcie->def_irq | MSIX_NON_AUTO_CLEAR_CAUSE;
1101 	/*
1102 	 * Access all non RX causes and map them to the default irq.
1103 	 * In case we are missing at least one interrupt vector,
1104 	 * the first interrupt vector will serve non-RX and FBQ causes.
1105 	 */
1106 	iwl_pcie_map_list(trans, causes_list_common,
1107 			  ARRAY_SIZE(causes_list_common), val);
1108 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
1109 		iwl_pcie_map_list(trans, causes_list_bz,
1110 				  ARRAY_SIZE(causes_list_bz), val);
1111 	else
1112 		iwl_pcie_map_list(trans, causes_list_pre_bz,
1113 				  ARRAY_SIZE(causes_list_pre_bz), val);
1114 }
1115 
iwl_pcie_map_rx_causes(struct iwl_trans * trans)1116 static void iwl_pcie_map_rx_causes(struct iwl_trans *trans)
1117 {
1118 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1119 	u32 offset =
1120 		trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
1121 	u32 val, idx;
1122 
1123 	/*
1124 	 * The first RX queue - fallback queue, which is designated for
1125 	 * management frame, command responses etc, is always mapped to the
1126 	 * first interrupt vector. The other RX queues are mapped to
1127 	 * the other (N - 2) interrupt vectors.
1128 	 */
1129 	val = BIT(MSIX_FH_INT_CAUSES_Q(0));
1130 	for (idx = 1; idx < trans->info.num_rxqs; idx++) {
1131 		iwl_write8(trans, CSR_MSIX_RX_IVAR(idx),
1132 			   MSIX_FH_INT_CAUSES_Q(idx - offset));
1133 		val |= BIT(MSIX_FH_INT_CAUSES_Q(idx));
1134 	}
1135 	iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~val);
1136 
1137 	val = MSIX_FH_INT_CAUSES_Q(0);
1138 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX)
1139 		val |= MSIX_NON_AUTO_CLEAR_CAUSE;
1140 	iwl_write8(trans, CSR_MSIX_RX_IVAR(0), val);
1141 
1142 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS)
1143 		iwl_write8(trans, CSR_MSIX_RX_IVAR(1), val);
1144 }
1145 
iwl_pcie_conf_msix_hw(struct iwl_trans_pcie * trans_pcie)1146 void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie)
1147 {
1148 	struct iwl_trans *trans = trans_pcie->trans;
1149 
1150 	if (!trans_pcie->msix_enabled) {
1151 		if (trans->mac_cfg->mq_rx_supported &&
1152 		    test_bit(STATUS_DEVICE_ENABLED, &trans->status))
1153 			iwl_write_umac_prph(trans, UREG_CHICK,
1154 					    UREG_CHICK_MSI_ENABLE);
1155 		return;
1156 	}
1157 	/*
1158 	 * The IVAR table needs to be configured again after reset,
1159 	 * but if the device is disabled, we can't write to
1160 	 * prph.
1161 	 */
1162 	if (test_bit(STATUS_DEVICE_ENABLED, &trans->status))
1163 		iwl_write_umac_prph(trans, UREG_CHICK, UREG_CHICK_MSIX_ENABLE);
1164 
1165 	/*
1166 	 * Each cause from the causes list above and the RX causes is
1167 	 * represented as a byte in the IVAR table. The first nibble
1168 	 * represents the bound interrupt vector of the cause, the second
1169 	 * represents no auto clear for this cause. This will be set if its
1170 	 * interrupt vector is bound to serve other causes.
1171 	 */
1172 	iwl_pcie_map_rx_causes(trans);
1173 
1174 	iwl_pcie_map_non_rx_causes(trans);
1175 }
1176 
iwl_pcie_init_msix(struct iwl_trans_pcie * trans_pcie)1177 static void iwl_pcie_init_msix(struct iwl_trans_pcie *trans_pcie)
1178 {
1179 	struct iwl_trans *trans = trans_pcie->trans;
1180 
1181 	iwl_pcie_conf_msix_hw(trans_pcie);
1182 
1183 	if (!trans_pcie->msix_enabled)
1184 		return;
1185 
1186 	trans_pcie->fh_init_mask = ~iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD);
1187 	trans_pcie->fh_mask = trans_pcie->fh_init_mask;
1188 	trans_pcie->hw_init_mask = ~iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD);
1189 	trans_pcie->hw_mask = trans_pcie->hw_init_mask;
1190 }
1191 
_iwl_trans_pcie_stop_device(struct iwl_trans * trans,bool from_irq)1192 static void _iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool from_irq)
1193 {
1194 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1195 
1196 	lockdep_assert_held(&trans_pcie->mutex);
1197 
1198 	if (trans_pcie->is_down)
1199 		return;
1200 
1201 	trans_pcie->is_down = true;
1202 
1203 	/* tell the device to stop sending interrupts */
1204 	iwl_disable_interrupts(trans);
1205 
1206 	/* device going down, Stop using ICT table */
1207 	iwl_pcie_disable_ict(trans);
1208 
1209 	/*
1210 	 * If a HW restart happens during firmware loading,
1211 	 * then the firmware loading might call this function
1212 	 * and later it might be called again due to the
1213 	 * restart. So don't process again if the device is
1214 	 * already dead.
1215 	 */
1216 	if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
1217 		IWL_DEBUG_INFO(trans,
1218 			       "DEVICE_ENABLED bit was set and is now cleared\n");
1219 		if (!from_irq)
1220 			iwl_pcie_synchronize_irqs(trans);
1221 		iwl_pcie_rx_napi_sync(trans);
1222 		iwl_pcie_tx_stop(trans);
1223 		iwl_pcie_rx_stop(trans);
1224 
1225 		/* Power-down device's busmaster DMA clocks */
1226 		if (!trans->mac_cfg->base->apmg_not_supported) {
1227 			iwl_write_prph(trans, APMG_CLK_DIS_REG,
1228 				       APMG_CLK_VAL_DMA_CLK_RQT);
1229 			udelay(5);
1230 		}
1231 	}
1232 
1233 	/* Make sure (redundant) we've released our request to stay awake */
1234 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
1235 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1236 			      CSR_GP_CNTRL_REG_FLAG_BZ_MAC_ACCESS_REQ);
1237 	else
1238 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1239 			      CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1240 
1241 	/* Stop the device, and put it in low power state */
1242 	iwl_pcie_apm_stop(trans, false);
1243 
1244 	/* re-take ownership to prevent other users from stealing the device */
1245 	iwl_trans_pcie_sw_reset(trans, true);
1246 
1247 	/*
1248 	 * Upon stop, the IVAR table gets erased, so msi-x won't
1249 	 * work. This causes a bug in RF-KILL flows, since the interrupt
1250 	 * that enables radio won't fire on the correct irq, and the
1251 	 * driver won't be able to handle the interrupt.
1252 	 * Configure the IVAR table again after reset.
1253 	 */
1254 	iwl_pcie_conf_msix_hw(trans_pcie);
1255 
1256 	/*
1257 	 * Upon stop, the APM issues an interrupt if HW RF kill is set.
1258 	 * This is a bug in certain verions of the hardware.
1259 	 * Certain devices also keep sending HW RF kill interrupt all
1260 	 * the time, unless the interrupt is ACKed even if the interrupt
1261 	 * should be masked. Re-ACK all the interrupts here.
1262 	 */
1263 	iwl_disable_interrupts(trans);
1264 
1265 	/* clear all status bits */
1266 	clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1267 	clear_bit(STATUS_INT_ENABLED, &trans->status);
1268 	clear_bit(STATUS_TPOWER_PMI, &trans->status);
1269 
1270 	/*
1271 	 * Even if we stop the HW, we still want the RF kill
1272 	 * interrupt
1273 	 */
1274 	iwl_enable_rfkill_int(trans);
1275 }
1276 
iwl_pcie_synchronize_irqs(struct iwl_trans * trans)1277 void iwl_pcie_synchronize_irqs(struct iwl_trans *trans)
1278 {
1279 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1280 
1281 	if (trans_pcie->msix_enabled) {
1282 		int i;
1283 
1284 		for (i = 0; i < trans_pcie->alloc_vecs; i++)
1285 			synchronize_irq(trans_pcie->msix_entries[i].vector);
1286 	} else {
1287 		synchronize_irq(trans_pcie->pci_dev->irq);
1288 	}
1289 }
1290 
iwl_trans_pcie_start_fw(struct iwl_trans * trans,const struct iwl_fw * fw,const struct fw_img * img,bool run_in_rfkill)1291 int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
1292 			    const struct iwl_fw *fw,
1293 			    const struct fw_img *img,
1294 			    bool run_in_rfkill)
1295 {
1296 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1297 	bool hw_rfkill;
1298 	int ret;
1299 
1300 	/* This may fail if AMT took ownership of the device */
1301 	if (iwl_pcie_prepare_card_hw(trans)) {
1302 		IWL_WARN(trans, "Exit HW not ready\n");
1303 		return -EIO;
1304 	}
1305 
1306 	iwl_enable_rfkill_int(trans);
1307 
1308 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
1309 
1310 	/*
1311 	 * We enabled the RF-Kill interrupt and the handler may very
1312 	 * well be running. Disable the interrupts to make sure no other
1313 	 * interrupt can be fired.
1314 	 */
1315 	iwl_disable_interrupts(trans);
1316 
1317 	/* Make sure it finished running */
1318 	iwl_pcie_synchronize_irqs(trans);
1319 
1320 	mutex_lock(&trans_pcie->mutex);
1321 
1322 	/* If platform's RF_KILL switch is NOT set to KILL */
1323 	hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
1324 	if (hw_rfkill && !run_in_rfkill) {
1325 		ret = -ERFKILL;
1326 		goto out;
1327 	}
1328 
1329 	/* Someone called stop_device, don't try to start_fw */
1330 	if (trans_pcie->is_down) {
1331 		IWL_WARN(trans,
1332 			 "Can't start_fw since the HW hasn't been started\n");
1333 		ret = -EIO;
1334 		goto out;
1335 	}
1336 
1337 	/* make sure rfkill handshake bits are cleared */
1338 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1339 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
1340 		    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1341 
1342 	/* clear (again), then enable host interrupts */
1343 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
1344 
1345 	ret = iwl_pcie_nic_init(trans);
1346 	if (ret) {
1347 		IWL_ERR(trans, "Unable to init nic\n");
1348 		goto out;
1349 	}
1350 
1351 	/*
1352 	 * Now, we load the firmware and don't want to be interrupted, even
1353 	 * by the RF-Kill interrupt (hence mask all the interrupt besides the
1354 	 * FH_TX interrupt which is needed to load the firmware). If the
1355 	 * RF-Kill switch is toggled, we will find out after having loaded
1356 	 * the firmware and return the proper value to the caller.
1357 	 */
1358 	iwl_enable_fw_load_int(trans);
1359 
1360 	/* really make sure rfkill handshake bits are cleared */
1361 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1362 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1363 
1364 	/* Load the given image to the HW */
1365 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_8000)
1366 		ret = iwl_pcie_load_given_ucode_8000(trans, img);
1367 	else
1368 		ret = iwl_pcie_load_given_ucode(trans, img);
1369 
1370 	/* re-check RF-Kill state since we may have missed the interrupt */
1371 	hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
1372 	if (hw_rfkill && !run_in_rfkill)
1373 		ret = -ERFKILL;
1374 
1375 out:
1376 	mutex_unlock(&trans_pcie->mutex);
1377 	return ret;
1378 }
1379 
iwl_trans_pcie_fw_alive(struct iwl_trans * trans)1380 void iwl_trans_pcie_fw_alive(struct iwl_trans *trans)
1381 {
1382 	iwl_pcie_reset_ict(trans);
1383 	iwl_pcie_tx_start(trans);
1384 }
1385 
iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans * trans,bool was_in_rfkill)1386 void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans,
1387 				       bool was_in_rfkill)
1388 {
1389 	bool hw_rfkill;
1390 
1391 	/*
1392 	 * Check again since the RF kill state may have changed while
1393 	 * all the interrupts were disabled, in this case we couldn't
1394 	 * receive the RF kill interrupt and update the state in the
1395 	 * op_mode.
1396 	 * Don't call the op_mode if the rkfill state hasn't changed.
1397 	 * This allows the op_mode to call stop_device from the rfkill
1398 	 * notification without endless recursion. Under very rare
1399 	 * circumstances, we might have a small recursion if the rfkill
1400 	 * state changed exactly now while we were called from stop_device.
1401 	 * This is very unlikely but can happen and is supported.
1402 	 */
1403 	hw_rfkill = iwl_is_rfkill_set(trans);
1404 	if (hw_rfkill) {
1405 		set_bit(STATUS_RFKILL_HW, &trans->status);
1406 		set_bit(STATUS_RFKILL_OPMODE, &trans->status);
1407 	} else {
1408 		clear_bit(STATUS_RFKILL_HW, &trans->status);
1409 		clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
1410 	}
1411 	if (hw_rfkill != was_in_rfkill)
1412 		iwl_trans_pcie_rf_kill(trans, hw_rfkill, false);
1413 }
1414 
iwl_trans_pcie_stop_device(struct iwl_trans * trans)1415 void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
1416 {
1417 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1418 	bool was_in_rfkill;
1419 
1420 	iwl_op_mode_time_point(trans->op_mode,
1421 			       IWL_FW_INI_TIME_POINT_HOST_DEVICE_DISABLE,
1422 			       NULL);
1423 
1424 	mutex_lock(&trans_pcie->mutex);
1425 	trans_pcie->opmode_down = true;
1426 	was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1427 	_iwl_trans_pcie_stop_device(trans, false);
1428 	iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill);
1429 	mutex_unlock(&trans_pcie->mutex);
1430 }
1431 
iwl_trans_pcie_rf_kill(struct iwl_trans * trans,bool state,bool from_irq)1432 void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state, bool from_irq)
1433 {
1434 	struct iwl_trans_pcie __maybe_unused *trans_pcie =
1435 		IWL_TRANS_GET_PCIE_TRANS(trans);
1436 
1437 	lockdep_assert_held(&trans_pcie->mutex);
1438 
1439 	IWL_WARN(trans, "reporting RF_KILL (radio %s)\n",
1440 		 state ? "disabled" : "enabled");
1441 	if (iwl_op_mode_hw_rf_kill(trans->op_mode, state) &&
1442 	    !WARN_ON(trans->mac_cfg->gen2))
1443 		_iwl_trans_pcie_stop_device(trans, from_irq);
1444 }
1445 
iwl_pcie_d3_complete_suspend(struct iwl_trans * trans,bool reset)1446 static void iwl_pcie_d3_complete_suspend(struct iwl_trans *trans,
1447 					 bool reset)
1448 {
1449 	iwl_disable_interrupts(trans);
1450 
1451 	iwl_pcie_disable_ict(trans);
1452 
1453 	iwl_pcie_synchronize_irqs(trans);
1454 
1455 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
1456 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1457 			      CSR_GP_CNTRL_REG_FLAG_BZ_MAC_ACCESS_REQ);
1458 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1459 			      CSR_GP_CNTRL_REG_FLAG_MAC_INIT);
1460 	} else {
1461 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1462 			      CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1463 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1464 			      CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1465 	}
1466 
1467 	if (reset) {
1468 		/*
1469 		 * reset TX queues -- some of their registers reset during S3
1470 		 * so if we don't reset everything here the D3 image would try
1471 		 * to execute some invalid memory upon resume
1472 		 */
1473 		iwl_trans_pcie_tx_reset(trans);
1474 	}
1475 
1476 	iwl_pcie_set_pwr(trans, true);
1477 }
1478 
iwl_pcie_d3_handshake(struct iwl_trans * trans,bool suspend)1479 static int iwl_pcie_d3_handshake(struct iwl_trans *trans, bool suspend)
1480 {
1481 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1482 	int ret;
1483 
1484 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
1485 		return 0;
1486 
1487 	trans_pcie->sx_state = IWL_SX_WAITING;
1488 
1489 	if (trans->mac_cfg->device_family == IWL_DEVICE_FAMILY_AX210)
1490 		iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
1491 				    suspend ? UREG_DOORBELL_TO_ISR6_SUSPEND :
1492 					      UREG_DOORBELL_TO_ISR6_RESUME);
1493 	else
1494 		iwl_write32(trans, CSR_IPC_SLEEP_CONTROL,
1495 			    suspend ? CSR_IPC_SLEEP_CONTROL_SUSPEND :
1496 				      CSR_IPC_SLEEP_CONTROL_RESUME);
1497 
1498 	ret = wait_event_timeout(trans_pcie->sx_waitq,
1499 				 trans_pcie->sx_state != IWL_SX_WAITING,
1500 				 2 * HZ);
1501 	if (!ret) {
1502 		IWL_ERR(trans, "Timeout %s D3\n",
1503 			suspend ? "entering" : "exiting");
1504 		ret = -ETIMEDOUT;
1505 	} else {
1506 		ret = 0;
1507 	}
1508 
1509 	if (trans_pcie->sx_state == IWL_SX_ERROR) {
1510 		IWL_ERR(trans, "FW error while %s D3\n",
1511 			suspend ? "entering" : "exiting");
1512 		ret = -EIO;
1513 	}
1514 
1515 	/* Invalidate it toward next suspend or resume */
1516 	trans_pcie->sx_state = IWL_SX_INVALID;
1517 
1518 	return ret;
1519 }
1520 
iwl_trans_pcie_d3_suspend(struct iwl_trans * trans,bool reset)1521 int iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool reset)
1522 {
1523 	int ret;
1524 
1525 	if (!reset)
1526 		/* Enable persistence mode to avoid reset */
1527 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
1528 			    CSR_HW_IF_CONFIG_REG_PERSISTENCE);
1529 
1530 	ret = iwl_pcie_d3_handshake(trans, true);
1531 	if (ret)
1532 		return ret;
1533 
1534 	iwl_pcie_d3_complete_suspend(trans, reset);
1535 
1536 	return 0;
1537 }
1538 
iwl_trans_pcie_d3_resume(struct iwl_trans * trans,bool reset)1539 int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
1540 			     bool reset)
1541 {
1542 	struct iwl_trans_pcie *trans_pcie =  IWL_TRANS_GET_PCIE_TRANS(trans);
1543 	u32 val;
1544 	int ret;
1545 
1546 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
1547 		iwl_set_bit(trans, CSR_GP_CNTRL,
1548 			    CSR_GP_CNTRL_REG_FLAG_BZ_MAC_ACCESS_REQ);
1549 	else
1550 		iwl_set_bit(trans, CSR_GP_CNTRL,
1551 			    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1552 
1553 	ret = iwl_trans_activate_nic(trans);
1554 	if (ret) {
1555 		IWL_ERR(trans, "Failed to init nic upon resume. err = %d\n",
1556 			ret);
1557 		return ret;
1558 	}
1559 
1560 	/*
1561 	 * Reconfigure IVAR table in case of MSIX or reset ict table in
1562 	 * MSI mode since HW reset erased it.
1563 	 * Also enables interrupts - none will happen as
1564 	 * the device doesn't know we're waking it up, only when
1565 	 * the opmode actually tells it after this call.
1566 	 */
1567 	iwl_pcie_conf_msix_hw(trans_pcie);
1568 	if (!trans_pcie->msix_enabled)
1569 		iwl_pcie_reset_ict(trans);
1570 	iwl_enable_interrupts(trans);
1571 
1572 	iwl_pcie_set_pwr(trans, false);
1573 
1574 	if (!reset) {
1575 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1576 			      CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1577 	} else {
1578 		iwl_trans_pcie_tx_reset(trans);
1579 
1580 		ret = iwl_pcie_rx_init(trans);
1581 		if (ret) {
1582 			IWL_ERR(trans,
1583 				"Failed to resume the device (RX reset)\n");
1584 			return ret;
1585 		}
1586 	}
1587 
1588 	IWL_DEBUG_POWER(trans, "WFPM value upon resume = 0x%08X\n",
1589 			iwl_read_umac_prph(trans, WFPM_GP2));
1590 
1591 	val = iwl_read32(trans, CSR_RESET);
1592 	if (val & CSR_RESET_REG_FLAG_NEVO_RESET) {
1593 		IWL_INFO(trans, "Device was reset during suspend\n");
1594 		trans->state = IWL_TRANS_NO_FW;
1595 		return -ENOENT;
1596 	}
1597 
1598 	return iwl_pcie_d3_handshake(trans, false);
1599 }
1600 
1601 static void
iwl_pcie_set_interrupt_capa(struct pci_dev * pdev,struct iwl_trans * trans,const struct iwl_mac_cfg * mac_cfg,struct iwl_trans_info * info)1602 iwl_pcie_set_interrupt_capa(struct pci_dev *pdev,
1603 			    struct iwl_trans *trans,
1604 			    const struct iwl_mac_cfg *mac_cfg,
1605 			    struct iwl_trans_info *info)
1606 {
1607 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1608 	int max_irqs, num_irqs, i, ret;
1609 	u16 pci_cmd;
1610 	u32 max_rx_queues = IWL_MAX_RX_HW_QUEUES;
1611 
1612 	if (!mac_cfg->mq_rx_supported)
1613 		goto enable_msi;
1614 
1615 	if (mac_cfg->device_family <= IWL_DEVICE_FAMILY_9000)
1616 		max_rx_queues = IWL_9000_MAX_RX_HW_QUEUES;
1617 
1618 	max_irqs = min_t(u32, num_online_cpus() + 2, max_rx_queues);
1619 	for (i = 0; i < max_irqs; i++)
1620 		trans_pcie->msix_entries[i].entry = i;
1621 
1622 	num_irqs = pci_enable_msix_range(pdev, trans_pcie->msix_entries,
1623 					 MSIX_MIN_INTERRUPT_VECTORS,
1624 					 max_irqs);
1625 	if (num_irqs < 0) {
1626 		IWL_DEBUG_INFO(trans,
1627 			       "Failed to enable msi-x mode (ret %d). Moving to msi mode.\n",
1628 			       num_irqs);
1629 		goto enable_msi;
1630 	}
1631 	trans_pcie->def_irq = (num_irqs == max_irqs) ? num_irqs - 1 : 0;
1632 
1633 	IWL_DEBUG_INFO(trans,
1634 		       "MSI-X enabled. %d interrupt vectors were allocated\n",
1635 		       num_irqs);
1636 
1637 	/*
1638 	 * In case the OS provides fewer interrupts than requested, different
1639 	 * causes will share the same interrupt vector as follows:
1640 	 * One interrupt less: non rx causes shared with FBQ.
1641 	 * Two interrupts less: non rx causes shared with FBQ and RSS.
1642 	 * More than two interrupts: we will use fewer RSS queues.
1643 	 */
1644 	if (num_irqs <= max_irqs - 2) {
1645 		info->num_rxqs = num_irqs + 1;
1646 		trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX |
1647 			IWL_SHARED_IRQ_FIRST_RSS;
1648 	} else if (num_irqs == max_irqs - 1) {
1649 		info->num_rxqs = num_irqs;
1650 		trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX;
1651 	} else {
1652 		info->num_rxqs = num_irqs - 1;
1653 	}
1654 
1655 	IWL_DEBUG_INFO(trans,
1656 		       "MSI-X enabled with rx queues %d, vec mask 0x%x\n",
1657 		       info->num_rxqs, trans_pcie->shared_vec_mask);
1658 
1659 	WARN_ON(info->num_rxqs > IWL_MAX_RX_HW_QUEUES);
1660 
1661 	trans_pcie->alloc_vecs = num_irqs;
1662 	trans_pcie->msix_enabled = true;
1663 	return;
1664 
1665 enable_msi:
1666 	info->num_rxqs = 1;
1667 	ret = pci_enable_msi(pdev);
1668 	if (ret) {
1669 		dev_err(&pdev->dev, "pci_enable_msi failed - %d\n", ret);
1670 		/* enable rfkill interrupt: hw bug w/a */
1671 		pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
1672 		if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
1673 			pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
1674 			pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
1675 		}
1676 	}
1677 }
1678 
iwl_pcie_irq_set_affinity(struct iwl_trans * trans,struct iwl_trans_info * info)1679 static void iwl_pcie_irq_set_affinity(struct iwl_trans *trans,
1680 				      struct iwl_trans_info *info)
1681 {
1682 #if defined(CONFIG_SMP)
1683 	int iter_rx_q, i, ret, cpu, offset;
1684 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1685 
1686 	i = trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 0 : 1;
1687 	iter_rx_q = info->num_rxqs - 1 + i;
1688 	offset = 1 + i;
1689 	for (; i < iter_rx_q ; i++) {
1690 		/*
1691 		 * Get the cpu prior to the place to search
1692 		 * (i.e. return will be > i - 1).
1693 		 */
1694 		cpu = cpumask_next(i - offset, cpu_online_mask);
1695 		cpumask_set_cpu(cpu, &trans_pcie->affinity_mask[i]);
1696 		ret = irq_set_affinity_hint(trans_pcie->msix_entries[i].vector,
1697 					    &trans_pcie->affinity_mask[i]);
1698 		if (ret)
1699 			IWL_ERR(trans_pcie->trans,
1700 				"Failed to set affinity mask for IRQ %d\n",
1701 				trans_pcie->msix_entries[i].vector);
1702 	}
1703 #endif
1704 }
1705 
iwl_pcie_init_msix_handler(struct pci_dev * pdev,struct iwl_trans_pcie * trans_pcie,struct iwl_trans_info * info)1706 static int iwl_pcie_init_msix_handler(struct pci_dev *pdev,
1707 				      struct iwl_trans_pcie *trans_pcie,
1708 				      struct iwl_trans_info *info)
1709 {
1710 	int i;
1711 
1712 	for (i = 0; i < trans_pcie->alloc_vecs; i++) {
1713 		int ret;
1714 		struct msix_entry *msix_entry;
1715 		const char *qname = queue_name(&pdev->dev, trans_pcie, i);
1716 
1717 		if (!qname)
1718 			return -ENOMEM;
1719 
1720 		msix_entry = &trans_pcie->msix_entries[i];
1721 		ret = devm_request_threaded_irq(&pdev->dev,
1722 						msix_entry->vector,
1723 						iwl_pcie_msix_isr,
1724 						(i == trans_pcie->def_irq) ?
1725 						iwl_pcie_irq_msix_handler :
1726 						iwl_pcie_irq_rx_msix_handler,
1727 						IRQF_SHARED,
1728 						qname,
1729 						msix_entry);
1730 		if (ret) {
1731 			IWL_ERR(trans_pcie->trans,
1732 				"Error allocating IRQ %d\n", i);
1733 
1734 			return ret;
1735 		}
1736 	}
1737 	iwl_pcie_irq_set_affinity(trans_pcie->trans, info);
1738 
1739 	return 0;
1740 }
1741 
iwl_trans_pcie_clear_persistence_bit(struct iwl_trans * trans)1742 static int iwl_trans_pcie_clear_persistence_bit(struct iwl_trans *trans)
1743 {
1744 	u32 hpm, wprot;
1745 
1746 	switch (trans->mac_cfg->device_family) {
1747 	case IWL_DEVICE_FAMILY_9000:
1748 		wprot = PREG_PRPH_WPROT_9000;
1749 		break;
1750 	case IWL_DEVICE_FAMILY_22000:
1751 		wprot = PREG_PRPH_WPROT_22000;
1752 		break;
1753 	default:
1754 		return 0;
1755 	}
1756 
1757 	hpm = iwl_read_umac_prph_no_grab(trans, HPM_DEBUG);
1758 	if (!iwl_trans_is_hw_error_value(hpm) && (hpm & PERSISTENCE_BIT)) {
1759 		u32 wprot_val = iwl_read_umac_prph_no_grab(trans, wprot);
1760 
1761 		if (wprot_val & PREG_WFPM_ACCESS) {
1762 			IWL_ERR(trans,
1763 				"Error, can not clear persistence bit\n");
1764 			return -EPERM;
1765 		}
1766 		iwl_write_umac_prph_no_grab(trans, HPM_DEBUG,
1767 					    hpm & ~PERSISTENCE_BIT);
1768 	}
1769 
1770 	return 0;
1771 }
1772 
iwl_pcie_gen2_force_power_gating(struct iwl_trans * trans)1773 static int iwl_pcie_gen2_force_power_gating(struct iwl_trans *trans)
1774 {
1775 	int ret;
1776 
1777 	ret = iwl_trans_activate_nic(trans);
1778 	if (ret < 0)
1779 		return ret;
1780 
1781 	iwl_set_bits_prph(trans, HPM_HIPM_GEN_CFG,
1782 			  HPM_HIPM_GEN_CFG_CR_FORCE_ACTIVE);
1783 	udelay(20);
1784 	iwl_set_bits_prph(trans, HPM_HIPM_GEN_CFG,
1785 			  HPM_HIPM_GEN_CFG_CR_PG_EN |
1786 			  HPM_HIPM_GEN_CFG_CR_SLP_EN);
1787 	udelay(20);
1788 	iwl_clear_bits_prph(trans, HPM_HIPM_GEN_CFG,
1789 			    HPM_HIPM_GEN_CFG_CR_FORCE_ACTIVE);
1790 
1791 	return iwl_trans_pcie_sw_reset(trans, true);
1792 }
1793 
_iwl_trans_pcie_start_hw(struct iwl_trans * trans)1794 int _iwl_trans_pcie_start_hw(struct iwl_trans *trans)
1795 {
1796 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1797 	int err;
1798 
1799 	lockdep_assert_held(&trans_pcie->mutex);
1800 
1801 	err = iwl_pcie_prepare_card_hw(trans);
1802 	if (err) {
1803 		IWL_ERR(trans, "Error while preparing HW: %d\n", err);
1804 		return err;
1805 	}
1806 
1807 	err = iwl_trans_pcie_clear_persistence_bit(trans);
1808 	if (err)
1809 		return err;
1810 
1811 	err = iwl_trans_pcie_sw_reset(trans, true);
1812 	if (err)
1813 		return err;
1814 
1815 	if (trans->mac_cfg->device_family == IWL_DEVICE_FAMILY_22000 &&
1816 	    trans->mac_cfg->integrated) {
1817 		err = iwl_pcie_gen2_force_power_gating(trans);
1818 		if (err)
1819 			return err;
1820 	}
1821 
1822 	err = iwl_pcie_apm_init(trans);
1823 	if (err)
1824 		return err;
1825 
1826 	iwl_pcie_init_msix(trans_pcie);
1827 
1828 	/* From now on, the op_mode will be kept updated about RF kill state */
1829 	iwl_enable_rfkill_int(trans);
1830 
1831 	trans_pcie->opmode_down = false;
1832 
1833 	/* Set is_down to false here so that...*/
1834 	trans_pcie->is_down = false;
1835 
1836 	/* ...rfkill can call stop_device and set it false if needed */
1837 	iwl_pcie_check_hw_rf_kill(trans);
1838 
1839 	return 0;
1840 }
1841 
iwl_trans_pcie_start_hw(struct iwl_trans * trans)1842 int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
1843 {
1844 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1845 	int ret;
1846 
1847 	mutex_lock(&trans_pcie->mutex);
1848 	ret = _iwl_trans_pcie_start_hw(trans);
1849 	mutex_unlock(&trans_pcie->mutex);
1850 
1851 	return ret;
1852 }
1853 
iwl_trans_pcie_op_mode_leave(struct iwl_trans * trans)1854 void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
1855 {
1856 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1857 
1858 	mutex_lock(&trans_pcie->mutex);
1859 
1860 	/* disable interrupts - don't enable HW RF kill interrupt */
1861 	iwl_disable_interrupts(trans);
1862 
1863 	iwl_pcie_apm_stop(trans, true);
1864 
1865 	iwl_disable_interrupts(trans);
1866 
1867 	iwl_pcie_disable_ict(trans);
1868 
1869 	mutex_unlock(&trans_pcie->mutex);
1870 
1871 	iwl_pcie_synchronize_irqs(trans);
1872 }
1873 
1874 #if defined(__linux__)
iwl_trans_pcie_write8(struct iwl_trans * trans,u32 ofs,u8 val)1875 void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
1876 {
1877 	writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
1878 }
1879 
iwl_trans_pcie_write32(struct iwl_trans * trans,u32 ofs,u32 val)1880 void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
1881 {
1882 	writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
1883 }
1884 
iwl_trans_pcie_read32(struct iwl_trans * trans,u32 ofs)1885 u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
1886 {
1887 	return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
1888 }
1889 #elif defined(__FreeBSD__)
iwl_trans_pcie_write8(struct iwl_trans * trans,u32 ofs,u8 val)1890 void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
1891 {
1892 	bus_write_1((struct resource *)IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base, ofs, val);
1893 }
1894 
iwl_trans_pcie_write32(struct iwl_trans * trans,u32 ofs,u32 val)1895 void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
1896 {
1897 	bus_write_4((struct resource *)IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base, ofs, val);
1898 }
1899 
iwl_trans_pcie_read32(struct iwl_trans * trans,u32 ofs)1900 u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
1901 {
1902 	u32 v;
1903 
1904 	v = bus_read_4((struct resource *)IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base, ofs);
1905 	return (v);
1906 }
1907 #endif
1908 
iwl_trans_pcie_prph_msk(struct iwl_trans * trans)1909 static u32 iwl_trans_pcie_prph_msk(struct iwl_trans *trans)
1910 {
1911 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1912 		return 0x00FFFFFF;
1913 	else
1914 		return 0x000FFFFF;
1915 }
1916 
iwl_trans_pcie_read_prph(struct iwl_trans * trans,u32 reg)1917 u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
1918 {
1919 	u32 mask = iwl_trans_pcie_prph_msk(trans);
1920 
1921 	iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
1922 			       ((reg & mask) | (3 << 24)));
1923 	return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
1924 }
1925 
iwl_trans_pcie_write_prph(struct iwl_trans * trans,u32 addr,u32 val)1926 void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr, u32 val)
1927 {
1928 	u32 mask = iwl_trans_pcie_prph_msk(trans);
1929 
1930 	iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
1931 			       ((addr & mask) | (3 << 24)));
1932 	iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
1933 }
1934 
iwl_pcie_gen1_2_op_mode_enter(struct iwl_trans * trans)1935 void iwl_pcie_gen1_2_op_mode_enter(struct iwl_trans *trans)
1936 {
1937 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1938 
1939 	/* free all first - we might be reconfigured for a different size */
1940 	iwl_pcie_free_rbs_pool(trans);
1941 
1942 	trans_pcie->rx_page_order =
1943 		iwl_trans_get_rb_size_order(trans->conf.rx_buf_size);
1944 	trans_pcie->rx_buf_bytes =
1945 		iwl_trans_get_rb_size(trans->conf.rx_buf_size);
1946 }
1947 
iwl_trans_pcie_free_pnvm_dram_regions(struct iwl_dram_regions * dram_regions,struct device * dev)1948 void iwl_trans_pcie_free_pnvm_dram_regions(struct iwl_dram_regions *dram_regions,
1949 					   struct device *dev)
1950 {
1951 	u8 i;
1952 	struct iwl_dram_data *desc_dram = &dram_regions->prph_scratch_mem_desc;
1953 
1954 	/* free DRAM payloads */
1955 	for (i = 0; i < dram_regions->n_regions; i++) {
1956 		dma_free_coherent(dev, dram_regions->drams[i].size,
1957 				  dram_regions->drams[i].block,
1958 				  dram_regions->drams[i].physical);
1959 	}
1960 	dram_regions->n_regions = 0;
1961 
1962 	/* free DRAM addresses array */
1963 	if (desc_dram->block) {
1964 		dma_free_coherent(dev, desc_dram->size,
1965 				  desc_dram->block,
1966 				  desc_dram->physical);
1967 	}
1968 	memset(desc_dram, 0, sizeof(*desc_dram));
1969 }
1970 
iwl_pcie_free_invalid_tx_cmd(struct iwl_trans * trans)1971 static void iwl_pcie_free_invalid_tx_cmd(struct iwl_trans *trans)
1972 {
1973 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1974 
1975 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->invalid_tx_cmd);
1976 }
1977 
iwl_pcie_alloc_invalid_tx_cmd(struct iwl_trans * trans)1978 static int iwl_pcie_alloc_invalid_tx_cmd(struct iwl_trans *trans)
1979 {
1980 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1981 	struct iwl_cmd_header_wide bad_cmd = {
1982 		.cmd = INVALID_WR_PTR_CMD,
1983 		.group_id = DEBUG_GROUP,
1984 		.sequence = cpu_to_le16(0xffff),
1985 		.length = cpu_to_le16(0),
1986 		.version = 0,
1987 	};
1988 	int ret;
1989 
1990 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->invalid_tx_cmd,
1991 				     sizeof(bad_cmd));
1992 	if (ret)
1993 		return ret;
1994 	memcpy(trans_pcie->invalid_tx_cmd.addr, &bad_cmd, sizeof(bad_cmd));
1995 	return 0;
1996 }
1997 
iwl_trans_pcie_free(struct iwl_trans * trans)1998 void iwl_trans_pcie_free(struct iwl_trans *trans)
1999 {
2000 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2001 	int i;
2002 
2003 	iwl_pcie_synchronize_irqs(trans);
2004 
2005 	if (trans->mac_cfg->gen2)
2006 		iwl_txq_gen2_tx_free(trans);
2007 	else
2008 		iwl_pcie_tx_free(trans);
2009 	iwl_pcie_rx_free(trans);
2010 
2011 	if (trans_pcie->rba.alloc_wq) {
2012 		destroy_workqueue(trans_pcie->rba.alloc_wq);
2013 		trans_pcie->rba.alloc_wq = NULL;
2014 	}
2015 
2016 	if (trans_pcie->msix_enabled) {
2017 		for (i = 0; i < trans_pcie->alloc_vecs; i++) {
2018 			irq_set_affinity_hint(
2019 				trans_pcie->msix_entries[i].vector,
2020 				NULL);
2021 		}
2022 
2023 		trans_pcie->msix_enabled = false;
2024 	} else {
2025 		iwl_pcie_free_ict(trans);
2026 	}
2027 
2028 	free_netdev(trans_pcie->napi_dev);
2029 
2030 	iwl_pcie_free_invalid_tx_cmd(trans);
2031 
2032 	iwl_pcie_free_fw_monitor(trans);
2033 
2034 	iwl_trans_pcie_free_pnvm_dram_regions(&trans_pcie->pnvm_data,
2035 					      trans->dev);
2036 	iwl_trans_pcie_free_pnvm_dram_regions(&trans_pcie->reduced_tables_data,
2037 					      trans->dev);
2038 
2039 	mutex_destroy(&trans_pcie->mutex);
2040 
2041 #ifdef CONFIG_INET
2042 	if (trans_pcie->txqs.tso_hdr_page) {
2043 		for_each_possible_cpu(i) {
2044 			struct iwl_tso_hdr_page *p =
2045 				per_cpu_ptr(trans_pcie->txqs.tso_hdr_page, i);
2046 
2047 			if (p && p->page)
2048 				__free_page(p->page);
2049 		}
2050 
2051 		free_percpu(trans_pcie->txqs.tso_hdr_page);
2052 	}
2053 #endif
2054 
2055 	kmem_cache_destroy(trans_pcie->dev_cmd_pool);
2056 	iwl_trans_free(trans);
2057 }
2058 
2059 static union acpi_object *
iwl_trans_pcie_call_prod_reset_dsm(struct pci_dev * pdev,u16 cmd,u16 value)2060 iwl_trans_pcie_call_prod_reset_dsm(struct pci_dev *pdev, u16 cmd, u16 value)
2061 {
2062 #ifdef CONFIG_ACPI
2063 	struct iwl_dsm_internal_product_reset_cmd pldr_arg = {
2064 		.cmd = cmd,
2065 		.value = value,
2066 	};
2067 	union acpi_object arg = {
2068 		.buffer.type = ACPI_TYPE_BUFFER,
2069 		.buffer.length = sizeof(pldr_arg),
2070 		.buffer.pointer = (void *)&pldr_arg,
2071 	};
2072 	static const guid_t dsm_guid = GUID_INIT(0x7266172C, 0x220B, 0x4B29,
2073 						 0x81, 0x4F, 0x75, 0xE4,
2074 						 0xDD, 0x26, 0xB5, 0xFD);
2075 
2076 	if (!acpi_check_dsm(ACPI_HANDLE(&pdev->dev), &dsm_guid, ACPI_DSM_REV,
2077 			    DSM_INTERNAL_FUNC_PRODUCT_RESET))
2078 		return ERR_PTR(-ENODEV);
2079 
2080 	return iwl_acpi_get_dsm_object(&pdev->dev, ACPI_DSM_REV,
2081 				       DSM_INTERNAL_FUNC_PRODUCT_RESET,
2082 				       &arg, &dsm_guid);
2083 #else
2084 	return ERR_PTR(-EOPNOTSUPP);
2085 #endif
2086 }
2087 
iwl_trans_pcie_check_product_reset_mode(struct pci_dev * pdev)2088 void iwl_trans_pcie_check_product_reset_mode(struct pci_dev *pdev)
2089 {
2090 	union acpi_object *res;
2091 
2092 	res = iwl_trans_pcie_call_prod_reset_dsm(pdev,
2093 						 DSM_INTERNAL_PLDR_CMD_GET_MODE,
2094 						 0);
2095 	if (IS_ERR(res))
2096 		return;
2097 
2098 	if (res->type != ACPI_TYPE_INTEGER)
2099 		IWL_ERR_DEV(&pdev->dev,
2100 			    "unexpected return type from product reset DSM\n");
2101 	else
2102 		IWL_DEBUG_DEV_POWER(&pdev->dev,
2103 				    "product reset mode is 0x%llx\n",
2104 				    res->integer.value);
2105 
2106 	ACPI_FREE(res);
2107 }
2108 
iwl_trans_pcie_set_product_reset(struct pci_dev * pdev,bool enable,bool integrated)2109 static void iwl_trans_pcie_set_product_reset(struct pci_dev *pdev, bool enable,
2110 					     bool integrated)
2111 {
2112 	union acpi_object *res;
2113 	u16 mode = enable ? DSM_INTERNAL_PLDR_MODE_EN_PROD_RESET : 0;
2114 
2115 	if (!integrated)
2116 		mode |= DSM_INTERNAL_PLDR_MODE_EN_WIFI_FLR |
2117 			DSM_INTERNAL_PLDR_MODE_EN_BT_OFF_ON;
2118 
2119 	res = iwl_trans_pcie_call_prod_reset_dsm(pdev,
2120 						 DSM_INTERNAL_PLDR_CMD_SET_MODE,
2121 						 mode);
2122 	if (IS_ERR(res)) {
2123 		if (enable)
2124 			IWL_ERR_DEV(&pdev->dev,
2125 				    "ACPI _DSM not available (%d), cannot do product reset\n",
2126 				    (int)PTR_ERR(res));
2127 		return;
2128 	}
2129 
2130 	ACPI_FREE(res);
2131 	IWL_DEBUG_DEV_POWER(&pdev->dev, "%sabled product reset via DSM\n",
2132 			    enable ? "En" : "Dis");
2133 	iwl_trans_pcie_check_product_reset_mode(pdev);
2134 }
2135 
iwl_trans_pcie_check_product_reset_status(struct pci_dev * pdev)2136 void iwl_trans_pcie_check_product_reset_status(struct pci_dev *pdev)
2137 {
2138 	union acpi_object *res;
2139 
2140 	res = iwl_trans_pcie_call_prod_reset_dsm(pdev,
2141 						 DSM_INTERNAL_PLDR_CMD_GET_STATUS,
2142 						 0);
2143 	if (IS_ERR(res))
2144 		return;
2145 
2146 	if (res->type != ACPI_TYPE_INTEGER)
2147 		IWL_ERR_DEV(&pdev->dev,
2148 			    "unexpected return type from product reset DSM\n");
2149 	else
2150 		IWL_DEBUG_DEV_POWER(&pdev->dev,
2151 				    "product reset status is 0x%llx\n",
2152 				    res->integer.value);
2153 
2154 	ACPI_FREE(res);
2155 }
2156 
iwl_trans_pcie_call_reset(struct pci_dev * pdev)2157 static void iwl_trans_pcie_call_reset(struct pci_dev *pdev)
2158 {
2159 #ifdef CONFIG_ACPI
2160 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2161 	union acpi_object *p, *ref;
2162 	acpi_status status;
2163 	int ret = -EINVAL;
2164 
2165 	status = acpi_evaluate_object(ACPI_HANDLE(&pdev->dev),
2166 				      "_PRR", NULL, &buffer);
2167 	if (ACPI_FAILURE(status)) {
2168 		IWL_DEBUG_DEV_POWER(&pdev->dev, "No _PRR method found\n");
2169 		goto out;
2170 	}
2171 	p = buffer.pointer;
2172 
2173 	if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 1) {
2174 		pci_err(pdev, "Bad _PRR return type\n");
2175 		goto out;
2176 	}
2177 
2178 	ref = &p->package.elements[0];
2179 	if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2180 		pci_err(pdev, "_PRR wasn't a reference\n");
2181 		goto out;
2182 	}
2183 
2184 	status = acpi_evaluate_object(ref->reference.handle,
2185 				      "_RST", NULL, NULL);
2186 	if (ACPI_FAILURE(status)) {
2187 		pci_err(pdev,
2188 			"Failed to call _RST on object returned by _PRR (%d)\n",
2189 			status);
2190 		goto out;
2191 	}
2192 	ret = 0;
2193 out:
2194 	kfree(buffer.pointer);
2195 	if (!ret) {
2196 		IWL_DEBUG_DEV_POWER(&pdev->dev, "called _RST on _PRR object\n");
2197 		return;
2198 	}
2199 	IWL_DEBUG_DEV_POWER(&pdev->dev,
2200 			    "No BIOS support, using pci_reset_function()\n");
2201 #endif
2202 	pci_reset_function(pdev);
2203 }
2204 
2205 struct iwl_trans_pcie_removal {
2206 	struct pci_dev *pdev;
2207 	struct work_struct work;
2208 	enum iwl_reset_mode mode;
2209 	bool integrated;
2210 };
2211 
iwl_trans_pcie_removal_wk(struct work_struct * wk)2212 static void iwl_trans_pcie_removal_wk(struct work_struct *wk)
2213 {
2214 	struct iwl_trans_pcie_removal *removal =
2215 		container_of(wk, struct iwl_trans_pcie_removal, work);
2216 	struct pci_dev *pdev = removal->pdev;
2217 	static char *prop[] = {"EVENT=INACCESSIBLE", NULL};
2218 	struct pci_bus *bus;
2219 
2220 	pci_lock_rescan_remove();
2221 
2222 	bus = pdev->bus;
2223 	/* in this case, something else already removed the device */
2224 	if (!bus)
2225 		goto out;
2226 
2227 	kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, prop);
2228 
2229 	if (removal->mode == IWL_RESET_MODE_PROD_RESET) {
2230 		struct pci_dev *bt = NULL;
2231 
2232 		if (!removal->integrated) {
2233 			/* discrete devices have WiFi/BT at function 0/1 */
2234 			int slot = PCI_SLOT(pdev->devfn);
2235 			int func = PCI_FUNC(pdev->devfn);
2236 
2237 			if (func == 0)
2238 				bt = pci_get_slot(bus, PCI_DEVFN(slot, 1));
2239 			else
2240 				pci_info(pdev, "Unexpected function %d\n",
2241 					 func);
2242 		} else {
2243 			/* on integrated we have to look up by ID (same bus) */
2244 			static const struct pci_device_id bt_device_ids[] = {
2245 #define BT_DEV(_id) { PCI_DEVICE(PCI_VENDOR_ID_INTEL, _id) }
2246 				BT_DEV(0xA876), /* LNL */
2247 				BT_DEV(0xE476), /* PTL-P */
2248 				BT_DEV(0xE376), /* PTL-H */
2249 				BT_DEV(0xD346), /* NVL-H */
2250 				BT_DEV(0x6E74), /* NVL-S */
2251 				BT_DEV(0x4D76), /* WCL */
2252 				BT_DEV(0xD246), /* RZL-H */
2253 				BT_DEV(0x6C46), /* RZL-M */
2254 				{}
2255 			};
2256 			struct pci_dev *tmp = NULL;
2257 
2258 			for_each_pci_dev(tmp) {
2259 				if (tmp->bus != bus)
2260 					continue;
2261 
2262 				if (pci_match_id(bt_device_ids, tmp)) {
2263 					bt = tmp;
2264 					break;
2265 				}
2266 			}
2267 		}
2268 
2269 		if (bt) {
2270 			pci_info(bt, "Removal by WiFi due to product reset\n");
2271 			pci_stop_and_remove_bus_device(bt);
2272 			pci_dev_put(bt);
2273 		}
2274 	}
2275 
2276 	iwl_trans_pcie_set_product_reset(pdev,
2277 					 removal->mode ==
2278 						IWL_RESET_MODE_PROD_RESET,
2279 					 removal->integrated);
2280 	if (removal->mode >= IWL_RESET_MODE_FUNC_RESET)
2281 		iwl_trans_pcie_call_reset(pdev);
2282 
2283 	pci_stop_and_remove_bus_device(pdev);
2284 	pci_dev_put(pdev);
2285 
2286 	if (removal->mode >= IWL_RESET_MODE_RESCAN) {
2287 #if defined(__linux__)
2288 		if (bus->parent)
2289 			bus = bus->parent;
2290 #elif defined(__FreeBSD__)
2291 		/* XXX-TODO */
2292 #endif
2293 		pci_rescan_bus(bus);
2294 	}
2295 
2296 out:
2297 	pci_unlock_rescan_remove();
2298 
2299 	kfree(removal);
2300 	module_put(THIS_MODULE);
2301 }
2302 
iwl_trans_pcie_reset(struct iwl_trans * trans,enum iwl_reset_mode mode)2303 void iwl_trans_pcie_reset(struct iwl_trans *trans, enum iwl_reset_mode mode)
2304 {
2305 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2306 	struct iwl_trans_pcie_removal *removal;
2307 	char _msg = 0, *msg = &_msg;
2308 
2309 	if (WARN_ON(mode < IWL_RESET_MODE_REMOVE_ONLY ||
2310 		    mode == IWL_RESET_MODE_BACKOFF))
2311 		return;
2312 
2313 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
2314 		return;
2315 
2316 	if (trans_pcie->me_present && mode == IWL_RESET_MODE_PROD_RESET) {
2317 		mode = IWL_RESET_MODE_FUNC_RESET;
2318 		if (trans_pcie->me_present < 0)
2319 			msg = " instead of product reset as ME may be present";
2320 		else
2321 			msg = " instead of product reset as ME is present";
2322 	}
2323 
2324 	IWL_INFO(trans, "scheduling reset (mode=%d%s)\n", mode, msg);
2325 
2326 	iwl_pcie_dump_csr(trans);
2327 
2328 	/*
2329 	 * get a module reference to avoid doing this
2330 	 * while unloading anyway and to avoid
2331 	 * scheduling a work with code that's being
2332 	 * removed.
2333 	 */
2334 	if (!try_module_get(THIS_MODULE)) {
2335 		IWL_ERR(trans,
2336 			"Module is being unloaded - abort\n");
2337 		return;
2338 	}
2339 
2340 	removal = kzalloc_obj(*removal, GFP_ATOMIC);
2341 	if (!removal) {
2342 		module_put(THIS_MODULE);
2343 		return;
2344 	}
2345 	/*
2346 	 * we don't need to clear this flag, because
2347 	 * the trans will be freed and reallocated.
2348 	 */
2349 	set_bit(STATUS_TRANS_DEAD, &trans->status);
2350 
2351 	removal->pdev = to_pci_dev(trans->dev);
2352 	removal->mode = mode;
2353 	removal->integrated = trans->mac_cfg->integrated;
2354 	INIT_WORK(&removal->work, iwl_trans_pcie_removal_wk);
2355 	pci_dev_get(removal->pdev);
2356 	schedule_work(&removal->work);
2357 }
2358 EXPORT_SYMBOL(iwl_trans_pcie_reset);
2359 
2360 /*
2361  * This version doesn't disable BHs but rather assumes they're
2362  * already disabled.
2363  */
_iwl_trans_pcie_grab_nic_access(struct iwl_trans * trans,bool silent)2364 bool _iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, bool silent)
2365 {
2366 	int ret;
2367 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2368 	u32 write = CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ;
2369 	u32 mask = CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
2370 		   CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP;
2371 	u32 poll = CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN;
2372 
2373 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
2374 		return false;
2375 
2376 	spin_lock(&trans_pcie->reg_lock);
2377 
2378 	if (trans_pcie->cmd_hold_nic_awake)
2379 		goto out;
2380 
2381 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
2382 		write = CSR_GP_CNTRL_REG_FLAG_BZ_MAC_ACCESS_REQ;
2383 		mask = CSR_GP_CNTRL_REG_FLAG_MAC_STATUS;
2384 		poll = CSR_GP_CNTRL_REG_FLAG_MAC_STATUS;
2385 	}
2386 
2387 	/* this bit wakes up the NIC */
2388 	iwl_trans_set_bit(trans, CSR_GP_CNTRL, write);
2389 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_8000)
2390 		udelay(2);
2391 
2392 	/*
2393 	 * These bits say the device is running, and should keep running for
2394 	 * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
2395 	 * but they do not indicate that embedded SRAM is restored yet;
2396 	 * HW with volatile SRAM must save/restore contents to/from
2397 	 * host DRAM when sleeping/waking for power-saving.
2398 	 * Each direction takes approximately 1/4 millisecond; with this
2399 	 * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
2400 	 * series of register accesses are expected (e.g. reading Event Log),
2401 	 * to keep device from sleeping.
2402 	 *
2403 	 * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
2404 	 * SRAM is okay/restored.  We don't check that here because this call
2405 	 * is just for hardware register access; but GP1 MAC_SLEEP
2406 	 * check is a good idea before accessing the SRAM of HW with
2407 	 * volatile SRAM (e.g. reading Event Log).
2408 	 *
2409 	 * 5000 series and later (including 1000 series) have non-volatile SRAM,
2410 	 * and do not save/restore SRAM when power cycling.
2411 	 */
2412 	ret = iwl_poll_bits_mask(trans, CSR_GP_CNTRL, poll, mask, 15000);
2413 	if (unlikely(ret)) {
2414 		u32 cntrl = iwl_read32(trans, CSR_GP_CNTRL);
2415 
2416 		if (silent) {
2417 			spin_unlock(&trans_pcie->reg_lock);
2418 			return false;
2419 		}
2420 
2421 		WARN_ONCE(1,
2422 			  "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
2423 			  cntrl);
2424 
2425 		iwl_trans_pcie_dump_regs(trans, trans_pcie->pci_dev);
2426 
2427 		if (iwlwifi_mod_params.remove_when_gone && cntrl == ~0U)
2428 			iwl_trans_pcie_reset(trans,
2429 					     IWL_RESET_MODE_REMOVE_ONLY);
2430 		else
2431 			iwl_write32(trans, CSR_RESET,
2432 				    CSR_RESET_REG_FLAG_FORCE_NMI);
2433 
2434 		spin_unlock(&trans_pcie->reg_lock);
2435 		return false;
2436 	}
2437 
2438 out:
2439 	/*
2440 	 * Fool sparse by faking we release the lock - sparse will
2441 	 * track nic_access anyway.
2442 	 */
2443 	__release(&trans_pcie->reg_lock);
2444 	return true;
2445 }
2446 
iwl_trans_pcie_grab_nic_access(struct iwl_trans * trans)2447 bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans)
2448 {
2449 	bool ret;
2450 
2451 	local_bh_disable();
2452 	ret = _iwl_trans_pcie_grab_nic_access(trans, false);
2453 	if (ret) {
2454 		/* keep BHs disabled until iwl_trans_pcie_release_nic_access */
2455 		return ret;
2456 	}
2457 	local_bh_enable();
2458 	return false;
2459 }
2460 
__releases(nic_access_nobh)2461 void __releases(nic_access_nobh)
2462 iwl_trans_pcie_release_nic_access(struct iwl_trans *trans)
2463 {
2464 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2465 
2466 	lockdep_assert_held(&trans_pcie->reg_lock);
2467 
2468 	/*
2469 	 * Fool sparse by faking we acquiring the lock - sparse will
2470 	 * track nic_access anyway.
2471 	 */
2472 	__acquire(&trans_pcie->reg_lock);
2473 
2474 	if (trans_pcie->cmd_hold_nic_awake)
2475 		goto out;
2476 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
2477 		iwl_trans_clear_bit(trans, CSR_GP_CNTRL,
2478 				    CSR_GP_CNTRL_REG_FLAG_BZ_MAC_ACCESS_REQ);
2479 	else
2480 		iwl_trans_clear_bit(trans, CSR_GP_CNTRL,
2481 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2482 	/*
2483 	 * Above we read the CSR_GP_CNTRL register, which will flush
2484 	 * any previous writes, but we need the write that clears the
2485 	 * MAC_ACCESS_REQ bit to be performed before any other writes
2486 	 * scheduled on different CPUs (after we drop reg_lock).
2487 	 */
2488 out:
2489 	__release(nic_access_nobh);
2490 	spin_unlock_bh(&trans_pcie->reg_lock);
2491 }
2492 
iwl_trans_pcie_read_mem(struct iwl_trans * trans,u32 addr,void * buf,int dwords)2493 int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
2494 			    void *buf, int dwords)
2495 {
2496 #define IWL_MAX_HW_ERRS 5
2497 	unsigned int num_consec_hw_errors = 0;
2498 	int offs = 0;
2499 	u32 *vals = buf;
2500 
2501 	while (offs < dwords) {
2502 		/* limit the time we spin here under lock to 1/2s */
2503 		unsigned long end = jiffies + HZ / 2;
2504 		bool resched = false;
2505 
2506 		if (iwl_trans_grab_nic_access(trans)) {
2507 			iwl_write32(trans, HBUS_TARG_MEM_RADDR,
2508 				    addr + 4 * offs);
2509 
2510 			while (offs < dwords) {
2511 				vals[offs] = iwl_read32(trans,
2512 							HBUS_TARG_MEM_RDAT);
2513 
2514 				if (iwl_trans_is_hw_error_value(vals[offs]))
2515 					num_consec_hw_errors++;
2516 				else
2517 					num_consec_hw_errors = 0;
2518 
2519 				if (num_consec_hw_errors >= IWL_MAX_HW_ERRS) {
2520 					iwl_trans_release_nic_access(trans);
2521 					return -EIO;
2522 				}
2523 
2524 				offs++;
2525 
2526 				if (time_after(jiffies, end)) {
2527 					resched = true;
2528 					break;
2529 				}
2530 			}
2531 			iwl_trans_release_nic_access(trans);
2532 
2533 			if (resched)
2534 				cond_resched();
2535 		} else {
2536 			return -EBUSY;
2537 		}
2538 	}
2539 
2540 	return 0;
2541 }
2542 
iwl_trans_pcie_read_config32(struct iwl_trans * trans,u32 ofs,u32 * val)2543 int iwl_trans_pcie_read_config32(struct iwl_trans *trans, u32 ofs,
2544 				 u32 *val)
2545 {
2546 	return pci_read_config_dword(IWL_TRANS_GET_PCIE_TRANS(trans)->pci_dev,
2547 				     ofs, val);
2548 }
2549 
2550 #define IWL_FLUSH_WAIT_MS	2000
2551 
iwl_trans_pcie_rxq_dma_data(struct iwl_trans * trans,int queue,struct iwl_trans_rxq_dma_data * data)2552 int iwl_trans_pcie_rxq_dma_data(struct iwl_trans *trans, int queue,
2553 				struct iwl_trans_rxq_dma_data *data)
2554 {
2555 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2556 
2557 	if (queue >= trans->info.num_rxqs || !trans_pcie->rxq)
2558 		return -EINVAL;
2559 
2560 	data->fr_bd_cb = trans_pcie->rxq[queue].bd_dma;
2561 	data->urbd_stts_wrptr = trans_pcie->rxq[queue].rb_stts_dma;
2562 	data->ur_bd_cb = trans_pcie->rxq[queue].used_bd_dma;
2563 	data->fr_bd_wid = 0;
2564 
2565 	return 0;
2566 }
2567 
iwl_trans_pcie_wait_txq_empty(struct iwl_trans * trans,int txq_idx)2568 int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, int txq_idx)
2569 {
2570 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2571 	struct iwl_txq *txq;
2572 	unsigned long now = jiffies;
2573 	bool overflow_tx;
2574 	u8 wr_ptr;
2575 
2576 	/* Make sure the NIC is still alive in the bus */
2577 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
2578 		return -ENODEV;
2579 
2580 	if (!test_bit(txq_idx, trans_pcie->txqs.queue_used))
2581 		return -EINVAL;
2582 
2583 	IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", txq_idx);
2584 	txq = trans_pcie->txqs.txq[txq_idx];
2585 
2586 	spin_lock_bh(&txq->lock);
2587 	overflow_tx = txq->overflow_tx ||
2588 		      !skb_queue_empty(&txq->overflow_q);
2589 	spin_unlock_bh(&txq->lock);
2590 
2591 	wr_ptr = READ_ONCE(txq->write_ptr);
2592 
2593 	while ((txq->read_ptr != READ_ONCE(txq->write_ptr) ||
2594 		overflow_tx) &&
2595 	       !time_after(jiffies,
2596 			   now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
2597 		u8 write_ptr = READ_ONCE(txq->write_ptr);
2598 
2599 		/*
2600 		 * If write pointer moved during the wait, warn only
2601 		 * if the TX came from op mode. In case TX came from
2602 		 * trans layer (overflow TX) don't warn.
2603 		 */
2604 		if (WARN_ONCE(wr_ptr != write_ptr && !overflow_tx,
2605 			      "WR pointer moved while flushing %d -> %d\n",
2606 			      wr_ptr, write_ptr))
2607 			return -ETIMEDOUT;
2608 		wr_ptr = write_ptr;
2609 
2610 		usleep_range(1000, 2000);
2611 
2612 		spin_lock_bh(&txq->lock);
2613 		overflow_tx = txq->overflow_tx ||
2614 			      !skb_queue_empty(&txq->overflow_q);
2615 		spin_unlock_bh(&txq->lock);
2616 	}
2617 
2618 	if (txq->read_ptr != txq->write_ptr) {
2619 		IWL_ERR(trans,
2620 			"fail to flush all tx fifo queues Q %d\n", txq_idx);
2621 		iwl_txq_log_scd_error(trans, txq);
2622 		return -ETIMEDOUT;
2623 	}
2624 
2625 	IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", txq_idx);
2626 
2627 	return 0;
2628 }
2629 
iwl_trans_pcie_wait_txqs_empty(struct iwl_trans * trans,u32 txq_bm)2630 int iwl_trans_pcie_wait_txqs_empty(struct iwl_trans *trans, u32 txq_bm)
2631 {
2632 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2633 	int cnt;
2634 	int ret = 0;
2635 
2636 	/* waiting for all the tx frames complete might take a while */
2637 	for (cnt = 0;
2638 	     cnt < trans->mac_cfg->base->num_of_queues;
2639 	     cnt++) {
2640 
2641 		if (cnt == trans->conf.cmd_queue)
2642 			continue;
2643 		if (!test_bit(cnt, trans_pcie->txqs.queue_used))
2644 			continue;
2645 		if (!(BIT(cnt) & txq_bm))
2646 			continue;
2647 
2648 		ret = iwl_trans_pcie_wait_txq_empty(trans, cnt);
2649 		if (ret)
2650 			break;
2651 	}
2652 
2653 	return ret;
2654 }
2655 
iwl_trans_pcie_set_bits_mask(struct iwl_trans * trans,u32 reg,u32 mask,u32 value)2656 void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
2657 				  u32 mask, u32 value)
2658 {
2659 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2660 
2661 	spin_lock_bh(&trans_pcie->reg_lock);
2662 	_iwl_trans_set_bits_mask(trans, reg, mask, value);
2663 	spin_unlock_bh(&trans_pcie->reg_lock);
2664 }
2665 
get_csr_string(int cmd)2666 static const char *get_csr_string(int cmd)
2667 {
2668 #define IWL_CMD(x) case x: return #x
2669 	switch (cmd) {
2670 	IWL_CMD(CSR_HW_IF_CONFIG_REG);
2671 	IWL_CMD(CSR_INT_COALESCING);
2672 	IWL_CMD(CSR_INT);
2673 	IWL_CMD(CSR_INT_MASK);
2674 	IWL_CMD(CSR_FH_INT_STATUS);
2675 	IWL_CMD(CSR_GPIO_IN);
2676 	IWL_CMD(CSR_RESET);
2677 	IWL_CMD(CSR_GP_CNTRL);
2678 	IWL_CMD(CSR_HW_REV);
2679 	IWL_CMD(CSR_EEPROM_REG);
2680 	IWL_CMD(CSR_EEPROM_GP);
2681 	IWL_CMD(CSR_OTP_GP_REG);
2682 	IWL_CMD(CSR_GIO_REG);
2683 	IWL_CMD(CSR_GP_UCODE_REG);
2684 	IWL_CMD(CSR_GP_DRIVER_REG);
2685 	IWL_CMD(CSR_UCODE_DRV_GP1);
2686 	IWL_CMD(CSR_UCODE_DRV_GP2);
2687 	IWL_CMD(CSR_LED_REG);
2688 	IWL_CMD(CSR_DRAM_INT_TBL_REG);
2689 	IWL_CMD(CSR_GIO_CHICKEN_BITS);
2690 	IWL_CMD(CSR_ANA_PLL_CFG);
2691 	IWL_CMD(CSR_HW_REV_WA_REG);
2692 	IWL_CMD(CSR_MONITOR_STATUS_REG);
2693 	IWL_CMD(CSR_DBG_HPET_MEM_REG);
2694 	default:
2695 		return "UNKNOWN";
2696 	}
2697 #undef IWL_CMD
2698 }
2699 
iwl_pcie_dump_csr(struct iwl_trans * trans)2700 void iwl_pcie_dump_csr(struct iwl_trans *trans)
2701 {
2702 	int i;
2703 	static const u32 csr_tbl[] = {
2704 		CSR_HW_IF_CONFIG_REG,
2705 		CSR_INT_COALESCING,
2706 		CSR_INT,
2707 		CSR_INT_MASK,
2708 		CSR_FH_INT_STATUS,
2709 		CSR_GPIO_IN,
2710 		CSR_RESET,
2711 		CSR_GP_CNTRL,
2712 		CSR_HW_REV,
2713 		CSR_EEPROM_REG,
2714 		CSR_EEPROM_GP,
2715 		CSR_OTP_GP_REG,
2716 		CSR_GIO_REG,
2717 		CSR_GP_UCODE_REG,
2718 		CSR_GP_DRIVER_REG,
2719 		CSR_UCODE_DRV_GP1,
2720 		CSR_UCODE_DRV_GP2,
2721 		CSR_LED_REG,
2722 		CSR_DRAM_INT_TBL_REG,
2723 		CSR_GIO_CHICKEN_BITS,
2724 		CSR_ANA_PLL_CFG,
2725 		CSR_MONITOR_STATUS_REG,
2726 		CSR_HW_REV_WA_REG,
2727 		CSR_DBG_HPET_MEM_REG
2728 	};
2729 	IWL_ERR(trans, "CSR values:\n");
2730 	IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
2731 		"CSR_INT_PERIODIC_REG)\n");
2732 	for (i = 0; i <  ARRAY_SIZE(csr_tbl); i++) {
2733 		IWL_ERR(trans, "  %25s: 0X%08x\n",
2734 			get_csr_string(csr_tbl[i]),
2735 			iwl_read32(trans, csr_tbl[i]));
2736 	}
2737 }
2738 
2739 #ifdef CONFIG_IWLWIFI_DEBUGFS
2740 /* create and remove of files */
2741 #define DEBUGFS_ADD_FILE(name, parent, mode) do {			\
2742 	debugfs_create_file(#name, mode, parent, trans,			\
2743 			    &iwl_dbgfs_##name##_ops);			\
2744 } while (0)
2745 
2746 /* file operation */
2747 #define DEBUGFS_READ_FILE_OPS(name)					\
2748 static const struct file_operations iwl_dbgfs_##name##_ops = {		\
2749 	.read = iwl_dbgfs_##name##_read,				\
2750 	.open = simple_open,						\
2751 	.llseek = generic_file_llseek,					\
2752 };
2753 
2754 #define DEBUGFS_WRITE_FILE_OPS(name)                                    \
2755 static const struct file_operations iwl_dbgfs_##name##_ops = {          \
2756 	.write = iwl_dbgfs_##name##_write,                              \
2757 	.open = simple_open,						\
2758 	.llseek = generic_file_llseek,					\
2759 };
2760 
2761 #define DEBUGFS_READ_WRITE_FILE_OPS(name)				\
2762 static const struct file_operations iwl_dbgfs_##name##_ops = {		\
2763 	.write = iwl_dbgfs_##name##_write,				\
2764 	.read = iwl_dbgfs_##name##_read,				\
2765 	.open = simple_open,						\
2766 	.llseek = generic_file_llseek,					\
2767 };
2768 
2769 struct iwl_dbgfs_tx_queue_priv {
2770 	struct iwl_trans *trans;
2771 };
2772 
2773 struct iwl_dbgfs_tx_queue_state {
2774 	loff_t pos;
2775 };
2776 
iwl_dbgfs_tx_queue_seq_start(struct seq_file * seq,loff_t * pos)2777 static void *iwl_dbgfs_tx_queue_seq_start(struct seq_file *seq, loff_t *pos)
2778 {
2779 	struct iwl_dbgfs_tx_queue_priv *priv = seq->private;
2780 	struct iwl_dbgfs_tx_queue_state *state;
2781 
2782 	if (*pos >= priv->trans->mac_cfg->base->num_of_queues)
2783 		return NULL;
2784 
2785 	state = kmalloc_obj(*state);
2786 	if (!state)
2787 		return NULL;
2788 	state->pos = *pos;
2789 	return state;
2790 }
2791 
iwl_dbgfs_tx_queue_seq_next(struct seq_file * seq,void * v,loff_t * pos)2792 static void *iwl_dbgfs_tx_queue_seq_next(struct seq_file *seq,
2793 					 void *v, loff_t *pos)
2794 {
2795 	struct iwl_dbgfs_tx_queue_priv *priv = seq->private;
2796 	struct iwl_dbgfs_tx_queue_state *state = v;
2797 
2798 	*pos = ++state->pos;
2799 
2800 	if (*pos >= priv->trans->mac_cfg->base->num_of_queues)
2801 		return NULL;
2802 
2803 	return state;
2804 }
2805 
iwl_dbgfs_tx_queue_seq_stop(struct seq_file * seq,void * v)2806 static void iwl_dbgfs_tx_queue_seq_stop(struct seq_file *seq, void *v)
2807 {
2808 	kfree(v);
2809 }
2810 
iwl_dbgfs_tx_queue_seq_show(struct seq_file * seq,void * v)2811 static int iwl_dbgfs_tx_queue_seq_show(struct seq_file *seq, void *v)
2812 {
2813 	struct iwl_dbgfs_tx_queue_priv *priv = seq->private;
2814 	struct iwl_dbgfs_tx_queue_state *state = v;
2815 	struct iwl_trans *trans = priv->trans;
2816 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2817 	struct iwl_txq *txq = trans_pcie->txqs.txq[state->pos];
2818 
2819 	seq_printf(seq, "hwq %.3u: used=%d stopped=%d ",
2820 		   (unsigned int)state->pos,
2821 		   !!test_bit(state->pos, trans_pcie->txqs.queue_used),
2822 		   !!test_bit(state->pos, trans_pcie->txqs.queue_stopped));
2823 	if (txq)
2824 		seq_printf(seq,
2825 			   "read=%u write=%u need_update=%d frozen=%d n_window=%d ampdu=%d",
2826 			   txq->read_ptr, txq->write_ptr,
2827 			   txq->need_update, txq->frozen,
2828 			   txq->n_window, txq->ampdu);
2829 	else
2830 		seq_puts(seq, "(unallocated)");
2831 
2832 	if (state->pos == trans->conf.cmd_queue)
2833 		seq_puts(seq, " (HCMD)");
2834 	seq_puts(seq, "\n");
2835 
2836 	return 0;
2837 }
2838 
2839 static const struct seq_operations iwl_dbgfs_tx_queue_seq_ops = {
2840 	.start = iwl_dbgfs_tx_queue_seq_start,
2841 	.next = iwl_dbgfs_tx_queue_seq_next,
2842 	.stop = iwl_dbgfs_tx_queue_seq_stop,
2843 	.show = iwl_dbgfs_tx_queue_seq_show,
2844 };
2845 
iwl_dbgfs_tx_queue_open(struct inode * inode,struct file * filp)2846 static int iwl_dbgfs_tx_queue_open(struct inode *inode, struct file *filp)
2847 {
2848 	struct iwl_dbgfs_tx_queue_priv *priv;
2849 
2850 	priv = __seq_open_private(filp, &iwl_dbgfs_tx_queue_seq_ops,
2851 				  sizeof(*priv));
2852 
2853 	if (!priv)
2854 		return -ENOMEM;
2855 
2856 	priv->trans = inode->i_private;
2857 	return 0;
2858 }
2859 
iwl_dbgfs_rx_queue_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)2860 static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
2861 				       char __user *user_buf,
2862 				       size_t count, loff_t *ppos)
2863 {
2864 	struct iwl_trans *trans = file->private_data;
2865 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2866 	char *buf;
2867 	int pos = 0, i, ret;
2868 	size_t bufsz;
2869 
2870 	bufsz = sizeof(char) * 121 * trans->info.num_rxqs;
2871 
2872 	if (!trans_pcie->rxq)
2873 		return -EAGAIN;
2874 
2875 	buf = kzalloc(bufsz, GFP_KERNEL);
2876 	if (!buf)
2877 		return -ENOMEM;
2878 
2879 	for (i = 0; i < trans->info.num_rxqs && pos < bufsz; i++) {
2880 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
2881 
2882 		spin_lock_bh(&rxq->lock);
2883 
2884 		pos += scnprintf(buf + pos, bufsz - pos, "queue#: %2d\n",
2885 				 i);
2886 		pos += scnprintf(buf + pos, bufsz - pos, "\tread: %u\n",
2887 				 rxq->read);
2888 		pos += scnprintf(buf + pos, bufsz - pos, "\twrite: %u\n",
2889 				 rxq->write);
2890 		pos += scnprintf(buf + pos, bufsz - pos, "\twrite_actual: %u\n",
2891 				 rxq->write_actual);
2892 		pos += scnprintf(buf + pos, bufsz - pos, "\tneed_update: %2d\n",
2893 				 rxq->need_update);
2894 		pos += scnprintf(buf + pos, bufsz - pos, "\tfree_count: %u\n",
2895 				 rxq->free_count);
2896 		if (rxq->rb_stts) {
2897 			u32 r =	iwl_get_closed_rb_stts(trans, rxq);
2898 			pos += scnprintf(buf + pos, bufsz - pos,
2899 					 "\tclosed_rb_num: %u\n", r);
2900 		} else {
2901 			pos += scnprintf(buf + pos, bufsz - pos,
2902 					 "\tclosed_rb_num: Not Allocated\n");
2903 		}
2904 		spin_unlock_bh(&rxq->lock);
2905 	}
2906 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2907 	kfree(buf);
2908 
2909 	return ret;
2910 }
2911 
iwl_dbgfs_interrupt_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)2912 static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
2913 					char __user *user_buf,
2914 					size_t count, loff_t *ppos)
2915 {
2916 	struct iwl_trans *trans = file->private_data;
2917 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2918 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
2919 
2920 	int pos = 0;
2921 	char *buf;
2922 	int bufsz = 24 * 64; /* 24 items * 64 char per item */
2923 	ssize_t ret;
2924 
2925 	buf = kzalloc(bufsz, GFP_KERNEL);
2926 	if (!buf)
2927 		return -ENOMEM;
2928 
2929 	pos += scnprintf(buf + pos, bufsz - pos,
2930 			"Interrupt Statistics Report:\n");
2931 
2932 	pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
2933 		isr_stats->hw);
2934 	pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
2935 		isr_stats->sw);
2936 	if (isr_stats->sw || isr_stats->hw) {
2937 		pos += scnprintf(buf + pos, bufsz - pos,
2938 			"\tLast Restarting Code:  0x%X\n",
2939 			isr_stats->err_code);
2940 	}
2941 #ifdef CONFIG_IWLWIFI_DEBUG
2942 	pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
2943 		isr_stats->sch);
2944 	pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
2945 		isr_stats->alive);
2946 #endif
2947 	pos += scnprintf(buf + pos, bufsz - pos,
2948 		"HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
2949 
2950 	pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
2951 		isr_stats->ctkill);
2952 
2953 	pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
2954 		isr_stats->wakeup);
2955 
2956 	pos += scnprintf(buf + pos, bufsz - pos,
2957 		"Rx command responses:\t\t %u\n", isr_stats->rx);
2958 
2959 	pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
2960 		isr_stats->tx);
2961 
2962 	pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
2963 		isr_stats->unhandled);
2964 
2965 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2966 	kfree(buf);
2967 	return ret;
2968 }
2969 
iwl_dbgfs_interrupt_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)2970 static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
2971 					 const char __user *user_buf,
2972 					 size_t count, loff_t *ppos)
2973 {
2974 	struct iwl_trans *trans = file->private_data;
2975 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2976 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
2977 	u32 reset_flag;
2978 	int ret;
2979 
2980 	ret = kstrtou32_from_user(user_buf, count, 16, &reset_flag);
2981 	if (ret)
2982 		return ret;
2983 	if (reset_flag == 0)
2984 		memset(isr_stats, 0, sizeof(*isr_stats));
2985 
2986 	return count;
2987 }
2988 
iwl_dbgfs_csr_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)2989 static ssize_t iwl_dbgfs_csr_write(struct file *file,
2990 				   const char __user *user_buf,
2991 				   size_t count, loff_t *ppos)
2992 {
2993 	struct iwl_trans *trans = file->private_data;
2994 
2995 	iwl_pcie_dump_csr(trans);
2996 
2997 	return count;
2998 }
2999 
iwl_dbgfs_fh_reg_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3000 static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
3001 				     char __user *user_buf,
3002 				     size_t count, loff_t *ppos)
3003 {
3004 	struct iwl_trans *trans = file->private_data;
3005 	char *buf = NULL;
3006 	ssize_t ret;
3007 
3008 	ret = iwl_dump_fh(trans, &buf);
3009 	if (ret < 0)
3010 		return ret;
3011 	if (!buf)
3012 		return -EINVAL;
3013 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3014 	kfree(buf);
3015 	return ret;
3016 }
3017 
iwl_dbgfs_rfkill_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3018 static ssize_t iwl_dbgfs_rfkill_read(struct file *file,
3019 				     char __user *user_buf,
3020 				     size_t count, loff_t *ppos)
3021 {
3022 	struct iwl_trans *trans = file->private_data;
3023 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3024 	char buf[100];
3025 	int pos;
3026 
3027 	pos = scnprintf(buf, sizeof(buf), "debug: %d\nhw: %d\n",
3028 			trans_pcie->debug_rfkill,
3029 			!(iwl_read32(trans, CSR_GP_CNTRL) &
3030 				CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW));
3031 
3032 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
3033 }
3034 
iwl_dbgfs_rfkill_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)3035 static ssize_t iwl_dbgfs_rfkill_write(struct file *file,
3036 				      const char __user *user_buf,
3037 				      size_t count, loff_t *ppos)
3038 {
3039 	struct iwl_trans *trans = file->private_data;
3040 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3041 	bool new_value;
3042 	int ret;
3043 
3044 	ret = kstrtobool_from_user(user_buf, count, &new_value);
3045 	if (ret)
3046 		return ret;
3047 	if (new_value == trans_pcie->debug_rfkill)
3048 		return count;
3049 	IWL_WARN(trans, "changing debug rfkill %d->%d\n",
3050 		 trans_pcie->debug_rfkill, new_value);
3051 	trans_pcie->debug_rfkill = new_value;
3052 	iwl_pcie_handle_rfkill_irq(trans, false);
3053 
3054 	return count;
3055 }
3056 
iwl_dbgfs_monitor_data_open(struct inode * inode,struct file * file)3057 static int iwl_dbgfs_monitor_data_open(struct inode *inode,
3058 				       struct file *file)
3059 {
3060 	struct iwl_trans *trans = inode->i_private;
3061 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3062 
3063 	if (!trans->dbg.dest_tlv ||
3064 	    trans->dbg.dest_tlv->monitor_mode != EXTERNAL_MODE) {
3065 		IWL_ERR(trans, "Debug destination is not set to DRAM\n");
3066 		return -ENOENT;
3067 	}
3068 
3069 	if (trans_pcie->fw_mon_data.state != IWL_FW_MON_DBGFS_STATE_CLOSED)
3070 		return -EBUSY;
3071 
3072 	trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_OPEN;
3073 	return simple_open(inode, file);
3074 }
3075 
iwl_dbgfs_monitor_data_release(struct inode * inode,struct file * file)3076 static int iwl_dbgfs_monitor_data_release(struct inode *inode,
3077 					  struct file *file)
3078 {
3079 	struct iwl_trans_pcie *trans_pcie =
3080 		IWL_TRANS_GET_PCIE_TRANS(inode->i_private);
3081 
3082 	if (trans_pcie->fw_mon_data.state == IWL_FW_MON_DBGFS_STATE_OPEN)
3083 		trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_CLOSED;
3084 	return 0;
3085 }
3086 
iwl_write_to_user_buf(char __user * user_buf,ssize_t count,void * buf,ssize_t * size,ssize_t * bytes_copied)3087 static bool iwl_write_to_user_buf(char __user *user_buf, ssize_t count,
3088 				  void *buf, ssize_t *size,
3089 				  ssize_t *bytes_copied)
3090 {
3091 	ssize_t buf_size_left = count - *bytes_copied;
3092 
3093 	buf_size_left = buf_size_left - (buf_size_left % sizeof(u32));
3094 	if (*size > buf_size_left)
3095 		*size = buf_size_left;
3096 
3097 	*size -= copy_to_user(user_buf, buf, *size);
3098 	*bytes_copied += *size;
3099 
3100 	if (buf_size_left == *size)
3101 		return true;
3102 	return false;
3103 }
3104 
iwl_dbgfs_monitor_data_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3105 static ssize_t iwl_dbgfs_monitor_data_read(struct file *file,
3106 					   char __user *user_buf,
3107 					   size_t count, loff_t *ppos)
3108 {
3109 	struct iwl_trans *trans = file->private_data;
3110 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3111 	u8 *cpu_addr = (void *)trans->dbg.fw_mon.block, *curr_buf;
3112 	struct cont_rec *data = &trans_pcie->fw_mon_data;
3113 	u32 write_ptr_addr, wrap_cnt_addr, write_ptr, wrap_cnt;
3114 	ssize_t size, bytes_copied = 0;
3115 	bool b_full;
3116 
3117 	if (trans->dbg.dest_tlv) {
3118 		write_ptr_addr =
3119 			le32_to_cpu(trans->dbg.dest_tlv->write_ptr_reg);
3120 		wrap_cnt_addr = le32_to_cpu(trans->dbg.dest_tlv->wrap_count);
3121 	} else {
3122 		write_ptr_addr = MON_BUFF_WRPTR;
3123 		wrap_cnt_addr = MON_BUFF_CYCLE_CNT;
3124 	}
3125 
3126 	if (unlikely(!trans->dbg.rec_on))
3127 		return 0;
3128 
3129 	mutex_lock(&data->mutex);
3130 	if (data->state ==
3131 	    IWL_FW_MON_DBGFS_STATE_DISABLED) {
3132 		mutex_unlock(&data->mutex);
3133 		return 0;
3134 	}
3135 
3136 	/* write_ptr position in bytes rather then DW */
3137 	write_ptr = iwl_read_prph(trans, write_ptr_addr) * sizeof(u32);
3138 	wrap_cnt = iwl_read_prph(trans, wrap_cnt_addr);
3139 
3140 	if (data->prev_wrap_cnt == wrap_cnt) {
3141 		size = write_ptr - data->prev_wr_ptr;
3142 		curr_buf = cpu_addr + data->prev_wr_ptr;
3143 		b_full = iwl_write_to_user_buf(user_buf, count,
3144 					       curr_buf, &size,
3145 					       &bytes_copied);
3146 		data->prev_wr_ptr += size;
3147 
3148 	} else if (data->prev_wrap_cnt == wrap_cnt - 1 &&
3149 		   write_ptr < data->prev_wr_ptr) {
3150 		size = trans->dbg.fw_mon.size - data->prev_wr_ptr;
3151 		curr_buf = cpu_addr + data->prev_wr_ptr;
3152 		b_full = iwl_write_to_user_buf(user_buf, count,
3153 					       curr_buf, &size,
3154 					       &bytes_copied);
3155 		data->prev_wr_ptr += size;
3156 
3157 		if (!b_full) {
3158 			size = write_ptr;
3159 			b_full = iwl_write_to_user_buf(user_buf, count,
3160 						       cpu_addr, &size,
3161 						       &bytes_copied);
3162 			data->prev_wr_ptr = size;
3163 			data->prev_wrap_cnt++;
3164 		}
3165 	} else {
3166 		if (data->prev_wrap_cnt == wrap_cnt - 1 &&
3167 		    write_ptr > data->prev_wr_ptr)
3168 			IWL_WARN(trans,
3169 				 "write pointer passed previous write pointer, start copying from the beginning\n");
3170 		else if (!unlikely(data->prev_wrap_cnt == 0 &&
3171 				   data->prev_wr_ptr == 0))
3172 			IWL_WARN(trans,
3173 				 "monitor data is out of sync, start copying from the beginning\n");
3174 
3175 		size = write_ptr;
3176 		b_full = iwl_write_to_user_buf(user_buf, count,
3177 					       cpu_addr, &size,
3178 					       &bytes_copied);
3179 		data->prev_wr_ptr = size;
3180 		data->prev_wrap_cnt = wrap_cnt;
3181 	}
3182 
3183 	mutex_unlock(&data->mutex);
3184 
3185 	return bytes_copied;
3186 }
3187 
iwl_dbgfs_rf_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3188 static ssize_t iwl_dbgfs_rf_read(struct file *file,
3189 				 char __user *user_buf,
3190 				 size_t count, loff_t *ppos)
3191 {
3192 	struct iwl_trans *trans = file->private_data;
3193 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3194 
3195 	if (!trans_pcie->rf_name[0])
3196 		return -ENODEV;
3197 
3198 	return simple_read_from_buffer(user_buf, count, ppos,
3199 				       trans_pcie->rf_name,
3200 				       strlen(trans_pcie->rf_name));
3201 }
3202 
iwl_dbgfs_reset_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)3203 static ssize_t iwl_dbgfs_reset_write(struct file *file,
3204 				     const char __user *user_buf,
3205 				     size_t count, loff_t *ppos)
3206 {
3207 	struct iwl_trans *trans = file->private_data;
3208 	static const char * const modes[] = {
3209 		[IWL_RESET_MODE_SW_RESET] = "sw",
3210 		[IWL_RESET_MODE_REPROBE] = "reprobe",
3211 		[IWL_RESET_MODE_TOP_RESET] = "top",
3212 		[IWL_RESET_MODE_REMOVE_ONLY] = "remove",
3213 		[IWL_RESET_MODE_RESCAN] = "rescan",
3214 		[IWL_RESET_MODE_FUNC_RESET] = "function",
3215 		[IWL_RESET_MODE_PROD_RESET] = "product",
3216 	};
3217 	char buf[10] = {};
3218 	int mode;
3219 
3220 	if (count > sizeof(buf) - 1)
3221 		return -EINVAL;
3222 
3223 	if (copy_from_user(buf, user_buf, count))
3224 		return -EFAULT;
3225 
3226 	mode = sysfs_match_string(modes, buf);
3227 	if (mode < 0)
3228 		return mode;
3229 
3230 	if (mode < IWL_RESET_MODE_REMOVE_ONLY) {
3231 		if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
3232 			return -EINVAL;
3233 		if (mode == IWL_RESET_MODE_TOP_RESET) {
3234 			if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_SC)
3235 				return -EINVAL;
3236 			trans->request_top_reset = 1;
3237 		}
3238 		iwl_op_mode_nic_error(trans->op_mode, IWL_ERR_TYPE_DEBUGFS);
3239 		iwl_trans_schedule_reset(trans, IWL_ERR_TYPE_DEBUGFS);
3240 		return count;
3241 	}
3242 
3243 	iwl_trans_pcie_reset(trans, mode);
3244 
3245 	return count;
3246 }
3247 
3248 DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
3249 DEBUGFS_READ_FILE_OPS(fh_reg);
3250 DEBUGFS_READ_FILE_OPS(rx_queue);
3251 DEBUGFS_WRITE_FILE_OPS(csr);
3252 DEBUGFS_READ_WRITE_FILE_OPS(rfkill);
3253 DEBUGFS_READ_FILE_OPS(rf);
3254 DEBUGFS_WRITE_FILE_OPS(reset);
3255 
3256 static const struct file_operations iwl_dbgfs_tx_queue_ops = {
3257 	.owner = THIS_MODULE,
3258 	.open = iwl_dbgfs_tx_queue_open,
3259 	.read = seq_read,
3260 	.llseek = seq_lseek,
3261 	.release = seq_release_private,
3262 };
3263 
3264 static const struct file_operations iwl_dbgfs_monitor_data_ops = {
3265 	.read = iwl_dbgfs_monitor_data_read,
3266 	.open = iwl_dbgfs_monitor_data_open,
3267 	.release = iwl_dbgfs_monitor_data_release,
3268 };
3269 
3270 /* Create the debugfs files and directories */
iwl_trans_pcie_dbgfs_register(struct iwl_trans * trans)3271 void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans)
3272 {
3273 	struct dentry *dir = trans->dbgfs_dir;
3274 
3275 	DEBUGFS_ADD_FILE(rx_queue, dir, 0400);
3276 	DEBUGFS_ADD_FILE(tx_queue, dir, 0400);
3277 	DEBUGFS_ADD_FILE(interrupt, dir, 0600);
3278 	DEBUGFS_ADD_FILE(csr, dir, 0200);
3279 	DEBUGFS_ADD_FILE(fh_reg, dir, 0400);
3280 	DEBUGFS_ADD_FILE(rfkill, dir, 0600);
3281 	DEBUGFS_ADD_FILE(monitor_data, dir, 0400);
3282 	DEBUGFS_ADD_FILE(rf, dir, 0400);
3283 	DEBUGFS_ADD_FILE(reset, dir, 0200);
3284 }
3285 
iwl_trans_pcie_debugfs_cleanup(struct iwl_trans * trans)3286 void iwl_trans_pcie_debugfs_cleanup(struct iwl_trans *trans)
3287 {
3288 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3289 	struct cont_rec *data = &trans_pcie->fw_mon_data;
3290 
3291 	mutex_lock(&data->mutex);
3292 	data->state = IWL_FW_MON_DBGFS_STATE_DISABLED;
3293 	mutex_unlock(&data->mutex);
3294 }
3295 #endif /*CONFIG_IWLWIFI_DEBUGFS */
3296 
iwl_trans_pcie_get_cmdlen(struct iwl_trans * trans,void * tfd)3297 static u32 iwl_trans_pcie_get_cmdlen(struct iwl_trans *trans, void *tfd)
3298 {
3299 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3300 	u32 cmdlen = 0;
3301 	int i;
3302 
3303 	for (i = 0; i < trans_pcie->txqs.tfd.max_tbs; i++)
3304 		cmdlen += iwl_txq_gen1_tfd_tb_get_len(trans, tfd, i);
3305 
3306 	return cmdlen;
3307 }
3308 
iwl_trans_pcie_dump_rbs(struct iwl_trans * trans,struct iwl_fw_error_dump_data ** data,int allocated_rb_nums)3309 static u32 iwl_trans_pcie_dump_rbs(struct iwl_trans *trans,
3310 				   struct iwl_fw_error_dump_data **data,
3311 				   int allocated_rb_nums)
3312 {
3313 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3314 	int max_len = trans_pcie->rx_buf_bytes;
3315 	/* Dump RBs is supported only for pre-9000 devices (1 queue) */
3316 	struct iwl_rxq *rxq = &trans_pcie->rxq[0];
3317 	u32 i, r, j, rb_len = 0;
3318 
3319 	spin_lock_bh(&rxq->lock);
3320 
3321 	r = iwl_get_closed_rb_stts(trans, rxq);
3322 
3323 	for (i = rxq->read, j = 0;
3324 	     i != r && j < allocated_rb_nums;
3325 	     i = (i + 1) & RX_QUEUE_MASK, j++) {
3326 		struct iwl_rx_mem_buffer *rxb = rxq->queue[i];
3327 		struct iwl_fw_error_dump_rb *rb;
3328 
3329 		dma_sync_single_for_cpu(trans->dev, rxb->page_dma,
3330 					max_len, DMA_FROM_DEVICE);
3331 
3332 		rb_len += sizeof(**data) + sizeof(*rb) + max_len;
3333 
3334 		(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RB);
3335 		(*data)->len = cpu_to_le32(sizeof(*rb) + max_len);
3336 		rb = (void *)(*data)->data;
3337 		rb->index = cpu_to_le32(i);
3338 		memcpy(rb->data, page_address(rxb->page), max_len);
3339 
3340 		*data = iwl_fw_error_next_data(*data);
3341 	}
3342 
3343 	spin_unlock_bh(&rxq->lock);
3344 
3345 	return rb_len;
3346 }
3347 #define IWL_CSR_TO_DUMP (0x250)
3348 
iwl_trans_pcie_dump_csr(struct iwl_trans * trans,struct iwl_fw_error_dump_data ** data)3349 static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans,
3350 				   struct iwl_fw_error_dump_data **data)
3351 {
3352 	u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP;
3353 	__le32 *val;
3354 	int i;
3355 
3356 	(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR);
3357 	(*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP);
3358 	val = (void *)(*data)->data;
3359 
3360 	for (i = 0; i < IWL_CSR_TO_DUMP; i += 4)
3361 		*val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
3362 
3363 	*data = iwl_fw_error_next_data(*data);
3364 
3365 	return csr_len;
3366 }
3367 
iwl_trans_pcie_fh_regs_dump(struct iwl_trans * trans,struct iwl_fw_error_dump_data ** data)3368 static u32 iwl_trans_pcie_fh_regs_dump(struct iwl_trans *trans,
3369 				       struct iwl_fw_error_dump_data **data)
3370 {
3371 	u32 fh_regs_len = FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND;
3372 	__le32 *val;
3373 	int i;
3374 
3375 	if (!iwl_trans_grab_nic_access(trans))
3376 		return 0;
3377 
3378 	(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FH_REGS);
3379 	(*data)->len = cpu_to_le32(fh_regs_len);
3380 	val = (void *)(*data)->data;
3381 
3382 	if (!trans->mac_cfg->gen2)
3383 		for (i = FH_MEM_LOWER_BOUND; i < FH_MEM_UPPER_BOUND;
3384 		     i += sizeof(u32))
3385 			*val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
3386 	else
3387 		for (i = iwl_umac_prph(trans, FH_MEM_LOWER_BOUND_GEN2);
3388 		     i < iwl_umac_prph(trans, FH_MEM_UPPER_BOUND_GEN2);
3389 		     i += sizeof(u32))
3390 			*val++ = cpu_to_le32(iwl_trans_pcie_read_prph(trans,
3391 								      i));
3392 
3393 	iwl_trans_release_nic_access(trans);
3394 
3395 	*data = iwl_fw_error_next_data(*data);
3396 
3397 	return sizeof(**data) + fh_regs_len;
3398 }
3399 
3400 static u32
iwl_trans_pci_dump_marbh_monitor(struct iwl_trans * trans,struct iwl_fw_error_dump_fw_mon * fw_mon_data,u32 monitor_len)3401 iwl_trans_pci_dump_marbh_monitor(struct iwl_trans *trans,
3402 				 struct iwl_fw_error_dump_fw_mon *fw_mon_data,
3403 				 u32 monitor_len)
3404 {
3405 	u32 buf_size_in_dwords = (monitor_len >> 2);
3406 	u32 *buffer = (u32 *)fw_mon_data->data;
3407 	u32 i;
3408 
3409 	if (!iwl_trans_grab_nic_access(trans))
3410 		return 0;
3411 
3412 	iwl_write_umac_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x1);
3413 	for (i = 0; i < buf_size_in_dwords; i++)
3414 		buffer[i] = iwl_read_umac_prph_no_grab(trans,
3415 						       MON_DMARB_RD_DATA_ADDR);
3416 	iwl_write_umac_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x0);
3417 
3418 	iwl_trans_release_nic_access(trans);
3419 
3420 	return monitor_len;
3421 }
3422 
3423 static void
iwl_trans_pcie_dump_pointers(struct iwl_trans * trans,struct iwl_fw_error_dump_fw_mon * fw_mon_data)3424 iwl_trans_pcie_dump_pointers(struct iwl_trans *trans,
3425 			     struct iwl_fw_error_dump_fw_mon *fw_mon_data)
3426 {
3427 	u32 base, base_high, write_ptr, write_ptr_val, wrap_cnt;
3428 
3429 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
3430 		base = DBGC_CUR_DBGBUF_BASE_ADDR_LSB;
3431 		base_high = DBGC_CUR_DBGBUF_BASE_ADDR_MSB;
3432 		write_ptr = DBGC_CUR_DBGBUF_STATUS;
3433 		wrap_cnt = DBGC_DBGBUF_WRAP_AROUND;
3434 	} else if (trans->dbg.dest_tlv) {
3435 		write_ptr = le32_to_cpu(trans->dbg.dest_tlv->write_ptr_reg);
3436 		wrap_cnt = le32_to_cpu(trans->dbg.dest_tlv->wrap_count);
3437 		base = le32_to_cpu(trans->dbg.dest_tlv->base_reg);
3438 	} else {
3439 		base = MON_BUFF_BASE_ADDR;
3440 		write_ptr = MON_BUFF_WRPTR;
3441 		wrap_cnt = MON_BUFF_CYCLE_CNT;
3442 	}
3443 
3444 	write_ptr_val = iwl_read_prph(trans, write_ptr);
3445 	fw_mon_data->fw_mon_cycle_cnt =
3446 		cpu_to_le32(iwl_read_prph(trans, wrap_cnt));
3447 	fw_mon_data->fw_mon_base_ptr =
3448 		cpu_to_le32(iwl_read_prph(trans, base));
3449 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
3450 		fw_mon_data->fw_mon_base_high_ptr =
3451 			cpu_to_le32(iwl_read_prph(trans, base_high));
3452 		write_ptr_val &= DBGC_CUR_DBGBUF_STATUS_OFFSET_MSK;
3453 		/* convert wrtPtr to DWs, to align with all HWs */
3454 		write_ptr_val >>= 2;
3455 	}
3456 	fw_mon_data->fw_mon_wr_ptr = cpu_to_le32(write_ptr_val);
3457 }
3458 
3459 static u32
iwl_trans_pcie_dump_monitor(struct iwl_trans * trans,struct iwl_fw_error_dump_data ** data,u32 monitor_len)3460 iwl_trans_pcie_dump_monitor(struct iwl_trans *trans,
3461 			    struct iwl_fw_error_dump_data **data,
3462 			    u32 monitor_len)
3463 {
3464 	struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon;
3465 	u32 len = 0;
3466 
3467 	if (trans->dbg.dest_tlv ||
3468 	    (fw_mon->size &&
3469 	     (trans->mac_cfg->device_family == IWL_DEVICE_FAMILY_7000 ||
3470 	      trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210))) {
3471 		struct iwl_fw_error_dump_fw_mon *fw_mon_data;
3472 
3473 		(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR);
3474 		fw_mon_data = (void *)(*data)->data;
3475 
3476 		iwl_trans_pcie_dump_pointers(trans, fw_mon_data);
3477 
3478 		len += sizeof(**data) + sizeof(*fw_mon_data);
3479 		if (fw_mon->size) {
3480 			memcpy(fw_mon_data->data, fw_mon->block, fw_mon->size);
3481 			monitor_len = fw_mon->size;
3482 		} else if (trans->dbg.dest_tlv->monitor_mode == SMEM_MODE) {
3483 			u32 base = le32_to_cpu(fw_mon_data->fw_mon_base_ptr);
3484 			/*
3485 			 * Update pointers to reflect actual values after
3486 			 * shifting
3487 			 */
3488 			if (trans->dbg.dest_tlv->version) {
3489 				base = (iwl_read_prph(trans, base) &
3490 					IWL_LDBG_M2S_BUF_BA_MSK) <<
3491 				       trans->dbg.dest_tlv->base_shift;
3492 				base *= IWL_M2S_UNIT_SIZE;
3493 				base += trans->mac_cfg->base->smem_offset;
3494 			} else {
3495 				base = iwl_read_prph(trans, base) <<
3496 				       trans->dbg.dest_tlv->base_shift;
3497 			}
3498 
3499 			iwl_trans_pcie_read_mem(trans, base, fw_mon_data->data,
3500 						monitor_len / sizeof(u32));
3501 		} else if (trans->dbg.dest_tlv->monitor_mode == MARBH_MODE) {
3502 			monitor_len =
3503 				iwl_trans_pci_dump_marbh_monitor(trans,
3504 								 fw_mon_data,
3505 								 monitor_len);
3506 		} else {
3507 			/* Didn't match anything - output no monitor data */
3508 			monitor_len = 0;
3509 		}
3510 
3511 		len += monitor_len;
3512 		(*data)->len = cpu_to_le32(monitor_len + sizeof(*fw_mon_data));
3513 	}
3514 
3515 	return len;
3516 }
3517 
iwl_trans_get_fw_monitor_len(struct iwl_trans * trans,u32 * len)3518 static int iwl_trans_get_fw_monitor_len(struct iwl_trans *trans, u32 *len)
3519 {
3520 	if (trans->dbg.fw_mon.size) {
3521 		*len += sizeof(struct iwl_fw_error_dump_data) +
3522 			sizeof(struct iwl_fw_error_dump_fw_mon) +
3523 			trans->dbg.fw_mon.size;
3524 		return trans->dbg.fw_mon.size;
3525 	} else if (trans->dbg.dest_tlv) {
3526 		u32 base, end, cfg_reg, monitor_len;
3527 
3528 		if (trans->dbg.dest_tlv->version == 1) {
3529 			cfg_reg = le32_to_cpu(trans->dbg.dest_tlv->base_reg);
3530 			cfg_reg = iwl_read_prph(trans, cfg_reg);
3531 			base = (cfg_reg & IWL_LDBG_M2S_BUF_BA_MSK) <<
3532 				trans->dbg.dest_tlv->base_shift;
3533 			base *= IWL_M2S_UNIT_SIZE;
3534 			base += trans->mac_cfg->base->smem_offset;
3535 
3536 			monitor_len =
3537 				(cfg_reg & IWL_LDBG_M2S_BUF_SIZE_MSK) >>
3538 				trans->dbg.dest_tlv->end_shift;
3539 			monitor_len *= IWL_M2S_UNIT_SIZE;
3540 		} else {
3541 			base = le32_to_cpu(trans->dbg.dest_tlv->base_reg);
3542 			end = le32_to_cpu(trans->dbg.dest_tlv->end_reg);
3543 
3544 			base = iwl_read_prph(trans, base) <<
3545 			       trans->dbg.dest_tlv->base_shift;
3546 			end = iwl_read_prph(trans, end) <<
3547 			      trans->dbg.dest_tlv->end_shift;
3548 
3549 			/* Make "end" point to the actual end */
3550 			if (trans->mac_cfg->device_family >=
3551 			    IWL_DEVICE_FAMILY_8000 ||
3552 			    trans->dbg.dest_tlv->monitor_mode == MARBH_MODE)
3553 				end += (1 << trans->dbg.dest_tlv->end_shift);
3554 			monitor_len = end - base;
3555 		}
3556 		*len += sizeof(struct iwl_fw_error_dump_data) +
3557 			sizeof(struct iwl_fw_error_dump_fw_mon) +
3558 			monitor_len;
3559 		return monitor_len;
3560 	}
3561 	return 0;
3562 }
3563 
3564 struct iwl_trans_dump_data *
iwl_trans_pcie_dump_data(struct iwl_trans * trans,u32 dump_mask,const struct iwl_dump_sanitize_ops * sanitize_ops,void * sanitize_ctx)3565 iwl_trans_pcie_dump_data(struct iwl_trans *trans, u32 dump_mask,
3566 			 const struct iwl_dump_sanitize_ops *sanitize_ops,
3567 			 void *sanitize_ctx)
3568 {
3569 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3570 	struct iwl_fw_error_dump_data *data;
3571 	struct iwl_txq *cmdq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
3572 	struct iwl_fw_error_dump_txcmd *txcmd;
3573 	struct iwl_trans_dump_data *dump_data;
3574 	u32 len, num_rbs = 0, monitor_len = 0;
3575 	int i, ptr;
3576 	bool dump_rbs = iwl_trans_is_fw_error(trans) &&
3577 			!trans->mac_cfg->mq_rx_supported &&
3578 			dump_mask & BIT(IWL_FW_ERROR_DUMP_RB);
3579 
3580 	if (!dump_mask)
3581 		return NULL;
3582 
3583 	/* transport dump header */
3584 	len = sizeof(*dump_data);
3585 
3586 	/* host commands */
3587 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_TXCMD) && cmdq)
3588 		len += sizeof(*data) +
3589 			cmdq->n_window * (sizeof(*txcmd) +
3590 					  TFD_MAX_PAYLOAD_SIZE);
3591 
3592 	/* FW monitor */
3593 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FW_MONITOR))
3594 		monitor_len = iwl_trans_get_fw_monitor_len(trans, &len);
3595 
3596 	/* CSR registers */
3597 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_CSR))
3598 		len += sizeof(*data) + IWL_CSR_TO_DUMP;
3599 
3600 	/* FH registers */
3601 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FH_REGS)) {
3602 		if (trans->mac_cfg->gen2)
3603 			len += sizeof(*data) +
3604 			       (iwl_umac_prph(trans, FH_MEM_UPPER_BOUND_GEN2) -
3605 				iwl_umac_prph(trans, FH_MEM_LOWER_BOUND_GEN2));
3606 		else
3607 			len += sizeof(*data) +
3608 			       (FH_MEM_UPPER_BOUND -
3609 				FH_MEM_LOWER_BOUND);
3610 	}
3611 
3612 	if (dump_rbs) {
3613 		/* Dump RBs is supported only for pre-9000 devices (1 queue) */
3614 		struct iwl_rxq *rxq = &trans_pcie->rxq[0];
3615 		/* RBs */
3616 		spin_lock_bh(&rxq->lock);
3617 		num_rbs = iwl_get_closed_rb_stts(trans, rxq);
3618 		num_rbs = (num_rbs - rxq->read) & RX_QUEUE_MASK;
3619 		spin_unlock_bh(&rxq->lock);
3620 
3621 		len += num_rbs * (sizeof(*data) +
3622 				  sizeof(struct iwl_fw_error_dump_rb) +
3623 				  (PAGE_SIZE << trans_pcie->rx_page_order));
3624 	}
3625 
3626 	/* Paged memory for gen2 HW */
3627 	if (trans->mac_cfg->gen2 && dump_mask & BIT(IWL_FW_ERROR_DUMP_PAGING))
3628 		for (i = 0; i < trans->init_dram.paging_cnt; i++)
3629 			len += sizeof(*data) +
3630 			       sizeof(struct iwl_fw_error_dump_paging) +
3631 			       trans->init_dram.paging[i].size;
3632 
3633 	dump_data = vzalloc(len);
3634 	if (!dump_data)
3635 		return NULL;
3636 
3637 	len = 0;
3638 	data = (void *)dump_data->data;
3639 
3640 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_TXCMD) && cmdq) {
3641 		u16 tfd_size = trans_pcie->txqs.tfd.size;
3642 
3643 		data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
3644 		txcmd = (void *)data->data;
3645 		spin_lock_bh(&cmdq->lock);
3646 		ptr = cmdq->write_ptr;
3647 		for (i = 0; i < cmdq->n_window; i++) {
3648 			u8 idx = iwl_txq_get_cmd_index(cmdq, ptr);
3649 			u8 tfdidx;
3650 			u32 caplen, cmdlen;
3651 
3652 			if (trans->mac_cfg->gen2)
3653 				tfdidx = idx;
3654 			else
3655 				tfdidx = ptr;
3656 
3657 			cmdlen = iwl_trans_pcie_get_cmdlen(trans,
3658 							   (u8 *)cmdq->tfds +
3659 							   tfd_size * tfdidx);
3660 			caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
3661 
3662 			if (cmdlen) {
3663 				len += sizeof(*txcmd) + caplen;
3664 				txcmd->cmdlen = cpu_to_le32(cmdlen);
3665 				txcmd->caplen = cpu_to_le32(caplen);
3666 				memcpy(txcmd->data, cmdq->entries[idx].cmd,
3667 				       caplen);
3668 				if (sanitize_ops && sanitize_ops->frob_hcmd)
3669 					sanitize_ops->frob_hcmd(sanitize_ctx,
3670 								txcmd->data,
3671 								caplen);
3672 				txcmd = (void *)((u8 *)txcmd->data + caplen);
3673 			}
3674 
3675 			ptr = iwl_txq_dec_wrap(trans, ptr);
3676 		}
3677 		spin_unlock_bh(&cmdq->lock);
3678 
3679 		data->len = cpu_to_le32(len);
3680 		len += sizeof(*data);
3681 		data = iwl_fw_error_next_data(data);
3682 	}
3683 
3684 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_CSR))
3685 		len += iwl_trans_pcie_dump_csr(trans, &data);
3686 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FH_REGS))
3687 		len += iwl_trans_pcie_fh_regs_dump(trans, &data);
3688 	if (dump_rbs)
3689 		len += iwl_trans_pcie_dump_rbs(trans, &data, num_rbs);
3690 
3691 	/* Paged memory for gen2 HW */
3692 	if (trans->mac_cfg->gen2 &&
3693 	    dump_mask & BIT(IWL_FW_ERROR_DUMP_PAGING)) {
3694 		for (i = 0; i < trans->init_dram.paging_cnt; i++) {
3695 			struct iwl_fw_error_dump_paging *paging;
3696 			u32 page_len = trans->init_dram.paging[i].size;
3697 
3698 			data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PAGING);
3699 			data->len = cpu_to_le32(sizeof(*paging) + page_len);
3700 			paging = (void *)data->data;
3701 			paging->index = cpu_to_le32(i);
3702 			memcpy(paging->data,
3703 			       trans->init_dram.paging[i].block, page_len);
3704 			data = iwl_fw_error_next_data(data);
3705 
3706 			len += sizeof(*data) + sizeof(*paging) + page_len;
3707 		}
3708 	}
3709 	if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FW_MONITOR))
3710 		len += iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
3711 
3712 	dump_data->len = len;
3713 
3714 	return dump_data;
3715 }
3716 
iwl_trans_pci_interrupts(struct iwl_trans * trans,bool enable)3717 void iwl_trans_pci_interrupts(struct iwl_trans *trans, bool enable)
3718 {
3719 	if (enable)
3720 		iwl_enable_interrupts(trans);
3721 	else
3722 		iwl_disable_interrupts(trans);
3723 }
3724 
iwl_trans_pcie_sync_nmi(struct iwl_trans * trans)3725 void iwl_trans_pcie_sync_nmi(struct iwl_trans *trans)
3726 {
3727 	u32 inta_addr, sw_err_bit;
3728 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3729 
3730 	if (trans_pcie->msix_enabled) {
3731 		inta_addr = CSR_MSIX_HW_INT_CAUSES_AD;
3732 		if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
3733 			sw_err_bit = MSIX_HW_INT_CAUSES_REG_SW_ERR_BZ;
3734 		else
3735 			sw_err_bit = MSIX_HW_INT_CAUSES_REG_SW_ERR;
3736 	} else {
3737 		inta_addr = CSR_INT;
3738 		sw_err_bit = CSR_INT_BIT_SW_ERR;
3739 	}
3740 
3741 	iwl_trans_sync_nmi_with_addr(trans, inta_addr, sw_err_bit);
3742 }
3743 
iwl_trans_pcie_alloc_txcmd_pool(struct iwl_trans * trans)3744 static int iwl_trans_pcie_alloc_txcmd_pool(struct iwl_trans *trans)
3745 {
3746 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3747 	unsigned int txcmd_size, txcmd_align;
3748 
3749 	if (!trans->mac_cfg->gen2) {
3750 		txcmd_size = sizeof(struct iwl_tx_cmd_v6);
3751 		txcmd_align = sizeof(void *);
3752 	} else if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210) {
3753 		txcmd_size = sizeof(struct iwl_tx_cmd_v9);
3754 		txcmd_align = 64;
3755 	} else {
3756 		txcmd_size = sizeof(struct iwl_tx_cmd);
3757 		txcmd_align = 128;
3758 	}
3759 
3760 	txcmd_size += sizeof(struct iwl_cmd_header);
3761 	txcmd_size += 36; /* biggest possible 802.11 header */
3762 
3763 	/* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
3764 	if (WARN_ON((trans->mac_cfg->gen2 && txcmd_size >= txcmd_align)))
3765 		return -EINVAL;
3766 
3767 	snprintf(trans_pcie->dev_cmd_pool_name,
3768 		 sizeof(trans_pcie->dev_cmd_pool_name),
3769 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
3770 
3771 	trans_pcie->dev_cmd_pool =
3772 		kmem_cache_create(trans_pcie->dev_cmd_pool_name,
3773 				  txcmd_size, txcmd_align,
3774 				  SLAB_HWCACHE_ALIGN, NULL);
3775 	if (!trans_pcie->dev_cmd_pool)
3776 		return -ENOMEM;
3777 
3778 	return 0;
3779 }
3780 
3781 static struct iwl_trans *
iwl_trans_pcie_alloc(struct pci_dev * pdev,const struct iwl_mac_cfg * mac_cfg,struct iwl_trans_info * info,u8 __iomem * hw_base)3782 iwl_trans_pcie_alloc(struct pci_dev *pdev,
3783 		     const struct iwl_mac_cfg *mac_cfg,
3784 		     struct iwl_trans_info *info, u8 __iomem *hw_base)
3785 {
3786 	struct iwl_trans_pcie *trans_pcie, **priv;
3787 	struct iwl_trans *trans;
3788 	unsigned int bc_tbl_n_entries;
3789 	int ret, addr_size;
3790 
3791 	trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie), &pdev->dev,
3792 				mac_cfg);
3793 	if (!trans)
3794 		return ERR_PTR(-ENOMEM);
3795 
3796 	trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3797 
3798 	trans_pcie->hw_base = hw_base;
3799 
3800 	/* Initialize the wait queue for commands */
3801 	init_waitqueue_head(&trans_pcie->wait_command_queue);
3802 
3803 	ret = iwl_trans_pcie_alloc_txcmd_pool(trans);
3804 	if (ret)
3805 		goto out_free_trans;
3806 
3807 	if (trans->mac_cfg->gen2) {
3808 		trans_pcie->txqs.tfd.addr_size = 64;
3809 		trans_pcie->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
3810 		trans_pcie->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
3811 	} else {
3812 		trans_pcie->txqs.tfd.addr_size = 36;
3813 		trans_pcie->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
3814 		trans_pcie->txqs.tfd.size = sizeof(struct iwl_tfd);
3815 	}
3816 
3817 	trans_pcie->supported_dma_mask = (u32)DMA_BIT_MASK(12);
3818 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
3819 		trans_pcie->supported_dma_mask = (u32)DMA_BIT_MASK(11);
3820 
3821 	info->max_skb_frags = IWL_TRANS_PCIE_MAX_FRAGS(trans_pcie);
3822 
3823 #ifdef CONFIG_INET
3824 	trans_pcie->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
3825 	if (!trans_pcie->txqs.tso_hdr_page) {
3826 		ret = -ENOMEM;
3827 		goto out_free_txcmd_pool;
3828 	}
3829 #endif
3830 
3831 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
3832 		bc_tbl_n_entries = TFD_QUEUE_BC_SIZE_BZ;
3833 	else if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
3834 		bc_tbl_n_entries = TFD_QUEUE_BC_SIZE_AX210;
3835 	else
3836 		bc_tbl_n_entries = TFD_QUEUE_BC_SIZE;
3837 
3838 	trans_pcie->txqs.bc_tbl_size =
3839 		sizeof(struct iwl_bc_tbl_entry) * bc_tbl_n_entries;
3840 	/*
3841 	 * For gen2 devices, we use a single allocation for each byte-count
3842 	 * table, but they're pretty small (1k) so use a DMA pool that we
3843 	 * allocate here.
3844 	 */
3845 	if (trans->mac_cfg->gen2) {
3846 		trans_pcie->txqs.bc_pool =
3847 			dmam_pool_create("iwlwifi:bc", trans->dev,
3848 					 trans_pcie->txqs.bc_tbl_size,
3849 					 256, 0);
3850 		if (!trans_pcie->txqs.bc_pool) {
3851 			ret = -ENOMEM;
3852 			goto out_free_tso;
3853 		}
3854 	}
3855 
3856 	/* Some things must not change even if the config does */
3857 	WARN_ON(trans_pcie->txqs.tfd.addr_size !=
3858 		(trans->mac_cfg->gen2 ? 64 : 36));
3859 
3860 	/* Initialize NAPI here - it should be before registering to mac80211
3861 	 * in the opmode but after the HW struct is allocated.
3862 	 */
3863 	trans_pcie->napi_dev = alloc_netdev_dummy(sizeof(struct iwl_trans_pcie *));
3864 	if (!trans_pcie->napi_dev) {
3865 		ret = -ENOMEM;
3866 		goto out_free_tso;
3867 	}
3868 	/* The private struct in netdev is a pointer to struct iwl_trans_pcie */
3869 	priv = netdev_priv(trans_pcie->napi_dev);
3870 	*priv = trans_pcie;
3871 
3872 	trans_pcie->trans = trans;
3873 	trans_pcie->opmode_down = true;
3874 	spin_lock_init(&trans_pcie->irq_lock);
3875 	spin_lock_init(&trans_pcie->reg_lock);
3876 	spin_lock_init(&trans_pcie->alloc_page_lock);
3877 	mutex_init(&trans_pcie->mutex);
3878 	init_waitqueue_head(&trans_pcie->ucode_write_waitq);
3879 	init_waitqueue_head(&trans_pcie->fw_reset_waitq);
3880 	init_waitqueue_head(&trans_pcie->imr_waitq);
3881 
3882 	trans_pcie->rba.alloc_wq = alloc_workqueue("rb_allocator",
3883 						   WQ_HIGHPRI | WQ_UNBOUND, 0);
3884 	if (!trans_pcie->rba.alloc_wq) {
3885 		ret = -ENOMEM;
3886 		goto out_free_ndev;
3887 	}
3888 	INIT_WORK(&trans_pcie->rba.rx_alloc, iwl_pcie_rx_allocator_work);
3889 
3890 	trans_pcie->debug_rfkill = -1;
3891 
3892 	if (!mac_cfg->base->pcie_l1_allowed) {
3893 		/*
3894 		 * W/A - seems to solve weird behavior. We need to remove this
3895 		 * if we don't want to stay in L1 all the time. This wastes a
3896 		 * lot of power.
3897 		 */
3898 		pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
3899 				       PCIE_LINK_STATE_L1 |
3900 				       PCIE_LINK_STATE_CLKPM);
3901 	}
3902 
3903 	addr_size = trans_pcie->txqs.tfd.addr_size;
3904 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(addr_size));
3905 	if (ret) {
3906 		ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3907 		/* both attempts failed: */
3908 		if (ret) {
3909 			dev_err(&pdev->dev, "No suitable DMA available\n");
3910 			goto out_no_pci;
3911 		}
3912 	}
3913 
3914 	/* We disable the RETRY_TIMEOUT register (0x41) to keep
3915 	 * PCI Tx retries from interfering with C3 CPU state */
3916 	pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
3917 
3918 	trans_pcie->pci_dev = pdev;
3919 	iwl_disable_interrupts(trans);
3920 
3921 	/*
3922 	 * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have
3923 	 * changed, and now the revision step also includes bit 0-1 (no more
3924 	 * "dash" value). To keep hw_rev backwards compatible - we'll store it
3925 	 * in the old format.
3926 	 */
3927 	if (mac_cfg->device_family >= IWL_DEVICE_FAMILY_8000)
3928 		info->hw_rev_step = info->hw_rev & 0xF;
3929 	else
3930 		info->hw_rev_step = (info->hw_rev & 0xC) >> 2;
3931 
3932 	IWL_DEBUG_INFO(trans, "HW REV: 0x%0x\n", info->hw_rev);
3933 
3934 	iwl_pcie_set_interrupt_capa(pdev, trans, mac_cfg, info);
3935 
3936 	init_waitqueue_head(&trans_pcie->sx_waitq);
3937 
3938 	ret = iwl_pcie_alloc_invalid_tx_cmd(trans);
3939 	if (ret)
3940 		goto out_no_pci;
3941 
3942 	if (trans_pcie->msix_enabled) {
3943 		ret = iwl_pcie_init_msix_handler(pdev, trans_pcie, info);
3944 		if (ret)
3945 			goto out_no_pci;
3946 	 } else {
3947 		ret = iwl_pcie_alloc_ict(trans);
3948 		if (ret)
3949 			goto out_no_pci;
3950 
3951 		ret = devm_request_threaded_irq(&pdev->dev, pdev->irq,
3952 						iwl_pcie_isr,
3953 						iwl_pcie_irq_handler,
3954 						IRQF_SHARED, DRV_NAME, trans);
3955 		if (ret) {
3956 			IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
3957 			goto out_free_ict;
3958 		}
3959 	 }
3960 
3961 #ifdef CONFIG_IWLWIFI_DEBUGFS
3962 	trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_CLOSED;
3963 	mutex_init(&trans_pcie->fw_mon_data.mutex);
3964 #endif
3965 
3966 	iwl_dbg_tlv_init(trans);
3967 
3968 	return trans;
3969 
3970 out_free_ict:
3971 	iwl_pcie_free_ict(trans);
3972 out_no_pci:
3973 	destroy_workqueue(trans_pcie->rba.alloc_wq);
3974 out_free_ndev:
3975 	free_netdev(trans_pcie->napi_dev);
3976 out_free_tso:
3977 #ifdef CONFIG_INET
3978 	free_percpu(trans_pcie->txqs.tso_hdr_page);
3979 out_free_txcmd_pool:
3980 	kmem_cache_destroy(trans_pcie->dev_cmd_pool);
3981 #endif
3982 out_free_trans:
3983 	iwl_trans_free(trans);
3984 	return ERR_PTR(ret);
3985 }
3986 
iwl_trans_pcie_copy_imr_fh(struct iwl_trans * trans,u32 dst_addr,u64 src_addr,u32 byte_cnt)3987 void iwl_trans_pcie_copy_imr_fh(struct iwl_trans *trans,
3988 				u32 dst_addr, u64 src_addr, u32 byte_cnt)
3989 {
3990 	iwl_write_prph(trans, IMR_UREG_CHICK,
3991 		       iwl_read_prph(trans, IMR_UREG_CHICK) |
3992 		       IMR_UREG_CHICK_HALT_UMAC_PERMANENTLY_MSK);
3993 	iwl_write_prph(trans, IMR_TFH_SRV_DMA_CHNL0_SRAM_ADDR, dst_addr);
3994 	iwl_write_prph(trans, IMR_TFH_SRV_DMA_CHNL0_DRAM_ADDR_LSB,
3995 		       (u32)(src_addr & 0xFFFFFFFF));
3996 	iwl_write_prph(trans, IMR_TFH_SRV_DMA_CHNL0_DRAM_ADDR_MSB,
3997 		       iwl_get_dma_hi_addr(src_addr));
3998 	iwl_write_prph(trans, IMR_TFH_SRV_DMA_CHNL0_BC, byte_cnt);
3999 	iwl_write_prph(trans, IMR_TFH_SRV_DMA_CHNL0_CTRL,
4000 		       IMR_TFH_SRV_DMA_CHNL0_CTRL_D2S_IRQ_TARGET_POS |
4001 		       IMR_TFH_SRV_DMA_CHNL0_CTRL_D2S_DMA_EN_POS |
4002 		       IMR_TFH_SRV_DMA_CHNL0_CTRL_D2S_RS_MSK);
4003 }
4004 
iwl_trans_pcie_copy_imr(struct iwl_trans * trans,u32 dst_addr,u64 src_addr,u32 byte_cnt)4005 int iwl_trans_pcie_copy_imr(struct iwl_trans *trans,
4006 			    u32 dst_addr, u64 src_addr, u32 byte_cnt)
4007 {
4008 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
4009 	int ret = -1;
4010 
4011 	trans_pcie->imr_status = IMR_D2S_REQUESTED;
4012 	iwl_trans_pcie_copy_imr_fh(trans, dst_addr, src_addr, byte_cnt);
4013 	ret = wait_event_timeout(trans_pcie->imr_waitq,
4014 				 trans_pcie->imr_status !=
4015 				 IMR_D2S_REQUESTED, 5 * HZ);
4016 	if (!ret || trans_pcie->imr_status == IMR_D2S_ERROR) {
4017 		IWL_ERR(trans, "Failed to copy IMR Memory chunk!\n");
4018 		iwl_trans_pcie_dump_regs(trans, trans_pcie->pci_dev);
4019 		return -ETIMEDOUT;
4020 	}
4021 	trans_pcie->imr_status = IMR_D2S_IDLE;
4022 	return 0;
4023 }
4024 
4025 /*
4026  * Read rf id and cdb info from prph register and store it
4027  */
get_crf_id(struct iwl_trans * iwl_trans,struct iwl_trans_info * info)4028 static void get_crf_id(struct iwl_trans *iwl_trans,
4029 		       struct iwl_trans_info *info)
4030 {
4031 	u32 sd_reg_ver_addr;
4032 	u32 hw_wfpm_id;
4033 	u32 val = 0;
4034 	u8 step;
4035 
4036 	if (iwl_trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
4037 		sd_reg_ver_addr = SD_REG_VER_GEN2;
4038 	else
4039 		sd_reg_ver_addr = SD_REG_VER;
4040 
4041 	/* Enable access to peripheral registers */
4042 	val = iwl_read_umac_prph_no_grab(iwl_trans, WFPM_CTRL_REG);
4043 	val |= WFPM_AUX_CTL_AUX_IF_MAC_OWNER_MSK;
4044 	iwl_write_umac_prph_no_grab(iwl_trans, WFPM_CTRL_REG, val);
4045 
4046 	/* Read crf info */
4047 	info->hw_crf_id = iwl_read_prph_no_grab(iwl_trans, sd_reg_ver_addr);
4048 
4049 	/* Read cnv info */
4050 	info->hw_cnv_id = iwl_read_prph_no_grab(iwl_trans, CNVI_AUX_MISC_CHIP);
4051 
4052 	/* For BZ-W, take B step also when A step is indicated */
4053 	if (CSR_HW_REV_TYPE(info->hw_rev) == IWL_CFG_MAC_TYPE_BZ_W)
4054 		step = SILICON_B_STEP;
4055 
4056 	/* In BZ, the MAC step must be read from the CNVI aux register */
4057 	if (CSR_HW_REV_TYPE(info->hw_rev) == IWL_CFG_MAC_TYPE_BZ) {
4058 		step = CNVI_AUX_MISC_CHIP_MAC_STEP(info->hw_cnv_id);
4059 
4060 		/* For BZ-U, take B step also when A step is indicated */
4061 		if ((CNVI_AUX_MISC_CHIP_PROD_TYPE(info->hw_cnv_id) ==
4062 		    CNVI_AUX_MISC_CHIP_PROD_TYPE_BZ_U) &&
4063 		    step == SILICON_A_STEP)
4064 			step = SILICON_B_STEP;
4065 	}
4066 
4067 	if (CSR_HW_REV_TYPE(info->hw_rev) == IWL_CFG_MAC_TYPE_BZ ||
4068 	    CSR_HW_REV_TYPE(info->hw_rev) == IWL_CFG_MAC_TYPE_BZ_W) {
4069 		info->hw_rev_step = step;
4070 		info->hw_rev |= step;
4071 	}
4072 
4073 	/* Read cdb info (also contains the jacket info if needed in the future */
4074 	hw_wfpm_id = iwl_read_umac_prph_no_grab(iwl_trans, WFPM_OTP_CFG1_ADDR);
4075 
4076 	IWL_INFO(iwl_trans, "Detected crf-id 0x%x, cnv-id 0x%x wfpm id 0x%x\n",
4077 		 info->hw_crf_id, info->hw_cnv_id, hw_wfpm_id);
4078 }
4079 
4080 /*
4081  * In case that there is no OTP on the NIC, map the rf id and cdb info
4082  * from the prph registers.
4083  */
map_crf_id(struct iwl_trans * iwl_trans,struct iwl_trans_info * info)4084 static int map_crf_id(struct iwl_trans *iwl_trans,
4085 		      struct iwl_trans_info *info)
4086 {
4087 	int ret = 0;
4088 	u32 val = info->hw_crf_id;
4089 	u32 step_id = REG_CRF_ID_STEP(val);
4090 	u32 slave_id = REG_CRF_ID_SLAVE(val);
4091 	u32 hw_wfpm_id = iwl_read_umac_prph_no_grab(iwl_trans,
4092 						    WFPM_OTP_CFG1_ADDR);
4093 	u32 cdb_id_wfpm = WFPM_OTP_CFG1_IS_CDB(hw_wfpm_id);
4094 
4095 	/* Map between crf id to rf id */
4096 	switch (REG_CRF_ID_TYPE(val)) {
4097 	case REG_CRF_ID_TYPE_JF_1:
4098 		info->hw_rf_id = (IWL_CFG_RF_TYPE_JF1 << 12);
4099 		break;
4100 	case REG_CRF_ID_TYPE_JF_2:
4101 		info->hw_rf_id = (IWL_CFG_RF_TYPE_JF2 << 12);
4102 		break;
4103 	case REG_CRF_ID_TYPE_HR_NONE_CDB_1X1:
4104 		info->hw_rf_id = (IWL_CFG_RF_TYPE_HR1 << 12);
4105 		break;
4106 	case REG_CRF_ID_TYPE_HR_NONE_CDB:
4107 		info->hw_rf_id = (IWL_CFG_RF_TYPE_HR2 << 12);
4108 		break;
4109 	case REG_CRF_ID_TYPE_HR_CDB:
4110 		info->hw_rf_id = (IWL_CFG_RF_TYPE_HR2 << 12);
4111 		break;
4112 	case REG_CRF_ID_TYPE_GF:
4113 		info->hw_rf_id = (IWL_CFG_RF_TYPE_GF << 12);
4114 		break;
4115 	case REG_CRF_ID_TYPE_FM:
4116 		info->hw_rf_id = (IWL_CFG_RF_TYPE_FM << 12);
4117 		break;
4118 	case REG_CRF_ID_TYPE_WHP:
4119 		info->hw_rf_id = (IWL_CFG_RF_TYPE_WH << 12);
4120 		break;
4121 	case REG_CRF_ID_TYPE_PE:
4122 		info->hw_rf_id = (IWL_CFG_RF_TYPE_PE << 12);
4123 		break;
4124 	default:
4125 		ret = -EIO;
4126 		IWL_ERR(iwl_trans,
4127 			"Can't find a correct rfid for crf id 0x%x\n",
4128 			REG_CRF_ID_TYPE(val));
4129 		goto out;
4130 	}
4131 
4132 	/* Set Step-id */
4133 	info->hw_rf_id |= (step_id << 8);
4134 
4135 	/* Set CDB capabilities */
4136 	if (cdb_id_wfpm || slave_id) {
4137 		info->hw_rf_id += BIT(28);
4138 		IWL_INFO(iwl_trans, "Adding cdb to rf id\n");
4139 	}
4140 
4141 	IWL_INFO(iwl_trans,
4142 		 "Detected rf-type 0x%x step-id 0x%x slave-id 0x%x from crf id 0x%x\n",
4143 		 REG_CRF_ID_TYPE(val), step_id, slave_id, info->hw_rf_id);
4144 	IWL_INFO(iwl_trans,
4145 		 "Detected cdb-id 0x%x from wfpm id 0x%x\n",
4146 		 cdb_id_wfpm, hw_wfpm_id);
4147 out:
4148 	return ret;
4149 }
4150 
iwl_pcie_recheck_me_status(struct work_struct * wk)4151 static void iwl_pcie_recheck_me_status(struct work_struct *wk)
4152 {
4153 	struct iwl_trans_pcie *trans_pcie = container_of(wk,
4154 							 typeof(*trans_pcie),
4155 							 me_recheck_wk.work);
4156 	u32 val;
4157 
4158 	val = iwl_read32(trans_pcie->trans, CSR_HW_IF_CONFIG_REG);
4159 	trans_pcie->me_present = !!(val & CSR_HW_IF_CONFIG_REG_IAMT_UP);
4160 }
4161 
iwl_pcie_check_me_status(struct iwl_trans * trans)4162 static void iwl_pcie_check_me_status(struct iwl_trans *trans)
4163 {
4164 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
4165 	u32 val;
4166 
4167 	trans_pcie->me_present = -1;
4168 
4169 	INIT_DELAYED_WORK(&trans_pcie->me_recheck_wk,
4170 			  iwl_pcie_recheck_me_status);
4171 
4172 	/* we don't have a good way of determining this until BZ */
4173 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_BZ)
4174 		return;
4175 
4176 	val = iwl_read_prph(trans, CNVI_SCU_REG_FOR_ECO_1);
4177 	if (val & CNVI_SCU_REG_FOR_ECO_1_WIAMT_KNOWN) {
4178 		trans_pcie->me_present =
4179 			!!(val & CNVI_SCU_REG_FOR_ECO_1_WIAMT_PRESENT);
4180 		return;
4181 	}
4182 
4183 	val = iwl_read32(trans, CSR_HW_IF_CONFIG_REG);
4184 	if (val & (CSR_HW_IF_CONFIG_REG_ME_OWN |
4185 		   CSR_HW_IF_CONFIG_REG_IAMT_UP)) {
4186 		trans_pcie->me_present = 1;
4187 		return;
4188 	}
4189 
4190 	/* recheck again later, ME might still be initializing */
4191 	schedule_delayed_work(&trans_pcie->me_recheck_wk, HZ);
4192 }
4193 
iwl_pci_gen1_2_probe(struct pci_dev * pdev,const struct pci_device_id * ent,const struct iwl_mac_cfg * mac_cfg,u8 __iomem * hw_base,u32 hw_rev)4194 int iwl_pci_gen1_2_probe(struct pci_dev *pdev,
4195 			 const struct pci_device_id *ent,
4196 			 const struct iwl_mac_cfg *mac_cfg,
4197 			 u8 __iomem *hw_base, u32 hw_rev)
4198 {
4199 	const struct iwl_dev_info *dev_info;
4200 	struct iwl_trans_info info = {
4201 		.hw_id = (pdev->device << 16) + pdev->subsystem_device,
4202 		.hw_rev = hw_rev,
4203 	};
4204 	struct iwl_trans *iwl_trans;
4205 	struct iwl_trans_pcie *trans_pcie;
4206 	int ret;
4207 
4208 	iwl_trans = iwl_trans_pcie_alloc(pdev, mac_cfg, &info, hw_base);
4209 	if (IS_ERR(iwl_trans))
4210 		return PTR_ERR(iwl_trans);
4211 
4212 	trans_pcie = IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
4213 
4214 	iwl_trans_pcie_check_product_reset_status(pdev);
4215 	iwl_trans_pcie_check_product_reset_mode(pdev);
4216 
4217 	/* set the things we know so far for the grab NIC access */
4218 	iwl_trans_set_info(iwl_trans, &info);
4219 
4220 	/*
4221 	 * Let's try to grab NIC access early here. Sometimes, NICs may
4222 	 * fail to initialize, and if that happens it's better if we see
4223 	 * issues early on (and can reprobe, per the logic inside), than
4224 	 * first trying to load the firmware etc. and potentially only
4225 	 * detecting any problems when the first interface is brought up.
4226 	 */
4227 	ret = iwl_pcie_prepare_card_hw(iwl_trans);
4228 	if (!ret) {
4229 		ret = iwl_trans_activate_nic(iwl_trans);
4230 		if (ret)
4231 			goto out_free_trans;
4232 		if (iwl_trans_grab_nic_access(iwl_trans)) {
4233 			get_crf_id(iwl_trans, &info);
4234 			/* all good */
4235 			iwl_trans_release_nic_access(iwl_trans);
4236 		} else {
4237 			ret = -EIO;
4238 			goto out_free_trans;
4239 		}
4240 	}
4241 
4242 	info.hw_rf_id = iwl_read32(iwl_trans, CSR_HW_RF_ID);
4243 
4244 	/*
4245 	 * The RF_ID is set to zero in blank OTP so read version to
4246 	 * extract the RF_ID.
4247 	 * This is relevant only for family 9000 and up.
4248 	 */
4249 	if (iwl_trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_9000 &&
4250 	    !CSR_HW_RFID_TYPE(info.hw_rf_id) && map_crf_id(iwl_trans, &info)) {
4251 		ret = -EINVAL;
4252 		goto out_free_trans;
4253 	}
4254 
4255 	IWL_INFO(iwl_trans, "PCI dev %04x/%04x, rev=0x%x, rfid=0x%x\n",
4256 		 pdev->device, pdev->subsystem_device,
4257 		 info.hw_rev, info.hw_rf_id);
4258 
4259 #if !IS_ENABLED(CONFIG_IWLMLD)
4260 	if (iwl_drv_is_wifi7_supported(iwl_trans)) {
4261 		IWL_ERR(iwl_trans,
4262 			"IWLMLD needs to be compiled to support this device\n");
4263 		ret = -EOPNOTSUPP;
4264 		goto out_free_trans;
4265 	}
4266 #endif
4267 
4268 	dev_info = iwl_pci_find_dev_info(pdev->device, pdev->subsystem_device,
4269 					 CSR_HW_RFID_TYPE(info.hw_rf_id),
4270 					 CSR_HW_RFID_IS_CDB(info.hw_rf_id),
4271 					 IWL_SUBDEVICE_RF_ID(pdev->subsystem_device),
4272 					 IWL_SUBDEVICE_BW_LIM(pdev->subsystem_device),
4273 					 !iwl_trans->mac_cfg->integrated);
4274 	if (dev_info) {
4275 		iwl_trans->cfg = dev_info->cfg;
4276 		info.name = dev_info->name;
4277 	}
4278 
4279 #if IS_ENABLED(CONFIG_IWLMVM)
4280 
4281 	/*
4282 	 * special-case 7265D, it has the same PCI IDs.
4283 	 *
4284 	 * Note that because we already pass the cfg to the transport above,
4285 	 * all the parameters that the transport uses must, until that is
4286 	 * changed, be identical to the ones in the 7265D configuration.
4287 	 */
4288 	if (iwl_trans->cfg == &iwl7265_cfg &&
4289 	    (info.hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_7265D)
4290 		iwl_trans->cfg = &iwl7265d_cfg;
4291 #endif
4292 	if (!iwl_trans->cfg) {
4293 		pr_err("No config found for PCI dev %04x/%04x, rev=0x%x, rfid=0x%x\n",
4294 		       pdev->device, pdev->subsystem_device,
4295 		       info.hw_rev, info.hw_rf_id);
4296 		ret = -EINVAL;
4297 		goto out_free_trans;
4298 	}
4299 
4300 	IWL_INFO(iwl_trans, "Detected %s\n", info.name);
4301 
4302 	if (iwl_trans->mac_cfg->mq_rx_supported) {
4303 		if (WARN_ON(!iwl_trans->cfg->num_rbds)) {
4304 			ret = -EINVAL;
4305 			goto out_free_trans;
4306 		}
4307 		trans_pcie->num_rx_bufs = iwl_trans_get_num_rbds(iwl_trans);
4308 	} else {
4309 		trans_pcie->num_rx_bufs = RX_QUEUE_SIZE;
4310 	}
4311 
4312 	if (!iwl_trans->mac_cfg->integrated) {
4313 		u16 link_status;
4314 
4315 		pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &link_status);
4316 
4317 		info.pcie_link_speed =
4318 			u16_get_bits(link_status, PCI_EXP_LNKSTA_CLS);
4319 	}
4320 
4321 	iwl_trans_set_info(iwl_trans, &info);
4322 
4323 	pci_set_drvdata(pdev, iwl_trans);
4324 
4325 	iwl_pcie_check_me_status(iwl_trans);
4326 
4327 	/* try to get ownership so that we'll know if we don't own it */
4328 	iwl_pcie_prepare_card_hw(iwl_trans);
4329 
4330 	iwl_trans->drv = iwl_drv_start(iwl_trans);
4331 
4332 	if (IS_ERR(iwl_trans->drv)) {
4333 		ret = PTR_ERR(iwl_trans->drv);
4334 		goto out_free_trans;
4335 	}
4336 
4337 	/* register transport layer debugfs here */
4338 	iwl_trans_pcie_dbgfs_register(iwl_trans);
4339 
4340 	return 0;
4341 
4342 out_free_trans:
4343 	iwl_trans_pcie_free(iwl_trans);
4344 	return ret;
4345 }
4346 
iwl_pcie_gen1_2_remove(struct iwl_trans * trans)4347 void iwl_pcie_gen1_2_remove(struct iwl_trans *trans)
4348 {
4349 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
4350 
4351 	cancel_delayed_work_sync(&trans_pcie->me_recheck_wk);
4352 
4353 	iwl_drv_stop(trans->drv);
4354 
4355 	iwl_trans_pcie_free(trans);
4356 }
4357 
iwl_pcie_gen1_2_activate_nic(struct iwl_trans * trans)4358 int iwl_pcie_gen1_2_activate_nic(struct iwl_trans *trans)
4359 {
4360 	const struct iwl_mac_cfg *mac_cfg = trans->mac_cfg;
4361 	u32 poll_ready;
4362 	int err;
4363 
4364 	if (mac_cfg->bisr_workaround) {
4365 		/* ensure the TOP FSM isn't still in previous reset */
4366 		mdelay(2);
4367 	}
4368 
4369 	/*
4370 	 * Set "initialization complete" bit to move adapter from
4371 	 * D0U* --> D0A* (powered-up active) state.
4372 	 */
4373 	if (mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
4374 		iwl_set_bit(trans, CSR_GP_CNTRL,
4375 			    CSR_GP_CNTRL_REG_FLAG_BZ_MAC_ACCESS_REQ |
4376 			    CSR_GP_CNTRL_REG_FLAG_MAC_INIT);
4377 		poll_ready = CSR_GP_CNTRL_REG_FLAG_MAC_STATUS;
4378 	} else {
4379 		iwl_set_bit(trans, CSR_GP_CNTRL,
4380 			    CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
4381 		poll_ready = CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY;
4382 	}
4383 
4384 	if (mac_cfg->device_family == IWL_DEVICE_FAMILY_8000)
4385 		udelay(2);
4386 
4387 	/*
4388 	 * Wait for clock stabilization; once stabilized, access to
4389 	 * device-internal resources is supported, e.g. iwl_write_prph()
4390 	 * and accesses to uCode SRAM.
4391 	 */
4392 	err = iwl_poll_bits(trans, CSR_GP_CNTRL, poll_ready, 25000);
4393 	if (err < 0) {
4394 		IWL_DEBUG_INFO(trans, "Failed to wake NIC\n");
4395 
4396 		iwl_pcie_dump_host_monitor(trans);
4397 	}
4398 
4399 	if (mac_cfg->bisr_workaround) {
4400 		/* ensure BISR shift has finished */
4401 		udelay(200);
4402 	}
4403 
4404 	return err;
4405 }
4406