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