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