1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2007 - 2015 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10afb84431SEmmanuel Grumbach  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
22e705c121SKalle Valo  * along with this program; if not, write to the Free Software
23e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24e705c121SKalle Valo  * USA
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
27e705c121SKalle Valo  * in the file called COPYING.
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * Contact Information:
30cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
31e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32e705c121SKalle Valo  *
33e705c121SKalle Valo  * BSD LICENSE
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Copyright(c) 2005 - 2015 Intel Corporation. All rights reserved.
36e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37afb84431SEmmanuel Grumbach  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
38e705c121SKalle Valo  * All rights reserved.
39e705c121SKalle Valo  *
40e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
41e705c121SKalle Valo  * modification, are permitted provided that the following conditions
42e705c121SKalle Valo  * are met:
43e705c121SKalle Valo  *
44e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
46e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
47e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
48e705c121SKalle Valo  *    the documentation and/or other materials provided with the
49e705c121SKalle Valo  *    distribution.
50e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
51e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
52e705c121SKalle Valo  *    from this software without specific prior written permission.
53e705c121SKalle Valo  *
54e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65e705c121SKalle Valo  *
66e705c121SKalle Valo  *****************************************************************************/
67e705c121SKalle Valo #include <linux/pci.h>
68e705c121SKalle Valo #include <linux/pci-aspm.h>
69e705c121SKalle Valo #include <linux/interrupt.h>
70e705c121SKalle Valo #include <linux/debugfs.h>
71e705c121SKalle Valo #include <linux/sched.h>
72e705c121SKalle Valo #include <linux/bitops.h>
73e705c121SKalle Valo #include <linux/gfp.h>
74e705c121SKalle Valo #include <linux/vmalloc.h>
75b3ff1270SLuca Coelho #include <linux/pm_runtime.h>
76e705c121SKalle Valo 
77e705c121SKalle Valo #include "iwl-drv.h"
78e705c121SKalle Valo #include "iwl-trans.h"
79e705c121SKalle Valo #include "iwl-csr.h"
80e705c121SKalle Valo #include "iwl-prph.h"
81e705c121SKalle Valo #include "iwl-scd.h"
82e705c121SKalle Valo #include "iwl-agn-hw.h"
83e705c121SKalle Valo #include "iwl-fw-error-dump.h"
84e705c121SKalle Valo #include "internal.h"
85e705c121SKalle Valo #include "iwl-fh.h"
86e705c121SKalle Valo 
87e705c121SKalle Valo /* extended range in FW SRAM */
88e705c121SKalle Valo #define IWL_FW_MEM_EXTENDED_START	0x40000
89e705c121SKalle Valo #define IWL_FW_MEM_EXTENDED_END		0x57FFF
90e705c121SKalle Valo 
91e705c121SKalle Valo static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans)
92e705c121SKalle Valo {
93e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
94e705c121SKalle Valo 
95e705c121SKalle Valo 	if (!trans_pcie->fw_mon_page)
96e705c121SKalle Valo 		return;
97e705c121SKalle Valo 
98e705c121SKalle Valo 	dma_unmap_page(trans->dev, trans_pcie->fw_mon_phys,
99e705c121SKalle Valo 		       trans_pcie->fw_mon_size, DMA_FROM_DEVICE);
100e705c121SKalle Valo 	__free_pages(trans_pcie->fw_mon_page,
101e705c121SKalle Valo 		     get_order(trans_pcie->fw_mon_size));
102e705c121SKalle Valo 	trans_pcie->fw_mon_page = NULL;
103e705c121SKalle Valo 	trans_pcie->fw_mon_phys = 0;
104e705c121SKalle Valo 	trans_pcie->fw_mon_size = 0;
105e705c121SKalle Valo }
106e705c121SKalle Valo 
107e705c121SKalle Valo static void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power)
108e705c121SKalle Valo {
109e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
110e705c121SKalle Valo 	struct page *page = NULL;
111e705c121SKalle Valo 	dma_addr_t phys;
112e705c121SKalle Valo 	u32 size = 0;
113e705c121SKalle Valo 	u8 power;
114e705c121SKalle Valo 
115e705c121SKalle Valo 	if (!max_power) {
116e705c121SKalle Valo 		/* default max_power is maximum */
117e705c121SKalle Valo 		max_power = 26;
118e705c121SKalle Valo 	} else {
119e705c121SKalle Valo 		max_power += 11;
120e705c121SKalle Valo 	}
121e705c121SKalle Valo 
122e705c121SKalle Valo 	if (WARN(max_power > 26,
123e705c121SKalle Valo 		 "External buffer size for monitor is too big %d, check the FW TLV\n",
124e705c121SKalle Valo 		 max_power))
125e705c121SKalle Valo 		return;
126e705c121SKalle Valo 
127e705c121SKalle Valo 	if (trans_pcie->fw_mon_page) {
128e705c121SKalle Valo 		dma_sync_single_for_device(trans->dev, trans_pcie->fw_mon_phys,
129e705c121SKalle Valo 					   trans_pcie->fw_mon_size,
130e705c121SKalle Valo 					   DMA_FROM_DEVICE);
131e705c121SKalle Valo 		return;
132e705c121SKalle Valo 	}
133e705c121SKalle Valo 
134e705c121SKalle Valo 	phys = 0;
135e705c121SKalle Valo 	for (power = max_power; power >= 11; power--) {
136e705c121SKalle Valo 		int order;
137e705c121SKalle Valo 
138e705c121SKalle Valo 		size = BIT(power);
139e705c121SKalle Valo 		order = get_order(size);
140e705c121SKalle Valo 		page = alloc_pages(__GFP_COMP | __GFP_NOWARN | __GFP_ZERO,
141e705c121SKalle Valo 				   order);
142e705c121SKalle Valo 		if (!page)
143e705c121SKalle Valo 			continue;
144e705c121SKalle Valo 
145e705c121SKalle Valo 		phys = dma_map_page(trans->dev, page, 0, PAGE_SIZE << order,
146e705c121SKalle Valo 				    DMA_FROM_DEVICE);
147e705c121SKalle Valo 		if (dma_mapping_error(trans->dev, phys)) {
148e705c121SKalle Valo 			__free_pages(page, order);
149e705c121SKalle Valo 			page = NULL;
150e705c121SKalle Valo 			continue;
151e705c121SKalle Valo 		}
152e705c121SKalle Valo 		IWL_INFO(trans,
153e705c121SKalle Valo 			 "Allocated 0x%08x bytes (order %d) for firmware monitor.\n",
154e705c121SKalle Valo 			 size, order);
155e705c121SKalle Valo 		break;
156e705c121SKalle Valo 	}
157e705c121SKalle Valo 
158e705c121SKalle Valo 	if (WARN_ON_ONCE(!page))
159e705c121SKalle Valo 		return;
160e705c121SKalle Valo 
161e705c121SKalle Valo 	if (power != max_power)
162e705c121SKalle Valo 		IWL_ERR(trans,
163e705c121SKalle Valo 			"Sorry - debug buffer is only %luK while you requested %luK\n",
164e705c121SKalle Valo 			(unsigned long)BIT(power - 10),
165e705c121SKalle Valo 			(unsigned long)BIT(max_power - 10));
166e705c121SKalle Valo 
167e705c121SKalle Valo 	trans_pcie->fw_mon_page = page;
168e705c121SKalle Valo 	trans_pcie->fw_mon_phys = phys;
169e705c121SKalle Valo 	trans_pcie->fw_mon_size = size;
170e705c121SKalle Valo }
171e705c121SKalle Valo 
172e705c121SKalle Valo static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
173e705c121SKalle Valo {
174e705c121SKalle Valo 	iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
175e705c121SKalle Valo 		    ((reg & 0x0000ffff) | (2 << 28)));
176e705c121SKalle Valo 	return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
177e705c121SKalle Valo }
178e705c121SKalle Valo 
179e705c121SKalle Valo static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
180e705c121SKalle Valo {
181e705c121SKalle Valo 	iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
182e705c121SKalle Valo 	iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
183e705c121SKalle Valo 		    ((reg & 0x0000ffff) | (3 << 28)));
184e705c121SKalle Valo }
185e705c121SKalle Valo 
186e705c121SKalle Valo static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
187e705c121SKalle Valo {
188e705c121SKalle Valo 	if (trans->cfg->apmg_not_supported)
189e705c121SKalle Valo 		return;
190e705c121SKalle Valo 
191e705c121SKalle Valo 	if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
192e705c121SKalle Valo 		iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
193e705c121SKalle Valo 				       APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
194e705c121SKalle Valo 				       ~APMG_PS_CTRL_MSK_PWR_SRC);
195e705c121SKalle Valo 	else
196e705c121SKalle Valo 		iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
197e705c121SKalle Valo 				       APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
198e705c121SKalle Valo 				       ~APMG_PS_CTRL_MSK_PWR_SRC);
199e705c121SKalle Valo }
200e705c121SKalle Valo 
201e705c121SKalle Valo /* PCI registers */
202e705c121SKalle Valo #define PCI_CFG_RETRY_TIMEOUT	0x041
203e705c121SKalle Valo 
204eda50cdeSSara Sharon void iwl_pcie_apm_config(struct iwl_trans *trans)
205e705c121SKalle Valo {
206e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
207e705c121SKalle Valo 	u16 lctl;
208e705c121SKalle Valo 	u16 cap;
209e705c121SKalle Valo 
210e705c121SKalle Valo 	/*
211e705c121SKalle Valo 	 * HW bug W/A for instability in PCIe bus L0S->L1 transition.
212e705c121SKalle Valo 	 * Check if BIOS (or OS) enabled L1-ASPM on this device.
213e705c121SKalle Valo 	 * If so (likely), disable L0S, so device moves directly L0->L1;
214e705c121SKalle Valo 	 *    costs negligible amount of power savings.
215e705c121SKalle Valo 	 * If not (unlikely), enable L0S, so there is at least some
216e705c121SKalle Valo 	 *    power savings, even without L1.
217e705c121SKalle Valo 	 */
218e705c121SKalle Valo 	pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
219e705c121SKalle Valo 	if (lctl & PCI_EXP_LNKCTL_ASPM_L1)
220e705c121SKalle Valo 		iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
221e705c121SKalle Valo 	else
222e705c121SKalle Valo 		iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
223e705c121SKalle Valo 	trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
224e705c121SKalle Valo 
225e705c121SKalle Valo 	pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap);
226e705c121SKalle Valo 	trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN;
227e705c121SKalle Valo 	dev_info(trans->dev, "L1 %sabled - LTR %sabled\n",
228e705c121SKalle Valo 		 (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis",
229e705c121SKalle Valo 		 trans->ltr_enabled ? "En" : "Dis");
230e705c121SKalle Valo }
231e705c121SKalle Valo 
232e705c121SKalle Valo /*
233e705c121SKalle Valo  * Start up NIC's basic functionality after it has been reset
234e705c121SKalle Valo  * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
235e705c121SKalle Valo  * NOTE:  This does not load uCode nor start the embedded processor
236e705c121SKalle Valo  */
237e705c121SKalle Valo static int iwl_pcie_apm_init(struct iwl_trans *trans)
238e705c121SKalle Valo {
239e705c121SKalle Valo 	int ret = 0;
240e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
241e705c121SKalle Valo 
242e705c121SKalle Valo 	/*
243e705c121SKalle Valo 	 * Use "set_bit" below rather than "write", to preserve any hardware
244e705c121SKalle Valo 	 * bits already set by default after reset.
245e705c121SKalle Valo 	 */
246e705c121SKalle Valo 
247e705c121SKalle Valo 	/* Disable L0S exit timer (platform NMI Work/Around) */
2486e584873SSara Sharon 	if (trans->cfg->device_family < IWL_DEVICE_FAMILY_8000)
249e705c121SKalle Valo 		iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
250e705c121SKalle Valo 			    CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
251e705c121SKalle Valo 
252e705c121SKalle Valo 	/*
253e705c121SKalle Valo 	 * Disable L0s without affecting L1;
254e705c121SKalle Valo 	 *  don't wait for ICH L0s (ICH bug W/A)
255e705c121SKalle Valo 	 */
256e705c121SKalle Valo 	iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
257e705c121SKalle Valo 		    CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
258e705c121SKalle Valo 
259e705c121SKalle Valo 	/* Set FH wait threshold to maximum (HW error during stress W/A) */
260e705c121SKalle Valo 	iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
261e705c121SKalle Valo 
262e705c121SKalle Valo 	/*
263e705c121SKalle Valo 	 * Enable HAP INTA (interrupt from management bus) to
264e705c121SKalle Valo 	 * wake device's PCI Express link L1a -> L0s
265e705c121SKalle Valo 	 */
266e705c121SKalle Valo 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
267e705c121SKalle Valo 		    CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
268e705c121SKalle Valo 
269e705c121SKalle Valo 	iwl_pcie_apm_config(trans);
270e705c121SKalle Valo 
271e705c121SKalle Valo 	/* Configure analog phase-lock-loop before activating to D0A */
27277d76931SJohannes Berg 	if (trans->cfg->base_params->pll_cfg)
27377d76931SJohannes Berg 		iwl_set_bit(trans, CSR_ANA_PLL_CFG, CSR50_ANA_PLL_CFG_VAL);
274e705c121SKalle Valo 
275e705c121SKalle Valo 	/*
276e705c121SKalle Valo 	 * Set "initialization complete" bit to move adapter from
277e705c121SKalle Valo 	 * D0U* --> D0A* (powered-up active) state.
278e705c121SKalle Valo 	 */
279e705c121SKalle Valo 	iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
280e705c121SKalle Valo 
281e705c121SKalle Valo 	/*
282e705c121SKalle Valo 	 * Wait for clock stabilization; once stabilized, access to
283e705c121SKalle Valo 	 * device-internal resources is supported, e.g. iwl_write_prph()
284e705c121SKalle Valo 	 * and accesses to uCode SRAM.
285e705c121SKalle Valo 	 */
286e705c121SKalle Valo 	ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
287e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
288e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
289e705c121SKalle Valo 	if (ret < 0) {
290e705c121SKalle Valo 		IWL_DEBUG_INFO(trans, "Failed to init the card\n");
291e705c121SKalle Valo 		goto out;
292e705c121SKalle Valo 	}
293e705c121SKalle Valo 
294e705c121SKalle Valo 	if (trans->cfg->host_interrupt_operation_mode) {
295e705c121SKalle Valo 		/*
296e705c121SKalle Valo 		 * This is a bit of an abuse - This is needed for 7260 / 3160
297e705c121SKalle Valo 		 * only check host_interrupt_operation_mode even if this is
298e705c121SKalle Valo 		 * not related to host_interrupt_operation_mode.
299e705c121SKalle Valo 		 *
300e705c121SKalle Valo 		 * Enable the oscillator to count wake up time for L1 exit. This
301e705c121SKalle Valo 		 * consumes slightly more power (100uA) - but allows to be sure
302e705c121SKalle Valo 		 * that we wake up from L1 on time.
303e705c121SKalle Valo 		 *
304e705c121SKalle Valo 		 * This looks weird: read twice the same register, discard the
305e705c121SKalle Valo 		 * value, set a bit, and yet again, read that same register
306e705c121SKalle Valo 		 * just to discard the value. But that's the way the hardware
307e705c121SKalle Valo 		 * seems to like it.
308e705c121SKalle Valo 		 */
309e705c121SKalle Valo 		iwl_read_prph(trans, OSC_CLK);
310e705c121SKalle Valo 		iwl_read_prph(trans, OSC_CLK);
311e705c121SKalle Valo 		iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
312e705c121SKalle Valo 		iwl_read_prph(trans, OSC_CLK);
313e705c121SKalle Valo 		iwl_read_prph(trans, OSC_CLK);
314e705c121SKalle Valo 	}
315e705c121SKalle Valo 
316e705c121SKalle Valo 	/*
317e705c121SKalle Valo 	 * Enable DMA clock and wait for it to stabilize.
318e705c121SKalle Valo 	 *
319e705c121SKalle Valo 	 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
320e705c121SKalle Valo 	 * bits do not disable clocks.  This preserves any hardware
321e705c121SKalle Valo 	 * bits already set by default in "CLK_CTRL_REG" after reset.
322e705c121SKalle Valo 	 */
323e705c121SKalle Valo 	if (!trans->cfg->apmg_not_supported) {
324e705c121SKalle Valo 		iwl_write_prph(trans, APMG_CLK_EN_REG,
325e705c121SKalle Valo 			       APMG_CLK_VAL_DMA_CLK_RQT);
326e705c121SKalle Valo 		udelay(20);
327e705c121SKalle Valo 
328e705c121SKalle Valo 		/* Disable L1-Active */
329e705c121SKalle Valo 		iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
330e705c121SKalle Valo 				  APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
331e705c121SKalle Valo 
332e705c121SKalle Valo 		/* Clear the interrupt in APMG if the NIC is in RFKILL */
333e705c121SKalle Valo 		iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
334e705c121SKalle Valo 			       APMG_RTC_INT_STT_RFKILL);
335e705c121SKalle Valo 	}
336e705c121SKalle Valo 
337e705c121SKalle Valo 	set_bit(STATUS_DEVICE_ENABLED, &trans->status);
338e705c121SKalle Valo 
339e705c121SKalle Valo out:
340e705c121SKalle Valo 	return ret;
341e705c121SKalle Valo }
342e705c121SKalle Valo 
343e705c121SKalle Valo /*
344e705c121SKalle Valo  * Enable LP XTAL to avoid HW bug where device may consume much power if
345e705c121SKalle Valo  * FW is not loaded after device reset. LP XTAL is disabled by default
346e705c121SKalle Valo  * after device HW reset. Do it only if XTAL is fed by internal source.
347e705c121SKalle Valo  * Configure device's "persistence" mode to avoid resetting XTAL again when
348e705c121SKalle Valo  * SHRD_HW_RST occurs in S3.
349e705c121SKalle Valo  */
350e705c121SKalle Valo static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
351e705c121SKalle Valo {
352e705c121SKalle Valo 	int ret;
353e705c121SKalle Valo 	u32 apmg_gp1_reg;
354e705c121SKalle Valo 	u32 apmg_xtal_cfg_reg;
355e705c121SKalle Valo 	u32 dl_cfg_reg;
356e705c121SKalle Valo 
357e705c121SKalle Valo 	/* Force XTAL ON */
358e705c121SKalle Valo 	__iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
359e705c121SKalle Valo 				 CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
360e705c121SKalle Valo 
361e705c121SKalle Valo 	/* Reset entire device - do controller reset (results in SHRD_HW_RST) */
362e705c121SKalle Valo 	iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
363b7a08b28SJohannes Berg 	usleep_range(1000, 2000);
364e705c121SKalle Valo 
365e705c121SKalle Valo 	/*
366e705c121SKalle Valo 	 * Set "initialization complete" bit to move adapter from
367e705c121SKalle Valo 	 * D0U* --> D0A* (powered-up active) state.
368e705c121SKalle Valo 	 */
369e705c121SKalle Valo 	iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
370e705c121SKalle Valo 
371e705c121SKalle Valo 	/*
372e705c121SKalle Valo 	 * Wait for clock stabilization; once stabilized, access to
373e705c121SKalle Valo 	 * device-internal resources is possible.
374e705c121SKalle Valo 	 */
375e705c121SKalle Valo 	ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
376e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
377e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
378e705c121SKalle Valo 			   25000);
379e705c121SKalle Valo 	if (WARN_ON(ret < 0)) {
380e705c121SKalle Valo 		IWL_ERR(trans, "Access time out - failed to enable LP XTAL\n");
381e705c121SKalle Valo 		/* Release XTAL ON request */
382e705c121SKalle Valo 		__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
383e705c121SKalle Valo 					   CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
384e705c121SKalle Valo 		return;
385e705c121SKalle Valo 	}
386e705c121SKalle Valo 
387e705c121SKalle Valo 	/*
388e705c121SKalle Valo 	 * Clear "disable persistence" to avoid LP XTAL resetting when
389e705c121SKalle Valo 	 * SHRD_HW_RST is applied in S3.
390e705c121SKalle Valo 	 */
391e705c121SKalle Valo 	iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
392e705c121SKalle Valo 				    APMG_PCIDEV_STT_VAL_PERSIST_DIS);
393e705c121SKalle Valo 
394e705c121SKalle Valo 	/*
395e705c121SKalle Valo 	 * Force APMG XTAL to be active to prevent its disabling by HW
396e705c121SKalle Valo 	 * caused by APMG idle state.
397e705c121SKalle Valo 	 */
398e705c121SKalle Valo 	apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
399e705c121SKalle Valo 						    SHR_APMG_XTAL_CFG_REG);
400e705c121SKalle Valo 	iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
401e705c121SKalle Valo 				 apmg_xtal_cfg_reg |
402e705c121SKalle Valo 				 SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
403e705c121SKalle Valo 
404e705c121SKalle Valo 	/*
405e705c121SKalle Valo 	 * Reset entire device again - do controller reset (results in
406e705c121SKalle Valo 	 * SHRD_HW_RST). Turn MAC off before proceeding.
407e705c121SKalle Valo 	 */
408e705c121SKalle Valo 	iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
409b7a08b28SJohannes Berg 	usleep_range(1000, 2000);
410e705c121SKalle Valo 
411e705c121SKalle Valo 	/* Enable LP XTAL by indirect access through CSR */
412e705c121SKalle Valo 	apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
413e705c121SKalle Valo 	iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
414e705c121SKalle Valo 				 SHR_APMG_GP1_WF_XTAL_LP_EN |
415e705c121SKalle Valo 				 SHR_APMG_GP1_CHICKEN_BIT_SELECT);
416e705c121SKalle Valo 
417e705c121SKalle Valo 	/* Clear delay line clock power up */
418e705c121SKalle Valo 	dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
419e705c121SKalle Valo 	iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
420e705c121SKalle Valo 				 ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
421e705c121SKalle Valo 
422e705c121SKalle Valo 	/*
423e705c121SKalle Valo 	 * Enable persistence mode to avoid LP XTAL resetting when
424e705c121SKalle Valo 	 * SHRD_HW_RST is applied in S3.
425e705c121SKalle Valo 	 */
426e705c121SKalle Valo 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
427e705c121SKalle Valo 		    CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
428e705c121SKalle Valo 
429e705c121SKalle Valo 	/*
430e705c121SKalle Valo 	 * Clear "initialization complete" bit to move adapter from
431e705c121SKalle Valo 	 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
432e705c121SKalle Valo 	 */
433e705c121SKalle Valo 	iwl_clear_bit(trans, CSR_GP_CNTRL,
434e705c121SKalle Valo 		      CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
435e705c121SKalle Valo 
436e705c121SKalle Valo 	/* Activates XTAL resources monitor */
437e705c121SKalle Valo 	__iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG,
438e705c121SKalle Valo 				 CSR_MONITOR_XTAL_RESOURCES);
439e705c121SKalle Valo 
440e705c121SKalle Valo 	/* Release XTAL ON request */
441e705c121SKalle Valo 	__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
442e705c121SKalle Valo 				   CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
443e705c121SKalle Valo 	udelay(10);
444e705c121SKalle Valo 
445e705c121SKalle Valo 	/* Release APMG XTAL */
446e705c121SKalle Valo 	iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
447e705c121SKalle Valo 				 apmg_xtal_cfg_reg &
448e705c121SKalle Valo 				 ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
449e705c121SKalle Valo }
450e705c121SKalle Valo 
45177c09bc8SSara Sharon int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
452e705c121SKalle Valo {
453e705c121SKalle Valo 	int ret = 0;
454e705c121SKalle Valo 
455e705c121SKalle Valo 	/* stop device's busmaster DMA activity */
456e705c121SKalle Valo 	iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
457e705c121SKalle Valo 
458e705c121SKalle Valo 	ret = iwl_poll_bit(trans, CSR_RESET,
459e705c121SKalle Valo 			   CSR_RESET_REG_FLAG_MASTER_DISABLED,
460e705c121SKalle Valo 			   CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
461e705c121SKalle Valo 	if (ret < 0)
462e705c121SKalle Valo 		IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
463e705c121SKalle Valo 
464e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "stop master\n");
465e705c121SKalle Valo 
466e705c121SKalle Valo 	return ret;
467e705c121SKalle Valo }
468e705c121SKalle Valo 
469e705c121SKalle Valo static void iwl_pcie_apm_stop(struct iwl_trans *trans, bool op_mode_leave)
470e705c121SKalle Valo {
471e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
472e705c121SKalle Valo 
473e705c121SKalle Valo 	if (op_mode_leave) {
474e705c121SKalle Valo 		if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
475e705c121SKalle Valo 			iwl_pcie_apm_init(trans);
476e705c121SKalle Valo 
477e705c121SKalle Valo 		/* inform ME that we are leaving */
478e705c121SKalle Valo 		if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000)
479e705c121SKalle Valo 			iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
480e705c121SKalle Valo 					  APMG_PCIDEV_STT_VAL_WAKE_ME);
4816e584873SSara Sharon 		else if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) {
482e705c121SKalle Valo 			iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
483e705c121SKalle Valo 				    CSR_RESET_LINK_PWR_MGMT_DISABLED);
484e705c121SKalle Valo 			iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
485e705c121SKalle Valo 				    CSR_HW_IF_CONFIG_REG_PREPARE |
486e705c121SKalle Valo 				    CSR_HW_IF_CONFIG_REG_ENABLE_PME);
487e705c121SKalle Valo 			mdelay(1);
488e705c121SKalle Valo 			iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
489e705c121SKalle Valo 				      CSR_RESET_LINK_PWR_MGMT_DISABLED);
490e705c121SKalle Valo 		}
491e705c121SKalle Valo 		mdelay(5);
492e705c121SKalle Valo 	}
493e705c121SKalle Valo 
494e705c121SKalle Valo 	clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
495e705c121SKalle Valo 
496e705c121SKalle Valo 	/* Stop device's DMA activity */
497e705c121SKalle Valo 	iwl_pcie_apm_stop_master(trans);
498e705c121SKalle Valo 
499e705c121SKalle Valo 	if (trans->cfg->lp_xtal_workaround) {
500e705c121SKalle Valo 		iwl_pcie_apm_lp_xtal_enable(trans);
501e705c121SKalle Valo 		return;
502e705c121SKalle Valo 	}
503e705c121SKalle Valo 
504e705c121SKalle Valo 	/* Reset the entire device */
505e705c121SKalle Valo 	iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
506b7a08b28SJohannes Berg 	usleep_range(1000, 2000);
507e705c121SKalle Valo 
508e705c121SKalle Valo 	/*
509e705c121SKalle Valo 	 * Clear "initialization complete" bit to move adapter from
510e705c121SKalle Valo 	 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
511e705c121SKalle Valo 	 */
512e705c121SKalle Valo 	iwl_clear_bit(trans, CSR_GP_CNTRL,
513e705c121SKalle Valo 		      CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
514e705c121SKalle Valo }
515e705c121SKalle Valo 
516e705c121SKalle Valo static int iwl_pcie_nic_init(struct iwl_trans *trans)
517e705c121SKalle Valo {
518e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
519e705c121SKalle Valo 
520e705c121SKalle Valo 	/* nic_init */
521e705c121SKalle Valo 	spin_lock(&trans_pcie->irq_lock);
522e705c121SKalle Valo 	iwl_pcie_apm_init(trans);
523e705c121SKalle Valo 
524e705c121SKalle Valo 	spin_unlock(&trans_pcie->irq_lock);
525e705c121SKalle Valo 
526e705c121SKalle Valo 	iwl_pcie_set_pwr(trans, false);
527e705c121SKalle Valo 
528e705c121SKalle Valo 	iwl_op_mode_nic_config(trans->op_mode);
529e705c121SKalle Valo 
530e705c121SKalle Valo 	/* Allocate the RX queue, or reset if it is already allocated */
531e705c121SKalle Valo 	iwl_pcie_rx_init(trans);
532e705c121SKalle Valo 
533e705c121SKalle Valo 	/* Allocate or reset and init all Tx and Command queues */
534e705c121SKalle Valo 	if (iwl_pcie_tx_init(trans))
535e705c121SKalle Valo 		return -ENOMEM;
536e705c121SKalle Valo 
537e705c121SKalle Valo 	if (trans->cfg->base_params->shadow_reg_enable) {
538e705c121SKalle Valo 		/* enable shadow regs in HW */
539e705c121SKalle Valo 		iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
540e705c121SKalle Valo 		IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
541e705c121SKalle Valo 	}
542e705c121SKalle Valo 
543e705c121SKalle Valo 	return 0;
544e705c121SKalle Valo }
545e705c121SKalle Valo 
546e705c121SKalle Valo #define HW_READY_TIMEOUT (50)
547e705c121SKalle Valo 
548e705c121SKalle Valo /* Note: returns poll_bit return value, which is >= 0 if success */
549e705c121SKalle Valo static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
550e705c121SKalle Valo {
551e705c121SKalle Valo 	int ret;
552e705c121SKalle Valo 
553e705c121SKalle Valo 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
554e705c121SKalle Valo 		    CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
555e705c121SKalle Valo 
556e705c121SKalle Valo 	/* See if we got it */
557e705c121SKalle Valo 	ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
558e705c121SKalle Valo 			   CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
559e705c121SKalle Valo 			   CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
560e705c121SKalle Valo 			   HW_READY_TIMEOUT);
561e705c121SKalle Valo 
562e705c121SKalle Valo 	if (ret >= 0)
563e705c121SKalle Valo 		iwl_set_bit(trans, CSR_MBOX_SET_REG, CSR_MBOX_SET_REG_OS_ALIVE);
564e705c121SKalle Valo 
565e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
566e705c121SKalle Valo 	return ret;
567e705c121SKalle Valo }
568e705c121SKalle Valo 
569e705c121SKalle Valo /* Note: returns standard 0/-ERROR code */
570eda50cdeSSara Sharon int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
571e705c121SKalle Valo {
572e705c121SKalle Valo 	int ret;
573e705c121SKalle Valo 	int t = 0;
574e705c121SKalle Valo 	int iter;
575e705c121SKalle Valo 
576e705c121SKalle Valo 	IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
577e705c121SKalle Valo 
578e705c121SKalle Valo 	ret = iwl_pcie_set_hw_ready(trans);
579e705c121SKalle Valo 	/* If the card is ready, exit 0 */
580e705c121SKalle Valo 	if (ret >= 0)
581e705c121SKalle Valo 		return 0;
582e705c121SKalle Valo 
583e705c121SKalle Valo 	iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
584e705c121SKalle Valo 		    CSR_RESET_LINK_PWR_MGMT_DISABLED);
585192185d6SJohannes Berg 	usleep_range(1000, 2000);
586e705c121SKalle Valo 
587e705c121SKalle Valo 	for (iter = 0; iter < 10; iter++) {
588e705c121SKalle Valo 		/* If HW is not ready, prepare the conditions to check again */
589e705c121SKalle Valo 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
590e705c121SKalle Valo 			    CSR_HW_IF_CONFIG_REG_PREPARE);
591e705c121SKalle Valo 
592e705c121SKalle Valo 		do {
593e705c121SKalle Valo 			ret = iwl_pcie_set_hw_ready(trans);
594e705c121SKalle Valo 			if (ret >= 0)
595e705c121SKalle Valo 				return 0;
596e705c121SKalle Valo 
597e705c121SKalle Valo 			usleep_range(200, 1000);
598e705c121SKalle Valo 			t += 200;
599e705c121SKalle Valo 		} while (t < 150000);
600e705c121SKalle Valo 		msleep(25);
601e705c121SKalle Valo 	}
602e705c121SKalle Valo 
603e705c121SKalle Valo 	IWL_ERR(trans, "Couldn't prepare the card\n");
604e705c121SKalle Valo 
605e705c121SKalle Valo 	return ret;
606e705c121SKalle Valo }
607e705c121SKalle Valo 
608e705c121SKalle Valo /*
609e705c121SKalle Valo  * ucode
610e705c121SKalle Valo  */
611564cdce7SSara Sharon static void iwl_pcie_load_firmware_chunk_fh(struct iwl_trans *trans,
612564cdce7SSara Sharon 					    u32 dst_addr, dma_addr_t phy_addr,
613564cdce7SSara Sharon 					    u32 byte_cnt)
614e705c121SKalle Valo {
615bac842daSEmmanuel Grumbach 	iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
616e705c121SKalle Valo 		    FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
617e705c121SKalle Valo 
618bac842daSEmmanuel Grumbach 	iwl_write32(trans, FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
619e705c121SKalle Valo 		    dst_addr);
620e705c121SKalle Valo 
621bac842daSEmmanuel Grumbach 	iwl_write32(trans, FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
622e705c121SKalle Valo 		    phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
623e705c121SKalle Valo 
624bac842daSEmmanuel Grumbach 	iwl_write32(trans, FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
625e705c121SKalle Valo 		    (iwl_get_dma_hi_addr(phy_addr)
626e705c121SKalle Valo 			<< FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
627e705c121SKalle Valo 
628bac842daSEmmanuel Grumbach 	iwl_write32(trans, FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
629bac842daSEmmanuel Grumbach 		    BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM) |
630bac842daSEmmanuel Grumbach 		    BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX) |
631e705c121SKalle Valo 		    FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
632e705c121SKalle Valo 
633bac842daSEmmanuel Grumbach 	iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
634e705c121SKalle Valo 		    FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
635e705c121SKalle Valo 		    FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
636e705c121SKalle Valo 		    FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
637564cdce7SSara Sharon }
638e705c121SKalle Valo 
639564cdce7SSara Sharon static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans,
640564cdce7SSara Sharon 					u32 dst_addr, dma_addr_t phy_addr,
641564cdce7SSara Sharon 					u32 byte_cnt)
642564cdce7SSara Sharon {
643564cdce7SSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
644564cdce7SSara Sharon 	unsigned long flags;
645564cdce7SSara Sharon 	int ret;
646564cdce7SSara Sharon 
647564cdce7SSara Sharon 	trans_pcie->ucode_write_complete = false;
648564cdce7SSara Sharon 
649564cdce7SSara Sharon 	if (!iwl_trans_grab_nic_access(trans, &flags))
650564cdce7SSara Sharon 		return -EIO;
651564cdce7SSara Sharon 
652564cdce7SSara Sharon 	iwl_pcie_load_firmware_chunk_fh(trans, dst_addr, phy_addr,
653564cdce7SSara Sharon 					byte_cnt);
654bac842daSEmmanuel Grumbach 	iwl_trans_release_nic_access(trans, &flags);
655bac842daSEmmanuel Grumbach 
656e705c121SKalle Valo 	ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
657e705c121SKalle Valo 				 trans_pcie->ucode_write_complete, 5 * HZ);
658e705c121SKalle Valo 	if (!ret) {
659e705c121SKalle Valo 		IWL_ERR(trans, "Failed to load firmware chunk!\n");
660e705c121SKalle Valo 		return -ETIMEDOUT;
661e705c121SKalle Valo 	}
662e705c121SKalle Valo 
663e705c121SKalle Valo 	return 0;
664e705c121SKalle Valo }
665e705c121SKalle Valo 
666e705c121SKalle Valo static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
667e705c121SKalle Valo 			    const struct fw_desc *section)
668e705c121SKalle Valo {
669e705c121SKalle Valo 	u8 *v_addr;
670e705c121SKalle Valo 	dma_addr_t p_addr;
671e705c121SKalle Valo 	u32 offset, chunk_sz = min_t(u32, FH_MEM_TB_MAX_LENGTH, section->len);
672e705c121SKalle Valo 	int ret = 0;
673e705c121SKalle Valo 
674e705c121SKalle Valo 	IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
675e705c121SKalle Valo 		     section_num);
676e705c121SKalle Valo 
677e705c121SKalle Valo 	v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
678e705c121SKalle Valo 				    GFP_KERNEL | __GFP_NOWARN);
679e705c121SKalle Valo 	if (!v_addr) {
680e705c121SKalle Valo 		IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
681e705c121SKalle Valo 		chunk_sz = PAGE_SIZE;
682e705c121SKalle Valo 		v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
683e705c121SKalle Valo 					    &p_addr, GFP_KERNEL);
684e705c121SKalle Valo 		if (!v_addr)
685e705c121SKalle Valo 			return -ENOMEM;
686e705c121SKalle Valo 	}
687e705c121SKalle Valo 
688e705c121SKalle Valo 	for (offset = 0; offset < section->len; offset += chunk_sz) {
689e705c121SKalle Valo 		u32 copy_size, dst_addr;
690e705c121SKalle Valo 		bool extended_addr = false;
691e705c121SKalle Valo 
692e705c121SKalle Valo 		copy_size = min_t(u32, chunk_sz, section->len - offset);
693e705c121SKalle Valo 		dst_addr = section->offset + offset;
694e705c121SKalle Valo 
695e705c121SKalle Valo 		if (dst_addr >= IWL_FW_MEM_EXTENDED_START &&
696e705c121SKalle Valo 		    dst_addr <= IWL_FW_MEM_EXTENDED_END)
697e705c121SKalle Valo 			extended_addr = true;
698e705c121SKalle Valo 
699e705c121SKalle Valo 		if (extended_addr)
700e705c121SKalle Valo 			iwl_set_bits_prph(trans, LMPM_CHICK,
701e705c121SKalle Valo 					  LMPM_CHICK_EXTENDED_ADDR_SPACE);
702e705c121SKalle Valo 
703e705c121SKalle Valo 		memcpy(v_addr, (u8 *)section->data + offset, copy_size);
704e705c121SKalle Valo 		ret = iwl_pcie_load_firmware_chunk(trans, dst_addr, p_addr,
705e705c121SKalle Valo 						   copy_size);
706e705c121SKalle Valo 
707e705c121SKalle Valo 		if (extended_addr)
708e705c121SKalle Valo 			iwl_clear_bits_prph(trans, LMPM_CHICK,
709e705c121SKalle Valo 					    LMPM_CHICK_EXTENDED_ADDR_SPACE);
710e705c121SKalle Valo 
711e705c121SKalle Valo 		if (ret) {
712e705c121SKalle Valo 			IWL_ERR(trans,
713e705c121SKalle Valo 				"Could not load the [%d] uCode section\n",
714e705c121SKalle Valo 				section_num);
715e705c121SKalle Valo 			break;
716e705c121SKalle Valo 		}
717e705c121SKalle Valo 	}
718e705c121SKalle Valo 
719e705c121SKalle Valo 	dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
720e705c121SKalle Valo 	return ret;
721e705c121SKalle Valo }
722e705c121SKalle Valo 
723e705c121SKalle Valo static int iwl_pcie_load_cpu_sections_8000(struct iwl_trans *trans,
724e705c121SKalle Valo 					   const struct fw_img *image,
725e705c121SKalle Valo 					   int cpu,
726e705c121SKalle Valo 					   int *first_ucode_section)
727e705c121SKalle Valo {
728e705c121SKalle Valo 	int shift_param;
729e705c121SKalle Valo 	int i, ret = 0, sec_num = 0x1;
730e705c121SKalle Valo 	u32 val, last_read_idx = 0;
731e705c121SKalle Valo 
732e705c121SKalle Valo 	if (cpu == 1) {
733e705c121SKalle Valo 		shift_param = 0;
734e705c121SKalle Valo 		*first_ucode_section = 0;
735e705c121SKalle Valo 	} else {
736e705c121SKalle Valo 		shift_param = 16;
737e705c121SKalle Valo 		(*first_ucode_section)++;
738e705c121SKalle Valo 	}
739e705c121SKalle Valo 
740eef187a7SSara Sharon 	for (i = *first_ucode_section; i < image->num_sec; i++) {
741e705c121SKalle Valo 		last_read_idx = i;
742e705c121SKalle Valo 
743e705c121SKalle Valo 		/*
744e705c121SKalle Valo 		 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
745e705c121SKalle Valo 		 * CPU1 to CPU2.
746e705c121SKalle Valo 		 * PAGING_SEPARATOR_SECTION delimiter - separate between
747e705c121SKalle Valo 		 * CPU2 non paged to CPU2 paging sec.
748e705c121SKalle Valo 		 */
749e705c121SKalle Valo 		if (!image->sec[i].data ||
750e705c121SKalle Valo 		    image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
751e705c121SKalle Valo 		    image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
752e705c121SKalle Valo 			IWL_DEBUG_FW(trans,
753e705c121SKalle Valo 				     "Break since Data not valid or Empty section, sec = %d\n",
754e705c121SKalle Valo 				     i);
755e705c121SKalle Valo 			break;
756e705c121SKalle Valo 		}
757e705c121SKalle Valo 
758e705c121SKalle Valo 		ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
759e705c121SKalle Valo 		if (ret)
760e705c121SKalle Valo 			return ret;
761e705c121SKalle Valo 
762d6a2c5c7SSara Sharon 		/* Notify ucode of loaded section number and status */
763e705c121SKalle Valo 		val = iwl_read_direct32(trans, FH_UCODE_LOAD_STATUS);
764e705c121SKalle Valo 		val = val | (sec_num << shift_param);
765e705c121SKalle Valo 		iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, val);
766eda50cdeSSara Sharon 
767e705c121SKalle Valo 		sec_num = (sec_num << 1) | 0x1;
768e705c121SKalle Valo 	}
769e705c121SKalle Valo 
770e705c121SKalle Valo 	*first_ucode_section = last_read_idx;
771e705c121SKalle Valo 
7722aabdbdcSEmmanuel Grumbach 	iwl_enable_interrupts(trans);
7732aabdbdcSEmmanuel Grumbach 
774d6a2c5c7SSara Sharon 	if (trans->cfg->use_tfh) {
775e705c121SKalle Valo 		if (cpu == 1)
776d6a2c5c7SSara Sharon 			iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS,
777d6a2c5c7SSara Sharon 				       0xFFFF);
778e705c121SKalle Valo 		else
779d6a2c5c7SSara Sharon 			iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS,
780d6a2c5c7SSara Sharon 				       0xFFFFFFFF);
781d6a2c5c7SSara Sharon 	} else {
782d6a2c5c7SSara Sharon 		if (cpu == 1)
783d6a2c5c7SSara Sharon 			iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS,
784d6a2c5c7SSara Sharon 					   0xFFFF);
785d6a2c5c7SSara Sharon 		else
786d6a2c5c7SSara Sharon 			iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS,
787d6a2c5c7SSara Sharon 					   0xFFFFFFFF);
788d6a2c5c7SSara Sharon 	}
789e705c121SKalle Valo 
790e705c121SKalle Valo 	return 0;
791e705c121SKalle Valo }
792e705c121SKalle Valo 
793e705c121SKalle Valo static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
794e705c121SKalle Valo 				      const struct fw_img *image,
795e705c121SKalle Valo 				      int cpu,
796e705c121SKalle Valo 				      int *first_ucode_section)
797e705c121SKalle Valo {
798e705c121SKalle Valo 	int i, ret = 0;
799e705c121SKalle Valo 	u32 last_read_idx = 0;
800e705c121SKalle Valo 
8013ce4a038SKirtika Ruchandani 	if (cpu == 1)
802e705c121SKalle Valo 		*first_ucode_section = 0;
8033ce4a038SKirtika Ruchandani 	else
804e705c121SKalle Valo 		(*first_ucode_section)++;
805e705c121SKalle Valo 
806eef187a7SSara Sharon 	for (i = *first_ucode_section; i < image->num_sec; i++) {
807e705c121SKalle Valo 		last_read_idx = i;
808e705c121SKalle Valo 
809e705c121SKalle Valo 		/*
810e705c121SKalle Valo 		 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
811e705c121SKalle Valo 		 * CPU1 to CPU2.
812e705c121SKalle Valo 		 * PAGING_SEPARATOR_SECTION delimiter - separate between
813e705c121SKalle Valo 		 * CPU2 non paged to CPU2 paging sec.
814e705c121SKalle Valo 		 */
815e705c121SKalle Valo 		if (!image->sec[i].data ||
816e705c121SKalle Valo 		    image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
817e705c121SKalle Valo 		    image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
818e705c121SKalle Valo 			IWL_DEBUG_FW(trans,
819e705c121SKalle Valo 				     "Break since Data not valid or Empty section, sec = %d\n",
820e705c121SKalle Valo 				     i);
821e705c121SKalle Valo 			break;
822e705c121SKalle Valo 		}
823e705c121SKalle Valo 
824e705c121SKalle Valo 		ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
825e705c121SKalle Valo 		if (ret)
826e705c121SKalle Valo 			return ret;
827e705c121SKalle Valo 	}
828e705c121SKalle Valo 
829e705c121SKalle Valo 	*first_ucode_section = last_read_idx;
830e705c121SKalle Valo 
831e705c121SKalle Valo 	return 0;
832e705c121SKalle Valo }
833e705c121SKalle Valo 
834c9be849dSLiad Kaufman void iwl_pcie_apply_destination(struct iwl_trans *trans)
835e705c121SKalle Valo {
836e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
837e705c121SKalle Valo 	const struct iwl_fw_dbg_dest_tlv *dest = trans->dbg_dest_tlv;
838e705c121SKalle Valo 	int i;
839e705c121SKalle Valo 
840e705c121SKalle Valo 	if (dest->version)
841e705c121SKalle Valo 		IWL_ERR(trans,
842e705c121SKalle Valo 			"DBG DEST version is %d - expect issues\n",
843e705c121SKalle Valo 			dest->version);
844e705c121SKalle Valo 
845e705c121SKalle Valo 	IWL_INFO(trans, "Applying debug destination %s\n",
846e705c121SKalle Valo 		 get_fw_dbg_mode_string(dest->monitor_mode));
847e705c121SKalle Valo 
848e705c121SKalle Valo 	if (dest->monitor_mode == EXTERNAL_MODE)
849e705c121SKalle Valo 		iwl_pcie_alloc_fw_monitor(trans, dest->size_power);
850e705c121SKalle Valo 	else
851e705c121SKalle Valo 		IWL_WARN(trans, "PCI should have external buffer debug\n");
852e705c121SKalle Valo 
853e705c121SKalle Valo 	for (i = 0; i < trans->dbg_dest_reg_num; i++) {
854e705c121SKalle Valo 		u32 addr = le32_to_cpu(dest->reg_ops[i].addr);
855e705c121SKalle Valo 		u32 val = le32_to_cpu(dest->reg_ops[i].val);
856e705c121SKalle Valo 
857e705c121SKalle Valo 		switch (dest->reg_ops[i].op) {
858e705c121SKalle Valo 		case CSR_ASSIGN:
859e705c121SKalle Valo 			iwl_write32(trans, addr, val);
860e705c121SKalle Valo 			break;
861e705c121SKalle Valo 		case CSR_SETBIT:
862e705c121SKalle Valo 			iwl_set_bit(trans, addr, BIT(val));
863e705c121SKalle Valo 			break;
864e705c121SKalle Valo 		case CSR_CLEARBIT:
865e705c121SKalle Valo 			iwl_clear_bit(trans, addr, BIT(val));
866e705c121SKalle Valo 			break;
867e705c121SKalle Valo 		case PRPH_ASSIGN:
868e705c121SKalle Valo 			iwl_write_prph(trans, addr, val);
869e705c121SKalle Valo 			break;
870e705c121SKalle Valo 		case PRPH_SETBIT:
871e705c121SKalle Valo 			iwl_set_bits_prph(trans, addr, BIT(val));
872e705c121SKalle Valo 			break;
873e705c121SKalle Valo 		case PRPH_CLEARBIT:
874e705c121SKalle Valo 			iwl_clear_bits_prph(trans, addr, BIT(val));
875e705c121SKalle Valo 			break;
876e705c121SKalle Valo 		case PRPH_BLOCKBIT:
877e705c121SKalle Valo 			if (iwl_read_prph(trans, addr) & BIT(val)) {
878e705c121SKalle Valo 				IWL_ERR(trans,
879e705c121SKalle Valo 					"BIT(%u) in address 0x%x is 1, stopping FW configuration\n",
880e705c121SKalle Valo 					val, addr);
881e705c121SKalle Valo 				goto monitor;
882e705c121SKalle Valo 			}
883e705c121SKalle Valo 			break;
884e705c121SKalle Valo 		default:
885e705c121SKalle Valo 			IWL_ERR(trans, "FW debug - unknown OP %d\n",
886e705c121SKalle Valo 				dest->reg_ops[i].op);
887e705c121SKalle Valo 			break;
888e705c121SKalle Valo 		}
889e705c121SKalle Valo 	}
890e705c121SKalle Valo 
891e705c121SKalle Valo monitor:
892e705c121SKalle Valo 	if (dest->monitor_mode == EXTERNAL_MODE && trans_pcie->fw_mon_size) {
893e705c121SKalle Valo 		iwl_write_prph(trans, le32_to_cpu(dest->base_reg),
894e705c121SKalle Valo 			       trans_pcie->fw_mon_phys >> dest->base_shift);
8956e584873SSara Sharon 		if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
896e705c121SKalle Valo 			iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
897e705c121SKalle Valo 				       (trans_pcie->fw_mon_phys +
89862d7476dSEmmanuel Grumbach 					trans_pcie->fw_mon_size - 256) >>
89962d7476dSEmmanuel Grumbach 						dest->end_shift);
90062d7476dSEmmanuel Grumbach 		else
90162d7476dSEmmanuel Grumbach 			iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
90262d7476dSEmmanuel Grumbach 				       (trans_pcie->fw_mon_phys +
90362d7476dSEmmanuel Grumbach 					trans_pcie->fw_mon_size) >>
90462d7476dSEmmanuel Grumbach 						dest->end_shift);
905e705c121SKalle Valo 	}
906e705c121SKalle Valo }
907e705c121SKalle Valo 
908e705c121SKalle Valo static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
909e705c121SKalle Valo 				const struct fw_img *image)
910e705c121SKalle Valo {
911e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
912e705c121SKalle Valo 	int ret = 0;
913e705c121SKalle Valo 	int first_ucode_section;
914e705c121SKalle Valo 
915e705c121SKalle Valo 	IWL_DEBUG_FW(trans, "working with %s CPU\n",
916e705c121SKalle Valo 		     image->is_dual_cpus ? "Dual" : "Single");
917e705c121SKalle Valo 
918e705c121SKalle Valo 	/* load to FW the binary non secured sections of CPU1 */
919e705c121SKalle Valo 	ret = iwl_pcie_load_cpu_sections(trans, image, 1, &first_ucode_section);
920e705c121SKalle Valo 	if (ret)
921e705c121SKalle Valo 		return ret;
922e705c121SKalle Valo 
923e705c121SKalle Valo 	if (image->is_dual_cpus) {
924e705c121SKalle Valo 		/* set CPU2 header address */
925e705c121SKalle Valo 		iwl_write_prph(trans,
926e705c121SKalle Valo 			       LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
927e705c121SKalle Valo 			       LMPM_SECURE_CPU2_HDR_MEM_SPACE);
928e705c121SKalle Valo 
929e705c121SKalle Valo 		/* load to FW the binary sections of CPU2 */
930e705c121SKalle Valo 		ret = iwl_pcie_load_cpu_sections(trans, image, 2,
931e705c121SKalle Valo 						 &first_ucode_section);
932e705c121SKalle Valo 		if (ret)
933e705c121SKalle Valo 			return ret;
934e705c121SKalle Valo 	}
935e705c121SKalle Valo 
936e705c121SKalle Valo 	/* supported for 7000 only for the moment */
937e705c121SKalle Valo 	if (iwlwifi_mod_params.fw_monitor &&
938e705c121SKalle Valo 	    trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
939e705c121SKalle Valo 		iwl_pcie_alloc_fw_monitor(trans, 0);
940e705c121SKalle Valo 
941e705c121SKalle Valo 		if (trans_pcie->fw_mon_size) {
942e705c121SKalle Valo 			iwl_write_prph(trans, MON_BUFF_BASE_ADDR,
943e705c121SKalle Valo 				       trans_pcie->fw_mon_phys >> 4);
944e705c121SKalle Valo 			iwl_write_prph(trans, MON_BUFF_END_ADDR,
945e705c121SKalle Valo 				       (trans_pcie->fw_mon_phys +
946e705c121SKalle Valo 					trans_pcie->fw_mon_size) >> 4);
947e705c121SKalle Valo 		}
948e705c121SKalle Valo 	} else if (trans->dbg_dest_tlv) {
949e705c121SKalle Valo 		iwl_pcie_apply_destination(trans);
950e705c121SKalle Valo 	}
951e705c121SKalle Valo 
9522aabdbdcSEmmanuel Grumbach 	iwl_enable_interrupts(trans);
9532aabdbdcSEmmanuel Grumbach 
954e705c121SKalle Valo 	/* release CPU reset */
955e705c121SKalle Valo 	iwl_write32(trans, CSR_RESET, 0);
956e705c121SKalle Valo 
957e705c121SKalle Valo 	return 0;
958e705c121SKalle Valo }
959e705c121SKalle Valo 
960e705c121SKalle Valo static int iwl_pcie_load_given_ucode_8000(struct iwl_trans *trans,
961e705c121SKalle Valo 					  const struct fw_img *image)
962e705c121SKalle Valo {
963e705c121SKalle Valo 	int ret = 0;
964e705c121SKalle Valo 	int first_ucode_section;
965e705c121SKalle Valo 
966e705c121SKalle Valo 	IWL_DEBUG_FW(trans, "working with %s CPU\n",
967e705c121SKalle Valo 		     image->is_dual_cpus ? "Dual" : "Single");
968e705c121SKalle Valo 
969e705c121SKalle Valo 	if (trans->dbg_dest_tlv)
970e705c121SKalle Valo 		iwl_pcie_apply_destination(trans);
971e705c121SKalle Valo 
97282ea7966SSara Sharon 	IWL_DEBUG_POWER(trans, "Original WFPM value = 0x%08X\n",
97382ea7966SSara Sharon 			iwl_read_prph(trans, WFPM_GP2));
97482ea7966SSara Sharon 
97582ea7966SSara Sharon 	/*
97682ea7966SSara Sharon 	 * Set default value. On resume reading the values that were
97782ea7966SSara Sharon 	 * zeored can provide debug data on the resume flow.
97882ea7966SSara Sharon 	 * This is for debugging only and has no functional impact.
97982ea7966SSara Sharon 	 */
98082ea7966SSara Sharon 	iwl_write_prph(trans, WFPM_GP2, 0x01010101);
98182ea7966SSara Sharon 
982e705c121SKalle Valo 	/* configure the ucode to be ready to get the secured image */
983e705c121SKalle Valo 	/* release CPU reset */
984e705c121SKalle Valo 	iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
985e705c121SKalle Valo 
986e705c121SKalle Valo 	/* load to FW the binary Secured sections of CPU1 */
987e705c121SKalle Valo 	ret = iwl_pcie_load_cpu_sections_8000(trans, image, 1,
988e705c121SKalle Valo 					      &first_ucode_section);
989e705c121SKalle Valo 	if (ret)
990e705c121SKalle Valo 		return ret;
991e705c121SKalle Valo 
992e705c121SKalle Valo 	/* load to FW the binary sections of CPU2 */
993e705c121SKalle Valo 	return iwl_pcie_load_cpu_sections_8000(trans, image, 2,
994e705c121SKalle Valo 					       &first_ucode_section);
995e705c121SKalle Valo }
996e705c121SKalle Valo 
997eda50cdeSSara Sharon bool iwl_trans_check_hw_rf_kill(struct iwl_trans *trans)
998727c02dfSSara Sharon {
999326477e4SJohannes Berg 	struct iwl_trans_pcie *trans_pcie =  IWL_TRANS_GET_PCIE_TRANS(trans);
1000727c02dfSSara Sharon 	bool hw_rfkill = iwl_is_rfkill_set(trans);
1001326477e4SJohannes Berg 	bool prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1002326477e4SJohannes Berg 	bool report;
1003727c02dfSSara Sharon 
1004326477e4SJohannes Berg 	if (hw_rfkill) {
1005326477e4SJohannes Berg 		set_bit(STATUS_RFKILL_HW, &trans->status);
1006326477e4SJohannes Berg 		set_bit(STATUS_RFKILL_OPMODE, &trans->status);
1007326477e4SJohannes Berg 	} else {
1008326477e4SJohannes Berg 		clear_bit(STATUS_RFKILL_HW, &trans->status);
1009326477e4SJohannes Berg 		if (trans_pcie->opmode_down)
1010326477e4SJohannes Berg 			clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
1011326477e4SJohannes Berg 	}
1012727c02dfSSara Sharon 
1013326477e4SJohannes Berg 	report = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1014326477e4SJohannes Berg 
1015326477e4SJohannes Berg 	if (prev != report)
1016326477e4SJohannes Berg 		iwl_trans_pcie_rf_kill(trans, report);
1017727c02dfSSara Sharon 
1018727c02dfSSara Sharon 	return hw_rfkill;
1019727c02dfSSara Sharon }
1020727c02dfSSara Sharon 
10217ca00409SHaim Dreyfuss struct iwl_causes_list {
10227ca00409SHaim Dreyfuss 	u32 cause_num;
10237ca00409SHaim Dreyfuss 	u32 mask_reg;
10247ca00409SHaim Dreyfuss 	u8 addr;
10257ca00409SHaim Dreyfuss };
10267ca00409SHaim Dreyfuss 
10277ca00409SHaim Dreyfuss static struct iwl_causes_list causes_list[] = {
10287ca00409SHaim Dreyfuss 	{MSIX_FH_INT_CAUSES_D2S_CH0_NUM,	CSR_MSIX_FH_INT_MASK_AD, 0},
10297ca00409SHaim Dreyfuss 	{MSIX_FH_INT_CAUSES_D2S_CH1_NUM,	CSR_MSIX_FH_INT_MASK_AD, 0x1},
10307ca00409SHaim Dreyfuss 	{MSIX_FH_INT_CAUSES_S2D,		CSR_MSIX_FH_INT_MASK_AD, 0x3},
10317ca00409SHaim Dreyfuss 	{MSIX_FH_INT_CAUSES_FH_ERR,		CSR_MSIX_FH_INT_MASK_AD, 0x5},
10327ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_ALIVE,		CSR_MSIX_HW_INT_MASK_AD, 0x10},
10337ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_WAKEUP,		CSR_MSIX_HW_INT_MASK_AD, 0x11},
10347ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_CT_KILL,	CSR_MSIX_HW_INT_MASK_AD, 0x16},
10357ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_RF_KILL,	CSR_MSIX_HW_INT_MASK_AD, 0x17},
10367ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_PERIODIC,	CSR_MSIX_HW_INT_MASK_AD, 0x18},
10377ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_SW_ERR,		CSR_MSIX_HW_INT_MASK_AD, 0x29},
10387ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_SCD,		CSR_MSIX_HW_INT_MASK_AD, 0x2A},
10397ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_FH_TX,		CSR_MSIX_HW_INT_MASK_AD, 0x2B},
10407ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_HW_ERR,		CSR_MSIX_HW_INT_MASK_AD, 0x2D},
10417ca00409SHaim Dreyfuss 	{MSIX_HW_INT_CAUSES_REG_HAP,		CSR_MSIX_HW_INT_MASK_AD, 0x2E},
10427ca00409SHaim Dreyfuss };
10437ca00409SHaim Dreyfuss 
10447ca00409SHaim Dreyfuss static void iwl_pcie_map_non_rx_causes(struct iwl_trans *trans)
10457ca00409SHaim Dreyfuss {
10467ca00409SHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie =  IWL_TRANS_GET_PCIE_TRANS(trans);
10477ca00409SHaim Dreyfuss 	int val = trans_pcie->def_irq | MSIX_NON_AUTO_CLEAR_CAUSE;
10487ca00409SHaim Dreyfuss 	int i;
10497ca00409SHaim Dreyfuss 
10507ca00409SHaim Dreyfuss 	/*
10517ca00409SHaim Dreyfuss 	 * Access all non RX causes and map them to the default irq.
10527ca00409SHaim Dreyfuss 	 * In case we are missing at least one interrupt vector,
10537ca00409SHaim Dreyfuss 	 * the first interrupt vector will serve non-RX and FBQ causes.
10547ca00409SHaim Dreyfuss 	 */
10557ca00409SHaim Dreyfuss 	for (i = 0; i < ARRAY_SIZE(causes_list); i++) {
10567ca00409SHaim Dreyfuss 		iwl_write8(trans, CSR_MSIX_IVAR(causes_list[i].addr), val);
10577ca00409SHaim Dreyfuss 		iwl_clear_bit(trans, causes_list[i].mask_reg,
10587ca00409SHaim Dreyfuss 			      causes_list[i].cause_num);
10597ca00409SHaim Dreyfuss 	}
10607ca00409SHaim Dreyfuss }
10617ca00409SHaim Dreyfuss 
10627ca00409SHaim Dreyfuss static void iwl_pcie_map_rx_causes(struct iwl_trans *trans)
10637ca00409SHaim Dreyfuss {
10647ca00409SHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
10657ca00409SHaim Dreyfuss 	u32 offset =
10667ca00409SHaim Dreyfuss 		trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
10677ca00409SHaim Dreyfuss 	u32 val, idx;
10687ca00409SHaim Dreyfuss 
10697ca00409SHaim Dreyfuss 	/*
10707ca00409SHaim Dreyfuss 	 * The first RX queue - fallback queue, which is designated for
10717ca00409SHaim Dreyfuss 	 * management frame, command responses etc, is always mapped to the
10727ca00409SHaim Dreyfuss 	 * first interrupt vector. The other RX queues are mapped to
10737ca00409SHaim Dreyfuss 	 * the other (N - 2) interrupt vectors.
10747ca00409SHaim Dreyfuss 	 */
10757ca00409SHaim Dreyfuss 	val = BIT(MSIX_FH_INT_CAUSES_Q(0));
10767ca00409SHaim Dreyfuss 	for (idx = 1; idx < trans->num_rx_queues; idx++) {
10777ca00409SHaim Dreyfuss 		iwl_write8(trans, CSR_MSIX_RX_IVAR(idx),
10787ca00409SHaim Dreyfuss 			   MSIX_FH_INT_CAUSES_Q(idx - offset));
10797ca00409SHaim Dreyfuss 		val |= BIT(MSIX_FH_INT_CAUSES_Q(idx));
10807ca00409SHaim Dreyfuss 	}
10817ca00409SHaim Dreyfuss 	iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~val);
10827ca00409SHaim Dreyfuss 
10837ca00409SHaim Dreyfuss 	val = MSIX_FH_INT_CAUSES_Q(0);
10847ca00409SHaim Dreyfuss 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX)
10857ca00409SHaim Dreyfuss 		val |= MSIX_NON_AUTO_CLEAR_CAUSE;
10867ca00409SHaim Dreyfuss 	iwl_write8(trans, CSR_MSIX_RX_IVAR(0), val);
10877ca00409SHaim Dreyfuss 
10887ca00409SHaim Dreyfuss 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS)
10897ca00409SHaim Dreyfuss 		iwl_write8(trans, CSR_MSIX_RX_IVAR(1), val);
10907ca00409SHaim Dreyfuss }
10917ca00409SHaim Dreyfuss 
109277c09bc8SSara Sharon void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie)
10937ca00409SHaim Dreyfuss {
10947ca00409SHaim Dreyfuss 	struct iwl_trans *trans = trans_pcie->trans;
10957ca00409SHaim Dreyfuss 
10967ca00409SHaim Dreyfuss 	if (!trans_pcie->msix_enabled) {
1097d7270d61SHaim Dreyfuss 		if (trans->cfg->mq_rx_supported &&
1098d7270d61SHaim Dreyfuss 		    test_bit(STATUS_DEVICE_ENABLED, &trans->status))
10997ca00409SHaim Dreyfuss 			iwl_write_prph(trans, UREG_CHICK,
11007ca00409SHaim Dreyfuss 				       UREG_CHICK_MSI_ENABLE);
11017ca00409SHaim Dreyfuss 		return;
11027ca00409SHaim Dreyfuss 	}
1103d7270d61SHaim Dreyfuss 	/*
1104d7270d61SHaim Dreyfuss 	 * The IVAR table needs to be configured again after reset,
1105d7270d61SHaim Dreyfuss 	 * but if the device is disabled, we can't write to
1106d7270d61SHaim Dreyfuss 	 * prph.
1107d7270d61SHaim Dreyfuss 	 */
1108d7270d61SHaim Dreyfuss 	if (test_bit(STATUS_DEVICE_ENABLED, &trans->status))
11097ca00409SHaim Dreyfuss 		iwl_write_prph(trans, UREG_CHICK, UREG_CHICK_MSIX_ENABLE);
11107ca00409SHaim Dreyfuss 
11117ca00409SHaim Dreyfuss 	/*
11127ca00409SHaim Dreyfuss 	 * Each cause from the causes list above and the RX causes is
11137ca00409SHaim Dreyfuss 	 * represented as a byte in the IVAR table. The first nibble
11147ca00409SHaim Dreyfuss 	 * represents the bound interrupt vector of the cause, the second
11157ca00409SHaim Dreyfuss 	 * represents no auto clear for this cause. This will be set if its
11167ca00409SHaim Dreyfuss 	 * interrupt vector is bound to serve other causes.
11177ca00409SHaim Dreyfuss 	 */
11187ca00409SHaim Dreyfuss 	iwl_pcie_map_rx_causes(trans);
11197ca00409SHaim Dreyfuss 
11207ca00409SHaim Dreyfuss 	iwl_pcie_map_non_rx_causes(trans);
112183730058SHaim Dreyfuss }
11227ca00409SHaim Dreyfuss 
112383730058SHaim Dreyfuss static void iwl_pcie_init_msix(struct iwl_trans_pcie *trans_pcie)
112483730058SHaim Dreyfuss {
112583730058SHaim Dreyfuss 	struct iwl_trans *trans = trans_pcie->trans;
112683730058SHaim Dreyfuss 
112783730058SHaim Dreyfuss 	iwl_pcie_conf_msix_hw(trans_pcie);
112883730058SHaim Dreyfuss 
112983730058SHaim Dreyfuss 	if (!trans_pcie->msix_enabled)
113083730058SHaim Dreyfuss 		return;
113183730058SHaim Dreyfuss 
113283730058SHaim Dreyfuss 	trans_pcie->fh_init_mask = ~iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD);
11337ca00409SHaim Dreyfuss 	trans_pcie->fh_mask = trans_pcie->fh_init_mask;
113483730058SHaim Dreyfuss 	trans_pcie->hw_init_mask = ~iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD);
11357ca00409SHaim Dreyfuss 	trans_pcie->hw_mask = trans_pcie->hw_init_mask;
11367ca00409SHaim Dreyfuss }
11377ca00409SHaim Dreyfuss 
1138e705c121SKalle Valo static void _iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power)
1139e705c121SKalle Valo {
1140e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1141e705c121SKalle Valo 
1142e705c121SKalle Valo 	lockdep_assert_held(&trans_pcie->mutex);
1143e705c121SKalle Valo 
1144e705c121SKalle Valo 	if (trans_pcie->is_down)
1145e705c121SKalle Valo 		return;
1146e705c121SKalle Valo 
1147e705c121SKalle Valo 	trans_pcie->is_down = true;
1148e705c121SKalle Valo 
1149e705c121SKalle Valo 	/* tell the device to stop sending interrupts */
1150e705c121SKalle Valo 	iwl_disable_interrupts(trans);
1151e705c121SKalle Valo 
1152e705c121SKalle Valo 	/* device going down, Stop using ICT table */
1153e705c121SKalle Valo 	iwl_pcie_disable_ict(trans);
1154e705c121SKalle Valo 
1155e705c121SKalle Valo 	/*
1156e705c121SKalle Valo 	 * If a HW restart happens during firmware loading,
1157e705c121SKalle Valo 	 * then the firmware loading might call this function
1158e705c121SKalle Valo 	 * and later it might be called again due to the
1159e705c121SKalle Valo 	 * restart. So don't process again if the device is
1160e705c121SKalle Valo 	 * already dead.
1161e705c121SKalle Valo 	 */
1162e705c121SKalle Valo 	if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
1163a6bd005fSEmmanuel Grumbach 		IWL_DEBUG_INFO(trans,
1164a6bd005fSEmmanuel Grumbach 			       "DEVICE_ENABLED bit was set and is now cleared\n");
1165e705c121SKalle Valo 		iwl_pcie_tx_stop(trans);
1166e705c121SKalle Valo 		iwl_pcie_rx_stop(trans);
1167e705c121SKalle Valo 
1168e705c121SKalle Valo 		/* Power-down device's busmaster DMA clocks */
1169e705c121SKalle Valo 		if (!trans->cfg->apmg_not_supported) {
1170e705c121SKalle Valo 			iwl_write_prph(trans, APMG_CLK_DIS_REG,
1171e705c121SKalle Valo 				       APMG_CLK_VAL_DMA_CLK_RQT);
1172e705c121SKalle Valo 			udelay(5);
1173e705c121SKalle Valo 		}
1174e705c121SKalle Valo 	}
1175e705c121SKalle Valo 
1176e705c121SKalle Valo 	/* Make sure (redundant) we've released our request to stay awake */
1177e705c121SKalle Valo 	iwl_clear_bit(trans, CSR_GP_CNTRL,
1178e705c121SKalle Valo 		      CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1179e705c121SKalle Valo 
1180e705c121SKalle Valo 	/* Stop the device, and put it in low power state */
1181e705c121SKalle Valo 	iwl_pcie_apm_stop(trans, false);
1182e705c121SKalle Valo 
1183e705c121SKalle Valo 	/* stop and reset the on-board processor */
1184e705c121SKalle Valo 	iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1185b7a08b28SJohannes Berg 	usleep_range(1000, 2000);
1186e705c121SKalle Valo 
1187e705c121SKalle Valo 	/*
1188f4a1f04aSGolan Ben Ami 	 * Upon stop, the IVAR table gets erased, so msi-x won't
1189f4a1f04aSGolan Ben Ami 	 * work. This causes a bug in RF-KILL flows, since the interrupt
1190f4a1f04aSGolan Ben Ami 	 * that enables radio won't fire on the correct irq, and the
1191f4a1f04aSGolan Ben Ami 	 * driver won't be able to handle the interrupt.
1192f4a1f04aSGolan Ben Ami 	 * Configure the IVAR table again after reset.
1193f4a1f04aSGolan Ben Ami 	 */
1194f4a1f04aSGolan Ben Ami 	iwl_pcie_conf_msix_hw(trans_pcie);
1195f4a1f04aSGolan Ben Ami 
1196f4a1f04aSGolan Ben Ami 	/*
1197e705c121SKalle Valo 	 * Upon stop, the APM issues an interrupt if HW RF kill is set.
1198e705c121SKalle Valo 	 * This is a bug in certain verions of the hardware.
1199e705c121SKalle Valo 	 * Certain devices also keep sending HW RF kill interrupt all
1200e705c121SKalle Valo 	 * the time, unless the interrupt is ACKed even if the interrupt
1201e705c121SKalle Valo 	 * should be masked. Re-ACK all the interrupts here.
1202e705c121SKalle Valo 	 */
1203e705c121SKalle Valo 	iwl_disable_interrupts(trans);
1204e705c121SKalle Valo 
1205e705c121SKalle Valo 	/* clear all status bits */
1206e705c121SKalle Valo 	clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1207e705c121SKalle Valo 	clear_bit(STATUS_INT_ENABLED, &trans->status);
1208e705c121SKalle Valo 	clear_bit(STATUS_TPOWER_PMI, &trans->status);
1209e705c121SKalle Valo 
1210e705c121SKalle Valo 	/*
1211e705c121SKalle Valo 	 * Even if we stop the HW, we still want the RF kill
1212e705c121SKalle Valo 	 * interrupt
1213e705c121SKalle Valo 	 */
1214e705c121SKalle Valo 	iwl_enable_rfkill_int(trans);
1215e705c121SKalle Valo 
1216a6bd005fSEmmanuel Grumbach 	/* re-take ownership to prevent other users from stealing the device */
1217e705c121SKalle Valo 	iwl_pcie_prepare_card_hw(trans);
1218e705c121SKalle Valo }
1219e705c121SKalle Valo 
1220eda50cdeSSara Sharon void iwl_pcie_synchronize_irqs(struct iwl_trans *trans)
12212e5d4a8fSHaim Dreyfuss {
12222e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
12232e5d4a8fSHaim Dreyfuss 
12242e5d4a8fSHaim Dreyfuss 	if (trans_pcie->msix_enabled) {
12252e5d4a8fSHaim Dreyfuss 		int i;
12262e5d4a8fSHaim Dreyfuss 
1227496d83caSHaim Dreyfuss 		for (i = 0; i < trans_pcie->alloc_vecs; i++)
12282e5d4a8fSHaim Dreyfuss 			synchronize_irq(trans_pcie->msix_entries[i].vector);
12292e5d4a8fSHaim Dreyfuss 	} else {
12302e5d4a8fSHaim Dreyfuss 		synchronize_irq(trans_pcie->pci_dev->irq);
12312e5d4a8fSHaim Dreyfuss 	}
12322e5d4a8fSHaim Dreyfuss }
12332e5d4a8fSHaim Dreyfuss 
1234a6bd005fSEmmanuel Grumbach static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
1235a6bd005fSEmmanuel Grumbach 				   const struct fw_img *fw, bool run_in_rfkill)
1236a6bd005fSEmmanuel Grumbach {
1237a6bd005fSEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1238a6bd005fSEmmanuel Grumbach 	bool hw_rfkill;
1239a6bd005fSEmmanuel Grumbach 	int ret;
1240a6bd005fSEmmanuel Grumbach 
1241a6bd005fSEmmanuel Grumbach 	/* This may fail if AMT took ownership of the device */
1242a6bd005fSEmmanuel Grumbach 	if (iwl_pcie_prepare_card_hw(trans)) {
1243a6bd005fSEmmanuel Grumbach 		IWL_WARN(trans, "Exit HW not ready\n");
1244a6bd005fSEmmanuel Grumbach 		ret = -EIO;
1245a6bd005fSEmmanuel Grumbach 		goto out;
1246a6bd005fSEmmanuel Grumbach 	}
1247a6bd005fSEmmanuel Grumbach 
1248a6bd005fSEmmanuel Grumbach 	iwl_enable_rfkill_int(trans);
1249a6bd005fSEmmanuel Grumbach 
1250a6bd005fSEmmanuel Grumbach 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
1251a6bd005fSEmmanuel Grumbach 
1252a6bd005fSEmmanuel Grumbach 	/*
1253a6bd005fSEmmanuel Grumbach 	 * We enabled the RF-Kill interrupt and the handler may very
1254a6bd005fSEmmanuel Grumbach 	 * well be running. Disable the interrupts to make sure no other
1255a6bd005fSEmmanuel Grumbach 	 * interrupt can be fired.
1256a6bd005fSEmmanuel Grumbach 	 */
1257a6bd005fSEmmanuel Grumbach 	iwl_disable_interrupts(trans);
1258a6bd005fSEmmanuel Grumbach 
1259a6bd005fSEmmanuel Grumbach 	/* Make sure it finished running */
12602e5d4a8fSHaim Dreyfuss 	iwl_pcie_synchronize_irqs(trans);
1261a6bd005fSEmmanuel Grumbach 
1262a6bd005fSEmmanuel Grumbach 	mutex_lock(&trans_pcie->mutex);
1263a6bd005fSEmmanuel Grumbach 
1264a6bd005fSEmmanuel Grumbach 	/* If platform's RF_KILL switch is NOT set to KILL */
1265727c02dfSSara Sharon 	hw_rfkill = iwl_trans_check_hw_rf_kill(trans);
1266a6bd005fSEmmanuel Grumbach 	if (hw_rfkill && !run_in_rfkill) {
1267a6bd005fSEmmanuel Grumbach 		ret = -ERFKILL;
1268a6bd005fSEmmanuel Grumbach 		goto out;
1269a6bd005fSEmmanuel Grumbach 	}
1270a6bd005fSEmmanuel Grumbach 
1271a6bd005fSEmmanuel Grumbach 	/* Someone called stop_device, don't try to start_fw */
1272a6bd005fSEmmanuel Grumbach 	if (trans_pcie->is_down) {
1273a6bd005fSEmmanuel Grumbach 		IWL_WARN(trans,
1274a6bd005fSEmmanuel Grumbach 			 "Can't start_fw since the HW hasn't been started\n");
127520aa99bbSAnton Protopopov 		ret = -EIO;
1276a6bd005fSEmmanuel Grumbach 		goto out;
1277a6bd005fSEmmanuel Grumbach 	}
1278a6bd005fSEmmanuel Grumbach 
1279a6bd005fSEmmanuel Grumbach 	/* make sure rfkill handshake bits are cleared */
1280a6bd005fSEmmanuel Grumbach 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1281a6bd005fSEmmanuel Grumbach 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
1282a6bd005fSEmmanuel Grumbach 		    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1283a6bd005fSEmmanuel Grumbach 
1284a6bd005fSEmmanuel Grumbach 	/* clear (again), then enable host interrupts */
1285a6bd005fSEmmanuel Grumbach 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
1286a6bd005fSEmmanuel Grumbach 
1287a6bd005fSEmmanuel Grumbach 	ret = iwl_pcie_nic_init(trans);
1288a6bd005fSEmmanuel Grumbach 	if (ret) {
1289a6bd005fSEmmanuel Grumbach 		IWL_ERR(trans, "Unable to init nic\n");
1290a6bd005fSEmmanuel Grumbach 		goto out;
1291a6bd005fSEmmanuel Grumbach 	}
1292a6bd005fSEmmanuel Grumbach 
1293a6bd005fSEmmanuel Grumbach 	/*
1294a6bd005fSEmmanuel Grumbach 	 * Now, we load the firmware and don't want to be interrupted, even
1295a6bd005fSEmmanuel Grumbach 	 * by the RF-Kill interrupt (hence mask all the interrupt besides the
1296a6bd005fSEmmanuel Grumbach 	 * FH_TX interrupt which is needed to load the firmware). If the
1297a6bd005fSEmmanuel Grumbach 	 * RF-Kill switch is toggled, we will find out after having loaded
1298a6bd005fSEmmanuel Grumbach 	 * the firmware and return the proper value to the caller.
1299a6bd005fSEmmanuel Grumbach 	 */
1300a6bd005fSEmmanuel Grumbach 	iwl_enable_fw_load_int(trans);
1301a6bd005fSEmmanuel Grumbach 
1302a6bd005fSEmmanuel Grumbach 	/* really make sure rfkill handshake bits are cleared */
1303a6bd005fSEmmanuel Grumbach 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1304a6bd005fSEmmanuel Grumbach 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1305a6bd005fSEmmanuel Grumbach 
1306a6bd005fSEmmanuel Grumbach 	/* Load the given image to the HW */
13076e584873SSara Sharon 	if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
1308a6bd005fSEmmanuel Grumbach 		ret = iwl_pcie_load_given_ucode_8000(trans, fw);
1309a6bd005fSEmmanuel Grumbach 	else
1310a6bd005fSEmmanuel Grumbach 		ret = iwl_pcie_load_given_ucode(trans, fw);
1311a6bd005fSEmmanuel Grumbach 
1312a6bd005fSEmmanuel Grumbach 	/* re-check RF-Kill state since we may have missed the interrupt */
1313727c02dfSSara Sharon 	hw_rfkill = iwl_trans_check_hw_rf_kill(trans);
1314a6bd005fSEmmanuel Grumbach 	if (hw_rfkill && !run_in_rfkill)
1315a6bd005fSEmmanuel Grumbach 		ret = -ERFKILL;
1316a6bd005fSEmmanuel Grumbach 
1317a6bd005fSEmmanuel Grumbach out:
1318a6bd005fSEmmanuel Grumbach 	mutex_unlock(&trans_pcie->mutex);
1319a6bd005fSEmmanuel Grumbach 	return ret;
1320a6bd005fSEmmanuel Grumbach }
1321a6bd005fSEmmanuel Grumbach 
1322a6bd005fSEmmanuel Grumbach static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
1323a6bd005fSEmmanuel Grumbach {
1324a6bd005fSEmmanuel Grumbach 	iwl_pcie_reset_ict(trans);
1325a6bd005fSEmmanuel Grumbach 	iwl_pcie_tx_start(trans, scd_addr);
1326a6bd005fSEmmanuel Grumbach }
1327a6bd005fSEmmanuel Grumbach 
1328326477e4SJohannes Berg void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans,
1329326477e4SJohannes Berg 				       bool was_in_rfkill)
1330326477e4SJohannes Berg {
1331326477e4SJohannes Berg 	bool hw_rfkill;
1332326477e4SJohannes Berg 
1333326477e4SJohannes Berg 	/*
1334326477e4SJohannes Berg 	 * Check again since the RF kill state may have changed while
1335326477e4SJohannes Berg 	 * all the interrupts were disabled, in this case we couldn't
1336326477e4SJohannes Berg 	 * receive the RF kill interrupt and update the state in the
1337326477e4SJohannes Berg 	 * op_mode.
1338326477e4SJohannes Berg 	 * Don't call the op_mode if the rkfill state hasn't changed.
1339326477e4SJohannes Berg 	 * This allows the op_mode to call stop_device from the rfkill
1340326477e4SJohannes Berg 	 * notification without endless recursion. Under very rare
1341326477e4SJohannes Berg 	 * circumstances, we might have a small recursion if the rfkill
1342326477e4SJohannes Berg 	 * state changed exactly now while we were called from stop_device.
1343326477e4SJohannes Berg 	 * This is very unlikely but can happen and is supported.
1344326477e4SJohannes Berg 	 */
1345326477e4SJohannes Berg 	hw_rfkill = iwl_is_rfkill_set(trans);
1346326477e4SJohannes Berg 	if (hw_rfkill) {
1347326477e4SJohannes Berg 		set_bit(STATUS_RFKILL_HW, &trans->status);
1348326477e4SJohannes Berg 		set_bit(STATUS_RFKILL_OPMODE, &trans->status);
1349326477e4SJohannes Berg 	} else {
1350326477e4SJohannes Berg 		clear_bit(STATUS_RFKILL_HW, &trans->status);
1351326477e4SJohannes Berg 		clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
1352326477e4SJohannes Berg 	}
1353326477e4SJohannes Berg 	if (hw_rfkill != was_in_rfkill)
1354326477e4SJohannes Berg 		iwl_trans_pcie_rf_kill(trans, hw_rfkill);
1355326477e4SJohannes Berg }
1356326477e4SJohannes Berg 
1357e705c121SKalle Valo static void iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power)
1358e705c121SKalle Valo {
1359e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1360326477e4SJohannes Berg 	bool was_in_rfkill;
1361e705c121SKalle Valo 
1362e705c121SKalle Valo 	mutex_lock(&trans_pcie->mutex);
1363326477e4SJohannes Berg 	trans_pcie->opmode_down = true;
1364326477e4SJohannes Berg 	was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1365e705c121SKalle Valo 	_iwl_trans_pcie_stop_device(trans, low_power);
1366326477e4SJohannes Berg 	iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill);
1367e705c121SKalle Valo 	mutex_unlock(&trans_pcie->mutex);
1368e705c121SKalle Valo }
1369e705c121SKalle Valo 
1370e705c121SKalle Valo void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state)
1371e705c121SKalle Valo {
1372e705c121SKalle Valo 	struct iwl_trans_pcie __maybe_unused *trans_pcie =
1373e705c121SKalle Valo 		IWL_TRANS_GET_PCIE_TRANS(trans);
1374e705c121SKalle Valo 
1375e705c121SKalle Valo 	lockdep_assert_held(&trans_pcie->mutex);
1376e705c121SKalle Valo 
1377326477e4SJohannes Berg 	IWL_WARN(trans, "reporting RF_KILL (radio %s)\n",
1378326477e4SJohannes Berg 		 state ? "disabled" : "enabled");
137977c09bc8SSara Sharon 	if (iwl_op_mode_hw_rf_kill(trans->op_mode, state)) {
138077c09bc8SSara Sharon 		if (trans->cfg->gen2)
138177c09bc8SSara Sharon 			_iwl_trans_pcie_gen2_stop_device(trans, true);
138277c09bc8SSara Sharon 		else
1383e705c121SKalle Valo 			_iwl_trans_pcie_stop_device(trans, true);
1384e705c121SKalle Valo 	}
138577c09bc8SSara Sharon }
1386e705c121SKalle Valo 
138723ae6128SMatti Gottlieb static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test,
138823ae6128SMatti Gottlieb 				      bool reset)
1389e705c121SKalle Valo {
139023ae6128SMatti Gottlieb 	if (!reset) {
1391e705c121SKalle Valo 		/* Enable persistence mode to avoid reset */
1392e705c121SKalle Valo 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
1393e705c121SKalle Valo 			    CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
1394e705c121SKalle Valo 	}
1395e705c121SKalle Valo 
1396e705c121SKalle Valo 	iwl_disable_interrupts(trans);
1397e705c121SKalle Valo 
1398e705c121SKalle Valo 	/*
1399e705c121SKalle Valo 	 * in testing mode, the host stays awake and the
1400e705c121SKalle Valo 	 * hardware won't be reset (not even partially)
1401e705c121SKalle Valo 	 */
1402e705c121SKalle Valo 	if (test)
1403e705c121SKalle Valo 		return;
1404e705c121SKalle Valo 
1405e705c121SKalle Valo 	iwl_pcie_disable_ict(trans);
1406e705c121SKalle Valo 
14072e5d4a8fSHaim Dreyfuss 	iwl_pcie_synchronize_irqs(trans);
1408e705c121SKalle Valo 
1409e705c121SKalle Valo 	iwl_clear_bit(trans, CSR_GP_CNTRL,
1410e705c121SKalle Valo 		      CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1411e705c121SKalle Valo 	iwl_clear_bit(trans, CSR_GP_CNTRL,
1412e705c121SKalle Valo 		      CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1413e705c121SKalle Valo 
14141316d595SSara Sharon 	iwl_pcie_enable_rx_wake(trans, false);
14151316d595SSara Sharon 
141623ae6128SMatti Gottlieb 	if (reset) {
1417e705c121SKalle Valo 		/*
1418e705c121SKalle Valo 		 * reset TX queues -- some of their registers reset during S3
1419e705c121SKalle Valo 		 * so if we don't reset everything here the D3 image would try
1420e705c121SKalle Valo 		 * to execute some invalid memory upon resume
1421e705c121SKalle Valo 		 */
1422e705c121SKalle Valo 		iwl_trans_pcie_tx_reset(trans);
1423e705c121SKalle Valo 	}
1424e705c121SKalle Valo 
1425e705c121SKalle Valo 	iwl_pcie_set_pwr(trans, true);
1426e705c121SKalle Valo }
1427e705c121SKalle Valo 
1428e705c121SKalle Valo static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
1429e705c121SKalle Valo 				    enum iwl_d3_status *status,
143023ae6128SMatti Gottlieb 				    bool test,  bool reset)
1431e705c121SKalle Valo {
1432d7270d61SHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie =  IWL_TRANS_GET_PCIE_TRANS(trans);
1433e705c121SKalle Valo 	u32 val;
1434e705c121SKalle Valo 	int ret;
1435e705c121SKalle Valo 
1436e705c121SKalle Valo 	if (test) {
1437e705c121SKalle Valo 		iwl_enable_interrupts(trans);
1438e705c121SKalle Valo 		*status = IWL_D3_STATUS_ALIVE;
1439e705c121SKalle Valo 		return 0;
1440e705c121SKalle Valo 	}
1441e705c121SKalle Valo 
14421316d595SSara Sharon 	iwl_pcie_enable_rx_wake(trans, true);
14431316d595SSara Sharon 
1444e705c121SKalle Valo 	/*
1445d7270d61SHaim Dreyfuss 	 * Reconfigure IVAR table in case of MSIX or reset ict table in
1446d7270d61SHaim Dreyfuss 	 * MSI mode since HW reset erased it.
1447d7270d61SHaim Dreyfuss 	 * Also enables interrupts - none will happen as
1448d7270d61SHaim Dreyfuss 	 * the device doesn't know we're waking it up, only when
1449d7270d61SHaim Dreyfuss 	 * the opmode actually tells it after this call.
1450e705c121SKalle Valo 	 */
1451d7270d61SHaim Dreyfuss 	iwl_pcie_conf_msix_hw(trans_pcie);
1452d7270d61SHaim Dreyfuss 	if (!trans_pcie->msix_enabled)
1453e705c121SKalle Valo 		iwl_pcie_reset_ict(trans);
145418dcb9a9SSara Sharon 	iwl_enable_interrupts(trans);
1455e705c121SKalle Valo 
1456e705c121SKalle Valo 	iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1457e705c121SKalle Valo 	iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1458e705c121SKalle Valo 
14596e584873SSara Sharon 	if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
1460e705c121SKalle Valo 		udelay(2);
1461e705c121SKalle Valo 
1462e705c121SKalle Valo 	ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
1463e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1464e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1465e705c121SKalle Valo 			   25000);
1466e705c121SKalle Valo 	if (ret < 0) {
1467e705c121SKalle Valo 		IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
1468e705c121SKalle Valo 		return ret;
1469e705c121SKalle Valo 	}
1470e705c121SKalle Valo 
1471e705c121SKalle Valo 	iwl_pcie_set_pwr(trans, false);
1472e705c121SKalle Valo 
147323ae6128SMatti Gottlieb 	if (!reset) {
1474e705c121SKalle Valo 		iwl_clear_bit(trans, CSR_GP_CNTRL,
1475e705c121SKalle Valo 			      CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1476e705c121SKalle Valo 	} else {
1477e705c121SKalle Valo 		iwl_trans_pcie_tx_reset(trans);
1478e705c121SKalle Valo 
1479e705c121SKalle Valo 		ret = iwl_pcie_rx_init(trans);
1480e705c121SKalle Valo 		if (ret) {
1481e705c121SKalle Valo 			IWL_ERR(trans,
1482e705c121SKalle Valo 				"Failed to resume the device (RX reset)\n");
1483e705c121SKalle Valo 			return ret;
1484e705c121SKalle Valo 		}
1485e705c121SKalle Valo 	}
1486e705c121SKalle Valo 
148782ea7966SSara Sharon 	IWL_DEBUG_POWER(trans, "WFPM value upon resume = 0x%08X\n",
148882ea7966SSara Sharon 			iwl_read_prph(trans, WFPM_GP2));
148982ea7966SSara Sharon 
1490e705c121SKalle Valo 	val = iwl_read32(trans, CSR_RESET);
1491e705c121SKalle Valo 	if (val & CSR_RESET_REG_FLAG_NEVO_RESET)
1492e705c121SKalle Valo 		*status = IWL_D3_STATUS_RESET;
1493e705c121SKalle Valo 	else
1494e705c121SKalle Valo 		*status = IWL_D3_STATUS_ALIVE;
1495e705c121SKalle Valo 
1496e705c121SKalle Valo 	return 0;
1497e705c121SKalle Valo }
1498e705c121SKalle Valo 
14992e5d4a8fSHaim Dreyfuss static void iwl_pcie_set_interrupt_capa(struct pci_dev *pdev,
15002e5d4a8fSHaim Dreyfuss 					struct iwl_trans *trans)
15012e5d4a8fSHaim Dreyfuss {
15022e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
15039fb064dfSHaim Dreyfuss 	int max_irqs, num_irqs, i, ret, nr_online_cpus;
15042e5d4a8fSHaim Dreyfuss 	u16 pci_cmd;
15052e5d4a8fSHaim Dreyfuss 
150606f4b081SSara Sharon 	if (!trans->cfg->mq_rx_supported)
150706f4b081SSara Sharon 		goto enable_msi;
150806f4b081SSara Sharon 
15099fb064dfSHaim Dreyfuss 	nr_online_cpus = num_online_cpus();
15109fb064dfSHaim Dreyfuss 	max_irqs = min_t(u32, nr_online_cpus + 2, IWL_MAX_RX_HW_QUEUES);
151106f4b081SSara Sharon 	for (i = 0; i < max_irqs; i++)
15122e5d4a8fSHaim Dreyfuss 		trans_pcie->msix_entries[i].entry = i;
15132e5d4a8fSHaim Dreyfuss 
151406f4b081SSara Sharon 	num_irqs = pci_enable_msix_range(pdev, trans_pcie->msix_entries,
15152e5d4a8fSHaim Dreyfuss 					 MSIX_MIN_INTERRUPT_VECTORS,
151606f4b081SSara Sharon 					 max_irqs);
151706f4b081SSara Sharon 	if (num_irqs < 0) {
1518496d83caSHaim Dreyfuss 		IWL_DEBUG_INFO(trans,
151906f4b081SSara Sharon 			       "Failed to enable msi-x mode (ret %d). Moving to msi mode.\n",
152006f4b081SSara Sharon 			       num_irqs);
152106f4b081SSara Sharon 		goto enable_msi;
1522496d83caSHaim Dreyfuss 	}
152306f4b081SSara Sharon 	trans_pcie->def_irq = (num_irqs == max_irqs) ? num_irqs - 1 : 0;
1524496d83caSHaim Dreyfuss 
15252e5d4a8fSHaim Dreyfuss 	IWL_DEBUG_INFO(trans,
152606f4b081SSara Sharon 		       "MSI-X enabled. %d interrupt vectors were allocated\n",
152706f4b081SSara Sharon 		       num_irqs);
152806f4b081SSara Sharon 
1529496d83caSHaim Dreyfuss 	/*
153006f4b081SSara Sharon 	 * In case the OS provides fewer interrupts than requested, different
153106f4b081SSara Sharon 	 * causes will share the same interrupt vector as follows:
1532496d83caSHaim Dreyfuss 	 * One interrupt less: non rx causes shared with FBQ.
1533496d83caSHaim Dreyfuss 	 * Two interrupts less: non rx causes shared with FBQ and RSS.
1534496d83caSHaim Dreyfuss 	 * More than two interrupts: we will use fewer RSS queues.
1535496d83caSHaim Dreyfuss 	 */
15369fb064dfSHaim Dreyfuss 	if (num_irqs <= nr_online_cpus) {
153706f4b081SSara Sharon 		trans_pcie->trans->num_rx_queues = num_irqs + 1;
1538496d83caSHaim Dreyfuss 		trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX |
1539496d83caSHaim Dreyfuss 			IWL_SHARED_IRQ_FIRST_RSS;
15409fb064dfSHaim Dreyfuss 	} else if (num_irqs == nr_online_cpus + 1) {
154106f4b081SSara Sharon 		trans_pcie->trans->num_rx_queues = num_irqs;
1542496d83caSHaim Dreyfuss 		trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX;
1543496d83caSHaim Dreyfuss 	} else {
154406f4b081SSara Sharon 		trans_pcie->trans->num_rx_queues = num_irqs - 1;
1545496d83caSHaim Dreyfuss 	}
15462e5d4a8fSHaim Dreyfuss 
154706f4b081SSara Sharon 	trans_pcie->alloc_vecs = num_irqs;
1548496d83caSHaim Dreyfuss 	trans_pcie->msix_enabled = true;
15492e5d4a8fSHaim Dreyfuss 	return;
15502e5d4a8fSHaim Dreyfuss 
155106f4b081SSara Sharon enable_msi:
155206f4b081SSara Sharon 	ret = pci_enable_msi(pdev);
155306f4b081SSara Sharon 	if (ret) {
155406f4b081SSara Sharon 		dev_err(&pdev->dev, "pci_enable_msi failed - %d\n", ret);
15552e5d4a8fSHaim Dreyfuss 		/* enable rfkill interrupt: hw bug w/a */
15562e5d4a8fSHaim Dreyfuss 		pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
15572e5d4a8fSHaim Dreyfuss 		if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
15582e5d4a8fSHaim Dreyfuss 			pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
15592e5d4a8fSHaim Dreyfuss 			pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
15602e5d4a8fSHaim Dreyfuss 		}
15612e5d4a8fSHaim Dreyfuss 	}
15622e5d4a8fSHaim Dreyfuss }
15632e5d4a8fSHaim Dreyfuss 
15647c8d91ebSHaim Dreyfuss static void iwl_pcie_irq_set_affinity(struct iwl_trans *trans)
15657c8d91ebSHaim Dreyfuss {
15667c8d91ebSHaim Dreyfuss 	int iter_rx_q, i, ret, cpu, offset;
15677c8d91ebSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
15687c8d91ebSHaim Dreyfuss 
15697c8d91ebSHaim Dreyfuss 	i = trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 0 : 1;
15707c8d91ebSHaim Dreyfuss 	iter_rx_q = trans_pcie->trans->num_rx_queues - 1 + i;
15717c8d91ebSHaim Dreyfuss 	offset = 1 + i;
15727c8d91ebSHaim Dreyfuss 	for (; i < iter_rx_q ; i++) {
15737c8d91ebSHaim Dreyfuss 		/*
15747c8d91ebSHaim Dreyfuss 		 * Get the cpu prior to the place to search
15757c8d91ebSHaim Dreyfuss 		 * (i.e. return will be > i - 1).
15767c8d91ebSHaim Dreyfuss 		 */
15777c8d91ebSHaim Dreyfuss 		cpu = cpumask_next(i - offset, cpu_online_mask);
15787c8d91ebSHaim Dreyfuss 		cpumask_set_cpu(cpu, &trans_pcie->affinity_mask[i]);
15797c8d91ebSHaim Dreyfuss 		ret = irq_set_affinity_hint(trans_pcie->msix_entries[i].vector,
15807c8d91ebSHaim Dreyfuss 					    &trans_pcie->affinity_mask[i]);
15817c8d91ebSHaim Dreyfuss 		if (ret)
15827c8d91ebSHaim Dreyfuss 			IWL_ERR(trans_pcie->trans,
15837c8d91ebSHaim Dreyfuss 				"Failed to set affinity mask for IRQ %d\n",
15847c8d91ebSHaim Dreyfuss 				i);
15857c8d91ebSHaim Dreyfuss 	}
15867c8d91ebSHaim Dreyfuss }
15877c8d91ebSHaim Dreyfuss 
158864fa3affSSharon Dvir static const char *queue_name(struct device *dev,
158964fa3affSSharon Dvir 			      struct iwl_trans_pcie *trans_p, int i)
159064fa3affSSharon Dvir {
159164fa3affSSharon Dvir 	if (trans_p->shared_vec_mask) {
159264fa3affSSharon Dvir 		int vec = trans_p->shared_vec_mask &
159364fa3affSSharon Dvir 			  IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
159464fa3affSSharon Dvir 
159564fa3affSSharon Dvir 		if (i == 0)
159664fa3affSSharon Dvir 			return DRV_NAME ": shared IRQ";
159764fa3affSSharon Dvir 
159864fa3affSSharon Dvir 		return devm_kasprintf(dev, GFP_KERNEL,
159964fa3affSSharon Dvir 				      DRV_NAME ": queue %d", i + vec);
160064fa3affSSharon Dvir 	}
160164fa3affSSharon Dvir 	if (i == 0)
160264fa3affSSharon Dvir 		return DRV_NAME ": default queue";
160364fa3affSSharon Dvir 
160464fa3affSSharon Dvir 	if (i == trans_p->alloc_vecs - 1)
160564fa3affSSharon Dvir 		return DRV_NAME ": exception";
160664fa3affSSharon Dvir 
160764fa3affSSharon Dvir 	return devm_kasprintf(dev, GFP_KERNEL,
160864fa3affSSharon Dvir 			      DRV_NAME  ": queue %d", i);
160964fa3affSSharon Dvir }
161064fa3affSSharon Dvir 
16112e5d4a8fSHaim Dreyfuss static int iwl_pcie_init_msix_handler(struct pci_dev *pdev,
16122e5d4a8fSHaim Dreyfuss 				      struct iwl_trans_pcie *trans_pcie)
16132e5d4a8fSHaim Dreyfuss {
1614496d83caSHaim Dreyfuss 	int i;
16152e5d4a8fSHaim Dreyfuss 
1616496d83caSHaim Dreyfuss 	for (i = 0; i < trans_pcie->alloc_vecs; i++) {
16172e5d4a8fSHaim Dreyfuss 		int ret;
16185a41a86cSSharon Dvir 		struct msix_entry *msix_entry;
161964fa3affSSharon Dvir 		const char *qname = queue_name(&pdev->dev, trans_pcie, i);
162064fa3affSSharon Dvir 
162164fa3affSSharon Dvir 		if (!qname)
162264fa3affSSharon Dvir 			return -ENOMEM;
16232e5d4a8fSHaim Dreyfuss 
16245a41a86cSSharon Dvir 		msix_entry = &trans_pcie->msix_entries[i];
16255a41a86cSSharon Dvir 		ret = devm_request_threaded_irq(&pdev->dev,
16265a41a86cSSharon Dvir 						msix_entry->vector,
16272e5d4a8fSHaim Dreyfuss 						iwl_pcie_msix_isr,
1628496d83caSHaim Dreyfuss 						(i == trans_pcie->def_irq) ?
16292e5d4a8fSHaim Dreyfuss 						iwl_pcie_irq_msix_handler :
16302e5d4a8fSHaim Dreyfuss 						iwl_pcie_irq_rx_msix_handler,
16312e5d4a8fSHaim Dreyfuss 						IRQF_SHARED,
163264fa3affSSharon Dvir 						qname,
16335a41a86cSSharon Dvir 						msix_entry);
16342e5d4a8fSHaim Dreyfuss 		if (ret) {
16352e5d4a8fSHaim Dreyfuss 			IWL_ERR(trans_pcie->trans,
16362e5d4a8fSHaim Dreyfuss 				"Error allocating IRQ %d\n", i);
16375a41a86cSSharon Dvir 
16382e5d4a8fSHaim Dreyfuss 			return ret;
16392e5d4a8fSHaim Dreyfuss 		}
16402e5d4a8fSHaim Dreyfuss 	}
16417c8d91ebSHaim Dreyfuss 	iwl_pcie_irq_set_affinity(trans_pcie->trans);
16422e5d4a8fSHaim Dreyfuss 
16432e5d4a8fSHaim Dreyfuss 	return 0;
16442e5d4a8fSHaim Dreyfuss }
16452e5d4a8fSHaim Dreyfuss 
1646e705c121SKalle Valo static int _iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power)
1647e705c121SKalle Valo {
1648e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1649e705c121SKalle Valo 	int err;
1650e705c121SKalle Valo 
1651e705c121SKalle Valo 	lockdep_assert_held(&trans_pcie->mutex);
1652e705c121SKalle Valo 
1653e705c121SKalle Valo 	err = iwl_pcie_prepare_card_hw(trans);
1654e705c121SKalle Valo 	if (err) {
1655e705c121SKalle Valo 		IWL_ERR(trans, "Error while preparing HW: %d\n", err);
1656e705c121SKalle Valo 		return err;
1657e705c121SKalle Valo 	}
1658e705c121SKalle Valo 
1659e705c121SKalle Valo 	/* Reset the entire device */
1660e705c121SKalle Valo 	iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1661b7a08b28SJohannes Berg 	usleep_range(1000, 2000);
1662e705c121SKalle Valo 
1663e705c121SKalle Valo 	iwl_pcie_apm_init(trans);
1664e705c121SKalle Valo 
16652e5d4a8fSHaim Dreyfuss 	iwl_pcie_init_msix(trans_pcie);
166683730058SHaim Dreyfuss 
1667e705c121SKalle Valo 	/* From now on, the op_mode will be kept updated about RF kill state */
1668e705c121SKalle Valo 	iwl_enable_rfkill_int(trans);
1669e705c121SKalle Valo 
1670326477e4SJohannes Berg 	trans_pcie->opmode_down = false;
1671326477e4SJohannes Berg 
1672e705c121SKalle Valo 	/* Set is_down to false here so that...*/
1673e705c121SKalle Valo 	trans_pcie->is_down = false;
1674e705c121SKalle Valo 
1675e705c121SKalle Valo 	/* ...rfkill can call stop_device and set it false if needed */
1676727c02dfSSara Sharon 	iwl_trans_check_hw_rf_kill(trans);
1677e705c121SKalle Valo 
16784cbb8e50SLuciano Coelho 	/* Make sure we sync here, because we'll need full access later */
16794cbb8e50SLuciano Coelho 	if (low_power)
16804cbb8e50SLuciano Coelho 		pm_runtime_resume(trans->dev);
16814cbb8e50SLuciano Coelho 
1682e705c121SKalle Valo 	return 0;
1683e705c121SKalle Valo }
1684e705c121SKalle Valo 
1685e705c121SKalle Valo static int iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power)
1686e705c121SKalle Valo {
1687e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1688e705c121SKalle Valo 	int ret;
1689e705c121SKalle Valo 
1690e705c121SKalle Valo 	mutex_lock(&trans_pcie->mutex);
1691e705c121SKalle Valo 	ret = _iwl_trans_pcie_start_hw(trans, low_power);
1692e705c121SKalle Valo 	mutex_unlock(&trans_pcie->mutex);
1693e705c121SKalle Valo 
1694e705c121SKalle Valo 	return ret;
1695e705c121SKalle Valo }
1696e705c121SKalle Valo 
1697e705c121SKalle Valo static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
1698e705c121SKalle Valo {
1699e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1700e705c121SKalle Valo 
1701e705c121SKalle Valo 	mutex_lock(&trans_pcie->mutex);
1702e705c121SKalle Valo 
1703e705c121SKalle Valo 	/* disable interrupts - don't enable HW RF kill interrupt */
1704e705c121SKalle Valo 	iwl_disable_interrupts(trans);
1705e705c121SKalle Valo 
1706e705c121SKalle Valo 	iwl_pcie_apm_stop(trans, true);
1707e705c121SKalle Valo 
1708e705c121SKalle Valo 	iwl_disable_interrupts(trans);
1709e705c121SKalle Valo 
1710e705c121SKalle Valo 	iwl_pcie_disable_ict(trans);
1711e705c121SKalle Valo 
1712e705c121SKalle Valo 	mutex_unlock(&trans_pcie->mutex);
1713e705c121SKalle Valo 
17142e5d4a8fSHaim Dreyfuss 	iwl_pcie_synchronize_irqs(trans);
1715e705c121SKalle Valo }
1716e705c121SKalle Valo 
1717e705c121SKalle Valo static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
1718e705c121SKalle Valo {
1719e705c121SKalle Valo 	writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
1720e705c121SKalle Valo }
1721e705c121SKalle Valo 
1722e705c121SKalle Valo static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
1723e705c121SKalle Valo {
1724e705c121SKalle Valo 	writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
1725e705c121SKalle Valo }
1726e705c121SKalle Valo 
1727e705c121SKalle Valo static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
1728e705c121SKalle Valo {
1729e705c121SKalle Valo 	return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
1730e705c121SKalle Valo }
1731e705c121SKalle Valo 
1732e705c121SKalle Valo static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
1733e705c121SKalle Valo {
1734e705c121SKalle Valo 	iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
1735e705c121SKalle Valo 			       ((reg & 0x000FFFFF) | (3 << 24)));
1736e705c121SKalle Valo 	return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
1737e705c121SKalle Valo }
1738e705c121SKalle Valo 
1739e705c121SKalle Valo static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
1740e705c121SKalle Valo 				      u32 val)
1741e705c121SKalle Valo {
1742e705c121SKalle Valo 	iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
1743e705c121SKalle Valo 			       ((addr & 0x000FFFFF) | (3 << 24)));
1744e705c121SKalle Valo 	iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
1745e705c121SKalle Valo }
1746e705c121SKalle Valo 
1747e705c121SKalle Valo static void iwl_trans_pcie_configure(struct iwl_trans *trans,
1748e705c121SKalle Valo 				     const struct iwl_trans_config *trans_cfg)
1749e705c121SKalle Valo {
1750e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1751e705c121SKalle Valo 
1752e705c121SKalle Valo 	trans_pcie->cmd_queue = trans_cfg->cmd_queue;
1753e705c121SKalle Valo 	trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
1754e705c121SKalle Valo 	trans_pcie->cmd_q_wdg_timeout = trans_cfg->cmd_q_wdg_timeout;
1755e705c121SKalle Valo 	if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
1756e705c121SKalle Valo 		trans_pcie->n_no_reclaim_cmds = 0;
1757e705c121SKalle Valo 	else
1758e705c121SKalle Valo 		trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
1759e705c121SKalle Valo 	if (trans_pcie->n_no_reclaim_cmds)
1760e705c121SKalle Valo 		memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
1761e705c121SKalle Valo 		       trans_pcie->n_no_reclaim_cmds * sizeof(u8));
1762e705c121SKalle Valo 
17636c4fbcbcSEmmanuel Grumbach 	trans_pcie->rx_buf_size = trans_cfg->rx_buf_size;
17646c4fbcbcSEmmanuel Grumbach 	trans_pcie->rx_page_order =
17656c4fbcbcSEmmanuel Grumbach 		iwl_trans_get_rb_size_order(trans_pcie->rx_buf_size);
1766e705c121SKalle Valo 
1767e705c121SKalle Valo 	trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
1768e705c121SKalle Valo 	trans_pcie->scd_set_active = trans_cfg->scd_set_active;
176941837ca9SEmmanuel Grumbach 	trans_pcie->sw_csum_tx = trans_cfg->sw_csum_tx;
1770e705c121SKalle Valo 
177121cb3222SJohannes Berg 	trans_pcie->page_offs = trans_cfg->cb_data_offs;
177221cb3222SJohannes Berg 	trans_pcie->dev_cmd_offs = trans_cfg->cb_data_offs + sizeof(void *);
177321cb3222SJohannes Berg 
177439bdb17eSSharon Dvir 	trans->command_groups = trans_cfg->command_groups;
177539bdb17eSSharon Dvir 	trans->command_groups_size = trans_cfg->command_groups_size;
177639bdb17eSSharon Dvir 
1777e705c121SKalle Valo 	/* Initialize NAPI here - it should be before registering to mac80211
1778e705c121SKalle Valo 	 * in the opmode but after the HW struct is allocated.
1779e705c121SKalle Valo 	 * As this function may be called again in some corner cases don't
1780e705c121SKalle Valo 	 * do anything if NAPI was already initialized.
1781e705c121SKalle Valo 	 */
1782bce97731SSara Sharon 	if (trans_pcie->napi_dev.reg_state != NETREG_DUMMY)
1783e705c121SKalle Valo 		init_dummy_netdev(&trans_pcie->napi_dev);
1784e705c121SKalle Valo }
1785e705c121SKalle Valo 
1786e705c121SKalle Valo void iwl_trans_pcie_free(struct iwl_trans *trans)
1787e705c121SKalle Valo {
1788e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
17896eb5e529SEmmanuel Grumbach 	int i;
1790e705c121SKalle Valo 
17912e5d4a8fSHaim Dreyfuss 	iwl_pcie_synchronize_irqs(trans);
1792e705c121SKalle Valo 
179313a3a390SSara Sharon 	if (trans->cfg->gen2)
179413a3a390SSara Sharon 		iwl_pcie_gen2_tx_free(trans);
179513a3a390SSara Sharon 	else
1796e705c121SKalle Valo 		iwl_pcie_tx_free(trans);
1797e705c121SKalle Valo 	iwl_pcie_rx_free(trans);
1798e705c121SKalle Valo 
17992e5d4a8fSHaim Dreyfuss 	if (trans_pcie->msix_enabled) {
18007c8d91ebSHaim Dreyfuss 		for (i = 0; i < trans_pcie->alloc_vecs; i++) {
18017c8d91ebSHaim Dreyfuss 			irq_set_affinity_hint(
18027c8d91ebSHaim Dreyfuss 				trans_pcie->msix_entries[i].vector,
18037c8d91ebSHaim Dreyfuss 				NULL);
18047c8d91ebSHaim Dreyfuss 		}
18052e5d4a8fSHaim Dreyfuss 
18062e5d4a8fSHaim Dreyfuss 		trans_pcie->msix_enabled = false;
18072e5d4a8fSHaim Dreyfuss 	} else {
1808e705c121SKalle Valo 		iwl_pcie_free_ict(trans);
18092e5d4a8fSHaim Dreyfuss 	}
1810e705c121SKalle Valo 
1811e705c121SKalle Valo 	iwl_pcie_free_fw_monitor(trans);
1812e705c121SKalle Valo 
18136eb5e529SEmmanuel Grumbach 	for_each_possible_cpu(i) {
18146eb5e529SEmmanuel Grumbach 		struct iwl_tso_hdr_page *p =
18156eb5e529SEmmanuel Grumbach 			per_cpu_ptr(trans_pcie->tso_hdr_page, i);
18166eb5e529SEmmanuel Grumbach 
18176eb5e529SEmmanuel Grumbach 		if (p->page)
18186eb5e529SEmmanuel Grumbach 			__free_page(p->page);
18196eb5e529SEmmanuel Grumbach 	}
18206eb5e529SEmmanuel Grumbach 
18216eb5e529SEmmanuel Grumbach 	free_percpu(trans_pcie->tso_hdr_page);
1822a2a57a35SEmmanuel Grumbach 	mutex_destroy(&trans_pcie->mutex);
1823e705c121SKalle Valo 	iwl_trans_free(trans);
1824e705c121SKalle Valo }
1825e705c121SKalle Valo 
1826e705c121SKalle Valo static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
1827e705c121SKalle Valo {
1828e705c121SKalle Valo 	if (state)
1829e705c121SKalle Valo 		set_bit(STATUS_TPOWER_PMI, &trans->status);
1830e705c121SKalle Valo 	else
1831e705c121SKalle Valo 		clear_bit(STATUS_TPOWER_PMI, &trans->status);
1832e705c121SKalle Valo }
1833e705c121SKalle Valo 
183423ba9340SEmmanuel Grumbach static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans,
1835e705c121SKalle Valo 					   unsigned long *flags)
1836e705c121SKalle Valo {
1837e705c121SKalle Valo 	int ret;
1838e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1839e705c121SKalle Valo 
1840e705c121SKalle Valo 	spin_lock_irqsave(&trans_pcie->reg_lock, *flags);
1841e705c121SKalle Valo 
1842e705c121SKalle Valo 	if (trans_pcie->cmd_hold_nic_awake)
1843e705c121SKalle Valo 		goto out;
1844e705c121SKalle Valo 
1845e705c121SKalle Valo 	/* this bit wakes up the NIC */
1846e705c121SKalle Valo 	__iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
1847e705c121SKalle Valo 				 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
18486e584873SSara Sharon 	if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000)
1849e705c121SKalle Valo 		udelay(2);
1850e705c121SKalle Valo 
1851e705c121SKalle Valo 	/*
1852e705c121SKalle Valo 	 * These bits say the device is running, and should keep running for
1853e705c121SKalle Valo 	 * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
1854e705c121SKalle Valo 	 * but they do not indicate that embedded SRAM is restored yet;
1855e705c121SKalle Valo 	 * 3945 and 4965 have volatile SRAM, and must save/restore contents
1856e705c121SKalle Valo 	 * to/from host DRAM when sleeping/waking for power-saving.
1857e705c121SKalle Valo 	 * Each direction takes approximately 1/4 millisecond; with this
1858e705c121SKalle Valo 	 * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
1859e705c121SKalle Valo 	 * series of register accesses are expected (e.g. reading Event Log),
1860e705c121SKalle Valo 	 * to keep device from sleeping.
1861e705c121SKalle Valo 	 *
1862e705c121SKalle Valo 	 * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
1863e705c121SKalle Valo 	 * SRAM is okay/restored.  We don't check that here because this call
1864e705c121SKalle Valo 	 * is just for hardware register access; but GP1 MAC_SLEEP check is a
1865e705c121SKalle Valo 	 * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
1866e705c121SKalle Valo 	 *
1867e705c121SKalle Valo 	 * 5000 series and later (including 1000 series) have non-volatile SRAM,
1868e705c121SKalle Valo 	 * and do not save/restore SRAM when power cycling.
1869e705c121SKalle Valo 	 */
1870e705c121SKalle Valo 	ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
1871e705c121SKalle Valo 			   CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
1872e705c121SKalle Valo 			   (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
1873e705c121SKalle Valo 			    CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
1874e705c121SKalle Valo 	if (unlikely(ret < 0)) {
1875e705c121SKalle Valo 		iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
1876e705c121SKalle Valo 		WARN_ONCE(1,
1877e705c121SKalle Valo 			  "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
187823ba9340SEmmanuel Grumbach 			  iwl_read32(trans, CSR_GP_CNTRL));
1879e705c121SKalle Valo 		spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
1880e705c121SKalle Valo 		return false;
1881e705c121SKalle Valo 	}
1882e705c121SKalle Valo 
1883e705c121SKalle Valo out:
1884e705c121SKalle Valo 	/*
1885e705c121SKalle Valo 	 * Fool sparse by faking we release the lock - sparse will
1886e705c121SKalle Valo 	 * track nic_access anyway.
1887e705c121SKalle Valo 	 */
1888e705c121SKalle Valo 	__release(&trans_pcie->reg_lock);
1889e705c121SKalle Valo 	return true;
1890e705c121SKalle Valo }
1891e705c121SKalle Valo 
1892e705c121SKalle Valo static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans,
1893e705c121SKalle Valo 					      unsigned long *flags)
1894e705c121SKalle Valo {
1895e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1896e705c121SKalle Valo 
1897e705c121SKalle Valo 	lockdep_assert_held(&trans_pcie->reg_lock);
1898e705c121SKalle Valo 
1899e705c121SKalle Valo 	/*
1900e705c121SKalle Valo 	 * Fool sparse by faking we acquiring the lock - sparse will
1901e705c121SKalle Valo 	 * track nic_access anyway.
1902e705c121SKalle Valo 	 */
1903e705c121SKalle Valo 	__acquire(&trans_pcie->reg_lock);
1904e705c121SKalle Valo 
1905e705c121SKalle Valo 	if (trans_pcie->cmd_hold_nic_awake)
1906e705c121SKalle Valo 		goto out;
1907e705c121SKalle Valo 
1908e705c121SKalle Valo 	__iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
1909e705c121SKalle Valo 				   CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1910e705c121SKalle Valo 	/*
1911e705c121SKalle Valo 	 * Above we read the CSR_GP_CNTRL register, which will flush
1912e705c121SKalle Valo 	 * any previous writes, but we need the write that clears the
1913e705c121SKalle Valo 	 * MAC_ACCESS_REQ bit to be performed before any other writes
1914e705c121SKalle Valo 	 * scheduled on different CPUs (after we drop reg_lock).
1915e705c121SKalle Valo 	 */
1916e705c121SKalle Valo 	mmiowb();
1917e705c121SKalle Valo out:
1918e705c121SKalle Valo 	spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
1919e705c121SKalle Valo }
1920e705c121SKalle Valo 
1921e705c121SKalle Valo static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
1922e705c121SKalle Valo 				   void *buf, int dwords)
1923e705c121SKalle Valo {
1924e705c121SKalle Valo 	unsigned long flags;
1925e705c121SKalle Valo 	int offs, ret = 0;
1926e705c121SKalle Valo 	u32 *vals = buf;
1927e705c121SKalle Valo 
192823ba9340SEmmanuel Grumbach 	if (iwl_trans_grab_nic_access(trans, &flags)) {
1929e705c121SKalle Valo 		iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
1930e705c121SKalle Valo 		for (offs = 0; offs < dwords; offs++)
1931e705c121SKalle Valo 			vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1932e705c121SKalle Valo 		iwl_trans_release_nic_access(trans, &flags);
1933e705c121SKalle Valo 	} else {
1934e705c121SKalle Valo 		ret = -EBUSY;
1935e705c121SKalle Valo 	}
1936e705c121SKalle Valo 	return ret;
1937e705c121SKalle Valo }
1938e705c121SKalle Valo 
1939e705c121SKalle Valo static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
1940e705c121SKalle Valo 				    const void *buf, int dwords)
1941e705c121SKalle Valo {
1942e705c121SKalle Valo 	unsigned long flags;
1943e705c121SKalle Valo 	int offs, ret = 0;
1944e705c121SKalle Valo 	const u32 *vals = buf;
1945e705c121SKalle Valo 
194623ba9340SEmmanuel Grumbach 	if (iwl_trans_grab_nic_access(trans, &flags)) {
1947e705c121SKalle Valo 		iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
1948e705c121SKalle Valo 		for (offs = 0; offs < dwords; offs++)
1949e705c121SKalle Valo 			iwl_write32(trans, HBUS_TARG_MEM_WDAT,
1950e705c121SKalle Valo 				    vals ? vals[offs] : 0);
1951e705c121SKalle Valo 		iwl_trans_release_nic_access(trans, &flags);
1952e705c121SKalle Valo 	} else {
1953e705c121SKalle Valo 		ret = -EBUSY;
1954e705c121SKalle Valo 	}
1955e705c121SKalle Valo 	return ret;
1956e705c121SKalle Valo }
1957e705c121SKalle Valo 
1958e705c121SKalle Valo static void iwl_trans_pcie_freeze_txq_timer(struct iwl_trans *trans,
1959e705c121SKalle Valo 					    unsigned long txqs,
1960e705c121SKalle Valo 					    bool freeze)
1961e705c121SKalle Valo {
1962e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1963e705c121SKalle Valo 	int queue;
1964e705c121SKalle Valo 
1965e705c121SKalle Valo 	for_each_set_bit(queue, &txqs, BITS_PER_LONG) {
1966b2a3b1c1SSara Sharon 		struct iwl_txq *txq = trans_pcie->txq[queue];
1967e705c121SKalle Valo 		unsigned long now;
1968e705c121SKalle Valo 
1969e705c121SKalle Valo 		spin_lock_bh(&txq->lock);
1970e705c121SKalle Valo 
1971e705c121SKalle Valo 		now = jiffies;
1972e705c121SKalle Valo 
1973e705c121SKalle Valo 		if (txq->frozen == freeze)
1974e705c121SKalle Valo 			goto next_queue;
1975e705c121SKalle Valo 
1976e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n",
1977e705c121SKalle Valo 				    freeze ? "Freezing" : "Waking", queue);
1978e705c121SKalle Valo 
1979e705c121SKalle Valo 		txq->frozen = freeze;
1980e705c121SKalle Valo 
1981bb98ecd4SSara Sharon 		if (txq->read_ptr == txq->write_ptr)
1982e705c121SKalle Valo 			goto next_queue;
1983e705c121SKalle Valo 
1984e705c121SKalle Valo 		if (freeze) {
1985e705c121SKalle Valo 			if (unlikely(time_after(now,
1986e705c121SKalle Valo 						txq->stuck_timer.expires))) {
1987e705c121SKalle Valo 				/*
1988e705c121SKalle Valo 				 * The timer should have fired, maybe it is
1989e705c121SKalle Valo 				 * spinning right now on the lock.
1990e705c121SKalle Valo 				 */
1991e705c121SKalle Valo 				goto next_queue;
1992e705c121SKalle Valo 			}
1993e705c121SKalle Valo 			/* remember how long until the timer fires */
1994e705c121SKalle Valo 			txq->frozen_expiry_remainder =
1995e705c121SKalle Valo 				txq->stuck_timer.expires - now;
1996e705c121SKalle Valo 			del_timer(&txq->stuck_timer);
1997e705c121SKalle Valo 			goto next_queue;
1998e705c121SKalle Valo 		}
1999e705c121SKalle Valo 
2000e705c121SKalle Valo 		/*
2001e705c121SKalle Valo 		 * Wake a non-empty queue -> arm timer with the
2002e705c121SKalle Valo 		 * remainder before it froze
2003e705c121SKalle Valo 		 */
2004e705c121SKalle Valo 		mod_timer(&txq->stuck_timer,
2005e705c121SKalle Valo 			  now + txq->frozen_expiry_remainder);
2006e705c121SKalle Valo 
2007e705c121SKalle Valo next_queue:
2008e705c121SKalle Valo 		spin_unlock_bh(&txq->lock);
2009e705c121SKalle Valo 	}
2010e705c121SKalle Valo }
2011e705c121SKalle Valo 
20120cd58eaaSEmmanuel Grumbach static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block)
20130cd58eaaSEmmanuel Grumbach {
20140cd58eaaSEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
20150cd58eaaSEmmanuel Grumbach 	int i;
20160cd58eaaSEmmanuel Grumbach 
20170cd58eaaSEmmanuel Grumbach 	for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
2018b2a3b1c1SSara Sharon 		struct iwl_txq *txq = trans_pcie->txq[i];
20190cd58eaaSEmmanuel Grumbach 
20200cd58eaaSEmmanuel Grumbach 		if (i == trans_pcie->cmd_queue)
20210cd58eaaSEmmanuel Grumbach 			continue;
20220cd58eaaSEmmanuel Grumbach 
20230cd58eaaSEmmanuel Grumbach 		spin_lock_bh(&txq->lock);
20240cd58eaaSEmmanuel Grumbach 
20250cd58eaaSEmmanuel Grumbach 		if (!block && !(WARN_ON_ONCE(!txq->block))) {
20260cd58eaaSEmmanuel Grumbach 			txq->block--;
20270cd58eaaSEmmanuel Grumbach 			if (!txq->block) {
20280cd58eaaSEmmanuel Grumbach 				iwl_write32(trans, HBUS_TARG_WRPTR,
2029bb98ecd4SSara Sharon 					    txq->write_ptr | (i << 8));
20300cd58eaaSEmmanuel Grumbach 			}
20310cd58eaaSEmmanuel Grumbach 		} else if (block) {
20320cd58eaaSEmmanuel Grumbach 			txq->block++;
20330cd58eaaSEmmanuel Grumbach 		}
20340cd58eaaSEmmanuel Grumbach 
20350cd58eaaSEmmanuel Grumbach 		spin_unlock_bh(&txq->lock);
20360cd58eaaSEmmanuel Grumbach 	}
20370cd58eaaSEmmanuel Grumbach }
20380cd58eaaSEmmanuel Grumbach 
2039e705c121SKalle Valo #define IWL_FLUSH_WAIT_MS	2000
2040e705c121SKalle Valo 
204138398efbSSara Sharon void iwl_trans_pcie_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq)
204238398efbSSara Sharon {
2043afb84431SEmmanuel Grumbach 	u32 txq_id = txq->id;
2044afb84431SEmmanuel Grumbach 	u32 status;
2045afb84431SEmmanuel Grumbach 	bool active;
2046afb84431SEmmanuel Grumbach 	u8 fifo;
204738398efbSSara Sharon 
2048afb84431SEmmanuel Grumbach 	if (trans->cfg->use_tfh) {
2049afb84431SEmmanuel Grumbach 		IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id,
2050bb98ecd4SSara Sharon 			txq->read_ptr, txq->write_ptr);
2051ae79785fSSara Sharon 		/* TODO: access new SCD registers and dump them */
2052ae79785fSSara Sharon 		return;
2053afb84431SEmmanuel Grumbach 	}
2054ae79785fSSara Sharon 
2055afb84431SEmmanuel Grumbach 	status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id));
2056afb84431SEmmanuel Grumbach 	fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
2057afb84431SEmmanuel Grumbach 	active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
205838398efbSSara Sharon 
205938398efbSSara Sharon 	IWL_ERR(trans,
2060afb84431SEmmanuel Grumbach 		"Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n",
2061afb84431SEmmanuel Grumbach 		txq_id, active ? "" : "in", fifo,
2062afb84431SEmmanuel Grumbach 		jiffies_to_msecs(txq->wd_timeout),
2063afb84431SEmmanuel Grumbach 		txq->read_ptr, txq->write_ptr,
2064afb84431SEmmanuel Grumbach 		iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) &
206538398efbSSara Sharon 			(TFD_QUEUE_SIZE_MAX - 1),
2066afb84431SEmmanuel Grumbach 		iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) &
2067afb84431SEmmanuel Grumbach 			(TFD_QUEUE_SIZE_MAX - 1),
2068afb84431SEmmanuel Grumbach 		iwl_read_direct32(trans, FH_TX_TRB_REG(fifo)));
206938398efbSSara Sharon }
207038398efbSSara Sharon 
2071d6d517b7SSara Sharon static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, int txq_idx)
2072e705c121SKalle Valo {
2073e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2074e705c121SKalle Valo 	struct iwl_txq *txq;
2075e705c121SKalle Valo 	unsigned long now = jiffies;
2076e705c121SKalle Valo 	u8 wr_ptr;
2077e705c121SKalle Valo 
2078d6d517b7SSara Sharon 	if (!test_bit(txq_idx, trans_pcie->queue_used))
2079d6d517b7SSara Sharon 		return -EINVAL;
2080e705c121SKalle Valo 
2081d6d517b7SSara Sharon 	IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", txq_idx);
2082d6d517b7SSara Sharon 	txq = trans_pcie->txq[txq_idx];
2083bb98ecd4SSara Sharon 	wr_ptr = ACCESS_ONCE(txq->write_ptr);
2084e705c121SKalle Valo 
2085bb98ecd4SSara Sharon 	while (txq->read_ptr != ACCESS_ONCE(txq->write_ptr) &&
2086e705c121SKalle Valo 	       !time_after(jiffies,
2087e705c121SKalle Valo 			   now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
2088bb98ecd4SSara Sharon 		u8 write_ptr = ACCESS_ONCE(txq->write_ptr);
2089e705c121SKalle Valo 
2090e705c121SKalle Valo 		if (WARN_ONCE(wr_ptr != write_ptr,
2091e705c121SKalle Valo 			      "WR pointer moved while flushing %d -> %d\n",
2092e705c121SKalle Valo 			      wr_ptr, write_ptr))
2093e705c121SKalle Valo 			return -ETIMEDOUT;
2094192185d6SJohannes Berg 		usleep_range(1000, 2000);
2095e705c121SKalle Valo 	}
2096e705c121SKalle Valo 
2097bb98ecd4SSara Sharon 	if (txq->read_ptr != txq->write_ptr) {
2098e705c121SKalle Valo 		IWL_ERR(trans,
2099d6d517b7SSara Sharon 			"fail to flush all tx fifo queues Q %d\n", txq_idx);
2100d6d517b7SSara Sharon 		iwl_trans_pcie_log_scd_error(trans, txq);
2101d6d517b7SSara Sharon 		return -ETIMEDOUT;
2102e705c121SKalle Valo 	}
2103e705c121SKalle Valo 
2104d6d517b7SSara Sharon 	IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", txq_idx);
2105d6d517b7SSara Sharon 
2106d6d517b7SSara Sharon 	return 0;
2107d6d517b7SSara Sharon }
2108d6d517b7SSara Sharon 
2109d6d517b7SSara Sharon static int iwl_trans_pcie_wait_txqs_empty(struct iwl_trans *trans, u32 txq_bm)
2110d6d517b7SSara Sharon {
2111d6d517b7SSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2112d6d517b7SSara Sharon 	int cnt;
2113d6d517b7SSara Sharon 	int ret = 0;
2114d6d517b7SSara Sharon 
2115d6d517b7SSara Sharon 	/* waiting for all the tx frames complete might take a while */
2116d6d517b7SSara Sharon 	for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
2117d6d517b7SSara Sharon 
2118d6d517b7SSara Sharon 		if (cnt == trans_pcie->cmd_queue)
2119d6d517b7SSara Sharon 			continue;
2120d6d517b7SSara Sharon 		if (!test_bit(cnt, trans_pcie->queue_used))
2121d6d517b7SSara Sharon 			continue;
2122d6d517b7SSara Sharon 		if (!(BIT(cnt) & txq_bm))
2123d6d517b7SSara Sharon 			continue;
2124d6d517b7SSara Sharon 
2125d6d517b7SSara Sharon 		ret = iwl_trans_pcie_wait_txq_empty(trans, cnt);
212638398efbSSara Sharon 		if (ret)
2127d6d517b7SSara Sharon 			break;
2128d6d517b7SSara Sharon 	}
2129e705c121SKalle Valo 
2130e705c121SKalle Valo 	return ret;
2131e705c121SKalle Valo }
2132e705c121SKalle Valo 
2133e705c121SKalle Valo static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
2134e705c121SKalle Valo 					 u32 mask, u32 value)
2135e705c121SKalle Valo {
2136e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2137e705c121SKalle Valo 	unsigned long flags;
2138e705c121SKalle Valo 
2139e705c121SKalle Valo 	spin_lock_irqsave(&trans_pcie->reg_lock, flags);
2140e705c121SKalle Valo 	__iwl_trans_pcie_set_bits_mask(trans, reg, mask, value);
2141e705c121SKalle Valo 	spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
2142e705c121SKalle Valo }
2143e705c121SKalle Valo 
2144c24c7f58SLuca Coelho static void iwl_trans_pcie_ref(struct iwl_trans *trans)
2145e705c121SKalle Valo {
2146e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2147e705c121SKalle Valo 
2148e705c121SKalle Valo 	if (iwlwifi_mod_params.d0i3_disable)
2149e705c121SKalle Valo 		return;
2150e705c121SKalle Valo 
2151b3ff1270SLuca Coelho 	pm_runtime_get(&trans_pcie->pci_dev->dev);
21525d93f3a2SLuca Coelho 
21535d93f3a2SLuca Coelho #ifdef CONFIG_PM
21545d93f3a2SLuca Coelho 	IWL_DEBUG_RPM(trans, "runtime usage count: %d\n",
21555d93f3a2SLuca Coelho 		      atomic_read(&trans_pcie->pci_dev->dev.power.usage_count));
21565d93f3a2SLuca Coelho #endif /* CONFIG_PM */
2157e705c121SKalle Valo }
2158e705c121SKalle Valo 
2159c24c7f58SLuca Coelho static void iwl_trans_pcie_unref(struct iwl_trans *trans)
2160e705c121SKalle Valo {
2161e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2162e705c121SKalle Valo 
2163e705c121SKalle Valo 	if (iwlwifi_mod_params.d0i3_disable)
2164e705c121SKalle Valo 		return;
2165e705c121SKalle Valo 
2166b3ff1270SLuca Coelho 	pm_runtime_mark_last_busy(&trans_pcie->pci_dev->dev);
2167b3ff1270SLuca Coelho 	pm_runtime_put_autosuspend(&trans_pcie->pci_dev->dev);
2168b3ff1270SLuca Coelho 
21695d93f3a2SLuca Coelho #ifdef CONFIG_PM
21705d93f3a2SLuca Coelho 	IWL_DEBUG_RPM(trans, "runtime usage count: %d\n",
21715d93f3a2SLuca Coelho 		      atomic_read(&trans_pcie->pci_dev->dev.power.usage_count));
21725d93f3a2SLuca Coelho #endif /* CONFIG_PM */
2173e705c121SKalle Valo }
2174e705c121SKalle Valo 
2175e705c121SKalle Valo static const char *get_csr_string(int cmd)
2176e705c121SKalle Valo {
2177e705c121SKalle Valo #define IWL_CMD(x) case x: return #x
2178e705c121SKalle Valo 	switch (cmd) {
2179e705c121SKalle Valo 	IWL_CMD(CSR_HW_IF_CONFIG_REG);
2180e705c121SKalle Valo 	IWL_CMD(CSR_INT_COALESCING);
2181e705c121SKalle Valo 	IWL_CMD(CSR_INT);
2182e705c121SKalle Valo 	IWL_CMD(CSR_INT_MASK);
2183e705c121SKalle Valo 	IWL_CMD(CSR_FH_INT_STATUS);
2184e705c121SKalle Valo 	IWL_CMD(CSR_GPIO_IN);
2185e705c121SKalle Valo 	IWL_CMD(CSR_RESET);
2186e705c121SKalle Valo 	IWL_CMD(CSR_GP_CNTRL);
2187e705c121SKalle Valo 	IWL_CMD(CSR_HW_REV);
2188e705c121SKalle Valo 	IWL_CMD(CSR_EEPROM_REG);
2189e705c121SKalle Valo 	IWL_CMD(CSR_EEPROM_GP);
2190e705c121SKalle Valo 	IWL_CMD(CSR_OTP_GP_REG);
2191e705c121SKalle Valo 	IWL_CMD(CSR_GIO_REG);
2192e705c121SKalle Valo 	IWL_CMD(CSR_GP_UCODE_REG);
2193e705c121SKalle Valo 	IWL_CMD(CSR_GP_DRIVER_REG);
2194e705c121SKalle Valo 	IWL_CMD(CSR_UCODE_DRV_GP1);
2195e705c121SKalle Valo 	IWL_CMD(CSR_UCODE_DRV_GP2);
2196e705c121SKalle Valo 	IWL_CMD(CSR_LED_REG);
2197e705c121SKalle Valo 	IWL_CMD(CSR_DRAM_INT_TBL_REG);
2198e705c121SKalle Valo 	IWL_CMD(CSR_GIO_CHICKEN_BITS);
2199e705c121SKalle Valo 	IWL_CMD(CSR_ANA_PLL_CFG);
2200e705c121SKalle Valo 	IWL_CMD(CSR_HW_REV_WA_REG);
2201e705c121SKalle Valo 	IWL_CMD(CSR_MONITOR_STATUS_REG);
2202e705c121SKalle Valo 	IWL_CMD(CSR_DBG_HPET_MEM_REG);
2203e705c121SKalle Valo 	default:
2204e705c121SKalle Valo 		return "UNKNOWN";
2205e705c121SKalle Valo 	}
2206e705c121SKalle Valo #undef IWL_CMD
2207e705c121SKalle Valo }
2208e705c121SKalle Valo 
2209e705c121SKalle Valo void iwl_pcie_dump_csr(struct iwl_trans *trans)
2210e705c121SKalle Valo {
2211e705c121SKalle Valo 	int i;
2212e705c121SKalle Valo 	static const u32 csr_tbl[] = {
2213e705c121SKalle Valo 		CSR_HW_IF_CONFIG_REG,
2214e705c121SKalle Valo 		CSR_INT_COALESCING,
2215e705c121SKalle Valo 		CSR_INT,
2216e705c121SKalle Valo 		CSR_INT_MASK,
2217e705c121SKalle Valo 		CSR_FH_INT_STATUS,
2218e705c121SKalle Valo 		CSR_GPIO_IN,
2219e705c121SKalle Valo 		CSR_RESET,
2220e705c121SKalle Valo 		CSR_GP_CNTRL,
2221e705c121SKalle Valo 		CSR_HW_REV,
2222e705c121SKalle Valo 		CSR_EEPROM_REG,
2223e705c121SKalle Valo 		CSR_EEPROM_GP,
2224e705c121SKalle Valo 		CSR_OTP_GP_REG,
2225e705c121SKalle Valo 		CSR_GIO_REG,
2226e705c121SKalle Valo 		CSR_GP_UCODE_REG,
2227e705c121SKalle Valo 		CSR_GP_DRIVER_REG,
2228e705c121SKalle Valo 		CSR_UCODE_DRV_GP1,
2229e705c121SKalle Valo 		CSR_UCODE_DRV_GP2,
2230e705c121SKalle Valo 		CSR_LED_REG,
2231e705c121SKalle Valo 		CSR_DRAM_INT_TBL_REG,
2232e705c121SKalle Valo 		CSR_GIO_CHICKEN_BITS,
2233e705c121SKalle Valo 		CSR_ANA_PLL_CFG,
2234e705c121SKalle Valo 		CSR_MONITOR_STATUS_REG,
2235e705c121SKalle Valo 		CSR_HW_REV_WA_REG,
2236e705c121SKalle Valo 		CSR_DBG_HPET_MEM_REG
2237e705c121SKalle Valo 	};
2238e705c121SKalle Valo 	IWL_ERR(trans, "CSR values:\n");
2239e705c121SKalle Valo 	IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
2240e705c121SKalle Valo 		"CSR_INT_PERIODIC_REG)\n");
2241e705c121SKalle Valo 	for (i = 0; i <  ARRAY_SIZE(csr_tbl); i++) {
2242e705c121SKalle Valo 		IWL_ERR(trans, "  %25s: 0X%08x\n",
2243e705c121SKalle Valo 			get_csr_string(csr_tbl[i]),
2244e705c121SKalle Valo 			iwl_read32(trans, csr_tbl[i]));
2245e705c121SKalle Valo 	}
2246e705c121SKalle Valo }
2247e705c121SKalle Valo 
2248e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
2249e705c121SKalle Valo /* create and remove of files */
2250e705c121SKalle Valo #define DEBUGFS_ADD_FILE(name, parent, mode) do {			\
2251e705c121SKalle Valo 	if (!debugfs_create_file(#name, mode, parent, trans,		\
2252e705c121SKalle Valo 				 &iwl_dbgfs_##name##_ops))		\
2253e705c121SKalle Valo 		goto err;						\
2254e705c121SKalle Valo } while (0)
2255e705c121SKalle Valo 
2256e705c121SKalle Valo /* file operation */
2257e705c121SKalle Valo #define DEBUGFS_READ_FILE_OPS(name)					\
2258e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = {		\
2259e705c121SKalle Valo 	.read = iwl_dbgfs_##name##_read,				\
2260e705c121SKalle Valo 	.open = simple_open,						\
2261e705c121SKalle Valo 	.llseek = generic_file_llseek,					\
2262e705c121SKalle Valo };
2263e705c121SKalle Valo 
2264e705c121SKalle Valo #define DEBUGFS_WRITE_FILE_OPS(name)                                    \
2265e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = {          \
2266e705c121SKalle Valo 	.write = iwl_dbgfs_##name##_write,                              \
2267e705c121SKalle Valo 	.open = simple_open,						\
2268e705c121SKalle Valo 	.llseek = generic_file_llseek,					\
2269e705c121SKalle Valo };
2270e705c121SKalle Valo 
2271e705c121SKalle Valo #define DEBUGFS_READ_WRITE_FILE_OPS(name)				\
2272e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = {		\
2273e705c121SKalle Valo 	.write = iwl_dbgfs_##name##_write,				\
2274e705c121SKalle Valo 	.read = iwl_dbgfs_##name##_read,				\
2275e705c121SKalle Valo 	.open = simple_open,						\
2276e705c121SKalle Valo 	.llseek = generic_file_llseek,					\
2277e705c121SKalle Valo };
2278e705c121SKalle Valo 
2279e705c121SKalle Valo static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
2280e705c121SKalle Valo 				       char __user *user_buf,
2281e705c121SKalle Valo 				       size_t count, loff_t *ppos)
2282e705c121SKalle Valo {
2283e705c121SKalle Valo 	struct iwl_trans *trans = file->private_data;
2284e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2285e705c121SKalle Valo 	struct iwl_txq *txq;
2286e705c121SKalle Valo 	char *buf;
2287e705c121SKalle Valo 	int pos = 0;
2288e705c121SKalle Valo 	int cnt;
2289e705c121SKalle Valo 	int ret;
2290e705c121SKalle Valo 	size_t bufsz;
2291e705c121SKalle Valo 
2292e705c121SKalle Valo 	bufsz = sizeof(char) * 75 * trans->cfg->base_params->num_of_queues;
2293e705c121SKalle Valo 
2294b2a3b1c1SSara Sharon 	if (!trans_pcie->txq_memory)
2295e705c121SKalle Valo 		return -EAGAIN;
2296e705c121SKalle Valo 
2297e705c121SKalle Valo 	buf = kzalloc(bufsz, GFP_KERNEL);
2298e705c121SKalle Valo 	if (!buf)
2299e705c121SKalle Valo 		return -ENOMEM;
2300e705c121SKalle Valo 
2301e705c121SKalle Valo 	for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
2302b2a3b1c1SSara Sharon 		txq = trans_pcie->txq[cnt];
2303e705c121SKalle Valo 		pos += scnprintf(buf + pos, bufsz - pos,
2304e705c121SKalle Valo 				"hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d frozen=%d%s\n",
2305bb98ecd4SSara Sharon 				cnt, txq->read_ptr, txq->write_ptr,
2306e705c121SKalle Valo 				!!test_bit(cnt, trans_pcie->queue_used),
2307e705c121SKalle Valo 				 !!test_bit(cnt, trans_pcie->queue_stopped),
2308e705c121SKalle Valo 				 txq->need_update, txq->frozen,
2309e705c121SKalle Valo 				 (cnt == trans_pcie->cmd_queue ? " HCMD" : ""));
2310e705c121SKalle Valo 	}
2311e705c121SKalle Valo 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2312e705c121SKalle Valo 	kfree(buf);
2313e705c121SKalle Valo 	return ret;
2314e705c121SKalle Valo }
2315e705c121SKalle Valo 
2316e705c121SKalle Valo static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
2317e705c121SKalle Valo 				       char __user *user_buf,
2318e705c121SKalle Valo 				       size_t count, loff_t *ppos)
2319e705c121SKalle Valo {
2320e705c121SKalle Valo 	struct iwl_trans *trans = file->private_data;
2321e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
232278485054SSara Sharon 	char *buf;
232378485054SSara Sharon 	int pos = 0, i, ret;
232478485054SSara Sharon 	size_t bufsz = sizeof(buf);
2325e705c121SKalle Valo 
232678485054SSara Sharon 	bufsz = sizeof(char) * 121 * trans->num_rx_queues;
232778485054SSara Sharon 
232878485054SSara Sharon 	if (!trans_pcie->rxq)
232978485054SSara Sharon 		return -EAGAIN;
233078485054SSara Sharon 
233178485054SSara Sharon 	buf = kzalloc(bufsz, GFP_KERNEL);
233278485054SSara Sharon 	if (!buf)
233378485054SSara Sharon 		return -ENOMEM;
233478485054SSara Sharon 
233578485054SSara Sharon 	for (i = 0; i < trans->num_rx_queues && pos < bufsz; i++) {
233678485054SSara Sharon 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
233778485054SSara Sharon 
233878485054SSara Sharon 		pos += scnprintf(buf + pos, bufsz - pos, "queue#: %2d\n",
233978485054SSara Sharon 				 i);
234078485054SSara Sharon 		pos += scnprintf(buf + pos, bufsz - pos, "\tread: %u\n",
2341e705c121SKalle Valo 				 rxq->read);
234278485054SSara Sharon 		pos += scnprintf(buf + pos, bufsz - pos, "\twrite: %u\n",
2343e705c121SKalle Valo 				 rxq->write);
234478485054SSara Sharon 		pos += scnprintf(buf + pos, bufsz - pos, "\twrite_actual: %u\n",
2345e705c121SKalle Valo 				 rxq->write_actual);
234678485054SSara Sharon 		pos += scnprintf(buf + pos, bufsz - pos, "\tneed_update: %2d\n",
2347e705c121SKalle Valo 				 rxq->need_update);
234878485054SSara Sharon 		pos += scnprintf(buf + pos, bufsz - pos, "\tfree_count: %u\n",
2349e705c121SKalle Valo 				 rxq->free_count);
2350e705c121SKalle Valo 		if (rxq->rb_stts) {
235178485054SSara Sharon 			pos += scnprintf(buf + pos, bufsz - pos,
235278485054SSara Sharon 					 "\tclosed_rb_num: %u\n",
235378485054SSara Sharon 					 le16_to_cpu(rxq->rb_stts->closed_rb_num) &
235478485054SSara Sharon 					 0x0FFF);
2355e705c121SKalle Valo 		} else {
2356e705c121SKalle Valo 			pos += scnprintf(buf + pos, bufsz - pos,
235778485054SSara Sharon 					 "\tclosed_rb_num: Not Allocated\n");
2358e705c121SKalle Valo 		}
235978485054SSara Sharon 	}
236078485054SSara Sharon 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
236178485054SSara Sharon 	kfree(buf);
236278485054SSara Sharon 
236378485054SSara Sharon 	return ret;
2364e705c121SKalle Valo }
2365e705c121SKalle Valo 
2366e705c121SKalle Valo static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
2367e705c121SKalle Valo 					char __user *user_buf,
2368e705c121SKalle Valo 					size_t count, loff_t *ppos)
2369e705c121SKalle Valo {
2370e705c121SKalle Valo 	struct iwl_trans *trans = file->private_data;
2371e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2372e705c121SKalle Valo 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
2373e705c121SKalle Valo 
2374e705c121SKalle Valo 	int pos = 0;
2375e705c121SKalle Valo 	char *buf;
2376e705c121SKalle Valo 	int bufsz = 24 * 64; /* 24 items * 64 char per item */
2377e705c121SKalle Valo 	ssize_t ret;
2378e705c121SKalle Valo 
2379e705c121SKalle Valo 	buf = kzalloc(bufsz, GFP_KERNEL);
2380e705c121SKalle Valo 	if (!buf)
2381e705c121SKalle Valo 		return -ENOMEM;
2382e705c121SKalle Valo 
2383e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos,
2384e705c121SKalle Valo 			"Interrupt Statistics Report:\n");
2385e705c121SKalle Valo 
2386e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
2387e705c121SKalle Valo 		isr_stats->hw);
2388e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
2389e705c121SKalle Valo 		isr_stats->sw);
2390e705c121SKalle Valo 	if (isr_stats->sw || isr_stats->hw) {
2391e705c121SKalle Valo 		pos += scnprintf(buf + pos, bufsz - pos,
2392e705c121SKalle Valo 			"\tLast Restarting Code:  0x%X\n",
2393e705c121SKalle Valo 			isr_stats->err_code);
2394e705c121SKalle Valo 	}
2395e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUG
2396e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
2397e705c121SKalle Valo 		isr_stats->sch);
2398e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
2399e705c121SKalle Valo 		isr_stats->alive);
2400e705c121SKalle Valo #endif
2401e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos,
2402e705c121SKalle Valo 		"HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
2403e705c121SKalle Valo 
2404e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
2405e705c121SKalle Valo 		isr_stats->ctkill);
2406e705c121SKalle Valo 
2407e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
2408e705c121SKalle Valo 		isr_stats->wakeup);
2409e705c121SKalle Valo 
2410e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos,
2411e705c121SKalle Valo 		"Rx command responses:\t\t %u\n", isr_stats->rx);
2412e705c121SKalle Valo 
2413e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
2414e705c121SKalle Valo 		isr_stats->tx);
2415e705c121SKalle Valo 
2416e705c121SKalle Valo 	pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
2417e705c121SKalle Valo 		isr_stats->unhandled);
2418e705c121SKalle Valo 
2419e705c121SKalle Valo 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2420e705c121SKalle Valo 	kfree(buf);
2421e705c121SKalle Valo 	return ret;
2422e705c121SKalle Valo }
2423e705c121SKalle Valo 
2424e705c121SKalle Valo static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
2425e705c121SKalle Valo 					 const char __user *user_buf,
2426e705c121SKalle Valo 					 size_t count, loff_t *ppos)
2427e705c121SKalle Valo {
2428e705c121SKalle Valo 	struct iwl_trans *trans = file->private_data;
2429e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2430e705c121SKalle Valo 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
2431e705c121SKalle Valo 	u32 reset_flag;
2432078f1131SJohannes Berg 	int ret;
2433e705c121SKalle Valo 
2434078f1131SJohannes Berg 	ret = kstrtou32_from_user(user_buf, count, 16, &reset_flag);
2435078f1131SJohannes Berg 	if (ret)
2436078f1131SJohannes Berg 		return ret;
2437e705c121SKalle Valo 	if (reset_flag == 0)
2438e705c121SKalle Valo 		memset(isr_stats, 0, sizeof(*isr_stats));
2439e705c121SKalle Valo 
2440e705c121SKalle Valo 	return count;
2441e705c121SKalle Valo }
2442e705c121SKalle Valo 
2443e705c121SKalle Valo static ssize_t iwl_dbgfs_csr_write(struct file *file,
2444e705c121SKalle Valo 				   const char __user *user_buf,
2445e705c121SKalle Valo 				   size_t count, loff_t *ppos)
2446e705c121SKalle Valo {
2447e705c121SKalle Valo 	struct iwl_trans *trans = file->private_data;
2448e705c121SKalle Valo 
2449e705c121SKalle Valo 	iwl_pcie_dump_csr(trans);
2450e705c121SKalle Valo 
2451e705c121SKalle Valo 	return count;
2452e705c121SKalle Valo }
2453e705c121SKalle Valo 
2454e705c121SKalle Valo static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
2455e705c121SKalle Valo 				     char __user *user_buf,
2456e705c121SKalle Valo 				     size_t count, loff_t *ppos)
2457e705c121SKalle Valo {
2458e705c121SKalle Valo 	struct iwl_trans *trans = file->private_data;
2459e705c121SKalle Valo 	char *buf = NULL;
2460e705c121SKalle Valo 	ssize_t ret;
2461e705c121SKalle Valo 
2462e705c121SKalle Valo 	ret = iwl_dump_fh(trans, &buf);
2463e705c121SKalle Valo 	if (ret < 0)
2464e705c121SKalle Valo 		return ret;
2465e705c121SKalle Valo 	if (!buf)
2466e705c121SKalle Valo 		return -EINVAL;
2467e705c121SKalle Valo 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2468e705c121SKalle Valo 	kfree(buf);
2469e705c121SKalle Valo 	return ret;
2470e705c121SKalle Valo }
2471e705c121SKalle Valo 
2472fa4de7f7SJohannes Berg static ssize_t iwl_dbgfs_rfkill_read(struct file *file,
2473fa4de7f7SJohannes Berg 				     char __user *user_buf,
2474fa4de7f7SJohannes Berg 				     size_t count, loff_t *ppos)
2475fa4de7f7SJohannes Berg {
2476fa4de7f7SJohannes Berg 	struct iwl_trans *trans = file->private_data;
2477fa4de7f7SJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2478fa4de7f7SJohannes Berg 	char buf[100];
2479fa4de7f7SJohannes Berg 	int pos;
2480fa4de7f7SJohannes Berg 
2481fa4de7f7SJohannes Berg 	pos = scnprintf(buf, sizeof(buf), "debug: %d\nhw: %d\n",
2482fa4de7f7SJohannes Berg 			trans_pcie->debug_rfkill,
2483fa4de7f7SJohannes Berg 			!(iwl_read32(trans, CSR_GP_CNTRL) &
2484fa4de7f7SJohannes Berg 				CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW));
2485fa4de7f7SJohannes Berg 
2486fa4de7f7SJohannes Berg 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
2487fa4de7f7SJohannes Berg }
2488fa4de7f7SJohannes Berg 
2489fa4de7f7SJohannes Berg static ssize_t iwl_dbgfs_rfkill_write(struct file *file,
2490fa4de7f7SJohannes Berg 				      const char __user *user_buf,
2491fa4de7f7SJohannes Berg 				      size_t count, loff_t *ppos)
2492fa4de7f7SJohannes Berg {
2493fa4de7f7SJohannes Berg 	struct iwl_trans *trans = file->private_data;
2494fa4de7f7SJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2495fa4de7f7SJohannes Berg 	bool old = trans_pcie->debug_rfkill;
2496fa4de7f7SJohannes Berg 	int ret;
2497fa4de7f7SJohannes Berg 
2498fa4de7f7SJohannes Berg 	ret = kstrtobool_from_user(user_buf, count, &trans_pcie->debug_rfkill);
2499fa4de7f7SJohannes Berg 	if (ret)
2500fa4de7f7SJohannes Berg 		return ret;
2501fa4de7f7SJohannes Berg 	if (old == trans_pcie->debug_rfkill)
2502fa4de7f7SJohannes Berg 		return count;
2503fa4de7f7SJohannes Berg 	IWL_WARN(trans, "changing debug rfkill %d->%d\n",
2504fa4de7f7SJohannes Berg 		 old, trans_pcie->debug_rfkill);
2505fa4de7f7SJohannes Berg 	iwl_pcie_handle_rfkill_irq(trans);
2506fa4de7f7SJohannes Berg 
2507fa4de7f7SJohannes Berg 	return count;
2508fa4de7f7SJohannes Berg }
2509fa4de7f7SJohannes Berg 
2510e705c121SKalle Valo DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
2511e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(fh_reg);
2512e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(rx_queue);
2513e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(tx_queue);
2514e705c121SKalle Valo DEBUGFS_WRITE_FILE_OPS(csr);
2515fa4de7f7SJohannes Berg DEBUGFS_READ_WRITE_FILE_OPS(rfkill);
2516e705c121SKalle Valo 
2517f8a1edb7SJohannes Berg /* Create the debugfs files and directories */
2518f8a1edb7SJohannes Berg int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans)
2519e705c121SKalle Valo {
2520f8a1edb7SJohannes Berg 	struct dentry *dir = trans->dbgfs_dir;
2521f8a1edb7SJohannes Berg 
2522e705c121SKalle Valo 	DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
2523e705c121SKalle Valo 	DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
2524e705c121SKalle Valo 	DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
2525e705c121SKalle Valo 	DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
2526e705c121SKalle Valo 	DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
2527fa4de7f7SJohannes Berg 	DEBUGFS_ADD_FILE(rfkill, dir, S_IWUSR | S_IRUSR);
2528e705c121SKalle Valo 	return 0;
2529e705c121SKalle Valo 
2530e705c121SKalle Valo err:
2531e705c121SKalle Valo 	IWL_ERR(trans, "failed to create the trans debugfs entry\n");
2532e705c121SKalle Valo 	return -ENOMEM;
2533e705c121SKalle Valo }
2534e705c121SKalle Valo #endif /*CONFIG_IWLWIFI_DEBUGFS */
2535e705c121SKalle Valo 
25366983ba69SSara Sharon static u32 iwl_trans_pcie_get_cmdlen(struct iwl_trans *trans, void *tfd)
2537e705c121SKalle Valo {
25383cd1980bSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2539e705c121SKalle Valo 	u32 cmdlen = 0;
2540e705c121SKalle Valo 	int i;
2541e705c121SKalle Valo 
25423cd1980bSSara Sharon 	for (i = 0; i < trans_pcie->max_tbs; i++)
25436983ba69SSara Sharon 		cmdlen += iwl_pcie_tfd_tb_get_len(trans, tfd, i);
2544e705c121SKalle Valo 
2545e705c121SKalle Valo 	return cmdlen;
2546e705c121SKalle Valo }
2547e705c121SKalle Valo 
2548e705c121SKalle Valo static u32 iwl_trans_pcie_dump_rbs(struct iwl_trans *trans,
2549e705c121SKalle Valo 				   struct iwl_fw_error_dump_data **data,
2550e705c121SKalle Valo 				   int allocated_rb_nums)
2551e705c121SKalle Valo {
2552e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2553e705c121SKalle Valo 	int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
255478485054SSara Sharon 	/* Dump RBs is supported only for pre-9000 devices (1 queue) */
255578485054SSara Sharon 	struct iwl_rxq *rxq = &trans_pcie->rxq[0];
2556e705c121SKalle Valo 	u32 i, r, j, rb_len = 0;
2557e705c121SKalle Valo 
2558e705c121SKalle Valo 	spin_lock(&rxq->lock);
2559e705c121SKalle Valo 
2560e705c121SKalle Valo 	r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
2561e705c121SKalle Valo 
2562e705c121SKalle Valo 	for (i = rxq->read, j = 0;
2563e705c121SKalle Valo 	     i != r && j < allocated_rb_nums;
2564e705c121SKalle Valo 	     i = (i + 1) & RX_QUEUE_MASK, j++) {
2565e705c121SKalle Valo 		struct iwl_rx_mem_buffer *rxb = rxq->queue[i];
2566e705c121SKalle Valo 		struct iwl_fw_error_dump_rb *rb;
2567e705c121SKalle Valo 
2568e705c121SKalle Valo 		dma_unmap_page(trans->dev, rxb->page_dma, max_len,
2569e705c121SKalle Valo 			       DMA_FROM_DEVICE);
2570e705c121SKalle Valo 
2571e705c121SKalle Valo 		rb_len += sizeof(**data) + sizeof(*rb) + max_len;
2572e705c121SKalle Valo 
2573e705c121SKalle Valo 		(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RB);
2574e705c121SKalle Valo 		(*data)->len = cpu_to_le32(sizeof(*rb) + max_len);
2575e705c121SKalle Valo 		rb = (void *)(*data)->data;
2576e705c121SKalle Valo 		rb->index = cpu_to_le32(i);
2577e705c121SKalle Valo 		memcpy(rb->data, page_address(rxb->page), max_len);
2578e705c121SKalle Valo 		/* remap the page for the free benefit */
2579e705c121SKalle Valo 		rxb->page_dma = dma_map_page(trans->dev, rxb->page, 0,
2580e705c121SKalle Valo 						     max_len,
2581e705c121SKalle Valo 						     DMA_FROM_DEVICE);
2582e705c121SKalle Valo 
2583e705c121SKalle Valo 		*data = iwl_fw_error_next_data(*data);
2584e705c121SKalle Valo 	}
2585e705c121SKalle Valo 
2586e705c121SKalle Valo 	spin_unlock(&rxq->lock);
2587e705c121SKalle Valo 
2588e705c121SKalle Valo 	return rb_len;
2589e705c121SKalle Valo }
2590e705c121SKalle Valo #define IWL_CSR_TO_DUMP (0x250)
2591e705c121SKalle Valo 
2592e705c121SKalle Valo static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans,
2593e705c121SKalle Valo 				   struct iwl_fw_error_dump_data **data)
2594e705c121SKalle Valo {
2595e705c121SKalle Valo 	u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP;
2596e705c121SKalle Valo 	__le32 *val;
2597e705c121SKalle Valo 	int i;
2598e705c121SKalle Valo 
2599e705c121SKalle Valo 	(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR);
2600e705c121SKalle Valo 	(*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP);
2601e705c121SKalle Valo 	val = (void *)(*data)->data;
2602e705c121SKalle Valo 
2603e705c121SKalle Valo 	for (i = 0; i < IWL_CSR_TO_DUMP; i += 4)
2604e705c121SKalle Valo 		*val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
2605e705c121SKalle Valo 
2606e705c121SKalle Valo 	*data = iwl_fw_error_next_data(*data);
2607e705c121SKalle Valo 
2608e705c121SKalle Valo 	return csr_len;
2609e705c121SKalle Valo }
2610e705c121SKalle Valo 
2611e705c121SKalle Valo static u32 iwl_trans_pcie_fh_regs_dump(struct iwl_trans *trans,
2612e705c121SKalle Valo 				       struct iwl_fw_error_dump_data **data)
2613e705c121SKalle Valo {
2614e705c121SKalle Valo 	u32 fh_regs_len = FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND;
2615e705c121SKalle Valo 	unsigned long flags;
2616e705c121SKalle Valo 	__le32 *val;
2617e705c121SKalle Valo 	int i;
2618e705c121SKalle Valo 
261923ba9340SEmmanuel Grumbach 	if (!iwl_trans_grab_nic_access(trans, &flags))
2620e705c121SKalle Valo 		return 0;
2621e705c121SKalle Valo 
2622e705c121SKalle Valo 	(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FH_REGS);
2623e705c121SKalle Valo 	(*data)->len = cpu_to_le32(fh_regs_len);
2624e705c121SKalle Valo 	val = (void *)(*data)->data;
2625e705c121SKalle Valo 
2626723b45e2SLiad Kaufman 	if (!trans->cfg->gen2)
2627723b45e2SLiad Kaufman 		for (i = FH_MEM_LOWER_BOUND; i < FH_MEM_UPPER_BOUND;
2628723b45e2SLiad Kaufman 		     i += sizeof(u32))
2629e705c121SKalle Valo 			*val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
2630723b45e2SLiad Kaufman 	else
2631723b45e2SLiad Kaufman 		for (i = FH_MEM_LOWER_BOUND_GEN2; i < FH_MEM_UPPER_BOUND_GEN2;
2632723b45e2SLiad Kaufman 		     i += sizeof(u32))
2633723b45e2SLiad Kaufman 			*val++ = cpu_to_le32(iwl_trans_pcie_read_prph(trans,
2634723b45e2SLiad Kaufman 								      i));
2635e705c121SKalle Valo 
2636e705c121SKalle Valo 	iwl_trans_release_nic_access(trans, &flags);
2637e705c121SKalle Valo 
2638e705c121SKalle Valo 	*data = iwl_fw_error_next_data(*data);
2639e705c121SKalle Valo 
2640e705c121SKalle Valo 	return sizeof(**data) + fh_regs_len;
2641e705c121SKalle Valo }
2642e705c121SKalle Valo 
2643e705c121SKalle Valo static u32
2644e705c121SKalle Valo iwl_trans_pci_dump_marbh_monitor(struct iwl_trans *trans,
2645e705c121SKalle Valo 				 struct iwl_fw_error_dump_fw_mon *fw_mon_data,
2646e705c121SKalle Valo 				 u32 monitor_len)
2647e705c121SKalle Valo {
2648e705c121SKalle Valo 	u32 buf_size_in_dwords = (monitor_len >> 2);
2649e705c121SKalle Valo 	u32 *buffer = (u32 *)fw_mon_data->data;
2650e705c121SKalle Valo 	unsigned long flags;
2651e705c121SKalle Valo 	u32 i;
2652e705c121SKalle Valo 
265323ba9340SEmmanuel Grumbach 	if (!iwl_trans_grab_nic_access(trans, &flags))
2654e705c121SKalle Valo 		return 0;
2655e705c121SKalle Valo 
265614ef1b43SGolan Ben-Ami 	iwl_write_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x1);
2657e705c121SKalle Valo 	for (i = 0; i < buf_size_in_dwords; i++)
265814ef1b43SGolan Ben-Ami 		buffer[i] = iwl_read_prph_no_grab(trans,
265914ef1b43SGolan Ben-Ami 				MON_DMARB_RD_DATA_ADDR);
266014ef1b43SGolan Ben-Ami 	iwl_write_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x0);
2661e705c121SKalle Valo 
2662e705c121SKalle Valo 	iwl_trans_release_nic_access(trans, &flags);
2663e705c121SKalle Valo 
2664e705c121SKalle Valo 	return monitor_len;
2665e705c121SKalle Valo }
2666e705c121SKalle Valo 
2667e705c121SKalle Valo static u32
2668e705c121SKalle Valo iwl_trans_pcie_dump_monitor(struct iwl_trans *trans,
2669e705c121SKalle Valo 			    struct iwl_fw_error_dump_data **data,
2670e705c121SKalle Valo 			    u32 monitor_len)
2671e705c121SKalle Valo {
2672e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2673e705c121SKalle Valo 	u32 len = 0;
2674e705c121SKalle Valo 
2675e705c121SKalle Valo 	if ((trans_pcie->fw_mon_page &&
2676e705c121SKalle Valo 	     trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) ||
2677e705c121SKalle Valo 	    trans->dbg_dest_tlv) {
2678e705c121SKalle Valo 		struct iwl_fw_error_dump_fw_mon *fw_mon_data;
2679e705c121SKalle Valo 		u32 base, write_ptr, wrap_cnt;
2680e705c121SKalle Valo 
2681e705c121SKalle Valo 		/* If there was a dest TLV - use the values from there */
2682e705c121SKalle Valo 		if (trans->dbg_dest_tlv) {
2683e705c121SKalle Valo 			write_ptr =
2684e705c121SKalle Valo 				le32_to_cpu(trans->dbg_dest_tlv->write_ptr_reg);
2685e705c121SKalle Valo 			wrap_cnt = le32_to_cpu(trans->dbg_dest_tlv->wrap_count);
2686e705c121SKalle Valo 			base = le32_to_cpu(trans->dbg_dest_tlv->base_reg);
2687e705c121SKalle Valo 		} else {
2688e705c121SKalle Valo 			base = MON_BUFF_BASE_ADDR;
2689e705c121SKalle Valo 			write_ptr = MON_BUFF_WRPTR;
2690e705c121SKalle Valo 			wrap_cnt = MON_BUFF_CYCLE_CNT;
2691e705c121SKalle Valo 		}
2692e705c121SKalle Valo 
2693e705c121SKalle Valo 		(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR);
2694e705c121SKalle Valo 		fw_mon_data = (void *)(*data)->data;
2695e705c121SKalle Valo 		fw_mon_data->fw_mon_wr_ptr =
2696e705c121SKalle Valo 			cpu_to_le32(iwl_read_prph(trans, write_ptr));
2697e705c121SKalle Valo 		fw_mon_data->fw_mon_cycle_cnt =
2698e705c121SKalle Valo 			cpu_to_le32(iwl_read_prph(trans, wrap_cnt));
2699e705c121SKalle Valo 		fw_mon_data->fw_mon_base_ptr =
2700e705c121SKalle Valo 			cpu_to_le32(iwl_read_prph(trans, base));
2701e705c121SKalle Valo 
2702e705c121SKalle Valo 		len += sizeof(**data) + sizeof(*fw_mon_data);
2703e705c121SKalle Valo 		if (trans_pcie->fw_mon_page) {
2704e705c121SKalle Valo 			/*
2705e705c121SKalle Valo 			 * The firmware is now asserted, it won't write anything
2706e705c121SKalle Valo 			 * to the buffer. CPU can take ownership to fetch the
2707e705c121SKalle Valo 			 * data. The buffer will be handed back to the device
2708e705c121SKalle Valo 			 * before the firmware will be restarted.
2709e705c121SKalle Valo 			 */
2710e705c121SKalle Valo 			dma_sync_single_for_cpu(trans->dev,
2711e705c121SKalle Valo 						trans_pcie->fw_mon_phys,
2712e705c121SKalle Valo 						trans_pcie->fw_mon_size,
2713e705c121SKalle Valo 						DMA_FROM_DEVICE);
2714e705c121SKalle Valo 			memcpy(fw_mon_data->data,
2715e705c121SKalle Valo 			       page_address(trans_pcie->fw_mon_page),
2716e705c121SKalle Valo 			       trans_pcie->fw_mon_size);
2717e705c121SKalle Valo 
2718e705c121SKalle Valo 			monitor_len = trans_pcie->fw_mon_size;
2719e705c121SKalle Valo 		} else if (trans->dbg_dest_tlv->monitor_mode == SMEM_MODE) {
2720e705c121SKalle Valo 			/*
2721e705c121SKalle Valo 			 * Update pointers to reflect actual values after
2722e705c121SKalle Valo 			 * shifting
2723e705c121SKalle Valo 			 */
2724e705c121SKalle Valo 			base = iwl_read_prph(trans, base) <<
2725e705c121SKalle Valo 			       trans->dbg_dest_tlv->base_shift;
2726e705c121SKalle Valo 			iwl_trans_read_mem(trans, base, fw_mon_data->data,
2727e705c121SKalle Valo 					   monitor_len / sizeof(u32));
2728e705c121SKalle Valo 		} else if (trans->dbg_dest_tlv->monitor_mode == MARBH_MODE) {
2729e705c121SKalle Valo 			monitor_len =
2730e705c121SKalle Valo 				iwl_trans_pci_dump_marbh_monitor(trans,
2731e705c121SKalle Valo 								 fw_mon_data,
2732e705c121SKalle Valo 								 monitor_len);
2733e705c121SKalle Valo 		} else {
2734e705c121SKalle Valo 			/* Didn't match anything - output no monitor data */
2735e705c121SKalle Valo 			monitor_len = 0;
2736e705c121SKalle Valo 		}
2737e705c121SKalle Valo 
2738e705c121SKalle Valo 		len += monitor_len;
2739e705c121SKalle Valo 		(*data)->len = cpu_to_le32(monitor_len + sizeof(*fw_mon_data));
2740e705c121SKalle Valo 	}
2741e705c121SKalle Valo 
2742e705c121SKalle Valo 	return len;
2743e705c121SKalle Valo }
2744e705c121SKalle Valo 
2745e705c121SKalle Valo static struct iwl_trans_dump_data
2746e705c121SKalle Valo *iwl_trans_pcie_dump_data(struct iwl_trans *trans,
2747a80c7a69SEmmanuel Grumbach 			  const struct iwl_fw_dbg_trigger_tlv *trigger)
2748e705c121SKalle Valo {
2749e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2750e705c121SKalle Valo 	struct iwl_fw_error_dump_data *data;
2751b2a3b1c1SSara Sharon 	struct iwl_txq *cmdq = trans_pcie->txq[trans_pcie->cmd_queue];
2752e705c121SKalle Valo 	struct iwl_fw_error_dump_txcmd *txcmd;
2753e705c121SKalle Valo 	struct iwl_trans_dump_data *dump_data;
2754e705c121SKalle Valo 	u32 len, num_rbs;
2755e705c121SKalle Valo 	u32 monitor_len;
2756e705c121SKalle Valo 	int i, ptr;
275796a6497bSSara Sharon 	bool dump_rbs = test_bit(STATUS_FW_ERROR, &trans->status) &&
275896a6497bSSara Sharon 			!trans->cfg->mq_rx_supported;
2759e705c121SKalle Valo 
2760e705c121SKalle Valo 	/* transport dump header */
2761e705c121SKalle Valo 	len = sizeof(*dump_data);
2762e705c121SKalle Valo 
2763e705c121SKalle Valo 	/* host commands */
2764e705c121SKalle Valo 	len += sizeof(*data) +
2765bb98ecd4SSara Sharon 		cmdq->n_window * (sizeof(*txcmd) + TFD_MAX_PAYLOAD_SIZE);
2766e705c121SKalle Valo 
2767e705c121SKalle Valo 	/* FW monitor */
2768e705c121SKalle Valo 	if (trans_pcie->fw_mon_page) {
2769e705c121SKalle Valo 		len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
2770e705c121SKalle Valo 		       trans_pcie->fw_mon_size;
2771e705c121SKalle Valo 		monitor_len = trans_pcie->fw_mon_size;
2772e705c121SKalle Valo 	} else if (trans->dbg_dest_tlv) {
2773e705c121SKalle Valo 		u32 base, end;
2774e705c121SKalle Valo 
2775e705c121SKalle Valo 		base = le32_to_cpu(trans->dbg_dest_tlv->base_reg);
2776e705c121SKalle Valo 		end = le32_to_cpu(trans->dbg_dest_tlv->end_reg);
2777e705c121SKalle Valo 
2778e705c121SKalle Valo 		base = iwl_read_prph(trans, base) <<
2779e705c121SKalle Valo 		       trans->dbg_dest_tlv->base_shift;
2780e705c121SKalle Valo 		end = iwl_read_prph(trans, end) <<
2781e705c121SKalle Valo 		      trans->dbg_dest_tlv->end_shift;
2782e705c121SKalle Valo 
2783e705c121SKalle Valo 		/* Make "end" point to the actual end */
27846e584873SSara Sharon 		if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000 ||
2785e705c121SKalle Valo 		    trans->dbg_dest_tlv->monitor_mode == MARBH_MODE)
2786e705c121SKalle Valo 			end += (1 << trans->dbg_dest_tlv->end_shift);
2787e705c121SKalle Valo 		monitor_len = end - base;
2788e705c121SKalle Valo 		len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
2789e705c121SKalle Valo 		       monitor_len;
2790e705c121SKalle Valo 	} else {
2791e705c121SKalle Valo 		monitor_len = 0;
2792e705c121SKalle Valo 	}
2793e705c121SKalle Valo 
2794e705c121SKalle Valo 	if (trigger && (trigger->mode & IWL_FW_DBG_TRIGGER_MONITOR_ONLY)) {
2795e705c121SKalle Valo 		dump_data = vzalloc(len);
2796e705c121SKalle Valo 		if (!dump_data)
2797e705c121SKalle Valo 			return NULL;
2798e705c121SKalle Valo 
2799e705c121SKalle Valo 		data = (void *)dump_data->data;
2800e705c121SKalle Valo 		len = iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
2801e705c121SKalle Valo 		dump_data->len = len;
2802e705c121SKalle Valo 
2803e705c121SKalle Valo 		return dump_data;
2804e705c121SKalle Valo 	}
2805e705c121SKalle Valo 
2806e705c121SKalle Valo 	/* CSR registers */
2807e705c121SKalle Valo 	len += sizeof(*data) + IWL_CSR_TO_DUMP;
2808e705c121SKalle Valo 
2809e705c121SKalle Valo 	/* FH registers */
2810723b45e2SLiad Kaufman 	if (trans->cfg->gen2)
2811723b45e2SLiad Kaufman 		len += sizeof(*data) +
2812723b45e2SLiad Kaufman 		       (FH_MEM_UPPER_BOUND_GEN2 - FH_MEM_LOWER_BOUND_GEN2);
2813723b45e2SLiad Kaufman 	else
2814723b45e2SLiad Kaufman 		len += sizeof(*data) +
2815723b45e2SLiad Kaufman 		       (FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND);
2816e705c121SKalle Valo 
2817e705c121SKalle Valo 	if (dump_rbs) {
281878485054SSara Sharon 		/* Dump RBs is supported only for pre-9000 devices (1 queue) */
281978485054SSara Sharon 		struct iwl_rxq *rxq = &trans_pcie->rxq[0];
2820e705c121SKalle Valo 		/* RBs */
282178485054SSara Sharon 		num_rbs = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num))
2822e705c121SKalle Valo 				      & 0x0FFF;
282378485054SSara Sharon 		num_rbs = (num_rbs - rxq->read) & RX_QUEUE_MASK;
2824e705c121SKalle Valo 		len += num_rbs * (sizeof(*data) +
2825e705c121SKalle Valo 				  sizeof(struct iwl_fw_error_dump_rb) +
2826e705c121SKalle Valo 				  (PAGE_SIZE << trans_pcie->rx_page_order));
2827e705c121SKalle Valo 	}
2828e705c121SKalle Valo 
28295538409bSLiad Kaufman 	/* Paged memory for gen2 HW */
28305538409bSLiad Kaufman 	if (trans->cfg->gen2)
28315538409bSLiad Kaufman 		for (i = 0; i < trans_pcie->init_dram.paging_cnt; i++)
28325538409bSLiad Kaufman 			len += sizeof(*data) +
28335538409bSLiad Kaufman 			       sizeof(struct iwl_fw_error_dump_paging) +
28345538409bSLiad Kaufman 			       trans_pcie->init_dram.paging[i].size;
28355538409bSLiad Kaufman 
2836e705c121SKalle Valo 	dump_data = vzalloc(len);
2837e705c121SKalle Valo 	if (!dump_data)
2838e705c121SKalle Valo 		return NULL;
2839e705c121SKalle Valo 
2840e705c121SKalle Valo 	len = 0;
2841e705c121SKalle Valo 	data = (void *)dump_data->data;
2842e705c121SKalle Valo 	data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
2843e705c121SKalle Valo 	txcmd = (void *)data->data;
2844e705c121SKalle Valo 	spin_lock_bh(&cmdq->lock);
2845bb98ecd4SSara Sharon 	ptr = cmdq->write_ptr;
2846bb98ecd4SSara Sharon 	for (i = 0; i < cmdq->n_window; i++) {
2847bb98ecd4SSara Sharon 		u8 idx = get_cmd_index(cmdq, ptr);
2848e705c121SKalle Valo 		u32 caplen, cmdlen;
2849e705c121SKalle Valo 
28506983ba69SSara Sharon 		cmdlen = iwl_trans_pcie_get_cmdlen(trans, cmdq->tfds +
28516983ba69SSara Sharon 						   trans_pcie->tfd_size * ptr);
2852e705c121SKalle Valo 		caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
2853e705c121SKalle Valo 
2854e705c121SKalle Valo 		if (cmdlen) {
2855e705c121SKalle Valo 			len += sizeof(*txcmd) + caplen;
2856e705c121SKalle Valo 			txcmd->cmdlen = cpu_to_le32(cmdlen);
2857e705c121SKalle Valo 			txcmd->caplen = cpu_to_le32(caplen);
2858e705c121SKalle Valo 			memcpy(txcmd->data, cmdq->entries[idx].cmd, caplen);
2859e705c121SKalle Valo 			txcmd = (void *)((u8 *)txcmd->data + caplen);
2860e705c121SKalle Valo 		}
2861e705c121SKalle Valo 
2862e705c121SKalle Valo 		ptr = iwl_queue_dec_wrap(ptr);
2863e705c121SKalle Valo 	}
2864e705c121SKalle Valo 	spin_unlock_bh(&cmdq->lock);
2865e705c121SKalle Valo 
2866e705c121SKalle Valo 	data->len = cpu_to_le32(len);
2867e705c121SKalle Valo 	len += sizeof(*data);
2868e705c121SKalle Valo 	data = iwl_fw_error_next_data(data);
2869e705c121SKalle Valo 
2870e705c121SKalle Valo 	len += iwl_trans_pcie_dump_csr(trans, &data);
2871e705c121SKalle Valo 	len += iwl_trans_pcie_fh_regs_dump(trans, &data);
2872e705c121SKalle Valo 	if (dump_rbs)
2873e705c121SKalle Valo 		len += iwl_trans_pcie_dump_rbs(trans, &data, num_rbs);
2874e705c121SKalle Valo 
28755538409bSLiad Kaufman 	/* Paged memory for gen2 HW */
28765538409bSLiad Kaufman 	if (trans->cfg->gen2) {
28775538409bSLiad Kaufman 		for (i = 0; i < trans_pcie->init_dram.paging_cnt; i++) {
28785538409bSLiad Kaufman 			struct iwl_fw_error_dump_paging *paging;
28795538409bSLiad Kaufman 			dma_addr_t addr =
28805538409bSLiad Kaufman 				trans_pcie->init_dram.paging[i].physical;
28815538409bSLiad Kaufman 			u32 page_len = trans_pcie->init_dram.paging[i].size;
28825538409bSLiad Kaufman 
28835538409bSLiad Kaufman 			data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PAGING);
28845538409bSLiad Kaufman 			data->len = cpu_to_le32(sizeof(*paging) + page_len);
28855538409bSLiad Kaufman 			paging = (void *)data->data;
28865538409bSLiad Kaufman 			paging->index = cpu_to_le32(i);
28875538409bSLiad Kaufman 			dma_sync_single_for_cpu(trans->dev, addr, page_len,
28885538409bSLiad Kaufman 						DMA_BIDIRECTIONAL);
28895538409bSLiad Kaufman 			memcpy(paging->data,
28905538409bSLiad Kaufman 			       trans_pcie->init_dram.paging[i].block, page_len);
28915538409bSLiad Kaufman 			data = iwl_fw_error_next_data(data);
28925538409bSLiad Kaufman 
28935538409bSLiad Kaufman 			len += sizeof(*data) + sizeof(*paging) + page_len;
28945538409bSLiad Kaufman 		}
28955538409bSLiad Kaufman 	}
28965538409bSLiad Kaufman 
2897e705c121SKalle Valo 	len += iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
2898e705c121SKalle Valo 
2899e705c121SKalle Valo 	dump_data->len = len;
2900e705c121SKalle Valo 
2901e705c121SKalle Valo 	return dump_data;
2902e705c121SKalle Valo }
2903e705c121SKalle Valo 
29044cbb8e50SLuciano Coelho #ifdef CONFIG_PM_SLEEP
29054cbb8e50SLuciano Coelho static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
29064cbb8e50SLuciano Coelho {
2907e4c49c49SLuca Coelho 	if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3 &&
2908e4c49c49SLuca Coelho 	    (trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3))
29094cbb8e50SLuciano Coelho 		return iwl_pci_fw_enter_d0i3(trans);
29104cbb8e50SLuciano Coelho 
29114cbb8e50SLuciano Coelho 	return 0;
29124cbb8e50SLuciano Coelho }
29134cbb8e50SLuciano Coelho 
29144cbb8e50SLuciano Coelho static void iwl_trans_pcie_resume(struct iwl_trans *trans)
29154cbb8e50SLuciano Coelho {
2916e4c49c49SLuca Coelho 	if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3 &&
2917e4c49c49SLuca Coelho 	    (trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3))
29184cbb8e50SLuciano Coelho 		iwl_pci_fw_exit_d0i3(trans);
29194cbb8e50SLuciano Coelho }
29204cbb8e50SLuciano Coelho #endif /* CONFIG_PM_SLEEP */
29214cbb8e50SLuciano Coelho 
2922623e7766SSara Sharon #define IWL_TRANS_COMMON_OPS						\
2923623e7766SSara Sharon 	.op_mode_leave = iwl_trans_pcie_op_mode_leave,			\
2924623e7766SSara Sharon 	.write8 = iwl_trans_pcie_write8,				\
2925623e7766SSara Sharon 	.write32 = iwl_trans_pcie_write32,				\
2926623e7766SSara Sharon 	.read32 = iwl_trans_pcie_read32,				\
2927623e7766SSara Sharon 	.read_prph = iwl_trans_pcie_read_prph,				\
2928623e7766SSara Sharon 	.write_prph = iwl_trans_pcie_write_prph,			\
2929623e7766SSara Sharon 	.read_mem = iwl_trans_pcie_read_mem,				\
2930623e7766SSara Sharon 	.write_mem = iwl_trans_pcie_write_mem,				\
2931623e7766SSara Sharon 	.configure = iwl_trans_pcie_configure,				\
2932623e7766SSara Sharon 	.set_pmi = iwl_trans_pcie_set_pmi,				\
2933623e7766SSara Sharon 	.grab_nic_access = iwl_trans_pcie_grab_nic_access,		\
2934623e7766SSara Sharon 	.release_nic_access = iwl_trans_pcie_release_nic_access,	\
2935623e7766SSara Sharon 	.set_bits_mask = iwl_trans_pcie_set_bits_mask,			\
2936623e7766SSara Sharon 	.ref = iwl_trans_pcie_ref,					\
2937623e7766SSara Sharon 	.unref = iwl_trans_pcie_unref,					\
2938623e7766SSara Sharon 	.dump_data = iwl_trans_pcie_dump_data,				\
2939623e7766SSara Sharon 	.d3_suspend = iwl_trans_pcie_d3_suspend,			\
2940623e7766SSara Sharon 	.d3_resume = iwl_trans_pcie_d3_resume
2941623e7766SSara Sharon 
2942623e7766SSara Sharon #ifdef CONFIG_PM_SLEEP
2943623e7766SSara Sharon #define IWL_TRANS_PM_OPS						\
2944623e7766SSara Sharon 	.suspend = iwl_trans_pcie_suspend,				\
2945623e7766SSara Sharon 	.resume = iwl_trans_pcie_resume,
2946623e7766SSara Sharon #else
2947623e7766SSara Sharon #define IWL_TRANS_PM_OPS
2948623e7766SSara Sharon #endif /* CONFIG_PM_SLEEP */
2949623e7766SSara Sharon 
2950e705c121SKalle Valo static const struct iwl_trans_ops trans_ops_pcie = {
2951623e7766SSara Sharon 	IWL_TRANS_COMMON_OPS,
2952623e7766SSara Sharon 	IWL_TRANS_PM_OPS
2953e705c121SKalle Valo 	.start_hw = iwl_trans_pcie_start_hw,
2954e705c121SKalle Valo 	.fw_alive = iwl_trans_pcie_fw_alive,
2955e705c121SKalle Valo 	.start_fw = iwl_trans_pcie_start_fw,
2956e705c121SKalle Valo 	.stop_device = iwl_trans_pcie_stop_device,
2957e705c121SKalle Valo 
2958e705c121SKalle Valo 	.send_cmd = iwl_trans_pcie_send_hcmd,
2959e705c121SKalle Valo 
2960e705c121SKalle Valo 	.tx = iwl_trans_pcie_tx,
2961e705c121SKalle Valo 	.reclaim = iwl_trans_pcie_reclaim,
2962e705c121SKalle Valo 
2963e705c121SKalle Valo 	.txq_disable = iwl_trans_pcie_txq_disable,
2964e705c121SKalle Valo 	.txq_enable = iwl_trans_pcie_txq_enable,
2965e705c121SKalle Valo 
296642db09c1SLiad Kaufman 	.txq_set_shared_mode = iwl_trans_pcie_txq_set_shared_mode,
296742db09c1SLiad Kaufman 
2968d6d517b7SSara Sharon 	.wait_tx_queues_empty = iwl_trans_pcie_wait_txqs_empty,
2969d6d517b7SSara Sharon 
2970e705c121SKalle Valo 	.freeze_txq_timer = iwl_trans_pcie_freeze_txq_timer,
29710cd58eaaSEmmanuel Grumbach 	.block_txq_ptrs = iwl_trans_pcie_block_txq_ptrs,
2972623e7766SSara Sharon };
2973e705c121SKalle Valo 
2974623e7766SSara Sharon static const struct iwl_trans_ops trans_ops_pcie_gen2 = {
2975623e7766SSara Sharon 	IWL_TRANS_COMMON_OPS,
2976623e7766SSara Sharon 	IWL_TRANS_PM_OPS
2977623e7766SSara Sharon 	.start_hw = iwl_trans_pcie_start_hw,
2978eda50cdeSSara Sharon 	.fw_alive = iwl_trans_pcie_gen2_fw_alive,
2979eda50cdeSSara Sharon 	.start_fw = iwl_trans_pcie_gen2_start_fw,
298077c09bc8SSara Sharon 	.stop_device = iwl_trans_pcie_gen2_stop_device,
2981e705c121SKalle Valo 
2982ca60da2eSSara Sharon 	.send_cmd = iwl_trans_pcie_gen2_send_hcmd,
2983e705c121SKalle Valo 
2984ab6c6445SSara Sharon 	.tx = iwl_trans_pcie_gen2_tx,
2985623e7766SSara Sharon 	.reclaim = iwl_trans_pcie_reclaim,
2986623e7766SSara Sharon 
29876b35ff91SSara Sharon 	.txq_alloc = iwl_trans_pcie_dyn_txq_alloc,
29886b35ff91SSara Sharon 	.txq_free = iwl_trans_pcie_dyn_txq_free,
2989d6d517b7SSara Sharon 	.wait_txq_empty = iwl_trans_pcie_wait_txq_empty,
2990e705c121SKalle Valo };
2991e705c121SKalle Valo 
2992e705c121SKalle Valo struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
2993e705c121SKalle Valo 				       const struct pci_device_id *ent,
2994e705c121SKalle Valo 				       const struct iwl_cfg *cfg)
2995e705c121SKalle Valo {
2996e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie;
2997e705c121SKalle Valo 	struct iwl_trans *trans;
299896a6497bSSara Sharon 	int ret, addr_size;
2999e705c121SKalle Valo 
30005a41a86cSSharon Dvir 	ret = pcim_enable_device(pdev);
30015a41a86cSSharon Dvir 	if (ret)
30025a41a86cSSharon Dvir 		return ERR_PTR(ret);
30035a41a86cSSharon Dvir 
3004623e7766SSara Sharon 	if (cfg->gen2)
3005623e7766SSara Sharon 		trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie),
3006623e7766SSara Sharon 					&pdev->dev, cfg, &trans_ops_pcie_gen2);
3007623e7766SSara Sharon 	else
3008e705c121SKalle Valo 		trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie),
30091ea423b0SLuca Coelho 					&pdev->dev, cfg, &trans_ops_pcie);
3010e705c121SKalle Valo 	if (!trans)
3011e705c121SKalle Valo 		return ERR_PTR(-ENOMEM);
3012e705c121SKalle Valo 
3013e705c121SKalle Valo 	trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
3014e705c121SKalle Valo 
3015e705c121SKalle Valo 	trans_pcie->trans = trans;
3016326477e4SJohannes Berg 	trans_pcie->opmode_down = true;
3017e705c121SKalle Valo 	spin_lock_init(&trans_pcie->irq_lock);
3018e705c121SKalle Valo 	spin_lock_init(&trans_pcie->reg_lock);
3019e705c121SKalle Valo 	mutex_init(&trans_pcie->mutex);
3020e705c121SKalle Valo 	init_waitqueue_head(&trans_pcie->ucode_write_waitq);
30216eb5e529SEmmanuel Grumbach 	trans_pcie->tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
30226eb5e529SEmmanuel Grumbach 	if (!trans_pcie->tso_hdr_page) {
30236eb5e529SEmmanuel Grumbach 		ret = -ENOMEM;
30246eb5e529SEmmanuel Grumbach 		goto out_no_pci;
30256eb5e529SEmmanuel Grumbach 	}
3026e705c121SKalle Valo 
3027e705c121SKalle Valo 
3028e705c121SKalle Valo 	if (!cfg->base_params->pcie_l1_allowed) {
3029e705c121SKalle Valo 		/*
3030e705c121SKalle Valo 		 * W/A - seems to solve weird behavior. We need to remove this
3031e705c121SKalle Valo 		 * if we don't want to stay in L1 all the time. This wastes a
3032e705c121SKalle Valo 		 * lot of power.
3033e705c121SKalle Valo 		 */
3034e705c121SKalle Valo 		pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
3035e705c121SKalle Valo 				       PCIE_LINK_STATE_L1 |
3036e705c121SKalle Valo 				       PCIE_LINK_STATE_CLKPM);
3037e705c121SKalle Valo 	}
3038e705c121SKalle Valo 
30396983ba69SSara Sharon 	if (cfg->use_tfh) {
30402c6262b7SSara Sharon 		addr_size = 64;
30413cd1980bSSara Sharon 		trans_pcie->max_tbs = IWL_TFH_NUM_TBS;
30428352e62aSSara Sharon 		trans_pcie->tfd_size = sizeof(struct iwl_tfh_tfd);
30436983ba69SSara Sharon 	} else {
30442c6262b7SSara Sharon 		addr_size = 36;
30453cd1980bSSara Sharon 		trans_pcie->max_tbs = IWL_NUM_OF_TBS;
30466983ba69SSara Sharon 		trans_pcie->tfd_size = sizeof(struct iwl_tfd);
30476983ba69SSara Sharon 	}
30483cd1980bSSara Sharon 	trans->max_skb_frags = IWL_PCIE_MAX_FRAGS(trans_pcie);
30493cd1980bSSara Sharon 
3050e705c121SKalle Valo 	pci_set_master(pdev);
3051e705c121SKalle Valo 
305296a6497bSSara Sharon 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(addr_size));
3053e705c121SKalle Valo 	if (!ret)
305496a6497bSSara Sharon 		ret = pci_set_consistent_dma_mask(pdev,
305596a6497bSSara Sharon 						  DMA_BIT_MASK(addr_size));
3056e705c121SKalle Valo 	if (ret) {
3057e705c121SKalle Valo 		ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3058e705c121SKalle Valo 		if (!ret)
3059e705c121SKalle Valo 			ret = pci_set_consistent_dma_mask(pdev,
3060e705c121SKalle Valo 							  DMA_BIT_MASK(32));
3061e705c121SKalle Valo 		/* both attempts failed: */
3062e705c121SKalle Valo 		if (ret) {
3063e705c121SKalle Valo 			dev_err(&pdev->dev, "No suitable DMA available\n");
30645a41a86cSSharon Dvir 			goto out_no_pci;
3065e705c121SKalle Valo 		}
3066e705c121SKalle Valo 	}
3067e705c121SKalle Valo 
30685a41a86cSSharon Dvir 	ret = pcim_iomap_regions_request_all(pdev, BIT(0), DRV_NAME);
3069e705c121SKalle Valo 	if (ret) {
30705a41a86cSSharon Dvir 		dev_err(&pdev->dev, "pcim_iomap_regions_request_all failed\n");
30715a41a86cSSharon Dvir 		goto out_no_pci;
3072e705c121SKalle Valo 	}
3073e705c121SKalle Valo 
30745a41a86cSSharon Dvir 	trans_pcie->hw_base = pcim_iomap_table(pdev)[0];
3075e705c121SKalle Valo 	if (!trans_pcie->hw_base) {
30765a41a86cSSharon Dvir 		dev_err(&pdev->dev, "pcim_iomap_table failed\n");
3077e705c121SKalle Valo 		ret = -ENODEV;
30785a41a86cSSharon Dvir 		goto out_no_pci;
3079e705c121SKalle Valo 	}
3080e705c121SKalle Valo 
3081e705c121SKalle Valo 	/* We disable the RETRY_TIMEOUT register (0x41) to keep
3082e705c121SKalle Valo 	 * PCI Tx retries from interfering with C3 CPU state */
3083e705c121SKalle Valo 	pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
3084e705c121SKalle Valo 
3085e705c121SKalle Valo 	trans_pcie->pci_dev = pdev;
3086e705c121SKalle Valo 	iwl_disable_interrupts(trans);
3087e705c121SKalle Valo 
3088e705c121SKalle Valo 	trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
3089e705c121SKalle Valo 	/*
3090e705c121SKalle Valo 	 * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have
3091e705c121SKalle Valo 	 * changed, and now the revision step also includes bit 0-1 (no more
3092e705c121SKalle Valo 	 * "dash" value). To keep hw_rev backwards compatible - we'll store it
3093e705c121SKalle Valo 	 * in the old format.
3094e705c121SKalle Valo 	 */
30956e584873SSara Sharon 	if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) {
3096e705c121SKalle Valo 		unsigned long flags;
3097e705c121SKalle Valo 
3098e705c121SKalle Valo 		trans->hw_rev = (trans->hw_rev & 0xfff0) |
3099e705c121SKalle Valo 				(CSR_HW_REV_STEP(trans->hw_rev << 2) << 2);
3100e705c121SKalle Valo 
3101e705c121SKalle Valo 		ret = iwl_pcie_prepare_card_hw(trans);
3102e705c121SKalle Valo 		if (ret) {
3103e705c121SKalle Valo 			IWL_WARN(trans, "Exit HW not ready\n");
31045a41a86cSSharon Dvir 			goto out_no_pci;
3105e705c121SKalle Valo 		}
3106e705c121SKalle Valo 
3107e705c121SKalle Valo 		/*
3108e705c121SKalle Valo 		 * in-order to recognize C step driver should read chip version
3109e705c121SKalle Valo 		 * id located at the AUX bus MISC address space.
3110e705c121SKalle Valo 		 */
3111e705c121SKalle Valo 		iwl_set_bit(trans, CSR_GP_CNTRL,
3112e705c121SKalle Valo 			    CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
3113e705c121SKalle Valo 		udelay(2);
3114e705c121SKalle Valo 
3115e705c121SKalle Valo 		ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
3116e705c121SKalle Valo 				   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
3117e705c121SKalle Valo 				   CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
3118e705c121SKalle Valo 				   25000);
3119e705c121SKalle Valo 		if (ret < 0) {
3120e705c121SKalle Valo 			IWL_DEBUG_INFO(trans, "Failed to wake up the nic\n");
31215a41a86cSSharon Dvir 			goto out_no_pci;
3122e705c121SKalle Valo 		}
3123e705c121SKalle Valo 
312423ba9340SEmmanuel Grumbach 		if (iwl_trans_grab_nic_access(trans, &flags)) {
3125e705c121SKalle Valo 			u32 hw_step;
3126e705c121SKalle Valo 
312714ef1b43SGolan Ben-Ami 			hw_step = iwl_read_prph_no_grab(trans, WFPM_CTRL_REG);
3128e705c121SKalle Valo 			hw_step |= ENABLE_WFPM;
312914ef1b43SGolan Ben-Ami 			iwl_write_prph_no_grab(trans, WFPM_CTRL_REG, hw_step);
313014ef1b43SGolan Ben-Ami 			hw_step = iwl_read_prph_no_grab(trans, AUX_MISC_REG);
3131e705c121SKalle Valo 			hw_step = (hw_step >> HW_STEP_LOCATION_BITS) & 0xF;
3132e705c121SKalle Valo 			if (hw_step == 0x3)
3133e705c121SKalle Valo 				trans->hw_rev = (trans->hw_rev & 0xFFFFFFF3) |
3134e705c121SKalle Valo 						(SILICON_C_STEP << 2);
3135e705c121SKalle Valo 			iwl_trans_release_nic_access(trans, &flags);
3136e705c121SKalle Valo 		}
3137e705c121SKalle Valo 	}
3138e705c121SKalle Valo 
31391afb0ae4SHaim Dreyfuss 	trans->hw_rf_id = iwl_read32(trans, CSR_HW_RF_ID);
31401afb0ae4SHaim Dreyfuss 
31412e5d4a8fSHaim Dreyfuss 	iwl_pcie_set_interrupt_capa(pdev, trans);
3142e705c121SKalle Valo 	trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
3143e705c121SKalle Valo 	snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
3144e705c121SKalle Valo 		 "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
3145e705c121SKalle Valo 
3146e705c121SKalle Valo 	/* Initialize the wait queue for commands */
3147e705c121SKalle Valo 	init_waitqueue_head(&trans_pcie->wait_command_queue);
3148e705c121SKalle Valo 
31494cbb8e50SLuciano Coelho 	init_waitqueue_head(&trans_pcie->d0i3_waitq);
31504cbb8e50SLuciano Coelho 
31512e5d4a8fSHaim Dreyfuss 	if (trans_pcie->msix_enabled) {
31522e5d4a8fSHaim Dreyfuss 		if (iwl_pcie_init_msix_handler(pdev, trans_pcie))
31535a41a86cSSharon Dvir 			goto out_no_pci;
31542e5d4a8fSHaim Dreyfuss 	 } else {
3155e705c121SKalle Valo 		ret = iwl_pcie_alloc_ict(trans);
3156e705c121SKalle Valo 		if (ret)
31575a41a86cSSharon Dvir 			goto out_no_pci;
3158e705c121SKalle Valo 
31595a41a86cSSharon Dvir 		ret = devm_request_threaded_irq(&pdev->dev, pdev->irq,
31605a41a86cSSharon Dvir 						iwl_pcie_isr,
3161e705c121SKalle Valo 						iwl_pcie_irq_handler,
3162e705c121SKalle Valo 						IRQF_SHARED, DRV_NAME, trans);
3163e705c121SKalle Valo 		if (ret) {
3164e705c121SKalle Valo 			IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
3165e705c121SKalle Valo 			goto out_free_ict;
3166e705c121SKalle Valo 		}
3167e705c121SKalle Valo 		trans_pcie->inta_mask = CSR_INI_SET_MASK;
31682e5d4a8fSHaim Dreyfuss 	 }
3169e705c121SKalle Valo 
3170b3ff1270SLuca Coelho #ifdef CONFIG_IWLWIFI_PCIE_RTPM
3171b3ff1270SLuca Coelho 	trans->runtime_pm_mode = IWL_PLAT_PM_MODE_D0I3;
3172b3ff1270SLuca Coelho #else
3173b3ff1270SLuca Coelho 	trans->runtime_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3174b3ff1270SLuca Coelho #endif /* CONFIG_IWLWIFI_PCIE_RTPM */
3175b3ff1270SLuca Coelho 
3176e705c121SKalle Valo 	return trans;
3177e705c121SKalle Valo 
3178e705c121SKalle Valo out_free_ict:
3179e705c121SKalle Valo 	iwl_pcie_free_ict(trans);
3180e705c121SKalle Valo out_no_pci:
31816eb5e529SEmmanuel Grumbach 	free_percpu(trans_pcie->tso_hdr_page);
3182e705c121SKalle Valo 	iwl_trans_free(trans);
3183e705c121SKalle Valo 	return ERR_PTR(ret);
3184e705c121SKalle Valo }
3185