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 11ea695b7cSShaul Triebitz * Copyright(c) 2018 - 2019 Intel Corporation 12e705c121SKalle Valo * 13e705c121SKalle Valo * This program is free software; you can redistribute it and/or modify 14e705c121SKalle Valo * it under the terms of version 2 of the GNU General Public License as 15e705c121SKalle Valo * published by the Free Software Foundation. 16e705c121SKalle Valo * 17e705c121SKalle Valo * This program is distributed in the hope that it will be useful, but 18e705c121SKalle Valo * WITHOUT ANY WARRANTY; without even the implied warranty of 19e705c121SKalle Valo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20e705c121SKalle Valo * General Public License for more details. 21e705c121SKalle Valo * 22e705c121SKalle Valo * The full GNU General Public License is included in this distribution 23e705c121SKalle Valo * in the file called COPYING. 24e705c121SKalle Valo * 25e705c121SKalle Valo * Contact Information: 26cb2f8277SEmmanuel Grumbach * Intel Linux Wireless <linuxwifi@intel.com> 27e705c121SKalle Valo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 28e705c121SKalle Valo * 29e705c121SKalle Valo * BSD LICENSE 30e705c121SKalle Valo * 31e705c121SKalle Valo * Copyright(c) 2005 - 2015 Intel Corporation. All rights reserved. 32e705c121SKalle Valo * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH 33afb84431SEmmanuel Grumbach * Copyright(c) 2016 - 2017 Intel Deutschland GmbH 34ea695b7cSShaul Triebitz * Copyright(c) 2018 - 2019 Intel Corporation 35e705c121SKalle Valo * All rights reserved. 36e705c121SKalle Valo * 37e705c121SKalle Valo * Redistribution and use in source and binary forms, with or without 38e705c121SKalle Valo * modification, are permitted provided that the following conditions 39e705c121SKalle Valo * are met: 40e705c121SKalle Valo * 41e705c121SKalle Valo * * Redistributions of source code must retain the above copyright 42e705c121SKalle Valo * notice, this list of conditions and the following disclaimer. 43e705c121SKalle Valo * * Redistributions in binary form must reproduce the above copyright 44e705c121SKalle Valo * notice, this list of conditions and the following disclaimer in 45e705c121SKalle Valo * the documentation and/or other materials provided with the 46e705c121SKalle Valo * distribution. 47e705c121SKalle Valo * * Neither the name Intel Corporation nor the names of its 48e705c121SKalle Valo * contributors may be used to endorse or promote products derived 49e705c121SKalle Valo * from this software without specific prior written permission. 50e705c121SKalle Valo * 51e705c121SKalle Valo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 52e705c121SKalle Valo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 53e705c121SKalle Valo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 54e705c121SKalle Valo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 55e705c121SKalle Valo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 56e705c121SKalle Valo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 57e705c121SKalle Valo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58e705c121SKalle Valo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59e705c121SKalle Valo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60e705c121SKalle Valo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 61e705c121SKalle Valo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62e705c121SKalle Valo * 63e705c121SKalle Valo *****************************************************************************/ 64e705c121SKalle Valo #include <linux/pci.h> 65e705c121SKalle Valo #include <linux/pci-aspm.h> 66e705c121SKalle Valo #include <linux/interrupt.h> 67e705c121SKalle Valo #include <linux/debugfs.h> 68e705c121SKalle Valo #include <linux/sched.h> 69e705c121SKalle Valo #include <linux/bitops.h> 70e705c121SKalle Valo #include <linux/gfp.h> 71e705c121SKalle Valo #include <linux/vmalloc.h> 72b3ff1270SLuca Coelho #include <linux/pm_runtime.h> 7349564a80SLuca Coelho #include <linux/module.h> 74f7805b33SLior Cohen #include <linux/wait.h> 75e705c121SKalle Valo 76e705c121SKalle Valo #include "iwl-drv.h" 77e705c121SKalle Valo #include "iwl-trans.h" 78e705c121SKalle Valo #include "iwl-csr.h" 79e705c121SKalle Valo #include "iwl-prph.h" 80e705c121SKalle Valo #include "iwl-scd.h" 81e705c121SKalle Valo #include "iwl-agn-hw.h" 82d962f9b1SJohannes Berg #include "fw/error-dump.h" 83520f03eaSShahar S Matityahu #include "fw/dbg.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 914290eaadSJohannes Berg void iwl_trans_pcie_dump_regs(struct iwl_trans *trans) 92a6d24fadSRajat Jain { 93a6d24fadSRajat Jain #define PCI_DUMP_SIZE 64 94a6d24fadSRajat Jain #define PREFIX_LEN 32 95a6d24fadSRajat Jain struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 96a6d24fadSRajat Jain struct pci_dev *pdev = trans_pcie->pci_dev; 97a6d24fadSRajat Jain u32 i, pos, alloc_size, *ptr, *buf; 98a6d24fadSRajat Jain char *prefix; 99a6d24fadSRajat Jain 100a6d24fadSRajat Jain if (trans_pcie->pcie_dbg_dumped_once) 101a6d24fadSRajat Jain return; 102a6d24fadSRajat Jain 103a6d24fadSRajat Jain /* Should be a multiple of 4 */ 104a6d24fadSRajat Jain BUILD_BUG_ON(PCI_DUMP_SIZE > 4096 || PCI_DUMP_SIZE & 0x3); 105a6d24fadSRajat Jain /* Alloc a max size buffer */ 106a6d24fadSRajat Jain if (PCI_ERR_ROOT_ERR_SRC + 4 > PCI_DUMP_SIZE) 107a6d24fadSRajat Jain alloc_size = PCI_ERR_ROOT_ERR_SRC + 4 + PREFIX_LEN; 108a6d24fadSRajat Jain else 109a6d24fadSRajat Jain alloc_size = PCI_DUMP_SIZE + PREFIX_LEN; 110a6d24fadSRajat Jain buf = kmalloc(alloc_size, GFP_ATOMIC); 111a6d24fadSRajat Jain if (!buf) 112a6d24fadSRajat Jain return; 113a6d24fadSRajat Jain prefix = (char *)buf + alloc_size - PREFIX_LEN; 114a6d24fadSRajat Jain 115a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi transaction failed, dumping registers\n"); 116a6d24fadSRajat Jain 117a6d24fadSRajat Jain /* Print wifi device registers */ 118a6d24fadSRajat Jain sprintf(prefix, "iwlwifi %s: ", pci_name(pdev)); 119a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi device config registers:\n"); 120a6d24fadSRajat Jain for (i = 0, ptr = buf; i < PCI_DUMP_SIZE; i += 4, ptr++) 121a6d24fadSRajat Jain if (pci_read_config_dword(pdev, i, ptr)) 122a6d24fadSRajat Jain goto err_read; 123a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 124a6d24fadSRajat Jain 125a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi device memory mapped registers:\n"); 126a6d24fadSRajat Jain for (i = 0, ptr = buf; i < PCI_DUMP_SIZE; i += 4, ptr++) 127a6d24fadSRajat Jain *ptr = iwl_read32(trans, i); 128a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 129a6d24fadSRajat Jain 130a6d24fadSRajat Jain pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); 131a6d24fadSRajat Jain if (pos) { 132a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi device AER capability structure:\n"); 133a6d24fadSRajat Jain for (i = 0, ptr = buf; i < PCI_ERR_ROOT_COMMAND; i += 4, ptr++) 134a6d24fadSRajat Jain if (pci_read_config_dword(pdev, pos + i, ptr)) 135a6d24fadSRajat Jain goto err_read; 136a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 137a6d24fadSRajat Jain 32, 4, buf, i, 0); 138a6d24fadSRajat Jain } 139a6d24fadSRajat Jain 140a6d24fadSRajat Jain /* Print parent device registers next */ 141a6d24fadSRajat Jain if (!pdev->bus->self) 142a6d24fadSRajat Jain goto out; 143a6d24fadSRajat Jain 144a6d24fadSRajat Jain pdev = pdev->bus->self; 145a6d24fadSRajat Jain sprintf(prefix, "iwlwifi %s: ", pci_name(pdev)); 146a6d24fadSRajat Jain 147a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi parent port (%s) config registers:\n", 148a6d24fadSRajat Jain pci_name(pdev)); 149a6d24fadSRajat Jain for (i = 0, ptr = buf; i < PCI_DUMP_SIZE; i += 4, ptr++) 150a6d24fadSRajat Jain if (pci_read_config_dword(pdev, i, ptr)) 151a6d24fadSRajat Jain goto err_read; 152a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 153a6d24fadSRajat Jain 154a6d24fadSRajat Jain /* Print root port AER registers */ 155a6d24fadSRajat Jain pos = 0; 156a6d24fadSRajat Jain pdev = pcie_find_root_port(pdev); 157a6d24fadSRajat Jain if (pdev) 158a6d24fadSRajat Jain pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); 159a6d24fadSRajat Jain if (pos) { 160a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi root port (%s) AER cap structure:\n", 161a6d24fadSRajat Jain pci_name(pdev)); 162a6d24fadSRajat Jain sprintf(prefix, "iwlwifi %s: ", pci_name(pdev)); 163a6d24fadSRajat Jain for (i = 0, ptr = buf; i <= PCI_ERR_ROOT_ERR_SRC; i += 4, ptr++) 164a6d24fadSRajat Jain if (pci_read_config_dword(pdev, pos + i, ptr)) 165a6d24fadSRajat Jain goto err_read; 166a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 167a6d24fadSRajat Jain 4, buf, i, 0); 168a6d24fadSRajat Jain } 169f3402d6dSSara Sharon goto out; 170a6d24fadSRajat Jain 171a6d24fadSRajat Jain err_read: 172a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 173a6d24fadSRajat Jain IWL_ERR(trans, "Read failed at 0x%X\n", i); 174a6d24fadSRajat Jain out: 175a6d24fadSRajat Jain trans_pcie->pcie_dbg_dumped_once = 1; 176a6d24fadSRajat Jain kfree(buf); 177a6d24fadSRajat Jain } 178a6d24fadSRajat Jain 179870c2a11SGolan Ben Ami static void iwl_trans_pcie_sw_reset(struct iwl_trans *trans) 180870c2a11SGolan Ben Ami { 181870c2a11SGolan Ben Ami /* Reset entire device - do controller reset (results in SHRD_HW_RST) */ 182a8cbb46fSGolan Ben Ami iwl_set_bit(trans, trans->cfg->csr->addr_sw_reset, 183a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_sw_reset)); 184870c2a11SGolan Ben Ami usleep_range(5000, 6000); 185870c2a11SGolan Ben Ami } 186870c2a11SGolan Ben Ami 187e705c121SKalle Valo static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans) 188e705c121SKalle Valo { 18988964b2eSSara Sharon int i; 190e705c121SKalle Valo 19188964b2eSSara Sharon for (i = 0; i < trans->num_blocks; i++) { 19288964b2eSSara Sharon dma_free_coherent(trans->dev, trans->fw_mon[i].size, 19388964b2eSSara Sharon trans->fw_mon[i].block, 19488964b2eSSara Sharon trans->fw_mon[i].physical); 19588964b2eSSara Sharon trans->fw_mon[i].block = NULL; 19688964b2eSSara Sharon trans->fw_mon[i].physical = 0; 19788964b2eSSara Sharon trans->fw_mon[i].size = 0; 19888964b2eSSara Sharon trans->num_blocks--; 19988964b2eSSara Sharon } 200e705c121SKalle Valo } 201e705c121SKalle Valo 20288964b2eSSara Sharon static void iwl_pcie_alloc_fw_monitor_block(struct iwl_trans *trans, 20388964b2eSSara Sharon u8 max_power, u8 min_power) 204e705c121SKalle Valo { 205c5f97542SShahar S Matityahu void *cpu_addr = NULL; 20688964b2eSSara Sharon dma_addr_t phys = 0; 207e705c121SKalle Valo u32 size = 0; 208e705c121SKalle Valo u8 power; 209e705c121SKalle Valo 21088964b2eSSara Sharon for (power = max_power; power >= min_power; power--) { 211e705c121SKalle Valo size = BIT(power); 212c5f97542SShahar S Matityahu cpu_addr = dma_alloc_coherent(trans->dev, size, &phys, 213c5f97542SShahar S Matityahu GFP_KERNEL | __GFP_NOWARN | 214c5f97542SShahar S Matityahu __GFP_ZERO | __GFP_COMP); 215c5f97542SShahar S Matityahu if (!cpu_addr) 216e705c121SKalle Valo continue; 217e705c121SKalle Valo 218e705c121SKalle Valo IWL_INFO(trans, 219c5f97542SShahar S Matityahu "Allocated 0x%08x bytes for firmware monitor.\n", 220c5f97542SShahar S Matityahu size); 221e705c121SKalle Valo break; 222e705c121SKalle Valo } 223e705c121SKalle Valo 224c5f97542SShahar S Matityahu if (WARN_ON_ONCE(!cpu_addr)) 225e705c121SKalle Valo return; 226e705c121SKalle Valo 227e705c121SKalle Valo if (power != max_power) 228e705c121SKalle Valo IWL_ERR(trans, 229e705c121SKalle Valo "Sorry - debug buffer is only %luK while you requested %luK\n", 230e705c121SKalle Valo (unsigned long)BIT(power - 10), 231e705c121SKalle Valo (unsigned long)BIT(max_power - 10)); 232e705c121SKalle Valo 23388964b2eSSara Sharon trans->fw_mon[trans->num_blocks].block = cpu_addr; 23488964b2eSSara Sharon trans->fw_mon[trans->num_blocks].physical = phys; 23588964b2eSSara Sharon trans->fw_mon[trans->num_blocks].size = size; 23688964b2eSSara Sharon trans->num_blocks++; 23788964b2eSSara Sharon } 23888964b2eSSara Sharon 23988964b2eSSara Sharon void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power) 24088964b2eSSara Sharon { 24188964b2eSSara Sharon if (!max_power) { 24288964b2eSSara Sharon /* default max_power is maximum */ 24388964b2eSSara Sharon max_power = 26; 24488964b2eSSara Sharon } else { 24588964b2eSSara Sharon max_power += 11; 24688964b2eSSara Sharon } 24788964b2eSSara Sharon 24888964b2eSSara Sharon if (WARN(max_power > 26, 24988964b2eSSara Sharon "External buffer size for monitor is too big %d, check the FW TLV\n", 25088964b2eSSara Sharon max_power)) 25188964b2eSSara Sharon return; 25288964b2eSSara Sharon 25388964b2eSSara Sharon /* 25488964b2eSSara Sharon * This function allocats the default fw monitor. 25588964b2eSSara Sharon * The optional additional ones will be allocated in runtime 25688964b2eSSara Sharon */ 25788964b2eSSara Sharon if (trans->num_blocks) 25888964b2eSSara Sharon return; 25988964b2eSSara Sharon 26088964b2eSSara Sharon iwl_pcie_alloc_fw_monitor_block(trans, max_power, 11); 261e705c121SKalle Valo } 262e705c121SKalle Valo 263e705c121SKalle Valo static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg) 264e705c121SKalle Valo { 265e705c121SKalle Valo iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG, 266e705c121SKalle Valo ((reg & 0x0000ffff) | (2 << 28))); 267e705c121SKalle Valo return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG); 268e705c121SKalle Valo } 269e705c121SKalle Valo 270e705c121SKalle Valo static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val) 271e705c121SKalle Valo { 272e705c121SKalle Valo iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val); 273e705c121SKalle Valo iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG, 274e705c121SKalle Valo ((reg & 0x0000ffff) | (3 << 28))); 275e705c121SKalle Valo } 276e705c121SKalle Valo 277e705c121SKalle Valo static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux) 278e705c121SKalle Valo { 279e705c121SKalle Valo if (trans->cfg->apmg_not_supported) 280e705c121SKalle Valo return; 281e705c121SKalle Valo 282e705c121SKalle Valo if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold)) 283e705c121SKalle Valo iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG, 284e705c121SKalle Valo APMG_PS_CTRL_VAL_PWR_SRC_VAUX, 285e705c121SKalle Valo ~APMG_PS_CTRL_MSK_PWR_SRC); 286e705c121SKalle Valo else 287e705c121SKalle Valo iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG, 288e705c121SKalle Valo APMG_PS_CTRL_VAL_PWR_SRC_VMAIN, 289e705c121SKalle Valo ~APMG_PS_CTRL_MSK_PWR_SRC); 290e705c121SKalle Valo } 291e705c121SKalle Valo 292e705c121SKalle Valo /* PCI registers */ 293e705c121SKalle Valo #define PCI_CFG_RETRY_TIMEOUT 0x041 294e705c121SKalle Valo 295eda50cdeSSara Sharon void iwl_pcie_apm_config(struct iwl_trans *trans) 296e705c121SKalle Valo { 297e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 298e705c121SKalle Valo u16 lctl; 299e705c121SKalle Valo u16 cap; 300e705c121SKalle Valo 301e705c121SKalle Valo /* 302e705c121SKalle Valo * HW bug W/A for instability in PCIe bus L0S->L1 transition. 303e705c121SKalle Valo * Check if BIOS (or OS) enabled L1-ASPM on this device. 304e705c121SKalle Valo * If so (likely), disable L0S, so device moves directly L0->L1; 305e705c121SKalle Valo * costs negligible amount of power savings. 306e705c121SKalle Valo * If not (unlikely), enable L0S, so there is at least some 307e705c121SKalle Valo * power savings, even without L1. 308e705c121SKalle Valo */ 309e705c121SKalle Valo pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl); 310e705c121SKalle Valo if (lctl & PCI_EXP_LNKCTL_ASPM_L1) 311e705c121SKalle Valo iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED); 312e705c121SKalle Valo else 313e705c121SKalle Valo iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED); 314e705c121SKalle Valo trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S); 315e705c121SKalle Valo 316e705c121SKalle Valo pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap); 317e705c121SKalle Valo trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN; 318d74a61fcSLuca Coelho IWL_DEBUG_POWER(trans, "L1 %sabled - LTR %sabled\n", 319e705c121SKalle Valo (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis", 320e705c121SKalle Valo trans->ltr_enabled ? "En" : "Dis"); 321e705c121SKalle Valo } 322e705c121SKalle Valo 323e705c121SKalle Valo /* 324e705c121SKalle Valo * Start up NIC's basic functionality after it has been reset 325e705c121SKalle Valo * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop()) 326e705c121SKalle Valo * NOTE: This does not load uCode nor start the embedded processor 327e705c121SKalle Valo */ 328e705c121SKalle Valo static int iwl_pcie_apm_init(struct iwl_trans *trans) 329e705c121SKalle Valo { 33052b6e168SEmmanuel Grumbach int ret; 33152b6e168SEmmanuel Grumbach 332e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Init card's basic functions\n"); 333e705c121SKalle Valo 334e705c121SKalle Valo /* 335e705c121SKalle Valo * Use "set_bit" below rather than "write", to preserve any hardware 336e705c121SKalle Valo * bits already set by default after reset. 337e705c121SKalle Valo */ 338e705c121SKalle Valo 339e705c121SKalle Valo /* Disable L0S exit timer (platform NMI Work/Around) */ 3406e584873SSara Sharon if (trans->cfg->device_family < IWL_DEVICE_FAMILY_8000) 341e705c121SKalle Valo iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS, 342e705c121SKalle Valo CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER); 343e705c121SKalle Valo 344e705c121SKalle Valo /* 345e705c121SKalle Valo * Disable L0s without affecting L1; 346e705c121SKalle Valo * don't wait for ICH L0s (ICH bug W/A) 347e705c121SKalle Valo */ 348e705c121SKalle Valo iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS, 349e705c121SKalle Valo CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX); 350e705c121SKalle Valo 351e705c121SKalle Valo /* Set FH wait threshold to maximum (HW error during stress W/A) */ 352e705c121SKalle Valo iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL); 353e705c121SKalle Valo 354e705c121SKalle Valo /* 355e705c121SKalle Valo * Enable HAP INTA (interrupt from management bus) to 356e705c121SKalle Valo * wake device's PCI Express link L1a -> L0s 357e705c121SKalle Valo */ 358e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 359e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A); 360e705c121SKalle Valo 361e705c121SKalle Valo iwl_pcie_apm_config(trans); 362e705c121SKalle Valo 363e705c121SKalle Valo /* Configure analog phase-lock-loop before activating to D0A */ 36477d76931SJohannes Berg if (trans->cfg->base_params->pll_cfg) 36577d76931SJohannes Berg iwl_set_bit(trans, CSR_ANA_PLL_CFG, CSR50_ANA_PLL_CFG_VAL); 366e705c121SKalle Valo 367c96b5eecSJohannes Berg ret = iwl_finish_nic_init(trans); 368c96b5eecSJohannes Berg if (ret) 36952b6e168SEmmanuel Grumbach return ret; 370e705c121SKalle Valo 371e705c121SKalle Valo if (trans->cfg->host_interrupt_operation_mode) { 372e705c121SKalle Valo /* 373e705c121SKalle Valo * This is a bit of an abuse - This is needed for 7260 / 3160 374e705c121SKalle Valo * only check host_interrupt_operation_mode even if this is 375e705c121SKalle Valo * not related to host_interrupt_operation_mode. 376e705c121SKalle Valo * 377e705c121SKalle Valo * Enable the oscillator to count wake up time for L1 exit. This 378e705c121SKalle Valo * consumes slightly more power (100uA) - but allows to be sure 379e705c121SKalle Valo * that we wake up from L1 on time. 380e705c121SKalle Valo * 381e705c121SKalle Valo * This looks weird: read twice the same register, discard the 382e705c121SKalle Valo * value, set a bit, and yet again, read that same register 383e705c121SKalle Valo * just to discard the value. But that's the way the hardware 384e705c121SKalle Valo * seems to like it. 385e705c121SKalle Valo */ 386e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 387e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 388e705c121SKalle Valo iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL); 389e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 390e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 391e705c121SKalle Valo } 392e705c121SKalle Valo 393e705c121SKalle Valo /* 394e705c121SKalle Valo * Enable DMA clock and wait for it to stabilize. 395e705c121SKalle Valo * 396e705c121SKalle Valo * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" 397e705c121SKalle Valo * bits do not disable clocks. This preserves any hardware 398e705c121SKalle Valo * bits already set by default in "CLK_CTRL_REG" after reset. 399e705c121SKalle Valo */ 400e705c121SKalle Valo if (!trans->cfg->apmg_not_supported) { 401e705c121SKalle Valo iwl_write_prph(trans, APMG_CLK_EN_REG, 402e705c121SKalle Valo APMG_CLK_VAL_DMA_CLK_RQT); 403e705c121SKalle Valo udelay(20); 404e705c121SKalle Valo 405e705c121SKalle Valo /* Disable L1-Active */ 406e705c121SKalle Valo iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG, 407e705c121SKalle Valo APMG_PCIDEV_STT_VAL_L1_ACT_DIS); 408e705c121SKalle Valo 409e705c121SKalle Valo /* Clear the interrupt in APMG if the NIC is in RFKILL */ 410e705c121SKalle Valo iwl_write_prph(trans, APMG_RTC_INT_STT_REG, 411e705c121SKalle Valo APMG_RTC_INT_STT_RFKILL); 412e705c121SKalle Valo } 413e705c121SKalle Valo 414e705c121SKalle Valo set_bit(STATUS_DEVICE_ENABLED, &trans->status); 415e705c121SKalle Valo 41652b6e168SEmmanuel Grumbach return 0; 417e705c121SKalle Valo } 418e705c121SKalle Valo 419e705c121SKalle Valo /* 420e705c121SKalle Valo * Enable LP XTAL to avoid HW bug where device may consume much power if 421e705c121SKalle Valo * FW is not loaded after device reset. LP XTAL is disabled by default 422e705c121SKalle Valo * after device HW reset. Do it only if XTAL is fed by internal source. 423e705c121SKalle Valo * Configure device's "persistence" mode to avoid resetting XTAL again when 424e705c121SKalle Valo * SHRD_HW_RST occurs in S3. 425e705c121SKalle Valo */ 426e705c121SKalle Valo static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans) 427e705c121SKalle Valo { 428e705c121SKalle Valo int ret; 429e705c121SKalle Valo u32 apmg_gp1_reg; 430e705c121SKalle Valo u32 apmg_xtal_cfg_reg; 431e705c121SKalle Valo u32 dl_cfg_reg; 432e705c121SKalle Valo 433e705c121SKalle Valo /* Force XTAL ON */ 434e705c121SKalle Valo __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL, 435e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_XTAL_ON); 436e705c121SKalle Valo 437870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 438e705c121SKalle Valo 439c96b5eecSJohannes Berg ret = iwl_finish_nic_init(trans); 440c96b5eecSJohannes Berg if (WARN_ON(ret)) { 441e705c121SKalle Valo /* Release XTAL ON request */ 442e705c121SKalle Valo __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL, 443e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_XTAL_ON); 444e705c121SKalle Valo return; 445e705c121SKalle Valo } 446e705c121SKalle Valo 447e705c121SKalle Valo /* 448e705c121SKalle Valo * Clear "disable persistence" to avoid LP XTAL resetting when 449e705c121SKalle Valo * SHRD_HW_RST is applied in S3. 450e705c121SKalle Valo */ 451e705c121SKalle Valo iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG, 452e705c121SKalle Valo APMG_PCIDEV_STT_VAL_PERSIST_DIS); 453e705c121SKalle Valo 454e705c121SKalle Valo /* 455e705c121SKalle Valo * Force APMG XTAL to be active to prevent its disabling by HW 456e705c121SKalle Valo * caused by APMG idle state. 457e705c121SKalle Valo */ 458e705c121SKalle Valo apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans, 459e705c121SKalle Valo SHR_APMG_XTAL_CFG_REG); 460e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG, 461e705c121SKalle Valo apmg_xtal_cfg_reg | 462e705c121SKalle Valo SHR_APMG_XTAL_CFG_XTAL_ON_REQ); 463e705c121SKalle Valo 464870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 465e705c121SKalle Valo 466e705c121SKalle Valo /* Enable LP XTAL by indirect access through CSR */ 467e705c121SKalle Valo apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG); 468e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg | 469e705c121SKalle Valo SHR_APMG_GP1_WF_XTAL_LP_EN | 470e705c121SKalle Valo SHR_APMG_GP1_CHICKEN_BIT_SELECT); 471e705c121SKalle Valo 472e705c121SKalle Valo /* Clear delay line clock power up */ 473e705c121SKalle Valo dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG); 474e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg & 475e705c121SKalle Valo ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP); 476e705c121SKalle Valo 477e705c121SKalle Valo /* 478e705c121SKalle Valo * Enable persistence mode to avoid LP XTAL resetting when 479e705c121SKalle Valo * SHRD_HW_RST is applied in S3. 480e705c121SKalle Valo */ 481e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 482e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PERSIST_MODE); 483e705c121SKalle Valo 484e705c121SKalle Valo /* 485e705c121SKalle Valo * Clear "initialization complete" bit to move adapter from 486e705c121SKalle Valo * D0A* (powered-up Active) --> D0U* (Uninitialized) state. 487e705c121SKalle Valo */ 488e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 489a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_init_done)); 490e705c121SKalle Valo 491e705c121SKalle Valo /* Activates XTAL resources monitor */ 492e705c121SKalle Valo __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG, 493e705c121SKalle Valo CSR_MONITOR_XTAL_RESOURCES); 494e705c121SKalle Valo 495e705c121SKalle Valo /* Release XTAL ON request */ 496e705c121SKalle Valo __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL, 497e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_XTAL_ON); 498e705c121SKalle Valo udelay(10); 499e705c121SKalle Valo 500e705c121SKalle Valo /* Release APMG XTAL */ 501e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG, 502e705c121SKalle Valo apmg_xtal_cfg_reg & 503e705c121SKalle Valo ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ); 504e705c121SKalle Valo } 505e705c121SKalle Valo 506e8c8935eSJohannes Berg void iwl_pcie_apm_stop_master(struct iwl_trans *trans) 507e705c121SKalle Valo { 508e8c8935eSJohannes Berg int ret; 509e705c121SKalle Valo 510e705c121SKalle Valo /* stop device's busmaster DMA activity */ 511a8cbb46fSGolan Ben Ami iwl_set_bit(trans, trans->cfg->csr->addr_sw_reset, 512a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_stop_master)); 513e705c121SKalle Valo 514a8cbb46fSGolan Ben Ami ret = iwl_poll_bit(trans, trans->cfg->csr->addr_sw_reset, 515a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_master_dis), 516a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_master_dis), 100); 517e705c121SKalle Valo if (ret < 0) 518e705c121SKalle Valo IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n"); 519e705c121SKalle Valo 520e705c121SKalle Valo IWL_DEBUG_INFO(trans, "stop master\n"); 521e705c121SKalle Valo } 522e705c121SKalle Valo 523e705c121SKalle Valo static void iwl_pcie_apm_stop(struct iwl_trans *trans, bool op_mode_leave) 524e705c121SKalle Valo { 525e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n"); 526e705c121SKalle Valo 527e705c121SKalle Valo if (op_mode_leave) { 528e705c121SKalle Valo if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 529e705c121SKalle Valo iwl_pcie_apm_init(trans); 530e705c121SKalle Valo 531e705c121SKalle Valo /* inform ME that we are leaving */ 532e705c121SKalle Valo if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) 533e705c121SKalle Valo iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG, 534e705c121SKalle Valo APMG_PCIDEV_STT_VAL_WAKE_ME); 5356e584873SSara Sharon else if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) { 536e705c121SKalle Valo iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 537e705c121SKalle Valo CSR_RESET_LINK_PWR_MGMT_DISABLED); 538e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 539e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PREPARE | 540e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_ENABLE_PME); 541e705c121SKalle Valo mdelay(1); 542e705c121SKalle Valo iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 543e705c121SKalle Valo CSR_RESET_LINK_PWR_MGMT_DISABLED); 544e705c121SKalle Valo } 545e705c121SKalle Valo mdelay(5); 546e705c121SKalle Valo } 547e705c121SKalle Valo 548e705c121SKalle Valo clear_bit(STATUS_DEVICE_ENABLED, &trans->status); 549e705c121SKalle Valo 550e705c121SKalle Valo /* Stop device's DMA activity */ 551e705c121SKalle Valo iwl_pcie_apm_stop_master(trans); 552e705c121SKalle Valo 553e705c121SKalle Valo if (trans->cfg->lp_xtal_workaround) { 554e705c121SKalle Valo iwl_pcie_apm_lp_xtal_enable(trans); 555e705c121SKalle Valo return; 556e705c121SKalle Valo } 557e705c121SKalle Valo 558870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 559e705c121SKalle Valo 560e705c121SKalle Valo /* 561e705c121SKalle Valo * Clear "initialization complete" bit to move adapter from 562e705c121SKalle Valo * D0A* (powered-up Active) --> D0U* (Uninitialized) state. 563e705c121SKalle Valo */ 564e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 565a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_init_done)); 566e705c121SKalle Valo } 567e705c121SKalle Valo 568e705c121SKalle Valo static int iwl_pcie_nic_init(struct iwl_trans *trans) 569e705c121SKalle Valo { 570e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 57152b6e168SEmmanuel Grumbach int ret; 572e705c121SKalle Valo 573e705c121SKalle Valo /* nic_init */ 574e705c121SKalle Valo spin_lock(&trans_pcie->irq_lock); 57552b6e168SEmmanuel Grumbach ret = iwl_pcie_apm_init(trans); 576e705c121SKalle Valo spin_unlock(&trans_pcie->irq_lock); 577e705c121SKalle Valo 57852b6e168SEmmanuel Grumbach if (ret) 57952b6e168SEmmanuel Grumbach return ret; 58052b6e168SEmmanuel Grumbach 581e705c121SKalle Valo iwl_pcie_set_pwr(trans, false); 582e705c121SKalle Valo 583e705c121SKalle Valo iwl_op_mode_nic_config(trans->op_mode); 584e705c121SKalle Valo 585e705c121SKalle Valo /* Allocate the RX queue, or reset if it is already allocated */ 586e705c121SKalle Valo iwl_pcie_rx_init(trans); 587e705c121SKalle Valo 588e705c121SKalle Valo /* Allocate or reset and init all Tx and Command queues */ 589e705c121SKalle Valo if (iwl_pcie_tx_init(trans)) 590e705c121SKalle Valo return -ENOMEM; 591e705c121SKalle Valo 592e705c121SKalle Valo if (trans->cfg->base_params->shadow_reg_enable) { 593e705c121SKalle Valo /* enable shadow regs in HW */ 594e705c121SKalle Valo iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF); 595e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n"); 596e705c121SKalle Valo } 597e705c121SKalle Valo 598e705c121SKalle Valo return 0; 599e705c121SKalle Valo } 600e705c121SKalle Valo 601e705c121SKalle Valo #define HW_READY_TIMEOUT (50) 602e705c121SKalle Valo 603e705c121SKalle Valo /* Note: returns poll_bit return value, which is >= 0 if success */ 604e705c121SKalle Valo static int iwl_pcie_set_hw_ready(struct iwl_trans *trans) 605e705c121SKalle Valo { 606e705c121SKalle Valo int ret; 607e705c121SKalle Valo 608e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 609e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_NIC_READY); 610e705c121SKalle Valo 611e705c121SKalle Valo /* See if we got it */ 612e705c121SKalle Valo ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG, 613e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, 614e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, 615e705c121SKalle Valo HW_READY_TIMEOUT); 616e705c121SKalle Valo 617e705c121SKalle Valo if (ret >= 0) 618e705c121SKalle Valo iwl_set_bit(trans, CSR_MBOX_SET_REG, CSR_MBOX_SET_REG_OS_ALIVE); 619e705c121SKalle Valo 620e705c121SKalle Valo IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : ""); 621e705c121SKalle Valo return ret; 622e705c121SKalle Valo } 623e705c121SKalle Valo 624e705c121SKalle Valo /* Note: returns standard 0/-ERROR code */ 625eda50cdeSSara Sharon int iwl_pcie_prepare_card_hw(struct iwl_trans *trans) 626e705c121SKalle Valo { 627e705c121SKalle Valo int ret; 628e705c121SKalle Valo int t = 0; 629e705c121SKalle Valo int iter; 630e705c121SKalle Valo 631e705c121SKalle Valo IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n"); 632e705c121SKalle Valo 633e705c121SKalle Valo ret = iwl_pcie_set_hw_ready(trans); 634e705c121SKalle Valo /* If the card is ready, exit 0 */ 635e705c121SKalle Valo if (ret >= 0) 636e705c121SKalle Valo return 0; 637e705c121SKalle Valo 638e705c121SKalle Valo iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 639e705c121SKalle Valo CSR_RESET_LINK_PWR_MGMT_DISABLED); 640192185d6SJohannes Berg usleep_range(1000, 2000); 641e705c121SKalle Valo 642e705c121SKalle Valo for (iter = 0; iter < 10; iter++) { 643e705c121SKalle Valo /* If HW is not ready, prepare the conditions to check again */ 644e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 645e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PREPARE); 646e705c121SKalle Valo 647e705c121SKalle Valo do { 648e705c121SKalle Valo ret = iwl_pcie_set_hw_ready(trans); 649e705c121SKalle Valo if (ret >= 0) 650e705c121SKalle Valo return 0; 651e705c121SKalle Valo 652e705c121SKalle Valo usleep_range(200, 1000); 653e705c121SKalle Valo t += 200; 654e705c121SKalle Valo } while (t < 150000); 655e705c121SKalle Valo msleep(25); 656e705c121SKalle Valo } 657e705c121SKalle Valo 658e705c121SKalle Valo IWL_ERR(trans, "Couldn't prepare the card\n"); 659e705c121SKalle Valo 660e705c121SKalle Valo return ret; 661e705c121SKalle Valo } 662e705c121SKalle Valo 663e705c121SKalle Valo /* 664e705c121SKalle Valo * ucode 665e705c121SKalle Valo */ 666564cdce7SSara Sharon static void iwl_pcie_load_firmware_chunk_fh(struct iwl_trans *trans, 667564cdce7SSara Sharon u32 dst_addr, dma_addr_t phy_addr, 668564cdce7SSara Sharon u32 byte_cnt) 669e705c121SKalle Valo { 670bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL), 671e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE); 672e705c121SKalle Valo 673bac842daSEmmanuel Grumbach iwl_write32(trans, FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL), 674e705c121SKalle Valo dst_addr); 675e705c121SKalle Valo 676bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL), 677e705c121SKalle Valo phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK); 678e705c121SKalle Valo 679bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL), 680e705c121SKalle Valo (iwl_get_dma_hi_addr(phy_addr) 681e705c121SKalle Valo << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt); 682e705c121SKalle Valo 683bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL), 684bac842daSEmmanuel Grumbach BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM) | 685bac842daSEmmanuel Grumbach BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX) | 686e705c121SKalle Valo FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID); 687e705c121SKalle Valo 688bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL), 689e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE | 690e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE | 691e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD); 692564cdce7SSara Sharon } 693e705c121SKalle Valo 694564cdce7SSara Sharon static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans, 695564cdce7SSara Sharon u32 dst_addr, dma_addr_t phy_addr, 696564cdce7SSara Sharon u32 byte_cnt) 697564cdce7SSara Sharon { 698564cdce7SSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 699564cdce7SSara Sharon unsigned long flags; 700564cdce7SSara Sharon int ret; 701564cdce7SSara Sharon 702564cdce7SSara Sharon trans_pcie->ucode_write_complete = false; 703564cdce7SSara Sharon 704564cdce7SSara Sharon if (!iwl_trans_grab_nic_access(trans, &flags)) 705564cdce7SSara Sharon return -EIO; 706564cdce7SSara Sharon 707564cdce7SSara Sharon iwl_pcie_load_firmware_chunk_fh(trans, dst_addr, phy_addr, 708564cdce7SSara Sharon byte_cnt); 709bac842daSEmmanuel Grumbach iwl_trans_release_nic_access(trans, &flags); 710bac842daSEmmanuel Grumbach 711e705c121SKalle Valo ret = wait_event_timeout(trans_pcie->ucode_write_waitq, 712e705c121SKalle Valo trans_pcie->ucode_write_complete, 5 * HZ); 713e705c121SKalle Valo if (!ret) { 714e705c121SKalle Valo IWL_ERR(trans, "Failed to load firmware chunk!\n"); 715fb12777aSKirtika Ruchandani iwl_trans_pcie_dump_regs(trans); 716e705c121SKalle Valo return -ETIMEDOUT; 717e705c121SKalle Valo } 718e705c121SKalle Valo 719e705c121SKalle Valo return 0; 720e705c121SKalle Valo } 721e705c121SKalle Valo 722e705c121SKalle Valo static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num, 723e705c121SKalle Valo const struct fw_desc *section) 724e705c121SKalle Valo { 725e705c121SKalle Valo u8 *v_addr; 726e705c121SKalle Valo dma_addr_t p_addr; 727e705c121SKalle Valo u32 offset, chunk_sz = min_t(u32, FH_MEM_TB_MAX_LENGTH, section->len); 728e705c121SKalle Valo int ret = 0; 729e705c121SKalle Valo 730e705c121SKalle Valo IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n", 731e705c121SKalle Valo section_num); 732e705c121SKalle Valo 733e705c121SKalle Valo v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr, 734e705c121SKalle Valo GFP_KERNEL | __GFP_NOWARN); 735e705c121SKalle Valo if (!v_addr) { 736e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n"); 737e705c121SKalle Valo chunk_sz = PAGE_SIZE; 738e705c121SKalle Valo v_addr = dma_alloc_coherent(trans->dev, chunk_sz, 739e705c121SKalle Valo &p_addr, GFP_KERNEL); 740e705c121SKalle Valo if (!v_addr) 741e705c121SKalle Valo return -ENOMEM; 742e705c121SKalle Valo } 743e705c121SKalle Valo 744e705c121SKalle Valo for (offset = 0; offset < section->len; offset += chunk_sz) { 745e705c121SKalle Valo u32 copy_size, dst_addr; 746e705c121SKalle Valo bool extended_addr = false; 747e705c121SKalle Valo 748e705c121SKalle Valo copy_size = min_t(u32, chunk_sz, section->len - offset); 749e705c121SKalle Valo dst_addr = section->offset + offset; 750e705c121SKalle Valo 751e705c121SKalle Valo if (dst_addr >= IWL_FW_MEM_EXTENDED_START && 752e705c121SKalle Valo dst_addr <= IWL_FW_MEM_EXTENDED_END) 753e705c121SKalle Valo extended_addr = true; 754e705c121SKalle Valo 755e705c121SKalle Valo if (extended_addr) 756e705c121SKalle Valo iwl_set_bits_prph(trans, LMPM_CHICK, 757e705c121SKalle Valo LMPM_CHICK_EXTENDED_ADDR_SPACE); 758e705c121SKalle Valo 759e705c121SKalle Valo memcpy(v_addr, (u8 *)section->data + offset, copy_size); 760e705c121SKalle Valo ret = iwl_pcie_load_firmware_chunk(trans, dst_addr, p_addr, 761e705c121SKalle Valo copy_size); 762e705c121SKalle Valo 763e705c121SKalle Valo if (extended_addr) 764e705c121SKalle Valo iwl_clear_bits_prph(trans, LMPM_CHICK, 765e705c121SKalle Valo LMPM_CHICK_EXTENDED_ADDR_SPACE); 766e705c121SKalle Valo 767e705c121SKalle Valo if (ret) { 768e705c121SKalle Valo IWL_ERR(trans, 769e705c121SKalle Valo "Could not load the [%d] uCode section\n", 770e705c121SKalle Valo section_num); 771e705c121SKalle Valo break; 772e705c121SKalle Valo } 773e705c121SKalle Valo } 774e705c121SKalle Valo 775e705c121SKalle Valo dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr); 776e705c121SKalle Valo return ret; 777e705c121SKalle Valo } 778e705c121SKalle Valo 779e705c121SKalle Valo static int iwl_pcie_load_cpu_sections_8000(struct iwl_trans *trans, 780e705c121SKalle Valo const struct fw_img *image, 781e705c121SKalle Valo int cpu, 782e705c121SKalle Valo int *first_ucode_section) 783e705c121SKalle Valo { 784e705c121SKalle Valo int shift_param; 785e705c121SKalle Valo int i, ret = 0, sec_num = 0x1; 786e705c121SKalle Valo u32 val, last_read_idx = 0; 787e705c121SKalle Valo 788e705c121SKalle Valo if (cpu == 1) { 789e705c121SKalle Valo shift_param = 0; 790e705c121SKalle Valo *first_ucode_section = 0; 791e705c121SKalle Valo } else { 792e705c121SKalle Valo shift_param = 16; 793e705c121SKalle Valo (*first_ucode_section)++; 794e705c121SKalle Valo } 795e705c121SKalle Valo 796eef187a7SSara Sharon for (i = *first_ucode_section; i < image->num_sec; i++) { 797e705c121SKalle Valo last_read_idx = i; 798e705c121SKalle Valo 799e705c121SKalle Valo /* 800e705c121SKalle Valo * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between 801e705c121SKalle Valo * CPU1 to CPU2. 802e705c121SKalle Valo * PAGING_SEPARATOR_SECTION delimiter - separate between 803e705c121SKalle Valo * CPU2 non paged to CPU2 paging sec. 804e705c121SKalle Valo */ 805e705c121SKalle Valo if (!image->sec[i].data || 806e705c121SKalle Valo image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION || 807e705c121SKalle Valo image->sec[i].offset == PAGING_SEPARATOR_SECTION) { 808e705c121SKalle Valo IWL_DEBUG_FW(trans, 809e705c121SKalle Valo "Break since Data not valid or Empty section, sec = %d\n", 810e705c121SKalle Valo i); 811e705c121SKalle Valo break; 812e705c121SKalle Valo } 813e705c121SKalle Valo 814e705c121SKalle Valo ret = iwl_pcie_load_section(trans, i, &image->sec[i]); 815e705c121SKalle Valo if (ret) 816e705c121SKalle Valo return ret; 817e705c121SKalle Valo 818d6a2c5c7SSara Sharon /* Notify ucode of loaded section number and status */ 819e705c121SKalle Valo val = iwl_read_direct32(trans, FH_UCODE_LOAD_STATUS); 820e705c121SKalle Valo val = val | (sec_num << shift_param); 821e705c121SKalle Valo iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, val); 822eda50cdeSSara Sharon 823e705c121SKalle Valo sec_num = (sec_num << 1) | 0x1; 824e705c121SKalle Valo } 825e705c121SKalle Valo 826e705c121SKalle Valo *first_ucode_section = last_read_idx; 827e705c121SKalle Valo 8282aabdbdcSEmmanuel Grumbach iwl_enable_interrupts(trans); 8292aabdbdcSEmmanuel Grumbach 830d6a2c5c7SSara Sharon if (trans->cfg->use_tfh) { 831e705c121SKalle Valo if (cpu == 1) 832d6a2c5c7SSara Sharon iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS, 833d6a2c5c7SSara Sharon 0xFFFF); 834e705c121SKalle Valo else 835d6a2c5c7SSara Sharon iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS, 836d6a2c5c7SSara Sharon 0xFFFFFFFF); 837d6a2c5c7SSara Sharon } else { 838d6a2c5c7SSara Sharon if (cpu == 1) 839d6a2c5c7SSara Sharon iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, 840d6a2c5c7SSara Sharon 0xFFFF); 841d6a2c5c7SSara Sharon else 842d6a2c5c7SSara Sharon iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, 843d6a2c5c7SSara Sharon 0xFFFFFFFF); 844d6a2c5c7SSara Sharon } 845e705c121SKalle Valo 846e705c121SKalle Valo return 0; 847e705c121SKalle Valo } 848e705c121SKalle Valo 849e705c121SKalle Valo static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans, 850e705c121SKalle Valo const struct fw_img *image, 851e705c121SKalle Valo int cpu, 852e705c121SKalle Valo int *first_ucode_section) 853e705c121SKalle Valo { 854e705c121SKalle Valo int i, ret = 0; 855e705c121SKalle Valo u32 last_read_idx = 0; 856e705c121SKalle Valo 8573ce4a038SKirtika Ruchandani if (cpu == 1) 858e705c121SKalle Valo *first_ucode_section = 0; 8593ce4a038SKirtika Ruchandani else 860e705c121SKalle Valo (*first_ucode_section)++; 861e705c121SKalle Valo 862eef187a7SSara Sharon for (i = *first_ucode_section; i < image->num_sec; i++) { 863e705c121SKalle Valo last_read_idx = i; 864e705c121SKalle Valo 865e705c121SKalle Valo /* 866e705c121SKalle Valo * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between 867e705c121SKalle Valo * CPU1 to CPU2. 868e705c121SKalle Valo * PAGING_SEPARATOR_SECTION delimiter - separate between 869e705c121SKalle Valo * CPU2 non paged to CPU2 paging sec. 870e705c121SKalle Valo */ 871e705c121SKalle Valo if (!image->sec[i].data || 872e705c121SKalle Valo image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION || 873e705c121SKalle Valo image->sec[i].offset == PAGING_SEPARATOR_SECTION) { 874e705c121SKalle Valo IWL_DEBUG_FW(trans, 875e705c121SKalle Valo "Break since Data not valid or Empty section, sec = %d\n", 876e705c121SKalle Valo i); 877e705c121SKalle Valo break; 878e705c121SKalle Valo } 879e705c121SKalle Valo 880e705c121SKalle Valo ret = iwl_pcie_load_section(trans, i, &image->sec[i]); 881e705c121SKalle Valo if (ret) 882e705c121SKalle Valo return ret; 883e705c121SKalle Valo } 884e705c121SKalle Valo 885e705c121SKalle Valo *first_ucode_section = last_read_idx; 886e705c121SKalle Valo 887e705c121SKalle Valo return 0; 888e705c121SKalle Valo } 889e705c121SKalle Valo 890c9be849dSLiad Kaufman void iwl_pcie_apply_destination(struct iwl_trans *trans) 891e705c121SKalle Valo { 892fd527eb5SGolan Ben Ami const struct iwl_fw_dbg_dest_tlv_v1 *dest = trans->dbg_dest_tlv; 893e705c121SKalle Valo int i; 894e705c121SKalle Valo 8957a14c23dSSara Sharon if (trans->ini_valid) { 8967a14c23dSSara Sharon if (!trans->num_blocks) 8977a14c23dSSara Sharon return; 8987a14c23dSSara Sharon 89953032e6eSShahar S Matityahu IWL_DEBUG_FW(trans, 90053032e6eSShahar S Matityahu "WRT: applying DRAM buffer[0] destination\n"); 901ea695b7cSShaul Triebitz iwl_write_umac_prph(trans, MON_BUFF_BASE_ADDR_VER2, 9027a14c23dSSara Sharon trans->fw_mon[0].physical >> 9037a14c23dSSara Sharon MON_BUFF_SHIFT_VER2); 904ea695b7cSShaul Triebitz iwl_write_umac_prph(trans, MON_BUFF_END_ADDR_VER2, 9057a14c23dSSara Sharon (trans->fw_mon[0].physical + 9067a14c23dSSara Sharon trans->fw_mon[0].size - 256) >> 9077a14c23dSSara Sharon MON_BUFF_SHIFT_VER2); 9087a14c23dSSara Sharon return; 9097a14c23dSSara Sharon } 9107a14c23dSSara Sharon 911e705c121SKalle Valo IWL_INFO(trans, "Applying debug destination %s\n", 912e705c121SKalle Valo get_fw_dbg_mode_string(dest->monitor_mode)); 913e705c121SKalle Valo 914e705c121SKalle Valo if (dest->monitor_mode == EXTERNAL_MODE) 915e705c121SKalle Valo iwl_pcie_alloc_fw_monitor(trans, dest->size_power); 916e705c121SKalle Valo else 917e705c121SKalle Valo IWL_WARN(trans, "PCI should have external buffer debug\n"); 918e705c121SKalle Valo 91917b809c9SSara Sharon for (i = 0; i < trans->dbg_n_dest_reg; i++) { 920e705c121SKalle Valo u32 addr = le32_to_cpu(dest->reg_ops[i].addr); 921e705c121SKalle Valo u32 val = le32_to_cpu(dest->reg_ops[i].val); 922e705c121SKalle Valo 923e705c121SKalle Valo switch (dest->reg_ops[i].op) { 924e705c121SKalle Valo case CSR_ASSIGN: 925e705c121SKalle Valo iwl_write32(trans, addr, val); 926e705c121SKalle Valo break; 927e705c121SKalle Valo case CSR_SETBIT: 928e705c121SKalle Valo iwl_set_bit(trans, addr, BIT(val)); 929e705c121SKalle Valo break; 930e705c121SKalle Valo case CSR_CLEARBIT: 931e705c121SKalle Valo iwl_clear_bit(trans, addr, BIT(val)); 932e705c121SKalle Valo break; 933e705c121SKalle Valo case PRPH_ASSIGN: 934e705c121SKalle Valo iwl_write_prph(trans, addr, val); 935e705c121SKalle Valo break; 936e705c121SKalle Valo case PRPH_SETBIT: 937e705c121SKalle Valo iwl_set_bits_prph(trans, addr, BIT(val)); 938e705c121SKalle Valo break; 939e705c121SKalle Valo case PRPH_CLEARBIT: 940e705c121SKalle Valo iwl_clear_bits_prph(trans, addr, BIT(val)); 941e705c121SKalle Valo break; 942e705c121SKalle Valo case PRPH_BLOCKBIT: 943e705c121SKalle Valo if (iwl_read_prph(trans, addr) & BIT(val)) { 944e705c121SKalle Valo IWL_ERR(trans, 945e705c121SKalle Valo "BIT(%u) in address 0x%x is 1, stopping FW configuration\n", 946e705c121SKalle Valo val, addr); 947e705c121SKalle Valo goto monitor; 948e705c121SKalle Valo } 949e705c121SKalle Valo break; 950e705c121SKalle Valo default: 951e705c121SKalle Valo IWL_ERR(trans, "FW debug - unknown OP %d\n", 952e705c121SKalle Valo dest->reg_ops[i].op); 953e705c121SKalle Valo break; 954e705c121SKalle Valo } 955e705c121SKalle Valo } 956e705c121SKalle Valo 957e705c121SKalle Valo monitor: 95888964b2eSSara Sharon if (dest->monitor_mode == EXTERNAL_MODE && trans->fw_mon[0].size) { 959e705c121SKalle Valo iwl_write_prph(trans, le32_to_cpu(dest->base_reg), 96088964b2eSSara Sharon trans->fw_mon[0].physical >> dest->base_shift); 9616e584873SSara Sharon if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) 962e705c121SKalle Valo iwl_write_prph(trans, le32_to_cpu(dest->end_reg), 96388964b2eSSara Sharon (trans->fw_mon[0].physical + 96488964b2eSSara Sharon trans->fw_mon[0].size - 256) >> 96562d7476dSEmmanuel Grumbach dest->end_shift); 96662d7476dSEmmanuel Grumbach else 96762d7476dSEmmanuel Grumbach iwl_write_prph(trans, le32_to_cpu(dest->end_reg), 96888964b2eSSara Sharon (trans->fw_mon[0].physical + 96988964b2eSSara Sharon trans->fw_mon[0].size) >> 97062d7476dSEmmanuel Grumbach dest->end_shift); 971e705c121SKalle Valo } 972e705c121SKalle Valo } 973e705c121SKalle Valo 974e705c121SKalle Valo static int iwl_pcie_load_given_ucode(struct iwl_trans *trans, 975e705c121SKalle Valo const struct fw_img *image) 976e705c121SKalle Valo { 977e705c121SKalle Valo int ret = 0; 978e705c121SKalle Valo int first_ucode_section; 979e705c121SKalle Valo 980e705c121SKalle Valo IWL_DEBUG_FW(trans, "working with %s CPU\n", 981e705c121SKalle Valo image->is_dual_cpus ? "Dual" : "Single"); 982e705c121SKalle Valo 983e705c121SKalle Valo /* load to FW the binary non secured sections of CPU1 */ 984e705c121SKalle Valo ret = iwl_pcie_load_cpu_sections(trans, image, 1, &first_ucode_section); 985e705c121SKalle Valo if (ret) 986e705c121SKalle Valo return ret; 987e705c121SKalle Valo 988e705c121SKalle Valo if (image->is_dual_cpus) { 989e705c121SKalle Valo /* set CPU2 header address */ 990e705c121SKalle Valo iwl_write_prph(trans, 991e705c121SKalle Valo LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR, 992e705c121SKalle Valo LMPM_SECURE_CPU2_HDR_MEM_SPACE); 993e705c121SKalle Valo 994e705c121SKalle Valo /* load to FW the binary sections of CPU2 */ 995e705c121SKalle Valo ret = iwl_pcie_load_cpu_sections(trans, image, 2, 996e705c121SKalle Valo &first_ucode_section); 997e705c121SKalle Valo if (ret) 998e705c121SKalle Valo return ret; 999e705c121SKalle Valo } 1000e705c121SKalle Valo 1001e705c121SKalle Valo /* supported for 7000 only for the moment */ 1002e705c121SKalle Valo if (iwlwifi_mod_params.fw_monitor && 1003e705c121SKalle Valo trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) { 1004e705c121SKalle Valo iwl_pcie_alloc_fw_monitor(trans, 0); 1005e705c121SKalle Valo 100688964b2eSSara Sharon if (trans->fw_mon[0].size) { 1007e705c121SKalle Valo iwl_write_prph(trans, MON_BUFF_BASE_ADDR, 100888964b2eSSara Sharon trans->fw_mon[0].physical >> 4); 1009e705c121SKalle Valo iwl_write_prph(trans, MON_BUFF_END_ADDR, 101088964b2eSSara Sharon (trans->fw_mon[0].physical + 101188964b2eSSara Sharon trans->fw_mon[0].size) >> 4); 1012e705c121SKalle Valo } 10137a14c23dSSara Sharon } else if (iwl_pcie_dbg_on(trans)) { 1014e705c121SKalle Valo iwl_pcie_apply_destination(trans); 1015e705c121SKalle Valo } 1016e705c121SKalle Valo 10172aabdbdcSEmmanuel Grumbach iwl_enable_interrupts(trans); 10182aabdbdcSEmmanuel Grumbach 1019e705c121SKalle Valo /* release CPU reset */ 1020e705c121SKalle Valo iwl_write32(trans, CSR_RESET, 0); 1021e705c121SKalle Valo 1022e705c121SKalle Valo return 0; 1023e705c121SKalle Valo } 1024e705c121SKalle Valo 1025e705c121SKalle Valo static int iwl_pcie_load_given_ucode_8000(struct iwl_trans *trans, 1026e705c121SKalle Valo const struct fw_img *image) 1027e705c121SKalle Valo { 1028e705c121SKalle Valo int ret = 0; 1029e705c121SKalle Valo int first_ucode_section; 1030e705c121SKalle Valo 1031e705c121SKalle Valo IWL_DEBUG_FW(trans, "working with %s CPU\n", 1032e705c121SKalle Valo image->is_dual_cpus ? "Dual" : "Single"); 1033e705c121SKalle Valo 10347a14c23dSSara Sharon if (iwl_pcie_dbg_on(trans)) 1035e705c121SKalle Valo iwl_pcie_apply_destination(trans); 1036e705c121SKalle Valo 103782ea7966SSara Sharon IWL_DEBUG_POWER(trans, "Original WFPM value = 0x%08X\n", 103882ea7966SSara Sharon iwl_read_prph(trans, WFPM_GP2)); 103982ea7966SSara Sharon 104082ea7966SSara Sharon /* 104182ea7966SSara Sharon * Set default value. On resume reading the values that were 104282ea7966SSara Sharon * zeored can provide debug data on the resume flow. 104382ea7966SSara Sharon * This is for debugging only and has no functional impact. 104482ea7966SSara Sharon */ 104582ea7966SSara Sharon iwl_write_prph(trans, WFPM_GP2, 0x01010101); 104682ea7966SSara Sharon 1047e705c121SKalle Valo /* configure the ucode to be ready to get the secured image */ 1048e705c121SKalle Valo /* release CPU reset */ 1049e705c121SKalle Valo iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT); 1050e705c121SKalle Valo 1051e705c121SKalle Valo /* load to FW the binary Secured sections of CPU1 */ 1052e705c121SKalle Valo ret = iwl_pcie_load_cpu_sections_8000(trans, image, 1, 1053e705c121SKalle Valo &first_ucode_section); 1054e705c121SKalle Valo if (ret) 1055e705c121SKalle Valo return ret; 1056e705c121SKalle Valo 1057e705c121SKalle Valo /* load to FW the binary sections of CPU2 */ 1058e705c121SKalle Valo return iwl_pcie_load_cpu_sections_8000(trans, image, 2, 1059e705c121SKalle Valo &first_ucode_section); 1060e705c121SKalle Valo } 1061e705c121SKalle Valo 10629ad8fd0bSJohannes Berg bool iwl_pcie_check_hw_rf_kill(struct iwl_trans *trans) 1063727c02dfSSara Sharon { 1064326477e4SJohannes Berg struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1065727c02dfSSara Sharon bool hw_rfkill = iwl_is_rfkill_set(trans); 1066326477e4SJohannes Berg bool prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 1067326477e4SJohannes Berg bool report; 1068727c02dfSSara Sharon 1069326477e4SJohannes Berg if (hw_rfkill) { 1070326477e4SJohannes Berg set_bit(STATUS_RFKILL_HW, &trans->status); 1071326477e4SJohannes Berg set_bit(STATUS_RFKILL_OPMODE, &trans->status); 1072326477e4SJohannes Berg } else { 1073326477e4SJohannes Berg clear_bit(STATUS_RFKILL_HW, &trans->status); 1074326477e4SJohannes Berg if (trans_pcie->opmode_down) 1075326477e4SJohannes Berg clear_bit(STATUS_RFKILL_OPMODE, &trans->status); 1076326477e4SJohannes Berg } 1077727c02dfSSara Sharon 1078326477e4SJohannes Berg report = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 1079326477e4SJohannes Berg 1080326477e4SJohannes Berg if (prev != report) 1081326477e4SJohannes Berg iwl_trans_pcie_rf_kill(trans, report); 1082727c02dfSSara Sharon 1083727c02dfSSara Sharon return hw_rfkill; 1084727c02dfSSara Sharon } 1085727c02dfSSara Sharon 10867ca00409SHaim Dreyfuss struct iwl_causes_list { 10877ca00409SHaim Dreyfuss u32 cause_num; 10887ca00409SHaim Dreyfuss u32 mask_reg; 10897ca00409SHaim Dreyfuss u8 addr; 10907ca00409SHaim Dreyfuss }; 10917ca00409SHaim Dreyfuss 10927ca00409SHaim Dreyfuss static struct iwl_causes_list causes_list[] = { 10937ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_D2S_CH0_NUM, CSR_MSIX_FH_INT_MASK_AD, 0}, 10947ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_D2S_CH1_NUM, CSR_MSIX_FH_INT_MASK_AD, 0x1}, 10957ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_S2D, CSR_MSIX_FH_INT_MASK_AD, 0x3}, 10967ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_FH_ERR, CSR_MSIX_FH_INT_MASK_AD, 0x5}, 10977ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_ALIVE, CSR_MSIX_HW_INT_MASK_AD, 0x10}, 10987ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_WAKEUP, CSR_MSIX_HW_INT_MASK_AD, 0x11}, 1099ff911dcaSShaul Triebitz {MSIX_HW_INT_CAUSES_REG_IML, CSR_MSIX_HW_INT_MASK_AD, 0x12}, 11007ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_CT_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x16}, 11017ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_RF_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x17}, 11027ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_PERIODIC, CSR_MSIX_HW_INT_MASK_AD, 0x18}, 11037ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_SW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x29}, 11047ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_SCD, CSR_MSIX_HW_INT_MASK_AD, 0x2A}, 11057ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_FH_TX, CSR_MSIX_HW_INT_MASK_AD, 0x2B}, 11067ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_HW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x2D}, 11077ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_HAP, CSR_MSIX_HW_INT_MASK_AD, 0x2E}, 11087ca00409SHaim Dreyfuss }; 11097ca00409SHaim Dreyfuss 11109b58419eSGolan Ben Ami static struct iwl_causes_list causes_list_v2[] = { 11119b58419eSGolan Ben Ami {MSIX_FH_INT_CAUSES_D2S_CH0_NUM, CSR_MSIX_FH_INT_MASK_AD, 0}, 11129b58419eSGolan Ben Ami {MSIX_FH_INT_CAUSES_D2S_CH1_NUM, CSR_MSIX_FH_INT_MASK_AD, 0x1}, 11139b58419eSGolan Ben Ami {MSIX_FH_INT_CAUSES_S2D, CSR_MSIX_FH_INT_MASK_AD, 0x3}, 11149b58419eSGolan Ben Ami {MSIX_FH_INT_CAUSES_FH_ERR, CSR_MSIX_FH_INT_MASK_AD, 0x5}, 11159b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_ALIVE, CSR_MSIX_HW_INT_MASK_AD, 0x10}, 11169b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_IPC, CSR_MSIX_HW_INT_MASK_AD, 0x11}, 11179b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_SW_ERR_V2, CSR_MSIX_HW_INT_MASK_AD, 0x15}, 11189b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_CT_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x16}, 11199b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_RF_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x17}, 11209b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_PERIODIC, CSR_MSIX_HW_INT_MASK_AD, 0x18}, 11219b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_SCD, CSR_MSIX_HW_INT_MASK_AD, 0x2A}, 11229b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_FH_TX, CSR_MSIX_HW_INT_MASK_AD, 0x2B}, 11239b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_HW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x2D}, 11249b58419eSGolan Ben Ami {MSIX_HW_INT_CAUSES_REG_HAP, CSR_MSIX_HW_INT_MASK_AD, 0x2E}, 11259b58419eSGolan Ben Ami }; 11269b58419eSGolan Ben Ami 11277ca00409SHaim Dreyfuss static void iwl_pcie_map_non_rx_causes(struct iwl_trans *trans) 11287ca00409SHaim Dreyfuss { 11297ca00409SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 11307ca00409SHaim Dreyfuss int val = trans_pcie->def_irq | MSIX_NON_AUTO_CLEAR_CAUSE; 11319b58419eSGolan Ben Ami int i, arr_size = 1132ff911dcaSShaul Triebitz (trans->cfg->device_family != IWL_DEVICE_FAMILY_22560) ? 11339b58419eSGolan Ben Ami ARRAY_SIZE(causes_list) : ARRAY_SIZE(causes_list_v2); 11347ca00409SHaim Dreyfuss 11357ca00409SHaim Dreyfuss /* 11367ca00409SHaim Dreyfuss * Access all non RX causes and map them to the default irq. 11377ca00409SHaim Dreyfuss * In case we are missing at least one interrupt vector, 11387ca00409SHaim Dreyfuss * the first interrupt vector will serve non-RX and FBQ causes. 11397ca00409SHaim Dreyfuss */ 11409b58419eSGolan Ben Ami for (i = 0; i < arr_size; i++) { 11419b58419eSGolan Ben Ami struct iwl_causes_list *causes = 1142ff911dcaSShaul Triebitz (trans->cfg->device_family != IWL_DEVICE_FAMILY_22560) ? 11439b58419eSGolan Ben Ami causes_list : causes_list_v2; 11449b58419eSGolan Ben Ami 11459b58419eSGolan Ben Ami iwl_write8(trans, CSR_MSIX_IVAR(causes[i].addr), val); 11469b58419eSGolan Ben Ami iwl_clear_bit(trans, causes[i].mask_reg, 11479b58419eSGolan Ben Ami causes[i].cause_num); 11487ca00409SHaim Dreyfuss } 11497ca00409SHaim Dreyfuss } 11507ca00409SHaim Dreyfuss 11517ca00409SHaim Dreyfuss static void iwl_pcie_map_rx_causes(struct iwl_trans *trans) 11527ca00409SHaim Dreyfuss { 11537ca00409SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 11547ca00409SHaim Dreyfuss u32 offset = 11557ca00409SHaim Dreyfuss trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0; 11567ca00409SHaim Dreyfuss u32 val, idx; 11577ca00409SHaim Dreyfuss 11587ca00409SHaim Dreyfuss /* 11597ca00409SHaim Dreyfuss * The first RX queue - fallback queue, which is designated for 11607ca00409SHaim Dreyfuss * management frame, command responses etc, is always mapped to the 11617ca00409SHaim Dreyfuss * first interrupt vector. The other RX queues are mapped to 11627ca00409SHaim Dreyfuss * the other (N - 2) interrupt vectors. 11637ca00409SHaim Dreyfuss */ 11647ca00409SHaim Dreyfuss val = BIT(MSIX_FH_INT_CAUSES_Q(0)); 11657ca00409SHaim Dreyfuss for (idx = 1; idx < trans->num_rx_queues; idx++) { 11667ca00409SHaim Dreyfuss iwl_write8(trans, CSR_MSIX_RX_IVAR(idx), 11677ca00409SHaim Dreyfuss MSIX_FH_INT_CAUSES_Q(idx - offset)); 11687ca00409SHaim Dreyfuss val |= BIT(MSIX_FH_INT_CAUSES_Q(idx)); 11697ca00409SHaim Dreyfuss } 11707ca00409SHaim Dreyfuss iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~val); 11717ca00409SHaim Dreyfuss 11727ca00409SHaim Dreyfuss val = MSIX_FH_INT_CAUSES_Q(0); 11737ca00409SHaim Dreyfuss if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX) 11747ca00409SHaim Dreyfuss val |= MSIX_NON_AUTO_CLEAR_CAUSE; 11757ca00409SHaim Dreyfuss iwl_write8(trans, CSR_MSIX_RX_IVAR(0), val); 11767ca00409SHaim Dreyfuss 11777ca00409SHaim Dreyfuss if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS) 11787ca00409SHaim Dreyfuss iwl_write8(trans, CSR_MSIX_RX_IVAR(1), val); 11797ca00409SHaim Dreyfuss } 11807ca00409SHaim Dreyfuss 118177c09bc8SSara Sharon void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie) 11827ca00409SHaim Dreyfuss { 11837ca00409SHaim Dreyfuss struct iwl_trans *trans = trans_pcie->trans; 11847ca00409SHaim Dreyfuss 11857ca00409SHaim Dreyfuss if (!trans_pcie->msix_enabled) { 1186d7270d61SHaim Dreyfuss if (trans->cfg->mq_rx_supported && 1187d7270d61SHaim Dreyfuss test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 1188ea695b7cSShaul Triebitz iwl_write_umac_prph(trans, UREG_CHICK, 11897ca00409SHaim Dreyfuss UREG_CHICK_MSI_ENABLE); 11907ca00409SHaim Dreyfuss return; 11917ca00409SHaim Dreyfuss } 1192d7270d61SHaim Dreyfuss /* 1193d7270d61SHaim Dreyfuss * The IVAR table needs to be configured again after reset, 1194d7270d61SHaim Dreyfuss * but if the device is disabled, we can't write to 1195d7270d61SHaim Dreyfuss * prph. 1196d7270d61SHaim Dreyfuss */ 1197d7270d61SHaim Dreyfuss if (test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 1198ea695b7cSShaul Triebitz iwl_write_umac_prph(trans, UREG_CHICK, UREG_CHICK_MSIX_ENABLE); 11997ca00409SHaim Dreyfuss 12007ca00409SHaim Dreyfuss /* 12017ca00409SHaim Dreyfuss * Each cause from the causes list above and the RX causes is 12027ca00409SHaim Dreyfuss * represented as a byte in the IVAR table. The first nibble 12037ca00409SHaim Dreyfuss * represents the bound interrupt vector of the cause, the second 12047ca00409SHaim Dreyfuss * represents no auto clear for this cause. This will be set if its 12057ca00409SHaim Dreyfuss * interrupt vector is bound to serve other causes. 12067ca00409SHaim Dreyfuss */ 12077ca00409SHaim Dreyfuss iwl_pcie_map_rx_causes(trans); 12087ca00409SHaim Dreyfuss 12097ca00409SHaim Dreyfuss iwl_pcie_map_non_rx_causes(trans); 121083730058SHaim Dreyfuss } 12117ca00409SHaim Dreyfuss 121283730058SHaim Dreyfuss static void iwl_pcie_init_msix(struct iwl_trans_pcie *trans_pcie) 121383730058SHaim Dreyfuss { 121483730058SHaim Dreyfuss struct iwl_trans *trans = trans_pcie->trans; 121583730058SHaim Dreyfuss 121683730058SHaim Dreyfuss iwl_pcie_conf_msix_hw(trans_pcie); 121783730058SHaim Dreyfuss 121883730058SHaim Dreyfuss if (!trans_pcie->msix_enabled) 121983730058SHaim Dreyfuss return; 122083730058SHaim Dreyfuss 122183730058SHaim Dreyfuss trans_pcie->fh_init_mask = ~iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD); 12227ca00409SHaim Dreyfuss trans_pcie->fh_mask = trans_pcie->fh_init_mask; 122383730058SHaim Dreyfuss trans_pcie->hw_init_mask = ~iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD); 12247ca00409SHaim Dreyfuss trans_pcie->hw_mask = trans_pcie->hw_init_mask; 12257ca00409SHaim Dreyfuss } 12267ca00409SHaim Dreyfuss 1227e705c121SKalle Valo static void _iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power) 1228e705c121SKalle Valo { 1229e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1230e705c121SKalle Valo 1231e705c121SKalle Valo lockdep_assert_held(&trans_pcie->mutex); 1232e705c121SKalle Valo 1233e705c121SKalle Valo if (trans_pcie->is_down) 1234e705c121SKalle Valo return; 1235e705c121SKalle Valo 1236e705c121SKalle Valo trans_pcie->is_down = true; 1237e705c121SKalle Valo 12380232d2cdSSara Sharon /* Stop dbgc before stopping device */ 12395cfe79c8SSara Sharon _iwl_fw_dbg_stop_recording(trans, NULL); 12400232d2cdSSara Sharon 1241e705c121SKalle Valo /* tell the device to stop sending interrupts */ 1242e705c121SKalle Valo iwl_disable_interrupts(trans); 1243e705c121SKalle Valo 1244e705c121SKalle Valo /* device going down, Stop using ICT table */ 1245e705c121SKalle Valo iwl_pcie_disable_ict(trans); 1246e705c121SKalle Valo 1247e705c121SKalle Valo /* 1248e705c121SKalle Valo * If a HW restart happens during firmware loading, 1249e705c121SKalle Valo * then the firmware loading might call this function 1250e705c121SKalle Valo * and later it might be called again due to the 1251e705c121SKalle Valo * restart. So don't process again if the device is 1252e705c121SKalle Valo * already dead. 1253e705c121SKalle Valo */ 1254e705c121SKalle Valo if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) { 1255a6bd005fSEmmanuel Grumbach IWL_DEBUG_INFO(trans, 1256a6bd005fSEmmanuel Grumbach "DEVICE_ENABLED bit was set and is now cleared\n"); 1257e705c121SKalle Valo iwl_pcie_tx_stop(trans); 1258e705c121SKalle Valo iwl_pcie_rx_stop(trans); 1259e705c121SKalle Valo 1260e705c121SKalle Valo /* Power-down device's busmaster DMA clocks */ 1261e705c121SKalle Valo if (!trans->cfg->apmg_not_supported) { 1262e705c121SKalle Valo iwl_write_prph(trans, APMG_CLK_DIS_REG, 1263e705c121SKalle Valo APMG_CLK_VAL_DMA_CLK_RQT); 1264e705c121SKalle Valo udelay(5); 1265e705c121SKalle Valo } 1266e705c121SKalle Valo } 1267e705c121SKalle Valo 1268e705c121SKalle Valo /* Make sure (redundant) we've released our request to stay awake */ 1269e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1270a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_mac_access_req)); 1271e705c121SKalle Valo 1272e705c121SKalle Valo /* Stop the device, and put it in low power state */ 1273e705c121SKalle Valo iwl_pcie_apm_stop(trans, false); 1274e705c121SKalle Valo 1275870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 1276e705c121SKalle Valo 1277e705c121SKalle Valo /* 1278f4a1f04aSGolan Ben Ami * Upon stop, the IVAR table gets erased, so msi-x won't 1279f4a1f04aSGolan Ben Ami * work. This causes a bug in RF-KILL flows, since the interrupt 1280f4a1f04aSGolan Ben Ami * that enables radio won't fire on the correct irq, and the 1281f4a1f04aSGolan Ben Ami * driver won't be able to handle the interrupt. 1282f4a1f04aSGolan Ben Ami * Configure the IVAR table again after reset. 1283f4a1f04aSGolan Ben Ami */ 1284f4a1f04aSGolan Ben Ami iwl_pcie_conf_msix_hw(trans_pcie); 1285f4a1f04aSGolan Ben Ami 1286f4a1f04aSGolan Ben Ami /* 1287e705c121SKalle Valo * Upon stop, the APM issues an interrupt if HW RF kill is set. 1288e705c121SKalle Valo * This is a bug in certain verions of the hardware. 1289e705c121SKalle Valo * Certain devices also keep sending HW RF kill interrupt all 1290e705c121SKalle Valo * the time, unless the interrupt is ACKed even if the interrupt 1291e705c121SKalle Valo * should be masked. Re-ACK all the interrupts here. 1292e705c121SKalle Valo */ 1293e705c121SKalle Valo iwl_disable_interrupts(trans); 1294e705c121SKalle Valo 1295e705c121SKalle Valo /* clear all status bits */ 1296e705c121SKalle Valo clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status); 1297e705c121SKalle Valo clear_bit(STATUS_INT_ENABLED, &trans->status); 1298e705c121SKalle Valo clear_bit(STATUS_TPOWER_PMI, &trans->status); 1299e705c121SKalle Valo 1300e705c121SKalle Valo /* 1301e705c121SKalle Valo * Even if we stop the HW, we still want the RF kill 1302e705c121SKalle Valo * interrupt 1303e705c121SKalle Valo */ 1304e705c121SKalle Valo iwl_enable_rfkill_int(trans); 1305e705c121SKalle Valo 1306a6bd005fSEmmanuel Grumbach /* re-take ownership to prevent other users from stealing the device */ 1307e705c121SKalle Valo iwl_pcie_prepare_card_hw(trans); 1308e705c121SKalle Valo } 1309e705c121SKalle Valo 1310eda50cdeSSara Sharon void iwl_pcie_synchronize_irqs(struct iwl_trans *trans) 13112e5d4a8fSHaim Dreyfuss { 13122e5d4a8fSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 13132e5d4a8fSHaim Dreyfuss 13142e5d4a8fSHaim Dreyfuss if (trans_pcie->msix_enabled) { 13152e5d4a8fSHaim Dreyfuss int i; 13162e5d4a8fSHaim Dreyfuss 1317496d83caSHaim Dreyfuss for (i = 0; i < trans_pcie->alloc_vecs; i++) 13182e5d4a8fSHaim Dreyfuss synchronize_irq(trans_pcie->msix_entries[i].vector); 13192e5d4a8fSHaim Dreyfuss } else { 13202e5d4a8fSHaim Dreyfuss synchronize_irq(trans_pcie->pci_dev->irq); 13212e5d4a8fSHaim Dreyfuss } 13222e5d4a8fSHaim Dreyfuss } 13232e5d4a8fSHaim Dreyfuss 1324a6bd005fSEmmanuel Grumbach static int iwl_trans_pcie_start_fw(struct iwl_trans *trans, 1325a6bd005fSEmmanuel Grumbach const struct fw_img *fw, bool run_in_rfkill) 1326a6bd005fSEmmanuel Grumbach { 1327a6bd005fSEmmanuel Grumbach struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1328a6bd005fSEmmanuel Grumbach bool hw_rfkill; 1329a6bd005fSEmmanuel Grumbach int ret; 1330a6bd005fSEmmanuel Grumbach 1331a6bd005fSEmmanuel Grumbach /* This may fail if AMT took ownership of the device */ 1332a6bd005fSEmmanuel Grumbach if (iwl_pcie_prepare_card_hw(trans)) { 1333a6bd005fSEmmanuel Grumbach IWL_WARN(trans, "Exit HW not ready\n"); 1334a6bd005fSEmmanuel Grumbach ret = -EIO; 1335a6bd005fSEmmanuel Grumbach goto out; 1336a6bd005fSEmmanuel Grumbach } 1337a6bd005fSEmmanuel Grumbach 1338a6bd005fSEmmanuel Grumbach iwl_enable_rfkill_int(trans); 1339a6bd005fSEmmanuel Grumbach 1340a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_INT, 0xFFFFFFFF); 1341a6bd005fSEmmanuel Grumbach 1342a6bd005fSEmmanuel Grumbach /* 1343a6bd005fSEmmanuel Grumbach * We enabled the RF-Kill interrupt and the handler may very 1344a6bd005fSEmmanuel Grumbach * well be running. Disable the interrupts to make sure no other 1345a6bd005fSEmmanuel Grumbach * interrupt can be fired. 1346a6bd005fSEmmanuel Grumbach */ 1347a6bd005fSEmmanuel Grumbach iwl_disable_interrupts(trans); 1348a6bd005fSEmmanuel Grumbach 1349a6bd005fSEmmanuel Grumbach /* Make sure it finished running */ 13502e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1351a6bd005fSEmmanuel Grumbach 1352a6bd005fSEmmanuel Grumbach mutex_lock(&trans_pcie->mutex); 1353a6bd005fSEmmanuel Grumbach 1354a6bd005fSEmmanuel Grumbach /* If platform's RF_KILL switch is NOT set to KILL */ 13559ad8fd0bSJohannes Berg hw_rfkill = iwl_pcie_check_hw_rf_kill(trans); 1356a6bd005fSEmmanuel Grumbach if (hw_rfkill && !run_in_rfkill) { 1357a6bd005fSEmmanuel Grumbach ret = -ERFKILL; 1358a6bd005fSEmmanuel Grumbach goto out; 1359a6bd005fSEmmanuel Grumbach } 1360a6bd005fSEmmanuel Grumbach 1361a6bd005fSEmmanuel Grumbach /* Someone called stop_device, don't try to start_fw */ 1362a6bd005fSEmmanuel Grumbach if (trans_pcie->is_down) { 1363a6bd005fSEmmanuel Grumbach IWL_WARN(trans, 1364a6bd005fSEmmanuel Grumbach "Can't start_fw since the HW hasn't been started\n"); 136520aa99bbSAnton Protopopov ret = -EIO; 1366a6bd005fSEmmanuel Grumbach goto out; 1367a6bd005fSEmmanuel Grumbach } 1368a6bd005fSEmmanuel Grumbach 1369a6bd005fSEmmanuel Grumbach /* make sure rfkill handshake bits are cleared */ 1370a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 1371a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, 1372a6bd005fSEmmanuel Grumbach CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED); 1373a6bd005fSEmmanuel Grumbach 1374a6bd005fSEmmanuel Grumbach /* clear (again), then enable host interrupts */ 1375a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_INT, 0xFFFFFFFF); 1376a6bd005fSEmmanuel Grumbach 1377a6bd005fSEmmanuel Grumbach ret = iwl_pcie_nic_init(trans); 1378a6bd005fSEmmanuel Grumbach if (ret) { 1379a6bd005fSEmmanuel Grumbach IWL_ERR(trans, "Unable to init nic\n"); 1380a6bd005fSEmmanuel Grumbach goto out; 1381a6bd005fSEmmanuel Grumbach } 1382a6bd005fSEmmanuel Grumbach 1383a6bd005fSEmmanuel Grumbach /* 1384a6bd005fSEmmanuel Grumbach * Now, we load the firmware and don't want to be interrupted, even 1385a6bd005fSEmmanuel Grumbach * by the RF-Kill interrupt (hence mask all the interrupt besides the 1386a6bd005fSEmmanuel Grumbach * FH_TX interrupt which is needed to load the firmware). If the 1387a6bd005fSEmmanuel Grumbach * RF-Kill switch is toggled, we will find out after having loaded 1388a6bd005fSEmmanuel Grumbach * the firmware and return the proper value to the caller. 1389a6bd005fSEmmanuel Grumbach */ 1390a6bd005fSEmmanuel Grumbach iwl_enable_fw_load_int(trans); 1391a6bd005fSEmmanuel Grumbach 1392a6bd005fSEmmanuel Grumbach /* really make sure rfkill handshake bits are cleared */ 1393a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 1394a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 1395a6bd005fSEmmanuel Grumbach 1396a6bd005fSEmmanuel Grumbach /* Load the given image to the HW */ 13976e584873SSara Sharon if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) 1398a6bd005fSEmmanuel Grumbach ret = iwl_pcie_load_given_ucode_8000(trans, fw); 1399a6bd005fSEmmanuel Grumbach else 1400a6bd005fSEmmanuel Grumbach ret = iwl_pcie_load_given_ucode(trans, fw); 1401a6bd005fSEmmanuel Grumbach 1402a6bd005fSEmmanuel Grumbach /* re-check RF-Kill state since we may have missed the interrupt */ 14039ad8fd0bSJohannes Berg hw_rfkill = iwl_pcie_check_hw_rf_kill(trans); 1404a6bd005fSEmmanuel Grumbach if (hw_rfkill && !run_in_rfkill) 1405a6bd005fSEmmanuel Grumbach ret = -ERFKILL; 1406a6bd005fSEmmanuel Grumbach 1407a6bd005fSEmmanuel Grumbach out: 1408a6bd005fSEmmanuel Grumbach mutex_unlock(&trans_pcie->mutex); 1409a6bd005fSEmmanuel Grumbach return ret; 1410a6bd005fSEmmanuel Grumbach } 1411a6bd005fSEmmanuel Grumbach 1412a6bd005fSEmmanuel Grumbach static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr) 1413a6bd005fSEmmanuel Grumbach { 1414a6bd005fSEmmanuel Grumbach iwl_pcie_reset_ict(trans); 1415a6bd005fSEmmanuel Grumbach iwl_pcie_tx_start(trans, scd_addr); 1416a6bd005fSEmmanuel Grumbach } 1417a6bd005fSEmmanuel Grumbach 1418326477e4SJohannes Berg void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans, 1419326477e4SJohannes Berg bool was_in_rfkill) 1420326477e4SJohannes Berg { 1421326477e4SJohannes Berg bool hw_rfkill; 1422326477e4SJohannes Berg 1423326477e4SJohannes Berg /* 1424326477e4SJohannes Berg * Check again since the RF kill state may have changed while 1425326477e4SJohannes Berg * all the interrupts were disabled, in this case we couldn't 1426326477e4SJohannes Berg * receive the RF kill interrupt and update the state in the 1427326477e4SJohannes Berg * op_mode. 1428326477e4SJohannes Berg * Don't call the op_mode if the rkfill state hasn't changed. 1429326477e4SJohannes Berg * This allows the op_mode to call stop_device from the rfkill 1430326477e4SJohannes Berg * notification without endless recursion. Under very rare 1431326477e4SJohannes Berg * circumstances, we might have a small recursion if the rfkill 1432326477e4SJohannes Berg * state changed exactly now while we were called from stop_device. 1433326477e4SJohannes Berg * This is very unlikely but can happen and is supported. 1434326477e4SJohannes Berg */ 1435326477e4SJohannes Berg hw_rfkill = iwl_is_rfkill_set(trans); 1436326477e4SJohannes Berg if (hw_rfkill) { 1437326477e4SJohannes Berg set_bit(STATUS_RFKILL_HW, &trans->status); 1438326477e4SJohannes Berg set_bit(STATUS_RFKILL_OPMODE, &trans->status); 1439326477e4SJohannes Berg } else { 1440326477e4SJohannes Berg clear_bit(STATUS_RFKILL_HW, &trans->status); 1441326477e4SJohannes Berg clear_bit(STATUS_RFKILL_OPMODE, &trans->status); 1442326477e4SJohannes Berg } 1443326477e4SJohannes Berg if (hw_rfkill != was_in_rfkill) 1444326477e4SJohannes Berg iwl_trans_pcie_rf_kill(trans, hw_rfkill); 1445326477e4SJohannes Berg } 1446326477e4SJohannes Berg 1447e705c121SKalle Valo static void iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power) 1448e705c121SKalle Valo { 1449e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1450326477e4SJohannes Berg bool was_in_rfkill; 1451e705c121SKalle Valo 1452e705c121SKalle Valo mutex_lock(&trans_pcie->mutex); 1453326477e4SJohannes Berg trans_pcie->opmode_down = true; 1454326477e4SJohannes Berg was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 1455e705c121SKalle Valo _iwl_trans_pcie_stop_device(trans, low_power); 1456326477e4SJohannes Berg iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill); 1457e705c121SKalle Valo mutex_unlock(&trans_pcie->mutex); 1458e705c121SKalle Valo } 1459e705c121SKalle Valo 1460e705c121SKalle Valo void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state) 1461e705c121SKalle Valo { 1462e705c121SKalle Valo struct iwl_trans_pcie __maybe_unused *trans_pcie = 1463e705c121SKalle Valo IWL_TRANS_GET_PCIE_TRANS(trans); 1464e705c121SKalle Valo 1465e705c121SKalle Valo lockdep_assert_held(&trans_pcie->mutex); 1466e705c121SKalle Valo 1467326477e4SJohannes Berg IWL_WARN(trans, "reporting RF_KILL (radio %s)\n", 1468326477e4SJohannes Berg state ? "disabled" : "enabled"); 146977c09bc8SSara Sharon if (iwl_op_mode_hw_rf_kill(trans->op_mode, state)) { 147077c09bc8SSara Sharon if (trans->cfg->gen2) 147177c09bc8SSara Sharon _iwl_trans_pcie_gen2_stop_device(trans, true); 147277c09bc8SSara Sharon else 1473e705c121SKalle Valo _iwl_trans_pcie_stop_device(trans, true); 1474e705c121SKalle Valo } 147577c09bc8SSara Sharon } 1476e705c121SKalle Valo 147723ae6128SMatti Gottlieb static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test, 147823ae6128SMatti Gottlieb bool reset) 1479e705c121SKalle Valo { 148023ae6128SMatti Gottlieb if (!reset) { 1481e705c121SKalle Valo /* Enable persistence mode to avoid reset */ 1482e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 1483e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PERSIST_MODE); 1484e705c121SKalle Valo } 1485e705c121SKalle Valo 1486e705c121SKalle Valo iwl_disable_interrupts(trans); 1487e705c121SKalle Valo 1488e705c121SKalle Valo /* 1489e705c121SKalle Valo * in testing mode, the host stays awake and the 1490e705c121SKalle Valo * hardware won't be reset (not even partially) 1491e705c121SKalle Valo */ 1492e705c121SKalle Valo if (test) 1493e705c121SKalle Valo return; 1494e705c121SKalle Valo 1495e705c121SKalle Valo iwl_pcie_disable_ict(trans); 1496e705c121SKalle Valo 14972e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1498e705c121SKalle Valo 1499e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1500a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_mac_access_req)); 1501e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1502a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_init_done)); 1503e705c121SKalle Valo 150423ae6128SMatti Gottlieb if (reset) { 1505e705c121SKalle Valo /* 1506e705c121SKalle Valo * reset TX queues -- some of their registers reset during S3 1507e705c121SKalle Valo * so if we don't reset everything here the D3 image would try 1508e705c121SKalle Valo * to execute some invalid memory upon resume 1509e705c121SKalle Valo */ 1510e705c121SKalle Valo iwl_trans_pcie_tx_reset(trans); 1511e705c121SKalle Valo } 1512e705c121SKalle Valo 1513e705c121SKalle Valo iwl_pcie_set_pwr(trans, true); 1514e705c121SKalle Valo } 1515e705c121SKalle Valo 1516e705c121SKalle Valo static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans, 1517e705c121SKalle Valo enum iwl_d3_status *status, 151823ae6128SMatti Gottlieb bool test, bool reset) 1519e705c121SKalle Valo { 1520d7270d61SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1521e705c121SKalle Valo u32 val; 1522e705c121SKalle Valo int ret; 1523e705c121SKalle Valo 1524e705c121SKalle Valo if (test) { 1525e705c121SKalle Valo iwl_enable_interrupts(trans); 1526e705c121SKalle Valo *status = IWL_D3_STATUS_ALIVE; 1527e705c121SKalle Valo return 0; 1528e705c121SKalle Valo } 1529e705c121SKalle Valo 1530a8cbb46fSGolan Ben Ami iwl_set_bit(trans, CSR_GP_CNTRL, 1531a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_mac_access_req)); 1532e705c121SKalle Valo 1533c96b5eecSJohannes Berg ret = iwl_finish_nic_init(trans); 1534c96b5eecSJohannes Berg if (ret) 1535e705c121SKalle Valo return ret; 1536e705c121SKalle Valo 1537f98ad635SEmmanuel Grumbach /* 1538f98ad635SEmmanuel Grumbach * Reconfigure IVAR table in case of MSIX or reset ict table in 1539f98ad635SEmmanuel Grumbach * MSI mode since HW reset erased it. 1540f98ad635SEmmanuel Grumbach * Also enables interrupts - none will happen as 1541f98ad635SEmmanuel Grumbach * the device doesn't know we're waking it up, only when 1542f98ad635SEmmanuel Grumbach * the opmode actually tells it after this call. 1543f98ad635SEmmanuel Grumbach */ 1544f98ad635SEmmanuel Grumbach iwl_pcie_conf_msix_hw(trans_pcie); 1545f98ad635SEmmanuel Grumbach if (!trans_pcie->msix_enabled) 1546f98ad635SEmmanuel Grumbach iwl_pcie_reset_ict(trans); 1547f98ad635SEmmanuel Grumbach iwl_enable_interrupts(trans); 1548f98ad635SEmmanuel Grumbach 1549e705c121SKalle Valo iwl_pcie_set_pwr(trans, false); 1550e705c121SKalle Valo 155123ae6128SMatti Gottlieb if (!reset) { 1552e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1553a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_mac_access_req)); 1554e705c121SKalle Valo } else { 1555e705c121SKalle Valo iwl_trans_pcie_tx_reset(trans); 1556e705c121SKalle Valo 1557e705c121SKalle Valo ret = iwl_pcie_rx_init(trans); 1558e705c121SKalle Valo if (ret) { 1559e705c121SKalle Valo IWL_ERR(trans, 1560e705c121SKalle Valo "Failed to resume the device (RX reset)\n"); 1561e705c121SKalle Valo return ret; 1562e705c121SKalle Valo } 1563e705c121SKalle Valo } 1564e705c121SKalle Valo 156582ea7966SSara Sharon IWL_DEBUG_POWER(trans, "WFPM value upon resume = 0x%08X\n", 1566ea695b7cSShaul Triebitz iwl_read_umac_prph(trans, WFPM_GP2)); 156782ea7966SSara Sharon 1568e705c121SKalle Valo val = iwl_read32(trans, CSR_RESET); 1569e705c121SKalle Valo if (val & CSR_RESET_REG_FLAG_NEVO_RESET) 1570e705c121SKalle Valo *status = IWL_D3_STATUS_RESET; 1571e705c121SKalle Valo else 1572e705c121SKalle Valo *status = IWL_D3_STATUS_ALIVE; 1573e705c121SKalle Valo 1574e705c121SKalle Valo return 0; 1575e705c121SKalle Valo } 1576e705c121SKalle Valo 15772e5d4a8fSHaim Dreyfuss static void iwl_pcie_set_interrupt_capa(struct pci_dev *pdev, 15782e5d4a8fSHaim Dreyfuss struct iwl_trans *trans) 15792e5d4a8fSHaim Dreyfuss { 15802e5d4a8fSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1581ab1068d6SHao Wei Tee int max_irqs, num_irqs, i, ret; 15822e5d4a8fSHaim Dreyfuss u16 pci_cmd; 15832e5d4a8fSHaim Dreyfuss 158406f4b081SSara Sharon if (!trans->cfg->mq_rx_supported) 158506f4b081SSara Sharon goto enable_msi; 158606f4b081SSara Sharon 1587ab1068d6SHao Wei Tee max_irqs = min_t(u32, num_online_cpus() + 2, IWL_MAX_RX_HW_QUEUES); 158806f4b081SSara Sharon for (i = 0; i < max_irqs; i++) 15892e5d4a8fSHaim Dreyfuss trans_pcie->msix_entries[i].entry = i; 15902e5d4a8fSHaim Dreyfuss 159106f4b081SSara Sharon num_irqs = pci_enable_msix_range(pdev, trans_pcie->msix_entries, 15922e5d4a8fSHaim Dreyfuss MSIX_MIN_INTERRUPT_VECTORS, 159306f4b081SSara Sharon max_irqs); 159406f4b081SSara Sharon if (num_irqs < 0) { 1595496d83caSHaim Dreyfuss IWL_DEBUG_INFO(trans, 159606f4b081SSara Sharon "Failed to enable msi-x mode (ret %d). Moving to msi mode.\n", 159706f4b081SSara Sharon num_irqs); 159806f4b081SSara Sharon goto enable_msi; 1599496d83caSHaim Dreyfuss } 160006f4b081SSara Sharon trans_pcie->def_irq = (num_irqs == max_irqs) ? num_irqs - 1 : 0; 1601496d83caSHaim Dreyfuss 16022e5d4a8fSHaim Dreyfuss IWL_DEBUG_INFO(trans, 160306f4b081SSara Sharon "MSI-X enabled. %d interrupt vectors were allocated\n", 160406f4b081SSara Sharon num_irqs); 160506f4b081SSara Sharon 1606496d83caSHaim Dreyfuss /* 160706f4b081SSara Sharon * In case the OS provides fewer interrupts than requested, different 160806f4b081SSara Sharon * causes will share the same interrupt vector as follows: 1609496d83caSHaim Dreyfuss * One interrupt less: non rx causes shared with FBQ. 1610496d83caSHaim Dreyfuss * Two interrupts less: non rx causes shared with FBQ and RSS. 1611496d83caSHaim Dreyfuss * More than two interrupts: we will use fewer RSS queues. 1612496d83caSHaim Dreyfuss */ 1613ab1068d6SHao Wei Tee if (num_irqs <= max_irqs - 2) { 161406f4b081SSara Sharon trans_pcie->trans->num_rx_queues = num_irqs + 1; 1615496d83caSHaim Dreyfuss trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX | 1616496d83caSHaim Dreyfuss IWL_SHARED_IRQ_FIRST_RSS; 1617ab1068d6SHao Wei Tee } else if (num_irqs == max_irqs - 1) { 161806f4b081SSara Sharon trans_pcie->trans->num_rx_queues = num_irqs; 1619496d83caSHaim Dreyfuss trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX; 1620496d83caSHaim Dreyfuss } else { 162106f4b081SSara Sharon trans_pcie->trans->num_rx_queues = num_irqs - 1; 1622496d83caSHaim Dreyfuss } 1623ab1068d6SHao Wei Tee WARN_ON(trans_pcie->trans->num_rx_queues > IWL_MAX_RX_HW_QUEUES); 16242e5d4a8fSHaim Dreyfuss 162506f4b081SSara Sharon trans_pcie->alloc_vecs = num_irqs; 1626496d83caSHaim Dreyfuss trans_pcie->msix_enabled = true; 16272e5d4a8fSHaim Dreyfuss return; 16282e5d4a8fSHaim Dreyfuss 162906f4b081SSara Sharon enable_msi: 163006f4b081SSara Sharon ret = pci_enable_msi(pdev); 163106f4b081SSara Sharon if (ret) { 163206f4b081SSara Sharon dev_err(&pdev->dev, "pci_enable_msi failed - %d\n", ret); 16332e5d4a8fSHaim Dreyfuss /* enable rfkill interrupt: hw bug w/a */ 16342e5d4a8fSHaim Dreyfuss pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd); 16352e5d4a8fSHaim Dreyfuss if (pci_cmd & PCI_COMMAND_INTX_DISABLE) { 16362e5d4a8fSHaim Dreyfuss pci_cmd &= ~PCI_COMMAND_INTX_DISABLE; 16372e5d4a8fSHaim Dreyfuss pci_write_config_word(pdev, PCI_COMMAND, pci_cmd); 16382e5d4a8fSHaim Dreyfuss } 16392e5d4a8fSHaim Dreyfuss } 16402e5d4a8fSHaim Dreyfuss } 16412e5d4a8fSHaim Dreyfuss 16427c8d91ebSHaim Dreyfuss static void iwl_pcie_irq_set_affinity(struct iwl_trans *trans) 16437c8d91ebSHaim Dreyfuss { 16447c8d91ebSHaim Dreyfuss int iter_rx_q, i, ret, cpu, offset; 16457c8d91ebSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 16467c8d91ebSHaim Dreyfuss 16477c8d91ebSHaim Dreyfuss i = trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 0 : 1; 16487c8d91ebSHaim Dreyfuss iter_rx_q = trans_pcie->trans->num_rx_queues - 1 + i; 16497c8d91ebSHaim Dreyfuss offset = 1 + i; 16507c8d91ebSHaim Dreyfuss for (; i < iter_rx_q ; i++) { 16517c8d91ebSHaim Dreyfuss /* 16527c8d91ebSHaim Dreyfuss * Get the cpu prior to the place to search 16537c8d91ebSHaim Dreyfuss * (i.e. return will be > i - 1). 16547c8d91ebSHaim Dreyfuss */ 16557c8d91ebSHaim Dreyfuss cpu = cpumask_next(i - offset, cpu_online_mask); 16567c8d91ebSHaim Dreyfuss cpumask_set_cpu(cpu, &trans_pcie->affinity_mask[i]); 16577c8d91ebSHaim Dreyfuss ret = irq_set_affinity_hint(trans_pcie->msix_entries[i].vector, 16587c8d91ebSHaim Dreyfuss &trans_pcie->affinity_mask[i]); 16597c8d91ebSHaim Dreyfuss if (ret) 16607c8d91ebSHaim Dreyfuss IWL_ERR(trans_pcie->trans, 16617c8d91ebSHaim Dreyfuss "Failed to set affinity mask for IRQ %d\n", 16627c8d91ebSHaim Dreyfuss i); 16637c8d91ebSHaim Dreyfuss } 16647c8d91ebSHaim Dreyfuss } 16657c8d91ebSHaim Dreyfuss 16662e5d4a8fSHaim Dreyfuss static int iwl_pcie_init_msix_handler(struct pci_dev *pdev, 16672e5d4a8fSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie) 16682e5d4a8fSHaim Dreyfuss { 1669496d83caSHaim Dreyfuss int i; 16702e5d4a8fSHaim Dreyfuss 1671496d83caSHaim Dreyfuss for (i = 0; i < trans_pcie->alloc_vecs; i++) { 16722e5d4a8fSHaim Dreyfuss int ret; 16735a41a86cSSharon Dvir struct msix_entry *msix_entry; 167464fa3affSSharon Dvir const char *qname = queue_name(&pdev->dev, trans_pcie, i); 167564fa3affSSharon Dvir 167664fa3affSSharon Dvir if (!qname) 167764fa3affSSharon Dvir return -ENOMEM; 16782e5d4a8fSHaim Dreyfuss 16795a41a86cSSharon Dvir msix_entry = &trans_pcie->msix_entries[i]; 16805a41a86cSSharon Dvir ret = devm_request_threaded_irq(&pdev->dev, 16815a41a86cSSharon Dvir msix_entry->vector, 16822e5d4a8fSHaim Dreyfuss iwl_pcie_msix_isr, 1683496d83caSHaim Dreyfuss (i == trans_pcie->def_irq) ? 16842e5d4a8fSHaim Dreyfuss iwl_pcie_irq_msix_handler : 16852e5d4a8fSHaim Dreyfuss iwl_pcie_irq_rx_msix_handler, 16862e5d4a8fSHaim Dreyfuss IRQF_SHARED, 168764fa3affSSharon Dvir qname, 16885a41a86cSSharon Dvir msix_entry); 16892e5d4a8fSHaim Dreyfuss if (ret) { 16902e5d4a8fSHaim Dreyfuss IWL_ERR(trans_pcie->trans, 16912e5d4a8fSHaim Dreyfuss "Error allocating IRQ %d\n", i); 16925a41a86cSSharon Dvir 16932e5d4a8fSHaim Dreyfuss return ret; 16942e5d4a8fSHaim Dreyfuss } 16952e5d4a8fSHaim Dreyfuss } 16967c8d91ebSHaim Dreyfuss iwl_pcie_irq_set_affinity(trans_pcie->trans); 16972e5d4a8fSHaim Dreyfuss 16982e5d4a8fSHaim Dreyfuss return 0; 16992e5d4a8fSHaim Dreyfuss } 17002e5d4a8fSHaim Dreyfuss 170144f61b5cSShahar S Matityahu static int iwl_trans_pcie_clear_persistence_bit(struct iwl_trans *trans) 170244f61b5cSShahar S Matityahu { 170344f61b5cSShahar S Matityahu u32 hpm, wprot; 170444f61b5cSShahar S Matityahu 170544f61b5cSShahar S Matityahu switch (trans->cfg->device_family) { 170644f61b5cSShahar S Matityahu case IWL_DEVICE_FAMILY_9000: 170744f61b5cSShahar S Matityahu wprot = PREG_PRPH_WPROT_9000; 170844f61b5cSShahar S Matityahu break; 170944f61b5cSShahar S Matityahu case IWL_DEVICE_FAMILY_22000: 171044f61b5cSShahar S Matityahu wprot = PREG_PRPH_WPROT_22000; 171144f61b5cSShahar S Matityahu break; 171244f61b5cSShahar S Matityahu default: 171344f61b5cSShahar S Matityahu return 0; 171444f61b5cSShahar S Matityahu } 171544f61b5cSShahar S Matityahu 171644f61b5cSShahar S Matityahu hpm = iwl_read_umac_prph_no_grab(trans, HPM_DEBUG); 171744f61b5cSShahar S Matityahu if (hpm != 0xa5a5a5a0 && (hpm & PERSISTENCE_BIT)) { 171844f61b5cSShahar S Matityahu u32 wprot_val = iwl_read_umac_prph_no_grab(trans, wprot); 171944f61b5cSShahar S Matityahu 172044f61b5cSShahar S Matityahu if (wprot_val & PREG_WFPM_ACCESS) { 172144f61b5cSShahar S Matityahu IWL_ERR(trans, 172244f61b5cSShahar S Matityahu "Error, can not clear persistence bit\n"); 172344f61b5cSShahar S Matityahu return -EPERM; 172444f61b5cSShahar S Matityahu } 172544f61b5cSShahar S Matityahu iwl_write_umac_prph_no_grab(trans, HPM_DEBUG, 172644f61b5cSShahar S Matityahu hpm & ~PERSISTENCE_BIT); 172744f61b5cSShahar S Matityahu } 172844f61b5cSShahar S Matityahu 172944f61b5cSShahar S Matityahu return 0; 173044f61b5cSShahar S Matityahu } 173144f61b5cSShahar S Matityahu 1732e705c121SKalle Valo static int _iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power) 1733e705c121SKalle Valo { 1734e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1735e705c121SKalle Valo int err; 1736e705c121SKalle Valo 1737e705c121SKalle Valo lockdep_assert_held(&trans_pcie->mutex); 1738e705c121SKalle Valo 1739e705c121SKalle Valo err = iwl_pcie_prepare_card_hw(trans); 1740e705c121SKalle Valo if (err) { 1741e705c121SKalle Valo IWL_ERR(trans, "Error while preparing HW: %d\n", err); 1742e705c121SKalle Valo return err; 1743e705c121SKalle Valo } 1744e705c121SKalle Valo 174544f61b5cSShahar S Matityahu err = iwl_trans_pcie_clear_persistence_bit(trans); 174644f61b5cSShahar S Matityahu if (err) 174744f61b5cSShahar S Matityahu return err; 17488954e1ebSShahar S Matityahu 1749870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 1750e705c121SKalle Valo 175152b6e168SEmmanuel Grumbach err = iwl_pcie_apm_init(trans); 175252b6e168SEmmanuel Grumbach if (err) 175352b6e168SEmmanuel Grumbach return err; 1754e705c121SKalle Valo 17552e5d4a8fSHaim Dreyfuss iwl_pcie_init_msix(trans_pcie); 175683730058SHaim Dreyfuss 1757e705c121SKalle Valo /* From now on, the op_mode will be kept updated about RF kill state */ 1758e705c121SKalle Valo iwl_enable_rfkill_int(trans); 1759e705c121SKalle Valo 1760326477e4SJohannes Berg trans_pcie->opmode_down = false; 1761326477e4SJohannes Berg 1762e705c121SKalle Valo /* Set is_down to false here so that...*/ 1763e705c121SKalle Valo trans_pcie->is_down = false; 1764e705c121SKalle Valo 1765e705c121SKalle Valo /* ...rfkill can call stop_device and set it false if needed */ 17669ad8fd0bSJohannes Berg iwl_pcie_check_hw_rf_kill(trans); 1767e705c121SKalle Valo 17684cbb8e50SLuciano Coelho /* Make sure we sync here, because we'll need full access later */ 17694cbb8e50SLuciano Coelho if (low_power) 17704cbb8e50SLuciano Coelho pm_runtime_resume(trans->dev); 17714cbb8e50SLuciano Coelho 1772e705c121SKalle Valo return 0; 1773e705c121SKalle Valo } 1774e705c121SKalle Valo 1775e705c121SKalle Valo static int iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power) 1776e705c121SKalle Valo { 1777e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1778e705c121SKalle Valo int ret; 1779e705c121SKalle Valo 1780e705c121SKalle Valo mutex_lock(&trans_pcie->mutex); 1781e705c121SKalle Valo ret = _iwl_trans_pcie_start_hw(trans, low_power); 1782e705c121SKalle Valo mutex_unlock(&trans_pcie->mutex); 1783e705c121SKalle Valo 1784e705c121SKalle Valo return ret; 1785e705c121SKalle Valo } 1786e705c121SKalle Valo 1787e705c121SKalle Valo static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans) 1788e705c121SKalle Valo { 1789e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1790e705c121SKalle Valo 1791e705c121SKalle Valo mutex_lock(&trans_pcie->mutex); 1792e705c121SKalle Valo 1793e705c121SKalle Valo /* disable interrupts - don't enable HW RF kill interrupt */ 1794e705c121SKalle Valo iwl_disable_interrupts(trans); 1795e705c121SKalle Valo 1796e705c121SKalle Valo iwl_pcie_apm_stop(trans, true); 1797e705c121SKalle Valo 1798e705c121SKalle Valo iwl_disable_interrupts(trans); 1799e705c121SKalle Valo 1800e705c121SKalle Valo iwl_pcie_disable_ict(trans); 1801e705c121SKalle Valo 1802e705c121SKalle Valo mutex_unlock(&trans_pcie->mutex); 1803e705c121SKalle Valo 18042e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1805e705c121SKalle Valo } 1806e705c121SKalle Valo 1807e705c121SKalle Valo static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val) 1808e705c121SKalle Valo { 1809e705c121SKalle Valo writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs); 1810e705c121SKalle Valo } 1811e705c121SKalle Valo 1812e705c121SKalle Valo static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val) 1813e705c121SKalle Valo { 1814e705c121SKalle Valo writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs); 1815e705c121SKalle Valo } 1816e705c121SKalle Valo 1817e705c121SKalle Valo static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs) 1818e705c121SKalle Valo { 1819e705c121SKalle Valo return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs); 1820e705c121SKalle Valo } 1821e705c121SKalle Valo 182284fb372cSSara Sharon static u32 iwl_trans_pcie_prph_msk(struct iwl_trans *trans) 182384fb372cSSara Sharon { 182484fb372cSSara Sharon if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) 182584fb372cSSara Sharon return 0x00FFFFFF; 182684fb372cSSara Sharon else 182784fb372cSSara Sharon return 0x000FFFFF; 182884fb372cSSara Sharon } 182984fb372cSSara Sharon 1830e705c121SKalle Valo static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg) 1831e705c121SKalle Valo { 183284fb372cSSara Sharon u32 mask = iwl_trans_pcie_prph_msk(trans); 183384fb372cSSara Sharon 1834e705c121SKalle Valo iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR, 183584fb372cSSara Sharon ((reg & mask) | (3 << 24))); 1836e705c121SKalle Valo return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT); 1837e705c121SKalle Valo } 1838e705c121SKalle Valo 1839e705c121SKalle Valo static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr, 1840e705c121SKalle Valo u32 val) 1841e705c121SKalle Valo { 184284fb372cSSara Sharon u32 mask = iwl_trans_pcie_prph_msk(trans); 184384fb372cSSara Sharon 1844e705c121SKalle Valo iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR, 184584fb372cSSara Sharon ((addr & mask) | (3 << 24))); 1846e705c121SKalle Valo iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val); 1847e705c121SKalle Valo } 1848e705c121SKalle Valo 1849e705c121SKalle Valo static void iwl_trans_pcie_configure(struct iwl_trans *trans, 1850e705c121SKalle Valo const struct iwl_trans_config *trans_cfg) 1851e705c121SKalle Valo { 1852e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1853e705c121SKalle Valo 1854e705c121SKalle Valo trans_pcie->cmd_queue = trans_cfg->cmd_queue; 1855e705c121SKalle Valo trans_pcie->cmd_fifo = trans_cfg->cmd_fifo; 1856e705c121SKalle Valo trans_pcie->cmd_q_wdg_timeout = trans_cfg->cmd_q_wdg_timeout; 1857e705c121SKalle Valo if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS)) 1858e705c121SKalle Valo trans_pcie->n_no_reclaim_cmds = 0; 1859e705c121SKalle Valo else 1860e705c121SKalle Valo trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds; 1861e705c121SKalle Valo if (trans_pcie->n_no_reclaim_cmds) 1862e705c121SKalle Valo memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds, 1863e705c121SKalle Valo trans_pcie->n_no_reclaim_cmds * sizeof(u8)); 1864e705c121SKalle Valo 18656c4fbcbcSEmmanuel Grumbach trans_pcie->rx_buf_size = trans_cfg->rx_buf_size; 18666c4fbcbcSEmmanuel Grumbach trans_pcie->rx_page_order = 18676c4fbcbcSEmmanuel Grumbach iwl_trans_get_rb_size_order(trans_pcie->rx_buf_size); 1868e705c121SKalle Valo 1869e705c121SKalle Valo trans_pcie->bc_table_dword = trans_cfg->bc_table_dword; 1870e705c121SKalle Valo trans_pcie->scd_set_active = trans_cfg->scd_set_active; 187141837ca9SEmmanuel Grumbach trans_pcie->sw_csum_tx = trans_cfg->sw_csum_tx; 1872e705c121SKalle Valo 187321cb3222SJohannes Berg trans_pcie->page_offs = trans_cfg->cb_data_offs; 187421cb3222SJohannes Berg trans_pcie->dev_cmd_offs = trans_cfg->cb_data_offs + sizeof(void *); 187521cb3222SJohannes Berg 187639bdb17eSSharon Dvir trans->command_groups = trans_cfg->command_groups; 187739bdb17eSSharon Dvir trans->command_groups_size = trans_cfg->command_groups_size; 187839bdb17eSSharon Dvir 1879e705c121SKalle Valo /* Initialize NAPI here - it should be before registering to mac80211 1880e705c121SKalle Valo * in the opmode but after the HW struct is allocated. 1881e705c121SKalle Valo * As this function may be called again in some corner cases don't 1882e705c121SKalle Valo * do anything if NAPI was already initialized. 1883e705c121SKalle Valo */ 1884bce97731SSara Sharon if (trans_pcie->napi_dev.reg_state != NETREG_DUMMY) 1885e705c121SKalle Valo init_dummy_netdev(&trans_pcie->napi_dev); 1886e705c121SKalle Valo } 1887e705c121SKalle Valo 1888e705c121SKalle Valo void iwl_trans_pcie_free(struct iwl_trans *trans) 1889e705c121SKalle Valo { 1890e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 18916eb5e529SEmmanuel Grumbach int i; 1892e705c121SKalle Valo 18932e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1894e705c121SKalle Valo 189513a3a390SSara Sharon if (trans->cfg->gen2) 189613a3a390SSara Sharon iwl_pcie_gen2_tx_free(trans); 189713a3a390SSara Sharon else 1898e705c121SKalle Valo iwl_pcie_tx_free(trans); 1899e705c121SKalle Valo iwl_pcie_rx_free(trans); 1900e705c121SKalle Valo 190110a54d81SLuca Coelho if (trans_pcie->rba.alloc_wq) { 190210a54d81SLuca Coelho destroy_workqueue(trans_pcie->rba.alloc_wq); 190310a54d81SLuca Coelho trans_pcie->rba.alloc_wq = NULL; 190410a54d81SLuca Coelho } 190510a54d81SLuca Coelho 19062e5d4a8fSHaim Dreyfuss if (trans_pcie->msix_enabled) { 19077c8d91ebSHaim Dreyfuss for (i = 0; i < trans_pcie->alloc_vecs; i++) { 19087c8d91ebSHaim Dreyfuss irq_set_affinity_hint( 19097c8d91ebSHaim Dreyfuss trans_pcie->msix_entries[i].vector, 19107c8d91ebSHaim Dreyfuss NULL); 19117c8d91ebSHaim Dreyfuss } 19122e5d4a8fSHaim Dreyfuss 19132e5d4a8fSHaim Dreyfuss trans_pcie->msix_enabled = false; 19142e5d4a8fSHaim Dreyfuss } else { 1915e705c121SKalle Valo iwl_pcie_free_ict(trans); 19162e5d4a8fSHaim Dreyfuss } 1917e705c121SKalle Valo 1918e705c121SKalle Valo iwl_pcie_free_fw_monitor(trans); 1919e705c121SKalle Valo 19206eb5e529SEmmanuel Grumbach for_each_possible_cpu(i) { 19216eb5e529SEmmanuel Grumbach struct iwl_tso_hdr_page *p = 19226eb5e529SEmmanuel Grumbach per_cpu_ptr(trans_pcie->tso_hdr_page, i); 19236eb5e529SEmmanuel Grumbach 19246eb5e529SEmmanuel Grumbach if (p->page) 19256eb5e529SEmmanuel Grumbach __free_page(p->page); 19266eb5e529SEmmanuel Grumbach } 19276eb5e529SEmmanuel Grumbach 19286eb5e529SEmmanuel Grumbach free_percpu(trans_pcie->tso_hdr_page); 1929a2a57a35SEmmanuel Grumbach mutex_destroy(&trans_pcie->mutex); 1930e705c121SKalle Valo iwl_trans_free(trans); 1931e705c121SKalle Valo } 1932e705c121SKalle Valo 1933e705c121SKalle Valo static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state) 1934e705c121SKalle Valo { 1935e705c121SKalle Valo if (state) 1936e705c121SKalle Valo set_bit(STATUS_TPOWER_PMI, &trans->status); 1937e705c121SKalle Valo else 1938e705c121SKalle Valo clear_bit(STATUS_TPOWER_PMI, &trans->status); 1939e705c121SKalle Valo } 1940e705c121SKalle Valo 194149564a80SLuca Coelho struct iwl_trans_pcie_removal { 194249564a80SLuca Coelho struct pci_dev *pdev; 194349564a80SLuca Coelho struct work_struct work; 194449564a80SLuca Coelho }; 194549564a80SLuca Coelho 194649564a80SLuca Coelho static void iwl_trans_pcie_removal_wk(struct work_struct *wk) 194749564a80SLuca Coelho { 194849564a80SLuca Coelho struct iwl_trans_pcie_removal *removal = 194949564a80SLuca Coelho container_of(wk, struct iwl_trans_pcie_removal, work); 195049564a80SLuca Coelho struct pci_dev *pdev = removal->pdev; 1951aba1e632SColin Ian King static char *prop[] = {"EVENT=INACCESSIBLE", NULL}; 195249564a80SLuca Coelho 195349564a80SLuca Coelho dev_err(&pdev->dev, "Device gone - attempting removal\n"); 195449564a80SLuca Coelho kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, prop); 195549564a80SLuca Coelho pci_lock_rescan_remove(); 195649564a80SLuca Coelho pci_dev_put(pdev); 195749564a80SLuca Coelho pci_stop_and_remove_bus_device(pdev); 195849564a80SLuca Coelho pci_unlock_rescan_remove(); 195949564a80SLuca Coelho 196049564a80SLuca Coelho kfree(removal); 196149564a80SLuca Coelho module_put(THIS_MODULE); 196249564a80SLuca Coelho } 196349564a80SLuca Coelho 196423ba9340SEmmanuel Grumbach static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, 1965e705c121SKalle Valo unsigned long *flags) 1966e705c121SKalle Valo { 1967e705c121SKalle Valo int ret; 1968e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1969e705c121SKalle Valo 1970e705c121SKalle Valo spin_lock_irqsave(&trans_pcie->reg_lock, *flags); 1971e705c121SKalle Valo 1972e705c121SKalle Valo if (trans_pcie->cmd_hold_nic_awake) 1973e705c121SKalle Valo goto out; 1974e705c121SKalle Valo 1975e705c121SKalle Valo /* this bit wakes up the NIC */ 1976e705c121SKalle Valo __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL, 1977a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_mac_access_req)); 19786e584873SSara Sharon if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) 1979e705c121SKalle Valo udelay(2); 1980e705c121SKalle Valo 1981e705c121SKalle Valo /* 1982e705c121SKalle Valo * These bits say the device is running, and should keep running for 1983e705c121SKalle Valo * at least a short while (at least as long as MAC_ACCESS_REQ stays 1), 1984e705c121SKalle Valo * but they do not indicate that embedded SRAM is restored yet; 1985fb70d49fSLuca Coelho * HW with volatile SRAM must save/restore contents to/from 1986fb70d49fSLuca Coelho * host DRAM when sleeping/waking for power-saving. 1987e705c121SKalle Valo * Each direction takes approximately 1/4 millisecond; with this 1988e705c121SKalle Valo * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a 1989e705c121SKalle Valo * series of register accesses are expected (e.g. reading Event Log), 1990e705c121SKalle Valo * to keep device from sleeping. 1991e705c121SKalle Valo * 1992e705c121SKalle Valo * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that 1993e705c121SKalle Valo * SRAM is okay/restored. We don't check that here because this call 1994fb70d49fSLuca Coelho * is just for hardware register access; but GP1 MAC_SLEEP 1995fb70d49fSLuca Coelho * check is a good idea before accessing the SRAM of HW with 1996fb70d49fSLuca Coelho * volatile SRAM (e.g. reading Event Log). 1997e705c121SKalle Valo * 1998e705c121SKalle Valo * 5000 series and later (including 1000 series) have non-volatile SRAM, 1999e705c121SKalle Valo * and do not save/restore SRAM when power cycling. 2000e705c121SKalle Valo */ 2001e705c121SKalle Valo ret = iwl_poll_bit(trans, CSR_GP_CNTRL, 2002a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_val_mac_access_en), 2003a8cbb46fSGolan Ben Ami (BIT(trans->cfg->csr->flag_mac_clock_ready) | 2004e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000); 2005e705c121SKalle Valo if (unlikely(ret < 0)) { 200649564a80SLuca Coelho u32 cntrl = iwl_read32(trans, CSR_GP_CNTRL); 200749564a80SLuca Coelho 2008e705c121SKalle Valo WARN_ONCE(1, 2009e705c121SKalle Valo "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n", 201049564a80SLuca Coelho cntrl); 201149564a80SLuca Coelho 201249564a80SLuca Coelho iwl_trans_pcie_dump_regs(trans); 201349564a80SLuca Coelho 201449564a80SLuca Coelho if (iwlwifi_mod_params.remove_when_gone && cntrl == ~0U) { 201549564a80SLuca Coelho struct iwl_trans_pcie_removal *removal; 201649564a80SLuca Coelho 2017f60c9e59SEmmanuel Grumbach if (test_bit(STATUS_TRANS_DEAD, &trans->status)) 201849564a80SLuca Coelho goto err; 201949564a80SLuca Coelho 202049564a80SLuca Coelho IWL_ERR(trans, "Device gone - scheduling removal!\n"); 202149564a80SLuca Coelho 202249564a80SLuca Coelho /* 202349564a80SLuca Coelho * get a module reference to avoid doing this 202449564a80SLuca Coelho * while unloading anyway and to avoid 202549564a80SLuca Coelho * scheduling a work with code that's being 202649564a80SLuca Coelho * removed. 202749564a80SLuca Coelho */ 202849564a80SLuca Coelho if (!try_module_get(THIS_MODULE)) { 202949564a80SLuca Coelho IWL_ERR(trans, 203049564a80SLuca Coelho "Module is being unloaded - abort\n"); 203149564a80SLuca Coelho goto err; 203249564a80SLuca Coelho } 203349564a80SLuca Coelho 203449564a80SLuca Coelho removal = kzalloc(sizeof(*removal), GFP_ATOMIC); 203549564a80SLuca Coelho if (!removal) { 203649564a80SLuca Coelho module_put(THIS_MODULE); 203749564a80SLuca Coelho goto err; 203849564a80SLuca Coelho } 203949564a80SLuca Coelho /* 204049564a80SLuca Coelho * we don't need to clear this flag, because 204149564a80SLuca Coelho * the trans will be freed and reallocated. 204249564a80SLuca Coelho */ 2043f60c9e59SEmmanuel Grumbach set_bit(STATUS_TRANS_DEAD, &trans->status); 204449564a80SLuca Coelho 204549564a80SLuca Coelho removal->pdev = to_pci_dev(trans->dev); 204649564a80SLuca Coelho INIT_WORK(&removal->work, iwl_trans_pcie_removal_wk); 204749564a80SLuca Coelho pci_dev_get(removal->pdev); 204849564a80SLuca Coelho schedule_work(&removal->work); 204949564a80SLuca Coelho } else { 205049564a80SLuca Coelho iwl_write32(trans, CSR_RESET, 205149564a80SLuca Coelho CSR_RESET_REG_FLAG_FORCE_NMI); 205249564a80SLuca Coelho } 205349564a80SLuca Coelho 205449564a80SLuca Coelho err: 2055e705c121SKalle Valo spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags); 2056e705c121SKalle Valo return false; 2057e705c121SKalle Valo } 2058e705c121SKalle Valo 2059e705c121SKalle Valo out: 2060e705c121SKalle Valo /* 2061e705c121SKalle Valo * Fool sparse by faking we release the lock - sparse will 2062e705c121SKalle Valo * track nic_access anyway. 2063e705c121SKalle Valo */ 2064e705c121SKalle Valo __release(&trans_pcie->reg_lock); 2065e705c121SKalle Valo return true; 2066e705c121SKalle Valo } 2067e705c121SKalle Valo 2068e705c121SKalle Valo static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans, 2069e705c121SKalle Valo unsigned long *flags) 2070e705c121SKalle Valo { 2071e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2072e705c121SKalle Valo 2073e705c121SKalle Valo lockdep_assert_held(&trans_pcie->reg_lock); 2074e705c121SKalle Valo 2075e705c121SKalle Valo /* 2076e705c121SKalle Valo * Fool sparse by faking we acquiring the lock - sparse will 2077e705c121SKalle Valo * track nic_access anyway. 2078e705c121SKalle Valo */ 2079e705c121SKalle Valo __acquire(&trans_pcie->reg_lock); 2080e705c121SKalle Valo 2081e705c121SKalle Valo if (trans_pcie->cmd_hold_nic_awake) 2082e705c121SKalle Valo goto out; 2083e705c121SKalle Valo 2084e705c121SKalle Valo __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL, 2085a8cbb46fSGolan Ben Ami BIT(trans->cfg->csr->flag_mac_access_req)); 2086e705c121SKalle Valo /* 2087e705c121SKalle Valo * Above we read the CSR_GP_CNTRL register, which will flush 2088e705c121SKalle Valo * any previous writes, but we need the write that clears the 2089e705c121SKalle Valo * MAC_ACCESS_REQ bit to be performed before any other writes 2090e705c121SKalle Valo * scheduled on different CPUs (after we drop reg_lock). 2091e705c121SKalle Valo */ 2092e705c121SKalle Valo out: 2093e705c121SKalle Valo spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags); 2094e705c121SKalle Valo } 2095e705c121SKalle Valo 2096e705c121SKalle Valo static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr, 2097e705c121SKalle Valo void *buf, int dwords) 2098e705c121SKalle Valo { 2099e705c121SKalle Valo unsigned long flags; 2100e705c121SKalle Valo int offs, ret = 0; 2101e705c121SKalle Valo u32 *vals = buf; 2102e705c121SKalle Valo 210323ba9340SEmmanuel Grumbach if (iwl_trans_grab_nic_access(trans, &flags)) { 2104e705c121SKalle Valo iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr); 2105e705c121SKalle Valo for (offs = 0; offs < dwords; offs++) 2106e705c121SKalle Valo vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT); 2107e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 2108e705c121SKalle Valo } else { 2109e705c121SKalle Valo ret = -EBUSY; 2110e705c121SKalle Valo } 2111e705c121SKalle Valo return ret; 2112e705c121SKalle Valo } 2113e705c121SKalle Valo 2114e705c121SKalle Valo static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr, 2115e705c121SKalle Valo const void *buf, int dwords) 2116e705c121SKalle Valo { 2117e705c121SKalle Valo unsigned long flags; 2118e705c121SKalle Valo int offs, ret = 0; 2119e705c121SKalle Valo const u32 *vals = buf; 2120e705c121SKalle Valo 212123ba9340SEmmanuel Grumbach if (iwl_trans_grab_nic_access(trans, &flags)) { 2122e705c121SKalle Valo iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr); 2123e705c121SKalle Valo for (offs = 0; offs < dwords; offs++) 2124e705c121SKalle Valo iwl_write32(trans, HBUS_TARG_MEM_WDAT, 2125e705c121SKalle Valo vals ? vals[offs] : 0); 2126e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 2127e705c121SKalle Valo } else { 2128e705c121SKalle Valo ret = -EBUSY; 2129e705c121SKalle Valo } 2130e705c121SKalle Valo return ret; 2131e705c121SKalle Valo } 2132e705c121SKalle Valo 2133e705c121SKalle Valo static void iwl_trans_pcie_freeze_txq_timer(struct iwl_trans *trans, 2134e705c121SKalle Valo unsigned long txqs, 2135e705c121SKalle Valo bool freeze) 2136e705c121SKalle Valo { 2137e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2138e705c121SKalle Valo int queue; 2139e705c121SKalle Valo 2140e705c121SKalle Valo for_each_set_bit(queue, &txqs, BITS_PER_LONG) { 2141b2a3b1c1SSara Sharon struct iwl_txq *txq = trans_pcie->txq[queue]; 2142e705c121SKalle Valo unsigned long now; 2143e705c121SKalle Valo 2144e705c121SKalle Valo spin_lock_bh(&txq->lock); 2145e705c121SKalle Valo 2146e705c121SKalle Valo now = jiffies; 2147e705c121SKalle Valo 2148e705c121SKalle Valo if (txq->frozen == freeze) 2149e705c121SKalle Valo goto next_queue; 2150e705c121SKalle Valo 2151e705c121SKalle Valo IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n", 2152e705c121SKalle Valo freeze ? "Freezing" : "Waking", queue); 2153e705c121SKalle Valo 2154e705c121SKalle Valo txq->frozen = freeze; 2155e705c121SKalle Valo 2156bb98ecd4SSara Sharon if (txq->read_ptr == txq->write_ptr) 2157e705c121SKalle Valo goto next_queue; 2158e705c121SKalle Valo 2159e705c121SKalle Valo if (freeze) { 2160e705c121SKalle Valo if (unlikely(time_after(now, 2161e705c121SKalle Valo txq->stuck_timer.expires))) { 2162e705c121SKalle Valo /* 2163e705c121SKalle Valo * The timer should have fired, maybe it is 2164e705c121SKalle Valo * spinning right now on the lock. 2165e705c121SKalle Valo */ 2166e705c121SKalle Valo goto next_queue; 2167e705c121SKalle Valo } 2168e705c121SKalle Valo /* remember how long until the timer fires */ 2169e705c121SKalle Valo txq->frozen_expiry_remainder = 2170e705c121SKalle Valo txq->stuck_timer.expires - now; 2171e705c121SKalle Valo del_timer(&txq->stuck_timer); 2172e705c121SKalle Valo goto next_queue; 2173e705c121SKalle Valo } 2174e705c121SKalle Valo 2175e705c121SKalle Valo /* 2176e705c121SKalle Valo * Wake a non-empty queue -> arm timer with the 2177e705c121SKalle Valo * remainder before it froze 2178e705c121SKalle Valo */ 2179e705c121SKalle Valo mod_timer(&txq->stuck_timer, 2180e705c121SKalle Valo now + txq->frozen_expiry_remainder); 2181e705c121SKalle Valo 2182e705c121SKalle Valo next_queue: 2183e705c121SKalle Valo spin_unlock_bh(&txq->lock); 2184e705c121SKalle Valo } 2185e705c121SKalle Valo } 2186e705c121SKalle Valo 21870cd58eaaSEmmanuel Grumbach static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block) 21880cd58eaaSEmmanuel Grumbach { 21890cd58eaaSEmmanuel Grumbach struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 21900cd58eaaSEmmanuel Grumbach int i; 21910cd58eaaSEmmanuel Grumbach 21920cd58eaaSEmmanuel Grumbach for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) { 2193b2a3b1c1SSara Sharon struct iwl_txq *txq = trans_pcie->txq[i]; 21940cd58eaaSEmmanuel Grumbach 21950cd58eaaSEmmanuel Grumbach if (i == trans_pcie->cmd_queue) 21960cd58eaaSEmmanuel Grumbach continue; 21970cd58eaaSEmmanuel Grumbach 21980cd58eaaSEmmanuel Grumbach spin_lock_bh(&txq->lock); 21990cd58eaaSEmmanuel Grumbach 22000cd58eaaSEmmanuel Grumbach if (!block && !(WARN_ON_ONCE(!txq->block))) { 22010cd58eaaSEmmanuel Grumbach txq->block--; 22020cd58eaaSEmmanuel Grumbach if (!txq->block) { 22030cd58eaaSEmmanuel Grumbach iwl_write32(trans, HBUS_TARG_WRPTR, 2204bb98ecd4SSara Sharon txq->write_ptr | (i << 8)); 22050cd58eaaSEmmanuel Grumbach } 22060cd58eaaSEmmanuel Grumbach } else if (block) { 22070cd58eaaSEmmanuel Grumbach txq->block++; 22080cd58eaaSEmmanuel Grumbach } 22090cd58eaaSEmmanuel Grumbach 22100cd58eaaSEmmanuel Grumbach spin_unlock_bh(&txq->lock); 22110cd58eaaSEmmanuel Grumbach } 22120cd58eaaSEmmanuel Grumbach } 22130cd58eaaSEmmanuel Grumbach 2214e705c121SKalle Valo #define IWL_FLUSH_WAIT_MS 2000 2215e705c121SKalle Valo 221638398efbSSara Sharon void iwl_trans_pcie_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq) 221738398efbSSara Sharon { 2218afb84431SEmmanuel Grumbach u32 txq_id = txq->id; 2219afb84431SEmmanuel Grumbach u32 status; 2220afb84431SEmmanuel Grumbach bool active; 2221afb84431SEmmanuel Grumbach u8 fifo; 222238398efbSSara Sharon 2223afb84431SEmmanuel Grumbach if (trans->cfg->use_tfh) { 2224afb84431SEmmanuel Grumbach IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id, 2225bb98ecd4SSara Sharon txq->read_ptr, txq->write_ptr); 2226ae79785fSSara Sharon /* TODO: access new SCD registers and dump them */ 2227ae79785fSSara Sharon return; 2228afb84431SEmmanuel Grumbach } 2229ae79785fSSara Sharon 2230afb84431SEmmanuel Grumbach status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id)); 2231afb84431SEmmanuel Grumbach fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7; 2232afb84431SEmmanuel Grumbach active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE)); 223338398efbSSara Sharon 223438398efbSSara Sharon IWL_ERR(trans, 2235afb84431SEmmanuel Grumbach "Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n", 2236afb84431SEmmanuel Grumbach txq_id, active ? "" : "in", fifo, 2237afb84431SEmmanuel Grumbach jiffies_to_msecs(txq->wd_timeout), 2238afb84431SEmmanuel Grumbach txq->read_ptr, txq->write_ptr, 2239afb84431SEmmanuel Grumbach iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) & 22407b3e42eaSGolan Ben Ami (trans->cfg->base_params->max_tfd_queue_size - 1), 2241afb84431SEmmanuel Grumbach iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) & 22427b3e42eaSGolan Ben Ami (trans->cfg->base_params->max_tfd_queue_size - 1), 2243afb84431SEmmanuel Grumbach iwl_read_direct32(trans, FH_TX_TRB_REG(fifo))); 224438398efbSSara Sharon } 224538398efbSSara Sharon 224692536c96SSara Sharon static int iwl_trans_pcie_rxq_dma_data(struct iwl_trans *trans, int queue, 224792536c96SSara Sharon struct iwl_trans_rxq_dma_data *data) 224892536c96SSara Sharon { 224992536c96SSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 225092536c96SSara Sharon 225192536c96SSara Sharon if (queue >= trans->num_rx_queues || !trans_pcie->rxq) 225292536c96SSara Sharon return -EINVAL; 225392536c96SSara Sharon 225492536c96SSara Sharon data->fr_bd_cb = trans_pcie->rxq[queue].bd_dma; 225592536c96SSara Sharon data->urbd_stts_wrptr = trans_pcie->rxq[queue].rb_stts_dma; 225692536c96SSara Sharon data->ur_bd_cb = trans_pcie->rxq[queue].used_bd_dma; 225792536c96SSara Sharon data->fr_bd_wid = 0; 225892536c96SSara Sharon 225992536c96SSara Sharon return 0; 226092536c96SSara Sharon } 226192536c96SSara Sharon 2262d6d517b7SSara Sharon static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, int txq_idx) 2263e705c121SKalle Valo { 2264e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2265e705c121SKalle Valo struct iwl_txq *txq; 2266e705c121SKalle Valo unsigned long now = jiffies; 22672ae48edcSSara Sharon bool overflow_tx; 2268e705c121SKalle Valo u8 wr_ptr; 2269e705c121SKalle Valo 22702b3fae66SMatt Chen /* Make sure the NIC is still alive in the bus */ 2271f60c9e59SEmmanuel Grumbach if (test_bit(STATUS_TRANS_DEAD, &trans->status)) 2272f60c9e59SEmmanuel Grumbach return -ENODEV; 22732b3fae66SMatt Chen 2274d6d517b7SSara Sharon if (!test_bit(txq_idx, trans_pcie->queue_used)) 2275d6d517b7SSara Sharon return -EINVAL; 2276e705c121SKalle Valo 2277d6d517b7SSara Sharon IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", txq_idx); 2278d6d517b7SSara Sharon txq = trans_pcie->txq[txq_idx]; 22792ae48edcSSara Sharon 22802ae48edcSSara Sharon spin_lock_bh(&txq->lock); 22812ae48edcSSara Sharon overflow_tx = txq->overflow_tx || 22822ae48edcSSara Sharon !skb_queue_empty(&txq->overflow_q); 22832ae48edcSSara Sharon spin_unlock_bh(&txq->lock); 22842ae48edcSSara Sharon 22856aa7de05SMark Rutland wr_ptr = READ_ONCE(txq->write_ptr); 2286e705c121SKalle Valo 22872ae48edcSSara Sharon while ((txq->read_ptr != READ_ONCE(txq->write_ptr) || 22882ae48edcSSara Sharon overflow_tx) && 2289e705c121SKalle Valo !time_after(jiffies, 2290e705c121SKalle Valo now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) { 22916aa7de05SMark Rutland u8 write_ptr = READ_ONCE(txq->write_ptr); 2292e705c121SKalle Valo 22932ae48edcSSara Sharon /* 22942ae48edcSSara Sharon * If write pointer moved during the wait, warn only 22952ae48edcSSara Sharon * if the TX came from op mode. In case TX came from 22962ae48edcSSara Sharon * trans layer (overflow TX) don't warn. 22972ae48edcSSara Sharon */ 22982ae48edcSSara Sharon if (WARN_ONCE(wr_ptr != write_ptr && !overflow_tx, 2299e705c121SKalle Valo "WR pointer moved while flushing %d -> %d\n", 2300e705c121SKalle Valo wr_ptr, write_ptr)) 2301e705c121SKalle Valo return -ETIMEDOUT; 23022ae48edcSSara Sharon wr_ptr = write_ptr; 23032ae48edcSSara Sharon 2304192185d6SJohannes Berg usleep_range(1000, 2000); 23052ae48edcSSara Sharon 23062ae48edcSSara Sharon spin_lock_bh(&txq->lock); 23072ae48edcSSara Sharon overflow_tx = txq->overflow_tx || 23082ae48edcSSara Sharon !skb_queue_empty(&txq->overflow_q); 23092ae48edcSSara Sharon spin_unlock_bh(&txq->lock); 2310e705c121SKalle Valo } 2311e705c121SKalle Valo 2312bb98ecd4SSara Sharon if (txq->read_ptr != txq->write_ptr) { 2313e705c121SKalle Valo IWL_ERR(trans, 2314d6d517b7SSara Sharon "fail to flush all tx fifo queues Q %d\n", txq_idx); 2315d6d517b7SSara Sharon iwl_trans_pcie_log_scd_error(trans, txq); 2316d6d517b7SSara Sharon return -ETIMEDOUT; 2317e705c121SKalle Valo } 2318e705c121SKalle Valo 2319d6d517b7SSara Sharon IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", txq_idx); 2320d6d517b7SSara Sharon 2321d6d517b7SSara Sharon return 0; 2322d6d517b7SSara Sharon } 2323d6d517b7SSara Sharon 2324d6d517b7SSara Sharon static int iwl_trans_pcie_wait_txqs_empty(struct iwl_trans *trans, u32 txq_bm) 2325d6d517b7SSara Sharon { 2326d6d517b7SSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2327d6d517b7SSara Sharon int cnt; 2328d6d517b7SSara Sharon int ret = 0; 2329d6d517b7SSara Sharon 2330d6d517b7SSara Sharon /* waiting for all the tx frames complete might take a while */ 2331d6d517b7SSara Sharon for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) { 2332d6d517b7SSara Sharon 2333d6d517b7SSara Sharon if (cnt == trans_pcie->cmd_queue) 2334d6d517b7SSara Sharon continue; 2335d6d517b7SSara Sharon if (!test_bit(cnt, trans_pcie->queue_used)) 2336d6d517b7SSara Sharon continue; 2337d6d517b7SSara Sharon if (!(BIT(cnt) & txq_bm)) 2338d6d517b7SSara Sharon continue; 2339d6d517b7SSara Sharon 2340d6d517b7SSara Sharon ret = iwl_trans_pcie_wait_txq_empty(trans, cnt); 234138398efbSSara Sharon if (ret) 2342d6d517b7SSara Sharon break; 2343d6d517b7SSara Sharon } 2344e705c121SKalle Valo 2345e705c121SKalle Valo return ret; 2346e705c121SKalle Valo } 2347e705c121SKalle Valo 2348e705c121SKalle Valo static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg, 2349e705c121SKalle Valo u32 mask, u32 value) 2350e705c121SKalle Valo { 2351e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2352e705c121SKalle Valo unsigned long flags; 2353e705c121SKalle Valo 2354e705c121SKalle Valo spin_lock_irqsave(&trans_pcie->reg_lock, flags); 2355e705c121SKalle Valo __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value); 2356e705c121SKalle Valo spin_unlock_irqrestore(&trans_pcie->reg_lock, flags); 2357e705c121SKalle Valo } 2358e705c121SKalle Valo 2359c24c7f58SLuca Coelho static void iwl_trans_pcie_ref(struct iwl_trans *trans) 2360e705c121SKalle Valo { 2361e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2362e705c121SKalle Valo 2363e705c121SKalle Valo if (iwlwifi_mod_params.d0i3_disable) 2364e705c121SKalle Valo return; 2365e705c121SKalle Valo 2366b3ff1270SLuca Coelho pm_runtime_get(&trans_pcie->pci_dev->dev); 23675d93f3a2SLuca Coelho 23685d93f3a2SLuca Coelho #ifdef CONFIG_PM 23695d93f3a2SLuca Coelho IWL_DEBUG_RPM(trans, "runtime usage count: %d\n", 23705d93f3a2SLuca Coelho atomic_read(&trans_pcie->pci_dev->dev.power.usage_count)); 23715d93f3a2SLuca Coelho #endif /* CONFIG_PM */ 2372e705c121SKalle Valo } 2373e705c121SKalle Valo 2374c24c7f58SLuca Coelho static void iwl_trans_pcie_unref(struct iwl_trans *trans) 2375e705c121SKalle Valo { 2376e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2377e705c121SKalle Valo 2378e705c121SKalle Valo if (iwlwifi_mod_params.d0i3_disable) 2379e705c121SKalle Valo return; 2380e705c121SKalle Valo 2381b3ff1270SLuca Coelho pm_runtime_mark_last_busy(&trans_pcie->pci_dev->dev); 2382b3ff1270SLuca Coelho pm_runtime_put_autosuspend(&trans_pcie->pci_dev->dev); 2383b3ff1270SLuca Coelho 23845d93f3a2SLuca Coelho #ifdef CONFIG_PM 23855d93f3a2SLuca Coelho IWL_DEBUG_RPM(trans, "runtime usage count: %d\n", 23865d93f3a2SLuca Coelho atomic_read(&trans_pcie->pci_dev->dev.power.usage_count)); 23875d93f3a2SLuca Coelho #endif /* CONFIG_PM */ 2388e705c121SKalle Valo } 2389e705c121SKalle Valo 2390e705c121SKalle Valo static const char *get_csr_string(int cmd) 2391e705c121SKalle Valo { 2392e705c121SKalle Valo #define IWL_CMD(x) case x: return #x 2393e705c121SKalle Valo switch (cmd) { 2394e705c121SKalle Valo IWL_CMD(CSR_HW_IF_CONFIG_REG); 2395e705c121SKalle Valo IWL_CMD(CSR_INT_COALESCING); 2396e705c121SKalle Valo IWL_CMD(CSR_INT); 2397e705c121SKalle Valo IWL_CMD(CSR_INT_MASK); 2398e705c121SKalle Valo IWL_CMD(CSR_FH_INT_STATUS); 2399e705c121SKalle Valo IWL_CMD(CSR_GPIO_IN); 2400e705c121SKalle Valo IWL_CMD(CSR_RESET); 2401e705c121SKalle Valo IWL_CMD(CSR_GP_CNTRL); 2402e705c121SKalle Valo IWL_CMD(CSR_HW_REV); 2403e705c121SKalle Valo IWL_CMD(CSR_EEPROM_REG); 2404e705c121SKalle Valo IWL_CMD(CSR_EEPROM_GP); 2405e705c121SKalle Valo IWL_CMD(CSR_OTP_GP_REG); 2406e705c121SKalle Valo IWL_CMD(CSR_GIO_REG); 2407e705c121SKalle Valo IWL_CMD(CSR_GP_UCODE_REG); 2408e705c121SKalle Valo IWL_CMD(CSR_GP_DRIVER_REG); 2409e705c121SKalle Valo IWL_CMD(CSR_UCODE_DRV_GP1); 2410e705c121SKalle Valo IWL_CMD(CSR_UCODE_DRV_GP2); 2411e705c121SKalle Valo IWL_CMD(CSR_LED_REG); 2412e705c121SKalle Valo IWL_CMD(CSR_DRAM_INT_TBL_REG); 2413e705c121SKalle Valo IWL_CMD(CSR_GIO_CHICKEN_BITS); 2414e705c121SKalle Valo IWL_CMD(CSR_ANA_PLL_CFG); 2415e705c121SKalle Valo IWL_CMD(CSR_HW_REV_WA_REG); 2416e705c121SKalle Valo IWL_CMD(CSR_MONITOR_STATUS_REG); 2417e705c121SKalle Valo IWL_CMD(CSR_DBG_HPET_MEM_REG); 2418e705c121SKalle Valo default: 2419e705c121SKalle Valo return "UNKNOWN"; 2420e705c121SKalle Valo } 2421e705c121SKalle Valo #undef IWL_CMD 2422e705c121SKalle Valo } 2423e705c121SKalle Valo 2424e705c121SKalle Valo void iwl_pcie_dump_csr(struct iwl_trans *trans) 2425e705c121SKalle Valo { 2426e705c121SKalle Valo int i; 2427e705c121SKalle Valo static const u32 csr_tbl[] = { 2428e705c121SKalle Valo CSR_HW_IF_CONFIG_REG, 2429e705c121SKalle Valo CSR_INT_COALESCING, 2430e705c121SKalle Valo CSR_INT, 2431e705c121SKalle Valo CSR_INT_MASK, 2432e705c121SKalle Valo CSR_FH_INT_STATUS, 2433e705c121SKalle Valo CSR_GPIO_IN, 2434e705c121SKalle Valo CSR_RESET, 2435e705c121SKalle Valo CSR_GP_CNTRL, 2436e705c121SKalle Valo CSR_HW_REV, 2437e705c121SKalle Valo CSR_EEPROM_REG, 2438e705c121SKalle Valo CSR_EEPROM_GP, 2439e705c121SKalle Valo CSR_OTP_GP_REG, 2440e705c121SKalle Valo CSR_GIO_REG, 2441e705c121SKalle Valo CSR_GP_UCODE_REG, 2442e705c121SKalle Valo CSR_GP_DRIVER_REG, 2443e705c121SKalle Valo CSR_UCODE_DRV_GP1, 2444e705c121SKalle Valo CSR_UCODE_DRV_GP2, 2445e705c121SKalle Valo CSR_LED_REG, 2446e705c121SKalle Valo CSR_DRAM_INT_TBL_REG, 2447e705c121SKalle Valo CSR_GIO_CHICKEN_BITS, 2448e705c121SKalle Valo CSR_ANA_PLL_CFG, 2449e705c121SKalle Valo CSR_MONITOR_STATUS_REG, 2450e705c121SKalle Valo CSR_HW_REV_WA_REG, 2451e705c121SKalle Valo CSR_DBG_HPET_MEM_REG 2452e705c121SKalle Valo }; 2453e705c121SKalle Valo IWL_ERR(trans, "CSR values:\n"); 2454e705c121SKalle Valo IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is " 2455e705c121SKalle Valo "CSR_INT_PERIODIC_REG)\n"); 2456e705c121SKalle Valo for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) { 2457e705c121SKalle Valo IWL_ERR(trans, " %25s: 0X%08x\n", 2458e705c121SKalle Valo get_csr_string(csr_tbl[i]), 2459e705c121SKalle Valo iwl_read32(trans, csr_tbl[i])); 2460e705c121SKalle Valo } 2461e705c121SKalle Valo } 2462e705c121SKalle Valo 2463e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS 2464e705c121SKalle Valo /* create and remove of files */ 2465e705c121SKalle Valo #define DEBUGFS_ADD_FILE(name, parent, mode) do { \ 2466cf5d5663SGreg Kroah-Hartman debugfs_create_file(#name, mode, parent, trans, \ 2467cf5d5663SGreg Kroah-Hartman &iwl_dbgfs_##name##_ops); \ 2468e705c121SKalle Valo } while (0) 2469e705c121SKalle Valo 2470e705c121SKalle Valo /* file operation */ 2471e705c121SKalle Valo #define DEBUGFS_READ_FILE_OPS(name) \ 2472e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = { \ 2473e705c121SKalle Valo .read = iwl_dbgfs_##name##_read, \ 2474e705c121SKalle Valo .open = simple_open, \ 2475e705c121SKalle Valo .llseek = generic_file_llseek, \ 2476e705c121SKalle Valo }; 2477e705c121SKalle Valo 2478e705c121SKalle Valo #define DEBUGFS_WRITE_FILE_OPS(name) \ 2479e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = { \ 2480e705c121SKalle Valo .write = iwl_dbgfs_##name##_write, \ 2481e705c121SKalle Valo .open = simple_open, \ 2482e705c121SKalle Valo .llseek = generic_file_llseek, \ 2483e705c121SKalle Valo }; 2484e705c121SKalle Valo 2485e705c121SKalle Valo #define DEBUGFS_READ_WRITE_FILE_OPS(name) \ 2486e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = { \ 2487e705c121SKalle Valo .write = iwl_dbgfs_##name##_write, \ 2488e705c121SKalle Valo .read = iwl_dbgfs_##name##_read, \ 2489e705c121SKalle Valo .open = simple_open, \ 2490e705c121SKalle Valo .llseek = generic_file_llseek, \ 2491e705c121SKalle Valo }; 2492e705c121SKalle Valo 2493e705c121SKalle Valo static ssize_t iwl_dbgfs_tx_queue_read(struct file *file, 2494e705c121SKalle Valo char __user *user_buf, 2495e705c121SKalle Valo size_t count, loff_t *ppos) 2496e705c121SKalle Valo { 2497e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2498e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2499e705c121SKalle Valo struct iwl_txq *txq; 2500e705c121SKalle Valo char *buf; 2501e705c121SKalle Valo int pos = 0; 2502e705c121SKalle Valo int cnt; 2503e705c121SKalle Valo int ret; 2504e705c121SKalle Valo size_t bufsz; 2505e705c121SKalle Valo 2506e705c121SKalle Valo bufsz = sizeof(char) * 75 * trans->cfg->base_params->num_of_queues; 2507e705c121SKalle Valo 2508b2a3b1c1SSara Sharon if (!trans_pcie->txq_memory) 2509e705c121SKalle Valo return -EAGAIN; 2510e705c121SKalle Valo 2511e705c121SKalle Valo buf = kzalloc(bufsz, GFP_KERNEL); 2512e705c121SKalle Valo if (!buf) 2513e705c121SKalle Valo return -ENOMEM; 2514e705c121SKalle Valo 2515e705c121SKalle Valo for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) { 2516b2a3b1c1SSara Sharon txq = trans_pcie->txq[cnt]; 2517e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2518e705c121SKalle Valo "hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d frozen=%d%s\n", 2519bb98ecd4SSara Sharon cnt, txq->read_ptr, txq->write_ptr, 2520e705c121SKalle Valo !!test_bit(cnt, trans_pcie->queue_used), 2521e705c121SKalle Valo !!test_bit(cnt, trans_pcie->queue_stopped), 2522e705c121SKalle Valo txq->need_update, txq->frozen, 2523e705c121SKalle Valo (cnt == trans_pcie->cmd_queue ? " HCMD" : "")); 2524e705c121SKalle Valo } 2525e705c121SKalle Valo ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos); 2526e705c121SKalle Valo kfree(buf); 2527e705c121SKalle Valo return ret; 2528e705c121SKalle Valo } 2529e705c121SKalle Valo 2530e705c121SKalle Valo static ssize_t iwl_dbgfs_rx_queue_read(struct file *file, 2531e705c121SKalle Valo char __user *user_buf, 2532e705c121SKalle Valo size_t count, loff_t *ppos) 2533e705c121SKalle Valo { 2534e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2535e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 253678485054SSara Sharon char *buf; 253778485054SSara Sharon int pos = 0, i, ret; 253878485054SSara Sharon size_t bufsz = sizeof(buf); 2539e705c121SKalle Valo 254078485054SSara Sharon bufsz = sizeof(char) * 121 * trans->num_rx_queues; 254178485054SSara Sharon 254278485054SSara Sharon if (!trans_pcie->rxq) 254378485054SSara Sharon return -EAGAIN; 254478485054SSara Sharon 254578485054SSara Sharon buf = kzalloc(bufsz, GFP_KERNEL); 254678485054SSara Sharon if (!buf) 254778485054SSara Sharon return -ENOMEM; 254878485054SSara Sharon 254978485054SSara Sharon for (i = 0; i < trans->num_rx_queues && pos < bufsz; i++) { 255078485054SSara Sharon struct iwl_rxq *rxq = &trans_pcie->rxq[i]; 255178485054SSara Sharon 255278485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "queue#: %2d\n", 255378485054SSara Sharon i); 255478485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\tread: %u\n", 2555e705c121SKalle Valo rxq->read); 255678485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\twrite: %u\n", 2557e705c121SKalle Valo rxq->write); 255878485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\twrite_actual: %u\n", 2559e705c121SKalle Valo rxq->write_actual); 256078485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\tneed_update: %2d\n", 2561e705c121SKalle Valo rxq->need_update); 256278485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\tfree_count: %u\n", 2563e705c121SKalle Valo rxq->free_count); 2564e705c121SKalle Valo if (rxq->rb_stts) { 25650307c839SGolan Ben Ami u32 r = __le16_to_cpu(iwl_get_closed_rb_stts(trans, 25660307c839SGolan Ben Ami rxq)); 256778485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, 256878485054SSara Sharon "\tclosed_rb_num: %u\n", 25690307c839SGolan Ben Ami r & 0x0FFF); 2570e705c121SKalle Valo } else { 2571e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 257278485054SSara Sharon "\tclosed_rb_num: Not Allocated\n"); 2573e705c121SKalle Valo } 257478485054SSara Sharon } 257578485054SSara Sharon ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos); 257678485054SSara Sharon kfree(buf); 257778485054SSara Sharon 257878485054SSara Sharon return ret; 2579e705c121SKalle Valo } 2580e705c121SKalle Valo 2581e705c121SKalle Valo static ssize_t iwl_dbgfs_interrupt_read(struct file *file, 2582e705c121SKalle Valo char __user *user_buf, 2583e705c121SKalle Valo size_t count, loff_t *ppos) 2584e705c121SKalle Valo { 2585e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2586e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2587e705c121SKalle Valo struct isr_statistics *isr_stats = &trans_pcie->isr_stats; 2588e705c121SKalle Valo 2589e705c121SKalle Valo int pos = 0; 2590e705c121SKalle Valo char *buf; 2591e705c121SKalle Valo int bufsz = 24 * 64; /* 24 items * 64 char per item */ 2592e705c121SKalle Valo ssize_t ret; 2593e705c121SKalle Valo 2594e705c121SKalle Valo buf = kzalloc(bufsz, GFP_KERNEL); 2595e705c121SKalle Valo if (!buf) 2596e705c121SKalle Valo return -ENOMEM; 2597e705c121SKalle Valo 2598e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2599e705c121SKalle Valo "Interrupt Statistics Report:\n"); 2600e705c121SKalle Valo 2601e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n", 2602e705c121SKalle Valo isr_stats->hw); 2603e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n", 2604e705c121SKalle Valo isr_stats->sw); 2605e705c121SKalle Valo if (isr_stats->sw || isr_stats->hw) { 2606e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2607e705c121SKalle Valo "\tLast Restarting Code: 0x%X\n", 2608e705c121SKalle Valo isr_stats->err_code); 2609e705c121SKalle Valo } 2610e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUG 2611e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n", 2612e705c121SKalle Valo isr_stats->sch); 2613e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n", 2614e705c121SKalle Valo isr_stats->alive); 2615e705c121SKalle Valo #endif 2616e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2617e705c121SKalle Valo "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill); 2618e705c121SKalle Valo 2619e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n", 2620e705c121SKalle Valo isr_stats->ctkill); 2621e705c121SKalle Valo 2622e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n", 2623e705c121SKalle Valo isr_stats->wakeup); 2624e705c121SKalle Valo 2625e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2626e705c121SKalle Valo "Rx command responses:\t\t %u\n", isr_stats->rx); 2627e705c121SKalle Valo 2628e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n", 2629e705c121SKalle Valo isr_stats->tx); 2630e705c121SKalle Valo 2631e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n", 2632e705c121SKalle Valo isr_stats->unhandled); 2633e705c121SKalle Valo 2634e705c121SKalle Valo ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos); 2635e705c121SKalle Valo kfree(buf); 2636e705c121SKalle Valo return ret; 2637e705c121SKalle Valo } 2638e705c121SKalle Valo 2639e705c121SKalle Valo static ssize_t iwl_dbgfs_interrupt_write(struct file *file, 2640e705c121SKalle Valo const char __user *user_buf, 2641e705c121SKalle Valo size_t count, loff_t *ppos) 2642e705c121SKalle Valo { 2643e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2644e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2645e705c121SKalle Valo struct isr_statistics *isr_stats = &trans_pcie->isr_stats; 2646e705c121SKalle Valo u32 reset_flag; 2647078f1131SJohannes Berg int ret; 2648e705c121SKalle Valo 2649078f1131SJohannes Berg ret = kstrtou32_from_user(user_buf, count, 16, &reset_flag); 2650078f1131SJohannes Berg if (ret) 2651078f1131SJohannes Berg return ret; 2652e705c121SKalle Valo if (reset_flag == 0) 2653e705c121SKalle Valo memset(isr_stats, 0, sizeof(*isr_stats)); 2654e705c121SKalle Valo 2655e705c121SKalle Valo return count; 2656e705c121SKalle Valo } 2657e705c121SKalle Valo 2658e705c121SKalle Valo static ssize_t iwl_dbgfs_csr_write(struct file *file, 2659e705c121SKalle Valo const char __user *user_buf, 2660e705c121SKalle Valo size_t count, loff_t *ppos) 2661e705c121SKalle Valo { 2662e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2663e705c121SKalle Valo 2664e705c121SKalle Valo iwl_pcie_dump_csr(trans); 2665e705c121SKalle Valo 2666e705c121SKalle Valo return count; 2667e705c121SKalle Valo } 2668e705c121SKalle Valo 2669e705c121SKalle Valo static ssize_t iwl_dbgfs_fh_reg_read(struct file *file, 2670e705c121SKalle Valo char __user *user_buf, 2671e705c121SKalle Valo size_t count, loff_t *ppos) 2672e705c121SKalle Valo { 2673e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2674e705c121SKalle Valo char *buf = NULL; 2675e705c121SKalle Valo ssize_t ret; 2676e705c121SKalle Valo 2677e705c121SKalle Valo ret = iwl_dump_fh(trans, &buf); 2678e705c121SKalle Valo if (ret < 0) 2679e705c121SKalle Valo return ret; 2680e705c121SKalle Valo if (!buf) 2681e705c121SKalle Valo return -EINVAL; 2682e705c121SKalle Valo ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 2683e705c121SKalle Valo kfree(buf); 2684e705c121SKalle Valo return ret; 2685e705c121SKalle Valo } 2686e705c121SKalle Valo 2687fa4de7f7SJohannes Berg static ssize_t iwl_dbgfs_rfkill_read(struct file *file, 2688fa4de7f7SJohannes Berg char __user *user_buf, 2689fa4de7f7SJohannes Berg size_t count, loff_t *ppos) 2690fa4de7f7SJohannes Berg { 2691fa4de7f7SJohannes Berg struct iwl_trans *trans = file->private_data; 2692fa4de7f7SJohannes Berg struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2693fa4de7f7SJohannes Berg char buf[100]; 2694fa4de7f7SJohannes Berg int pos; 2695fa4de7f7SJohannes Berg 2696fa4de7f7SJohannes Berg pos = scnprintf(buf, sizeof(buf), "debug: %d\nhw: %d\n", 2697fa4de7f7SJohannes Berg trans_pcie->debug_rfkill, 2698fa4de7f7SJohannes Berg !(iwl_read32(trans, CSR_GP_CNTRL) & 2699fa4de7f7SJohannes Berg CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)); 2700fa4de7f7SJohannes Berg 2701fa4de7f7SJohannes Berg return simple_read_from_buffer(user_buf, count, ppos, buf, pos); 2702fa4de7f7SJohannes Berg } 2703fa4de7f7SJohannes Berg 2704fa4de7f7SJohannes Berg static ssize_t iwl_dbgfs_rfkill_write(struct file *file, 2705fa4de7f7SJohannes Berg const char __user *user_buf, 2706fa4de7f7SJohannes Berg size_t count, loff_t *ppos) 2707fa4de7f7SJohannes Berg { 2708fa4de7f7SJohannes Berg struct iwl_trans *trans = file->private_data; 2709fa4de7f7SJohannes Berg struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2710c5bf4fa1SJohannes Berg bool new_value; 2711fa4de7f7SJohannes Berg int ret; 2712fa4de7f7SJohannes Berg 2713c5bf4fa1SJohannes Berg ret = kstrtobool_from_user(user_buf, count, &new_value); 2714fa4de7f7SJohannes Berg if (ret) 2715fa4de7f7SJohannes Berg return ret; 2716c5bf4fa1SJohannes Berg if (new_value == trans_pcie->debug_rfkill) 2717fa4de7f7SJohannes Berg return count; 2718fa4de7f7SJohannes Berg IWL_WARN(trans, "changing debug rfkill %d->%d\n", 2719c5bf4fa1SJohannes Berg trans_pcie->debug_rfkill, new_value); 2720c5bf4fa1SJohannes Berg trans_pcie->debug_rfkill = new_value; 2721fa4de7f7SJohannes Berg iwl_pcie_handle_rfkill_irq(trans); 2722fa4de7f7SJohannes Berg 2723fa4de7f7SJohannes Berg return count; 2724fa4de7f7SJohannes Berg } 2725fa4de7f7SJohannes Berg 2726f7805b33SLior Cohen static int iwl_dbgfs_monitor_data_open(struct inode *inode, 2727f7805b33SLior Cohen struct file *file) 2728f7805b33SLior Cohen { 2729f7805b33SLior Cohen struct iwl_trans *trans = inode->i_private; 2730f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2731f7805b33SLior Cohen 2732f7805b33SLior Cohen if (!trans->dbg_dest_tlv || 2733f7805b33SLior Cohen trans->dbg_dest_tlv->monitor_mode != EXTERNAL_MODE) { 2734f7805b33SLior Cohen IWL_ERR(trans, "Debug destination is not set to DRAM\n"); 2735f7805b33SLior Cohen return -ENOENT; 2736f7805b33SLior Cohen } 2737f7805b33SLior Cohen 2738f7805b33SLior Cohen if (trans_pcie->fw_mon_data.state != IWL_FW_MON_DBGFS_STATE_CLOSED) 2739f7805b33SLior Cohen return -EBUSY; 2740f7805b33SLior Cohen 2741f7805b33SLior Cohen trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_OPEN; 2742f7805b33SLior Cohen return simple_open(inode, file); 2743f7805b33SLior Cohen } 2744f7805b33SLior Cohen 2745f7805b33SLior Cohen static int iwl_dbgfs_monitor_data_release(struct inode *inode, 2746f7805b33SLior Cohen struct file *file) 2747f7805b33SLior Cohen { 2748f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = 2749f7805b33SLior Cohen IWL_TRANS_GET_PCIE_TRANS(inode->i_private); 2750f7805b33SLior Cohen 2751f7805b33SLior Cohen if (trans_pcie->fw_mon_data.state == IWL_FW_MON_DBGFS_STATE_OPEN) 2752f7805b33SLior Cohen trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_CLOSED; 2753f7805b33SLior Cohen return 0; 2754f7805b33SLior Cohen } 2755f7805b33SLior Cohen 2756f7805b33SLior Cohen static bool iwl_write_to_user_buf(char __user *user_buf, ssize_t count, 2757f7805b33SLior Cohen void *buf, ssize_t *size, 2758f7805b33SLior Cohen ssize_t *bytes_copied) 2759f7805b33SLior Cohen { 2760f7805b33SLior Cohen int buf_size_left = count - *bytes_copied; 2761f7805b33SLior Cohen 2762f7805b33SLior Cohen buf_size_left = buf_size_left - (buf_size_left % sizeof(u32)); 2763f7805b33SLior Cohen if (*size > buf_size_left) 2764f7805b33SLior Cohen *size = buf_size_left; 2765f7805b33SLior Cohen 2766f7805b33SLior Cohen *size -= copy_to_user(user_buf, buf, *size); 2767f7805b33SLior Cohen *bytes_copied += *size; 2768f7805b33SLior Cohen 2769f7805b33SLior Cohen if (buf_size_left == *size) 2770f7805b33SLior Cohen return true; 2771f7805b33SLior Cohen return false; 2772f7805b33SLior Cohen } 2773f7805b33SLior Cohen 2774f7805b33SLior Cohen static ssize_t iwl_dbgfs_monitor_data_read(struct file *file, 2775f7805b33SLior Cohen char __user *user_buf, 2776f7805b33SLior Cohen size_t count, loff_t *ppos) 2777f7805b33SLior Cohen { 2778f7805b33SLior Cohen struct iwl_trans *trans = file->private_data; 2779f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2780f7805b33SLior Cohen void *cpu_addr = (void *)trans->fw_mon[0].block, *curr_buf; 2781f7805b33SLior Cohen struct cont_rec *data = &trans_pcie->fw_mon_data; 2782f7805b33SLior Cohen u32 write_ptr_addr, wrap_cnt_addr, write_ptr, wrap_cnt; 2783f7805b33SLior Cohen ssize_t size, bytes_copied = 0; 2784f7805b33SLior Cohen bool b_full; 2785f7805b33SLior Cohen 2786f7805b33SLior Cohen if (trans->dbg_dest_tlv) { 2787f7805b33SLior Cohen write_ptr_addr = 2788f7805b33SLior Cohen le32_to_cpu(trans->dbg_dest_tlv->write_ptr_reg); 2789f7805b33SLior Cohen wrap_cnt_addr = le32_to_cpu(trans->dbg_dest_tlv->wrap_count); 2790f7805b33SLior Cohen } else { 2791f7805b33SLior Cohen write_ptr_addr = MON_BUFF_WRPTR; 2792f7805b33SLior Cohen wrap_cnt_addr = MON_BUFF_CYCLE_CNT; 2793f7805b33SLior Cohen } 2794f7805b33SLior Cohen 2795f7805b33SLior Cohen if (unlikely(!trans->dbg_rec_on)) 2796f7805b33SLior Cohen return 0; 2797f7805b33SLior Cohen 2798f7805b33SLior Cohen mutex_lock(&data->mutex); 2799f7805b33SLior Cohen if (data->state == 2800f7805b33SLior Cohen IWL_FW_MON_DBGFS_STATE_DISABLED) { 2801f7805b33SLior Cohen mutex_unlock(&data->mutex); 2802f7805b33SLior Cohen return 0; 2803f7805b33SLior Cohen } 2804f7805b33SLior Cohen 2805f7805b33SLior Cohen /* write_ptr position in bytes rather then DW */ 2806f7805b33SLior Cohen write_ptr = iwl_read_prph(trans, write_ptr_addr) * sizeof(u32); 2807f7805b33SLior Cohen wrap_cnt = iwl_read_prph(trans, wrap_cnt_addr); 2808f7805b33SLior Cohen 2809f7805b33SLior Cohen if (data->prev_wrap_cnt == wrap_cnt) { 2810f7805b33SLior Cohen size = write_ptr - data->prev_wr_ptr; 2811f7805b33SLior Cohen curr_buf = cpu_addr + data->prev_wr_ptr; 2812f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2813f7805b33SLior Cohen curr_buf, &size, 2814f7805b33SLior Cohen &bytes_copied); 2815f7805b33SLior Cohen data->prev_wr_ptr += size; 2816f7805b33SLior Cohen 2817f7805b33SLior Cohen } else if (data->prev_wrap_cnt == wrap_cnt - 1 && 2818f7805b33SLior Cohen write_ptr < data->prev_wr_ptr) { 2819f7805b33SLior Cohen size = trans->fw_mon[0].size - data->prev_wr_ptr; 2820f7805b33SLior Cohen curr_buf = cpu_addr + data->prev_wr_ptr; 2821f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2822f7805b33SLior Cohen curr_buf, &size, 2823f7805b33SLior Cohen &bytes_copied); 2824f7805b33SLior Cohen data->prev_wr_ptr += size; 2825f7805b33SLior Cohen 2826f7805b33SLior Cohen if (!b_full) { 2827f7805b33SLior Cohen size = write_ptr; 2828f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2829f7805b33SLior Cohen cpu_addr, &size, 2830f7805b33SLior Cohen &bytes_copied); 2831f7805b33SLior Cohen data->prev_wr_ptr = size; 2832f7805b33SLior Cohen data->prev_wrap_cnt++; 2833f7805b33SLior Cohen } 2834f7805b33SLior Cohen } else { 2835f7805b33SLior Cohen if (data->prev_wrap_cnt == wrap_cnt - 1 && 2836f7805b33SLior Cohen write_ptr > data->prev_wr_ptr) 2837f7805b33SLior Cohen IWL_WARN(trans, 2838f7805b33SLior Cohen "write pointer passed previous write pointer, start copying from the beginning\n"); 2839f7805b33SLior Cohen else if (!unlikely(data->prev_wrap_cnt == 0 && 2840f7805b33SLior Cohen data->prev_wr_ptr == 0)) 2841f7805b33SLior Cohen IWL_WARN(trans, 2842f7805b33SLior Cohen "monitor data is out of sync, start copying from the beginning\n"); 2843f7805b33SLior Cohen 2844f7805b33SLior Cohen size = write_ptr; 2845f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2846f7805b33SLior Cohen cpu_addr, &size, 2847f7805b33SLior Cohen &bytes_copied); 2848f7805b33SLior Cohen data->prev_wr_ptr = size; 2849f7805b33SLior Cohen data->prev_wrap_cnt = wrap_cnt; 2850f7805b33SLior Cohen } 2851f7805b33SLior Cohen 2852f7805b33SLior Cohen mutex_unlock(&data->mutex); 2853f7805b33SLior Cohen 2854f7805b33SLior Cohen return bytes_copied; 2855f7805b33SLior Cohen } 2856f7805b33SLior Cohen 2857e705c121SKalle Valo DEBUGFS_READ_WRITE_FILE_OPS(interrupt); 2858e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(fh_reg); 2859e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(rx_queue); 2860e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(tx_queue); 2861e705c121SKalle Valo DEBUGFS_WRITE_FILE_OPS(csr); 2862fa4de7f7SJohannes Berg DEBUGFS_READ_WRITE_FILE_OPS(rfkill); 2863e705c121SKalle Valo 2864f7805b33SLior Cohen static const struct file_operations iwl_dbgfs_monitor_data_ops = { 2865f7805b33SLior Cohen .read = iwl_dbgfs_monitor_data_read, 2866f7805b33SLior Cohen .open = iwl_dbgfs_monitor_data_open, 2867f7805b33SLior Cohen .release = iwl_dbgfs_monitor_data_release, 2868f7805b33SLior Cohen }; 2869f7805b33SLior Cohen 2870f8a1edb7SJohannes Berg /* Create the debugfs files and directories */ 2871cf5d5663SGreg Kroah-Hartman void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans) 2872e705c121SKalle Valo { 2873f8a1edb7SJohannes Berg struct dentry *dir = trans->dbgfs_dir; 2874f8a1edb7SJohannes Berg 28752ef00c53SJoe Perches DEBUGFS_ADD_FILE(rx_queue, dir, 0400); 28762ef00c53SJoe Perches DEBUGFS_ADD_FILE(tx_queue, dir, 0400); 28772ef00c53SJoe Perches DEBUGFS_ADD_FILE(interrupt, dir, 0600); 28782ef00c53SJoe Perches DEBUGFS_ADD_FILE(csr, dir, 0200); 28792ef00c53SJoe Perches DEBUGFS_ADD_FILE(fh_reg, dir, 0400); 28802ef00c53SJoe Perches DEBUGFS_ADD_FILE(rfkill, dir, 0600); 2881f7805b33SLior Cohen DEBUGFS_ADD_FILE(monitor_data, dir, 0400); 2882e705c121SKalle Valo } 2883f7805b33SLior Cohen 2884f7805b33SLior Cohen static void iwl_trans_pcie_debugfs_cleanup(struct iwl_trans *trans) 2885f7805b33SLior Cohen { 2886f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2887f7805b33SLior Cohen struct cont_rec *data = &trans_pcie->fw_mon_data; 2888f7805b33SLior Cohen 2889f7805b33SLior Cohen mutex_lock(&data->mutex); 2890f7805b33SLior Cohen data->state = IWL_FW_MON_DBGFS_STATE_DISABLED; 2891f7805b33SLior Cohen mutex_unlock(&data->mutex); 2892f7805b33SLior Cohen } 2893e705c121SKalle Valo #endif /*CONFIG_IWLWIFI_DEBUGFS */ 2894e705c121SKalle Valo 28956983ba69SSara Sharon static u32 iwl_trans_pcie_get_cmdlen(struct iwl_trans *trans, void *tfd) 2896e705c121SKalle Valo { 28973cd1980bSSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2898e705c121SKalle Valo u32 cmdlen = 0; 2899e705c121SKalle Valo int i; 2900e705c121SKalle Valo 29013cd1980bSSara Sharon for (i = 0; i < trans_pcie->max_tbs; i++) 29026983ba69SSara Sharon cmdlen += iwl_pcie_tfd_tb_get_len(trans, tfd, i); 2903e705c121SKalle Valo 2904e705c121SKalle Valo return cmdlen; 2905e705c121SKalle Valo } 2906e705c121SKalle Valo 2907e705c121SKalle Valo static u32 iwl_trans_pcie_dump_rbs(struct iwl_trans *trans, 2908e705c121SKalle Valo struct iwl_fw_error_dump_data **data, 2909e705c121SKalle Valo int allocated_rb_nums) 2910e705c121SKalle Valo { 2911e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2912e705c121SKalle Valo int max_len = PAGE_SIZE << trans_pcie->rx_page_order; 291378485054SSara Sharon /* Dump RBs is supported only for pre-9000 devices (1 queue) */ 291478485054SSara Sharon struct iwl_rxq *rxq = &trans_pcie->rxq[0]; 2915e705c121SKalle Valo u32 i, r, j, rb_len = 0; 2916e705c121SKalle Valo 2917e705c121SKalle Valo spin_lock(&rxq->lock); 2918e705c121SKalle Valo 29190307c839SGolan Ben Ami r = le16_to_cpu(iwl_get_closed_rb_stts(trans, rxq)) & 0x0FFF; 2920e705c121SKalle Valo 2921e705c121SKalle Valo for (i = rxq->read, j = 0; 2922e705c121SKalle Valo i != r && j < allocated_rb_nums; 2923e705c121SKalle Valo i = (i + 1) & RX_QUEUE_MASK, j++) { 2924e705c121SKalle Valo struct iwl_rx_mem_buffer *rxb = rxq->queue[i]; 2925e705c121SKalle Valo struct iwl_fw_error_dump_rb *rb; 2926e705c121SKalle Valo 2927e705c121SKalle Valo dma_unmap_page(trans->dev, rxb->page_dma, max_len, 2928e705c121SKalle Valo DMA_FROM_DEVICE); 2929e705c121SKalle Valo 2930e705c121SKalle Valo rb_len += sizeof(**data) + sizeof(*rb) + max_len; 2931e705c121SKalle Valo 2932e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RB); 2933e705c121SKalle Valo (*data)->len = cpu_to_le32(sizeof(*rb) + max_len); 2934e705c121SKalle Valo rb = (void *)(*data)->data; 2935e705c121SKalle Valo rb->index = cpu_to_le32(i); 2936e705c121SKalle Valo memcpy(rb->data, page_address(rxb->page), max_len); 2937e705c121SKalle Valo /* remap the page for the free benefit */ 2938e705c121SKalle Valo rxb->page_dma = dma_map_page(trans->dev, rxb->page, 0, 2939e705c121SKalle Valo max_len, 2940e705c121SKalle Valo DMA_FROM_DEVICE); 2941e705c121SKalle Valo 2942e705c121SKalle Valo *data = iwl_fw_error_next_data(*data); 2943e705c121SKalle Valo } 2944e705c121SKalle Valo 2945e705c121SKalle Valo spin_unlock(&rxq->lock); 2946e705c121SKalle Valo 2947e705c121SKalle Valo return rb_len; 2948e705c121SKalle Valo } 2949e705c121SKalle Valo #define IWL_CSR_TO_DUMP (0x250) 2950e705c121SKalle Valo 2951e705c121SKalle Valo static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans, 2952e705c121SKalle Valo struct iwl_fw_error_dump_data **data) 2953e705c121SKalle Valo { 2954e705c121SKalle Valo u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP; 2955e705c121SKalle Valo __le32 *val; 2956e705c121SKalle Valo int i; 2957e705c121SKalle Valo 2958e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR); 2959e705c121SKalle Valo (*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP); 2960e705c121SKalle Valo val = (void *)(*data)->data; 2961e705c121SKalle Valo 2962e705c121SKalle Valo for (i = 0; i < IWL_CSR_TO_DUMP; i += 4) 2963e705c121SKalle Valo *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i)); 2964e705c121SKalle Valo 2965e705c121SKalle Valo *data = iwl_fw_error_next_data(*data); 2966e705c121SKalle Valo 2967e705c121SKalle Valo return csr_len; 2968e705c121SKalle Valo } 2969e705c121SKalle Valo 2970e705c121SKalle Valo static u32 iwl_trans_pcie_fh_regs_dump(struct iwl_trans *trans, 2971e705c121SKalle Valo struct iwl_fw_error_dump_data **data) 2972e705c121SKalle Valo { 2973e705c121SKalle Valo u32 fh_regs_len = FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND; 2974e705c121SKalle Valo unsigned long flags; 2975e705c121SKalle Valo __le32 *val; 2976e705c121SKalle Valo int i; 2977e705c121SKalle Valo 297823ba9340SEmmanuel Grumbach if (!iwl_trans_grab_nic_access(trans, &flags)) 2979e705c121SKalle Valo return 0; 2980e705c121SKalle Valo 2981e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FH_REGS); 2982e705c121SKalle Valo (*data)->len = cpu_to_le32(fh_regs_len); 2983e705c121SKalle Valo val = (void *)(*data)->data; 2984e705c121SKalle Valo 2985723b45e2SLiad Kaufman if (!trans->cfg->gen2) 2986723b45e2SLiad Kaufman for (i = FH_MEM_LOWER_BOUND; i < FH_MEM_UPPER_BOUND; 2987723b45e2SLiad Kaufman i += sizeof(u32)) 2988e705c121SKalle Valo *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i)); 2989723b45e2SLiad Kaufman else 2990ea695b7cSShaul Triebitz for (i = iwl_umac_prph(trans, FH_MEM_LOWER_BOUND_GEN2); 2991ea695b7cSShaul Triebitz i < iwl_umac_prph(trans, FH_MEM_UPPER_BOUND_GEN2); 2992723b45e2SLiad Kaufman i += sizeof(u32)) 2993723b45e2SLiad Kaufman *val++ = cpu_to_le32(iwl_trans_pcie_read_prph(trans, 2994723b45e2SLiad Kaufman i)); 2995e705c121SKalle Valo 2996e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 2997e705c121SKalle Valo 2998e705c121SKalle Valo *data = iwl_fw_error_next_data(*data); 2999e705c121SKalle Valo 3000e705c121SKalle Valo return sizeof(**data) + fh_regs_len; 3001e705c121SKalle Valo } 3002e705c121SKalle Valo 3003e705c121SKalle Valo static u32 3004e705c121SKalle Valo iwl_trans_pci_dump_marbh_monitor(struct iwl_trans *trans, 3005e705c121SKalle Valo struct iwl_fw_error_dump_fw_mon *fw_mon_data, 3006e705c121SKalle Valo u32 monitor_len) 3007e705c121SKalle Valo { 3008e705c121SKalle Valo u32 buf_size_in_dwords = (monitor_len >> 2); 3009e705c121SKalle Valo u32 *buffer = (u32 *)fw_mon_data->data; 3010e705c121SKalle Valo unsigned long flags; 3011e705c121SKalle Valo u32 i; 3012e705c121SKalle Valo 301323ba9340SEmmanuel Grumbach if (!iwl_trans_grab_nic_access(trans, &flags)) 3014e705c121SKalle Valo return 0; 3015e705c121SKalle Valo 3016ea695b7cSShaul Triebitz iwl_write_umac_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x1); 3017e705c121SKalle Valo for (i = 0; i < buf_size_in_dwords; i++) 3018ea695b7cSShaul Triebitz buffer[i] = iwl_read_umac_prph_no_grab(trans, 301914ef1b43SGolan Ben-Ami MON_DMARB_RD_DATA_ADDR); 3020ea695b7cSShaul Triebitz iwl_write_umac_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x0); 3021e705c121SKalle Valo 3022e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 3023e705c121SKalle Valo 3024e705c121SKalle Valo return monitor_len; 3025e705c121SKalle Valo } 3026e705c121SKalle Valo 30277a14c23dSSara Sharon static void 30287a14c23dSSara Sharon iwl_trans_pcie_dump_pointers(struct iwl_trans *trans, 30297a14c23dSSara Sharon struct iwl_fw_error_dump_fw_mon *fw_mon_data) 30307a14c23dSSara Sharon { 3031c88580e1SShahar S Matityahu u32 base, base_high, write_ptr, write_ptr_val, wrap_cnt; 30327a14c23dSSara Sharon 3033c88580e1SShahar S Matityahu if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 3034c88580e1SShahar S Matityahu base = DBGC_CUR_DBGBUF_BASE_ADDR_LSB; 3035c88580e1SShahar S Matityahu base_high = DBGC_CUR_DBGBUF_BASE_ADDR_MSB; 3036c88580e1SShahar S Matityahu write_ptr = DBGC_CUR_DBGBUF_STATUS; 3037c88580e1SShahar S Matityahu wrap_cnt = DBGC_DBGBUF_WRAP_AROUND; 3038c88580e1SShahar S Matityahu } else if (trans->ini_valid) { 3039ea695b7cSShaul Triebitz base = iwl_umac_prph(trans, MON_BUFF_BASE_ADDR_VER2); 3040ea695b7cSShaul Triebitz write_ptr = iwl_umac_prph(trans, MON_BUFF_WRPTR_VER2); 3041ea695b7cSShaul Triebitz wrap_cnt = iwl_umac_prph(trans, MON_BUFF_CYCLE_CNT_VER2); 30427a14c23dSSara Sharon } else if (trans->dbg_dest_tlv) { 30437a14c23dSSara Sharon write_ptr = le32_to_cpu(trans->dbg_dest_tlv->write_ptr_reg); 30447a14c23dSSara Sharon wrap_cnt = le32_to_cpu(trans->dbg_dest_tlv->wrap_count); 30457a14c23dSSara Sharon base = le32_to_cpu(trans->dbg_dest_tlv->base_reg); 30467a14c23dSSara Sharon } else { 30477a14c23dSSara Sharon base = MON_BUFF_BASE_ADDR; 30487a14c23dSSara Sharon write_ptr = MON_BUFF_WRPTR; 30497a14c23dSSara Sharon wrap_cnt = MON_BUFF_CYCLE_CNT; 30507a14c23dSSara Sharon } 3051c88580e1SShahar S Matityahu 3052c88580e1SShahar S Matityahu write_ptr_val = iwl_read_prph(trans, write_ptr); 30537a14c23dSSara Sharon fw_mon_data->fw_mon_cycle_cnt = 30547a14c23dSSara Sharon cpu_to_le32(iwl_read_prph(trans, wrap_cnt)); 30557a14c23dSSara Sharon fw_mon_data->fw_mon_base_ptr = 30567a14c23dSSara Sharon cpu_to_le32(iwl_read_prph(trans, base)); 3057c88580e1SShahar S Matityahu if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 3058c88580e1SShahar S Matityahu fw_mon_data->fw_mon_base_high_ptr = 3059c88580e1SShahar S Matityahu cpu_to_le32(iwl_read_prph(trans, base_high)); 3060c88580e1SShahar S Matityahu write_ptr_val &= DBGC_CUR_DBGBUF_STATUS_OFFSET_MSK; 3061c88580e1SShahar S Matityahu } 3062c88580e1SShahar S Matityahu fw_mon_data->fw_mon_wr_ptr = cpu_to_le32(write_ptr_val); 30637a14c23dSSara Sharon } 30647a14c23dSSara Sharon 3065e705c121SKalle Valo static u32 3066e705c121SKalle Valo iwl_trans_pcie_dump_monitor(struct iwl_trans *trans, 3067e705c121SKalle Valo struct iwl_fw_error_dump_data **data, 3068e705c121SKalle Valo u32 monitor_len) 3069e705c121SKalle Valo { 3070e705c121SKalle Valo u32 len = 0; 3071e705c121SKalle Valo 307288964b2eSSara Sharon if ((trans->num_blocks && 3073c88580e1SShahar S Matityahu (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000 || 3074c88580e1SShahar S Matityahu trans->cfg->device_family >= IWL_DEVICE_FAMILY_AX210 || 3075c88580e1SShahar S Matityahu trans->ini_valid)) || 3076c88580e1SShahar S Matityahu (trans->dbg_dest_tlv && !trans->ini_valid)) { 3077e705c121SKalle Valo struct iwl_fw_error_dump_fw_mon *fw_mon_data; 3078e705c121SKalle Valo 3079e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR); 3080e705c121SKalle Valo fw_mon_data = (void *)(*data)->data; 30817a14c23dSSara Sharon 30827a14c23dSSara Sharon iwl_trans_pcie_dump_pointers(trans, fw_mon_data); 3083e705c121SKalle Valo 3084e705c121SKalle Valo len += sizeof(**data) + sizeof(*fw_mon_data); 308588964b2eSSara Sharon if (trans->num_blocks) { 3086e705c121SKalle Valo memcpy(fw_mon_data->data, 308788964b2eSSara Sharon trans->fw_mon[0].block, 308888964b2eSSara Sharon trans->fw_mon[0].size); 3089e705c121SKalle Valo 309088964b2eSSara Sharon monitor_len = trans->fw_mon[0].size; 3091e705c121SKalle Valo } else if (trans->dbg_dest_tlv->monitor_mode == SMEM_MODE) { 30927a14c23dSSara Sharon u32 base = le32_to_cpu(fw_mon_data->fw_mon_base_ptr); 3093e705c121SKalle Valo /* 3094e705c121SKalle Valo * Update pointers to reflect actual values after 3095e705c121SKalle Valo * shifting 3096e705c121SKalle Valo */ 3097fd527eb5SGolan Ben Ami if (trans->dbg_dest_tlv->version) { 3098fd527eb5SGolan Ben Ami base = (iwl_read_prph(trans, base) & 3099fd527eb5SGolan Ben Ami IWL_LDBG_M2S_BUF_BA_MSK) << 3100fd527eb5SGolan Ben Ami trans->dbg_dest_tlv->base_shift; 3101fd527eb5SGolan Ben Ami base *= IWL_M2S_UNIT_SIZE; 3102fd527eb5SGolan Ben Ami base += trans->cfg->smem_offset; 3103fd527eb5SGolan Ben Ami } else { 3104e705c121SKalle Valo base = iwl_read_prph(trans, base) << 3105e705c121SKalle Valo trans->dbg_dest_tlv->base_shift; 3106fd527eb5SGolan Ben Ami } 3107fd527eb5SGolan Ben Ami 3108e705c121SKalle Valo iwl_trans_read_mem(trans, base, fw_mon_data->data, 3109e705c121SKalle Valo monitor_len / sizeof(u32)); 3110e705c121SKalle Valo } else if (trans->dbg_dest_tlv->monitor_mode == MARBH_MODE) { 3111e705c121SKalle Valo monitor_len = 3112e705c121SKalle Valo iwl_trans_pci_dump_marbh_monitor(trans, 3113e705c121SKalle Valo fw_mon_data, 3114e705c121SKalle Valo monitor_len); 3115e705c121SKalle Valo } else { 3116e705c121SKalle Valo /* Didn't match anything - output no monitor data */ 3117e705c121SKalle Valo monitor_len = 0; 3118e705c121SKalle Valo } 3119e705c121SKalle Valo 3120e705c121SKalle Valo len += monitor_len; 3121e705c121SKalle Valo (*data)->len = cpu_to_le32(monitor_len + sizeof(*fw_mon_data)); 3122e705c121SKalle Valo } 3123e705c121SKalle Valo 3124e705c121SKalle Valo return len; 3125e705c121SKalle Valo } 3126e705c121SKalle Valo 312793079fd5SJohannes Berg static int iwl_trans_get_fw_monitor_len(struct iwl_trans *trans, u32 *len) 3128e705c121SKalle Valo { 312988964b2eSSara Sharon if (trans->num_blocks) { 3130da752717SShahar S Matityahu *len += sizeof(struct iwl_fw_error_dump_data) + 3131da752717SShahar S Matityahu sizeof(struct iwl_fw_error_dump_fw_mon) + 313288964b2eSSara Sharon trans->fw_mon[0].size; 313388964b2eSSara Sharon return trans->fw_mon[0].size; 3134e705c121SKalle Valo } else if (trans->dbg_dest_tlv) { 3135da752717SShahar S Matityahu u32 base, end, cfg_reg, monitor_len; 3136e705c121SKalle Valo 3137fd527eb5SGolan Ben Ami if (trans->dbg_dest_tlv->version == 1) { 3138fd527eb5SGolan Ben Ami cfg_reg = le32_to_cpu(trans->dbg_dest_tlv->base_reg); 3139fd527eb5SGolan Ben Ami cfg_reg = iwl_read_prph(trans, cfg_reg); 3140fd527eb5SGolan Ben Ami base = (cfg_reg & IWL_LDBG_M2S_BUF_BA_MSK) << 3141fd527eb5SGolan Ben Ami trans->dbg_dest_tlv->base_shift; 3142fd527eb5SGolan Ben Ami base *= IWL_M2S_UNIT_SIZE; 3143fd527eb5SGolan Ben Ami base += trans->cfg->smem_offset; 3144fd527eb5SGolan Ben Ami 3145fd527eb5SGolan Ben Ami monitor_len = 3146fd527eb5SGolan Ben Ami (cfg_reg & IWL_LDBG_M2S_BUF_SIZE_MSK) >> 3147fd527eb5SGolan Ben Ami trans->dbg_dest_tlv->end_shift; 3148fd527eb5SGolan Ben Ami monitor_len *= IWL_M2S_UNIT_SIZE; 3149fd527eb5SGolan Ben Ami } else { 3150e705c121SKalle Valo base = le32_to_cpu(trans->dbg_dest_tlv->base_reg); 3151e705c121SKalle Valo end = le32_to_cpu(trans->dbg_dest_tlv->end_reg); 3152e705c121SKalle Valo 3153e705c121SKalle Valo base = iwl_read_prph(trans, base) << 3154e705c121SKalle Valo trans->dbg_dest_tlv->base_shift; 3155e705c121SKalle Valo end = iwl_read_prph(trans, end) << 3156e705c121SKalle Valo trans->dbg_dest_tlv->end_shift; 3157e705c121SKalle Valo 3158e705c121SKalle Valo /* Make "end" point to the actual end */ 3159fd527eb5SGolan Ben Ami if (trans->cfg->device_family >= 3160fd527eb5SGolan Ben Ami IWL_DEVICE_FAMILY_8000 || 3161e705c121SKalle Valo trans->dbg_dest_tlv->monitor_mode == MARBH_MODE) 3162e705c121SKalle Valo end += (1 << trans->dbg_dest_tlv->end_shift); 3163e705c121SKalle Valo monitor_len = end - base; 3164fd527eb5SGolan Ben Ami } 3165da752717SShahar S Matityahu *len += sizeof(struct iwl_fw_error_dump_data) + 3166da752717SShahar S Matityahu sizeof(struct iwl_fw_error_dump_fw_mon) + 3167e705c121SKalle Valo monitor_len; 3168da752717SShahar S Matityahu return monitor_len; 3169e705c121SKalle Valo } 3170da752717SShahar S Matityahu return 0; 3171da752717SShahar S Matityahu } 3172da752717SShahar S Matityahu 3173da752717SShahar S Matityahu static struct iwl_trans_dump_data 3174da752717SShahar S Matityahu *iwl_trans_pcie_dump_data(struct iwl_trans *trans, 317579f033f6SSara Sharon u32 dump_mask) 3176da752717SShahar S Matityahu { 3177da752717SShahar S Matityahu struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 3178da752717SShahar S Matityahu struct iwl_fw_error_dump_data *data; 3179da752717SShahar S Matityahu struct iwl_txq *cmdq = trans_pcie->txq[trans_pcie->cmd_queue]; 3180da752717SShahar S Matityahu struct iwl_fw_error_dump_txcmd *txcmd; 3181da752717SShahar S Matityahu struct iwl_trans_dump_data *dump_data; 3182fefbf853SShahar S Matityahu u32 len, num_rbs = 0, monitor_len = 0; 3183da752717SShahar S Matityahu int i, ptr; 3184da752717SShahar S Matityahu bool dump_rbs = test_bit(STATUS_FW_ERROR, &trans->status) && 3185da752717SShahar S Matityahu !trans->cfg->mq_rx_supported && 318679f033f6SSara Sharon dump_mask & BIT(IWL_FW_ERROR_DUMP_RB); 318779f033f6SSara Sharon 318879f033f6SSara Sharon if (!dump_mask) 318979f033f6SSara Sharon return NULL; 3190da752717SShahar S Matityahu 3191da752717SShahar S Matityahu /* transport dump header */ 3192da752717SShahar S Matityahu len = sizeof(*dump_data); 3193da752717SShahar S Matityahu 3194da752717SShahar S Matityahu /* host commands */ 31958672aad3SShahar S Matityahu if (dump_mask & BIT(IWL_FW_ERROR_DUMP_TXCMD)) 3196da752717SShahar S Matityahu len += sizeof(*data) + 31978672aad3SShahar S Matityahu cmdq->n_window * (sizeof(*txcmd) + 31988672aad3SShahar S Matityahu TFD_MAX_PAYLOAD_SIZE); 3199da752717SShahar S Matityahu 3200da752717SShahar S Matityahu /* FW monitor */ 3201fefbf853SShahar S Matityahu if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FW_MONITOR)) 3202da752717SShahar S Matityahu monitor_len = iwl_trans_get_fw_monitor_len(trans, &len); 3203e705c121SKalle Valo 3204e705c121SKalle Valo /* CSR registers */ 320579f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_CSR)) 3206e705c121SKalle Valo len += sizeof(*data) + IWL_CSR_TO_DUMP; 3207e705c121SKalle Valo 3208e705c121SKalle Valo /* FH registers */ 320979f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FH_REGS)) { 3210723b45e2SLiad Kaufman if (trans->cfg->gen2) 3211723b45e2SLiad Kaufman len += sizeof(*data) + 3212ea695b7cSShaul Triebitz (iwl_umac_prph(trans, FH_MEM_UPPER_BOUND_GEN2) - 3213ea695b7cSShaul Triebitz iwl_umac_prph(trans, FH_MEM_LOWER_BOUND_GEN2)); 3214723b45e2SLiad Kaufman else 3215723b45e2SLiad Kaufman len += sizeof(*data) + 3216520f03eaSShahar S Matityahu (FH_MEM_UPPER_BOUND - 3217520f03eaSShahar S Matityahu FH_MEM_LOWER_BOUND); 3218520f03eaSShahar S Matityahu } 3219e705c121SKalle Valo 3220e705c121SKalle Valo if (dump_rbs) { 322178485054SSara Sharon /* Dump RBs is supported only for pre-9000 devices (1 queue) */ 322278485054SSara Sharon struct iwl_rxq *rxq = &trans_pcie->rxq[0]; 3223e705c121SKalle Valo /* RBs */ 32240307c839SGolan Ben Ami num_rbs = 32250307c839SGolan Ben Ami le16_to_cpu(iwl_get_closed_rb_stts(trans, rxq)) 3226e705c121SKalle Valo & 0x0FFF; 322778485054SSara Sharon num_rbs = (num_rbs - rxq->read) & RX_QUEUE_MASK; 3228e705c121SKalle Valo len += num_rbs * (sizeof(*data) + 3229e705c121SKalle Valo sizeof(struct iwl_fw_error_dump_rb) + 3230e705c121SKalle Valo (PAGE_SIZE << trans_pcie->rx_page_order)); 3231e705c121SKalle Valo } 3232e705c121SKalle Valo 32335538409bSLiad Kaufman /* Paged memory for gen2 HW */ 323479f033f6SSara Sharon if (trans->cfg->gen2 && dump_mask & BIT(IWL_FW_ERROR_DUMP_PAGING)) 3235505a00c0SShahar S Matityahu for (i = 0; i < trans->init_dram.paging_cnt; i++) 32365538409bSLiad Kaufman len += sizeof(*data) + 32375538409bSLiad Kaufman sizeof(struct iwl_fw_error_dump_paging) + 3238505a00c0SShahar S Matityahu trans->init_dram.paging[i].size; 32395538409bSLiad Kaufman 3240e705c121SKalle Valo dump_data = vzalloc(len); 3241e705c121SKalle Valo if (!dump_data) 3242e705c121SKalle Valo return NULL; 3243e705c121SKalle Valo 3244e705c121SKalle Valo len = 0; 3245e705c121SKalle Valo data = (void *)dump_data->data; 3246520f03eaSShahar S Matityahu 324779f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_TXCMD)) { 3248520f03eaSShahar S Matityahu u16 tfd_size = trans_pcie->tfd_size; 3249520f03eaSShahar S Matityahu 3250e705c121SKalle Valo data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD); 3251e705c121SKalle Valo txcmd = (void *)data->data; 3252e705c121SKalle Valo spin_lock_bh(&cmdq->lock); 3253bb98ecd4SSara Sharon ptr = cmdq->write_ptr; 3254bb98ecd4SSara Sharon for (i = 0; i < cmdq->n_window; i++) { 32554ecab561SEmmanuel Grumbach u8 idx = iwl_pcie_get_cmd_index(cmdq, ptr); 3256e705c121SKalle Valo u32 caplen, cmdlen; 3257e705c121SKalle Valo 3258520f03eaSShahar S Matityahu cmdlen = iwl_trans_pcie_get_cmdlen(trans, 3259520f03eaSShahar S Matityahu cmdq->tfds + 3260520f03eaSShahar S Matityahu tfd_size * ptr); 3261e705c121SKalle Valo caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen); 3262e705c121SKalle Valo 3263e705c121SKalle Valo if (cmdlen) { 3264e705c121SKalle Valo len += sizeof(*txcmd) + caplen; 3265e705c121SKalle Valo txcmd->cmdlen = cpu_to_le32(cmdlen); 3266e705c121SKalle Valo txcmd->caplen = cpu_to_le32(caplen); 3267520f03eaSShahar S Matityahu memcpy(txcmd->data, cmdq->entries[idx].cmd, 3268520f03eaSShahar S Matityahu caplen); 3269e705c121SKalle Valo txcmd = (void *)((u8 *)txcmd->data + caplen); 3270e705c121SKalle Valo } 3271e705c121SKalle Valo 32727b3e42eaSGolan Ben Ami ptr = iwl_queue_dec_wrap(trans, ptr); 3273e705c121SKalle Valo } 3274e705c121SKalle Valo spin_unlock_bh(&cmdq->lock); 3275e705c121SKalle Valo 3276e705c121SKalle Valo data->len = cpu_to_le32(len); 3277e705c121SKalle Valo len += sizeof(*data); 3278e705c121SKalle Valo data = iwl_fw_error_next_data(data); 3279520f03eaSShahar S Matityahu } 3280e705c121SKalle Valo 328179f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_CSR)) 3282e705c121SKalle Valo len += iwl_trans_pcie_dump_csr(trans, &data); 328379f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FH_REGS)) 3284e705c121SKalle Valo len += iwl_trans_pcie_fh_regs_dump(trans, &data); 3285e705c121SKalle Valo if (dump_rbs) 3286e705c121SKalle Valo len += iwl_trans_pcie_dump_rbs(trans, &data, num_rbs); 3287e705c121SKalle Valo 32885538409bSLiad Kaufman /* Paged memory for gen2 HW */ 328979f033f6SSara Sharon if (trans->cfg->gen2 && dump_mask & BIT(IWL_FW_ERROR_DUMP_PAGING)) { 3290505a00c0SShahar S Matityahu for (i = 0; i < trans->init_dram.paging_cnt; i++) { 32915538409bSLiad Kaufman struct iwl_fw_error_dump_paging *paging; 3292505a00c0SShahar S Matityahu u32 page_len = trans->init_dram.paging[i].size; 32935538409bSLiad Kaufman 32945538409bSLiad Kaufman data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PAGING); 32955538409bSLiad Kaufman data->len = cpu_to_le32(sizeof(*paging) + page_len); 32965538409bSLiad Kaufman paging = (void *)data->data; 32975538409bSLiad Kaufman paging->index = cpu_to_le32(i); 32985538409bSLiad Kaufman memcpy(paging->data, 3299505a00c0SShahar S Matityahu trans->init_dram.paging[i].block, page_len); 33005538409bSLiad Kaufman data = iwl_fw_error_next_data(data); 33015538409bSLiad Kaufman 33025538409bSLiad Kaufman len += sizeof(*data) + sizeof(*paging) + page_len; 33035538409bSLiad Kaufman } 33045538409bSLiad Kaufman } 330579f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FW_MONITOR)) 3306e705c121SKalle Valo len += iwl_trans_pcie_dump_monitor(trans, &data, monitor_len); 3307e705c121SKalle Valo 3308e705c121SKalle Valo dump_data->len = len; 3309e705c121SKalle Valo 3310e705c121SKalle Valo return dump_data; 3311e705c121SKalle Valo } 3312e705c121SKalle Valo 33134cbb8e50SLuciano Coelho #ifdef CONFIG_PM_SLEEP 33144cbb8e50SLuciano Coelho static int iwl_trans_pcie_suspend(struct iwl_trans *trans) 33154cbb8e50SLuciano Coelho { 3316e4c49c49SLuca Coelho if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3 && 3317e4c49c49SLuca Coelho (trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)) 33184cbb8e50SLuciano Coelho return iwl_pci_fw_enter_d0i3(trans); 33194cbb8e50SLuciano Coelho 33204cbb8e50SLuciano Coelho return 0; 33214cbb8e50SLuciano Coelho } 33224cbb8e50SLuciano Coelho 33234cbb8e50SLuciano Coelho static void iwl_trans_pcie_resume(struct iwl_trans *trans) 33244cbb8e50SLuciano Coelho { 3325e4c49c49SLuca Coelho if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3 && 3326e4c49c49SLuca Coelho (trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)) 33274cbb8e50SLuciano Coelho iwl_pci_fw_exit_d0i3(trans); 33284cbb8e50SLuciano Coelho } 33294cbb8e50SLuciano Coelho #endif /* CONFIG_PM_SLEEP */ 33304cbb8e50SLuciano Coelho 3331623e7766SSara Sharon #define IWL_TRANS_COMMON_OPS \ 3332623e7766SSara Sharon .op_mode_leave = iwl_trans_pcie_op_mode_leave, \ 3333623e7766SSara Sharon .write8 = iwl_trans_pcie_write8, \ 3334623e7766SSara Sharon .write32 = iwl_trans_pcie_write32, \ 3335623e7766SSara Sharon .read32 = iwl_trans_pcie_read32, \ 3336623e7766SSara Sharon .read_prph = iwl_trans_pcie_read_prph, \ 3337623e7766SSara Sharon .write_prph = iwl_trans_pcie_write_prph, \ 3338623e7766SSara Sharon .read_mem = iwl_trans_pcie_read_mem, \ 3339623e7766SSara Sharon .write_mem = iwl_trans_pcie_write_mem, \ 3340623e7766SSara Sharon .configure = iwl_trans_pcie_configure, \ 3341623e7766SSara Sharon .set_pmi = iwl_trans_pcie_set_pmi, \ 3342870c2a11SGolan Ben Ami .sw_reset = iwl_trans_pcie_sw_reset, \ 3343623e7766SSara Sharon .grab_nic_access = iwl_trans_pcie_grab_nic_access, \ 3344623e7766SSara Sharon .release_nic_access = iwl_trans_pcie_release_nic_access, \ 3345623e7766SSara Sharon .set_bits_mask = iwl_trans_pcie_set_bits_mask, \ 3346623e7766SSara Sharon .ref = iwl_trans_pcie_ref, \ 3347623e7766SSara Sharon .unref = iwl_trans_pcie_unref, \ 3348623e7766SSara Sharon .dump_data = iwl_trans_pcie_dump_data, \ 3349623e7766SSara Sharon .d3_suspend = iwl_trans_pcie_d3_suspend, \ 3350d1967ce6SShahar S Matityahu .d3_resume = iwl_trans_pcie_d3_resume, \ 3351d1967ce6SShahar S Matityahu .sync_nmi = iwl_trans_pcie_sync_nmi 3352623e7766SSara Sharon 3353623e7766SSara Sharon #ifdef CONFIG_PM_SLEEP 3354623e7766SSara Sharon #define IWL_TRANS_PM_OPS \ 3355623e7766SSara Sharon .suspend = iwl_trans_pcie_suspend, \ 3356623e7766SSara Sharon .resume = iwl_trans_pcie_resume, 3357623e7766SSara Sharon #else 3358623e7766SSara Sharon #define IWL_TRANS_PM_OPS 3359623e7766SSara Sharon #endif /* CONFIG_PM_SLEEP */ 3360623e7766SSara Sharon 3361e705c121SKalle Valo static const struct iwl_trans_ops trans_ops_pcie = { 3362623e7766SSara Sharon IWL_TRANS_COMMON_OPS, 3363623e7766SSara Sharon IWL_TRANS_PM_OPS 3364e705c121SKalle Valo .start_hw = iwl_trans_pcie_start_hw, 3365e705c121SKalle Valo .fw_alive = iwl_trans_pcie_fw_alive, 3366e705c121SKalle Valo .start_fw = iwl_trans_pcie_start_fw, 3367e705c121SKalle Valo .stop_device = iwl_trans_pcie_stop_device, 3368e705c121SKalle Valo 3369e705c121SKalle Valo .send_cmd = iwl_trans_pcie_send_hcmd, 3370e705c121SKalle Valo 3371e705c121SKalle Valo .tx = iwl_trans_pcie_tx, 3372e705c121SKalle Valo .reclaim = iwl_trans_pcie_reclaim, 3373e705c121SKalle Valo 3374e705c121SKalle Valo .txq_disable = iwl_trans_pcie_txq_disable, 3375e705c121SKalle Valo .txq_enable = iwl_trans_pcie_txq_enable, 3376e705c121SKalle Valo 337742db09c1SLiad Kaufman .txq_set_shared_mode = iwl_trans_pcie_txq_set_shared_mode, 337842db09c1SLiad Kaufman 3379d6d517b7SSara Sharon .wait_tx_queues_empty = iwl_trans_pcie_wait_txqs_empty, 3380d6d517b7SSara Sharon 3381e705c121SKalle Valo .freeze_txq_timer = iwl_trans_pcie_freeze_txq_timer, 33820cd58eaaSEmmanuel Grumbach .block_txq_ptrs = iwl_trans_pcie_block_txq_ptrs, 3383f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS 3384f7805b33SLior Cohen .debugfs_cleanup = iwl_trans_pcie_debugfs_cleanup, 3385f7805b33SLior Cohen #endif 3386623e7766SSara Sharon }; 3387e705c121SKalle Valo 3388623e7766SSara Sharon static const struct iwl_trans_ops trans_ops_pcie_gen2 = { 3389623e7766SSara Sharon IWL_TRANS_COMMON_OPS, 3390623e7766SSara Sharon IWL_TRANS_PM_OPS 3391623e7766SSara Sharon .start_hw = iwl_trans_pcie_start_hw, 3392eda50cdeSSara Sharon .fw_alive = iwl_trans_pcie_gen2_fw_alive, 3393eda50cdeSSara Sharon .start_fw = iwl_trans_pcie_gen2_start_fw, 339477c09bc8SSara Sharon .stop_device = iwl_trans_pcie_gen2_stop_device, 3395e705c121SKalle Valo 3396ca60da2eSSara Sharon .send_cmd = iwl_trans_pcie_gen2_send_hcmd, 3397e705c121SKalle Valo 3398ab6c6445SSara Sharon .tx = iwl_trans_pcie_gen2_tx, 3399623e7766SSara Sharon .reclaim = iwl_trans_pcie_reclaim, 3400623e7766SSara Sharon 34016b35ff91SSara Sharon .txq_alloc = iwl_trans_pcie_dyn_txq_alloc, 34026b35ff91SSara Sharon .txq_free = iwl_trans_pcie_dyn_txq_free, 3403d6d517b7SSara Sharon .wait_txq_empty = iwl_trans_pcie_wait_txq_empty, 340492536c96SSara Sharon .rxq_dma_data = iwl_trans_pcie_rxq_dma_data, 3405f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS 3406f7805b33SLior Cohen .debugfs_cleanup = iwl_trans_pcie_debugfs_cleanup, 3407f7805b33SLior Cohen #endif 3408e705c121SKalle Valo }; 3409e705c121SKalle Valo 3410e705c121SKalle Valo struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev, 3411e705c121SKalle Valo const struct pci_device_id *ent, 3412e705c121SKalle Valo const struct iwl_cfg *cfg) 3413e705c121SKalle Valo { 3414e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie; 3415e705c121SKalle Valo struct iwl_trans *trans; 341696a6497bSSara Sharon int ret, addr_size; 3417e705c121SKalle Valo 34185a41a86cSSharon Dvir ret = pcim_enable_device(pdev); 34195a41a86cSSharon Dvir if (ret) 34205a41a86cSSharon Dvir return ERR_PTR(ret); 34215a41a86cSSharon Dvir 3422623e7766SSara Sharon if (cfg->gen2) 3423623e7766SSara Sharon trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie), 3424623e7766SSara Sharon &pdev->dev, cfg, &trans_ops_pcie_gen2); 3425623e7766SSara Sharon else 3426e705c121SKalle Valo trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie), 34271ea423b0SLuca Coelho &pdev->dev, cfg, &trans_ops_pcie); 3428e705c121SKalle Valo if (!trans) 3429e705c121SKalle Valo return ERR_PTR(-ENOMEM); 3430e705c121SKalle Valo 3431e705c121SKalle Valo trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 3432e705c121SKalle Valo 3433e705c121SKalle Valo trans_pcie->trans = trans; 3434326477e4SJohannes Berg trans_pcie->opmode_down = true; 3435e705c121SKalle Valo spin_lock_init(&trans_pcie->irq_lock); 3436e705c121SKalle Valo spin_lock_init(&trans_pcie->reg_lock); 3437e705c121SKalle Valo mutex_init(&trans_pcie->mutex); 3438e705c121SKalle Valo init_waitqueue_head(&trans_pcie->ucode_write_waitq); 34396eb5e529SEmmanuel Grumbach trans_pcie->tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page); 34406eb5e529SEmmanuel Grumbach if (!trans_pcie->tso_hdr_page) { 34416eb5e529SEmmanuel Grumbach ret = -ENOMEM; 34426eb5e529SEmmanuel Grumbach goto out_no_pci; 34436eb5e529SEmmanuel Grumbach } 3444c5bf4fa1SJohannes Berg trans_pcie->debug_rfkill = -1; 3445e705c121SKalle Valo 3446e705c121SKalle Valo if (!cfg->base_params->pcie_l1_allowed) { 3447e705c121SKalle Valo /* 3448e705c121SKalle Valo * W/A - seems to solve weird behavior. We need to remove this 3449e705c121SKalle Valo * if we don't want to stay in L1 all the time. This wastes a 3450e705c121SKalle Valo * lot of power. 3451e705c121SKalle Valo */ 3452e705c121SKalle Valo pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | 3453e705c121SKalle Valo PCIE_LINK_STATE_L1 | 3454e705c121SKalle Valo PCIE_LINK_STATE_CLKPM); 3455e705c121SKalle Valo } 3456e705c121SKalle Valo 34579416560eSGolan Ben Ami trans_pcie->def_rx_queue = 0; 34589416560eSGolan Ben Ami 34596983ba69SSara Sharon if (cfg->use_tfh) { 34602c6262b7SSara Sharon addr_size = 64; 34613cd1980bSSara Sharon trans_pcie->max_tbs = IWL_TFH_NUM_TBS; 34628352e62aSSara Sharon trans_pcie->tfd_size = sizeof(struct iwl_tfh_tfd); 34636983ba69SSara Sharon } else { 34642c6262b7SSara Sharon addr_size = 36; 34653cd1980bSSara Sharon trans_pcie->max_tbs = IWL_NUM_OF_TBS; 34666983ba69SSara Sharon trans_pcie->tfd_size = sizeof(struct iwl_tfd); 34676983ba69SSara Sharon } 34683cd1980bSSara Sharon trans->max_skb_frags = IWL_PCIE_MAX_FRAGS(trans_pcie); 34693cd1980bSSara Sharon 3470e705c121SKalle Valo pci_set_master(pdev); 3471e705c121SKalle Valo 347296a6497bSSara Sharon ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(addr_size)); 3473e705c121SKalle Valo if (!ret) 347496a6497bSSara Sharon ret = pci_set_consistent_dma_mask(pdev, 347596a6497bSSara Sharon DMA_BIT_MASK(addr_size)); 3476e705c121SKalle Valo if (ret) { 3477e705c121SKalle Valo ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 3478e705c121SKalle Valo if (!ret) 3479e705c121SKalle Valo ret = pci_set_consistent_dma_mask(pdev, 3480e705c121SKalle Valo DMA_BIT_MASK(32)); 3481e705c121SKalle Valo /* both attempts failed: */ 3482e705c121SKalle Valo if (ret) { 3483e705c121SKalle Valo dev_err(&pdev->dev, "No suitable DMA available\n"); 34845a41a86cSSharon Dvir goto out_no_pci; 3485e705c121SKalle Valo } 3486e705c121SKalle Valo } 3487e705c121SKalle Valo 34885a41a86cSSharon Dvir ret = pcim_iomap_regions_request_all(pdev, BIT(0), DRV_NAME); 3489e705c121SKalle Valo if (ret) { 34905a41a86cSSharon Dvir dev_err(&pdev->dev, "pcim_iomap_regions_request_all failed\n"); 34915a41a86cSSharon Dvir goto out_no_pci; 3492e705c121SKalle Valo } 3493e705c121SKalle Valo 34945a41a86cSSharon Dvir trans_pcie->hw_base = pcim_iomap_table(pdev)[0]; 3495e705c121SKalle Valo if (!trans_pcie->hw_base) { 34965a41a86cSSharon Dvir dev_err(&pdev->dev, "pcim_iomap_table failed\n"); 3497e705c121SKalle Valo ret = -ENODEV; 34985a41a86cSSharon Dvir goto out_no_pci; 3499e705c121SKalle Valo } 3500e705c121SKalle Valo 3501e705c121SKalle Valo /* We disable the RETRY_TIMEOUT register (0x41) to keep 3502e705c121SKalle Valo * PCI Tx retries from interfering with C3 CPU state */ 3503e705c121SKalle Valo pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00); 3504e705c121SKalle Valo 3505e705c121SKalle Valo trans_pcie->pci_dev = pdev; 3506e705c121SKalle Valo iwl_disable_interrupts(trans); 3507e705c121SKalle Valo 3508e705c121SKalle Valo trans->hw_rev = iwl_read32(trans, CSR_HW_REV); 35099a098a89SRajat Jain if (trans->hw_rev == 0xffffffff) { 35109a098a89SRajat Jain dev_err(&pdev->dev, "HW_REV=0xFFFFFFFF, PCI issues?\n"); 35119a098a89SRajat Jain ret = -EIO; 35129a098a89SRajat Jain goto out_no_pci; 35139a098a89SRajat Jain } 35149a098a89SRajat Jain 3515e705c121SKalle Valo /* 3516e705c121SKalle Valo * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have 3517e705c121SKalle Valo * changed, and now the revision step also includes bit 0-1 (no more 3518e705c121SKalle Valo * "dash" value). To keep hw_rev backwards compatible - we'll store it 3519e705c121SKalle Valo * in the old format. 3520e705c121SKalle Valo */ 35216e584873SSara Sharon if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_8000) { 3522e705c121SKalle Valo unsigned long flags; 3523e705c121SKalle Valo 3524e705c121SKalle Valo trans->hw_rev = (trans->hw_rev & 0xfff0) | 3525e705c121SKalle Valo (CSR_HW_REV_STEP(trans->hw_rev << 2) << 2); 3526e705c121SKalle Valo 3527e705c121SKalle Valo ret = iwl_pcie_prepare_card_hw(trans); 3528e705c121SKalle Valo if (ret) { 3529e705c121SKalle Valo IWL_WARN(trans, "Exit HW not ready\n"); 35305a41a86cSSharon Dvir goto out_no_pci; 3531e705c121SKalle Valo } 3532e705c121SKalle Valo 3533e705c121SKalle Valo /* 3534e705c121SKalle Valo * in-order to recognize C step driver should read chip version 3535e705c121SKalle Valo * id located at the AUX bus MISC address space. 3536e705c121SKalle Valo */ 3537c96b5eecSJohannes Berg ret = iwl_finish_nic_init(trans); 3538c96b5eecSJohannes Berg if (ret) 35395a41a86cSSharon Dvir goto out_no_pci; 3540e705c121SKalle Valo 354123ba9340SEmmanuel Grumbach if (iwl_trans_grab_nic_access(trans, &flags)) { 3542e705c121SKalle Valo u32 hw_step; 3543e705c121SKalle Valo 3544ea695b7cSShaul Triebitz hw_step = iwl_read_umac_prph_no_grab(trans, 3545ea695b7cSShaul Triebitz WFPM_CTRL_REG); 3546e705c121SKalle Valo hw_step |= ENABLE_WFPM; 3547ea695b7cSShaul Triebitz iwl_write_umac_prph_no_grab(trans, WFPM_CTRL_REG, 3548ea695b7cSShaul Triebitz hw_step); 3549cc5470dfSShahar S Matityahu hw_step = iwl_read_prph_no_grab(trans, 3550cc5470dfSShahar S Matityahu CNVI_AUX_MISC_CHIP); 3551e705c121SKalle Valo hw_step = (hw_step >> HW_STEP_LOCATION_BITS) & 0xF; 3552e705c121SKalle Valo if (hw_step == 0x3) 3553e705c121SKalle Valo trans->hw_rev = (trans->hw_rev & 0xFFFFFFF3) | 3554e705c121SKalle Valo (SILICON_C_STEP << 2); 3555e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 3556e705c121SKalle Valo } 3557e705c121SKalle Valo } 3558e705c121SKalle Valo 355999be6166SLuca Coelho IWL_DEBUG_INFO(trans, "HW REV: 0x%0x\n", trans->hw_rev); 356099be6166SLuca Coelho 3561f6586b69STzipi Peres #if IS_ENABLED(CONFIG_IWLMVM) 35621afb0ae4SHaim Dreyfuss trans->hw_rf_id = iwl_read32(trans, CSR_HW_RF_ID); 356333708052SLuca Coelho 3564ff911dcaSShaul Triebitz if (cfg == &iwlax210_2ax_cfg_so_hr_a0) { 3565ff911dcaSShaul Triebitz if (trans->hw_rev == CSR_HW_REV_TYPE_TY) { 3566ff911dcaSShaul Triebitz trans->cfg = &iwlax210_2ax_cfg_ty_gf_a0; 3567ff911dcaSShaul Triebitz } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 3568ff911dcaSShaul Triebitz CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_JF)) { 3569ff911dcaSShaul Triebitz trans->cfg = &iwlax210_2ax_cfg_so_jf_a0; 3570ff911dcaSShaul Triebitz } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 3571ff911dcaSShaul Triebitz CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_GF)) { 3572ff911dcaSShaul Triebitz trans->cfg = &iwlax210_2ax_cfg_so_gf_a0; 35735bd757a6SShaul Triebitz } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 35745bd757a6SShaul Triebitz CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_GF4)) { 35755bd757a6SShaul Triebitz trans->cfg = &iwlax210_2ax_cfg_so_gf4_a0; 3576ff911dcaSShaul Triebitz } 3577085486deSIhab Zhaika } else if (cfg == &iwl_ax101_cfg_qu_hr) { 357833708052SLuca Coelho if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 3579debec2f2SLuca Coelho CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_HR) && 3580debec2f2SLuca Coelho trans->hw_rev == CSR_HW_REV_TYPE_QNJ_B0) { 3581debec2f2SLuca Coelho trans->cfg = &iwl22000_2ax_cfg_qnj_hr_b0; 3582debec2f2SLuca Coelho } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 358333708052SLuca Coelho CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_HR)) { 3584085486deSIhab Zhaika trans->cfg = &iwl_ax101_cfg_qu_hr; 3585b1bbc1a6SLuca Coelho } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 3586b1bbc1a6SLuca Coelho CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_JF)) { 3587b1bbc1a6SLuca Coelho trans->cfg = &iwl22000_2ax_cfg_jf; 3588b1bbc1a6SLuca Coelho } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 3589b1bbc1a6SLuca Coelho CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_HRCDB)) { 3590b1bbc1a6SLuca Coelho IWL_ERR(trans, "RF ID HRCDB is not supported\n"); 3591b1bbc1a6SLuca Coelho ret = -EINVAL; 3592b1bbc1a6SLuca Coelho goto out_no_pci; 3593b1bbc1a6SLuca Coelho } else { 3594b1bbc1a6SLuca Coelho IWL_ERR(trans, "Unrecognized RF ID 0x%08x\n", 3595b1bbc1a6SLuca Coelho CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id)); 3596b1bbc1a6SLuca Coelho ret = -EINVAL; 3597b1bbc1a6SLuca Coelho goto out_no_pci; 3598b1bbc1a6SLuca Coelho } 3599b1bbc1a6SLuca Coelho } else if (CSR_HW_RF_ID_TYPE_CHIP_ID(trans->hw_rf_id) == 36008093bb6dSLuca Coelho CSR_HW_RF_ID_TYPE_CHIP_ID(CSR_HW_RF_ID_TYPE_HR) && 36010d5bad14SIhab Zhaika (trans->cfg != &iwl_ax200_cfg_cc || 360299be6166SLuca Coelho trans->hw_rev == CSR_HW_REV_TYPE_QNJ_B0)) { 3603f6586b69STzipi Peres u32 hw_status; 3604f6586b69STzipi Peres 3605f6586b69STzipi Peres hw_status = iwl_read_prph(trans, UMAG_GEN_HW_STATUS); 360633708052SLuca Coelho if (CSR_HW_RF_STEP(trans->hw_rf_id) == SILICON_B_STEP) 360733708052SLuca Coelho /* 360833708052SLuca Coelho * b step fw is the same for physical card and fpga 360933708052SLuca Coelho */ 361033708052SLuca Coelho trans->cfg = &iwl22000_2ax_cfg_qnj_hr_b0; 361133708052SLuca Coelho else if ((hw_status & UMAG_GEN_HW_IS_FPGA) && 361233708052SLuca Coelho CSR_HW_RF_STEP(trans->hw_rf_id) == SILICON_A_STEP) { 361333708052SLuca Coelho trans->cfg = &iwl22000_2ax_cfg_qnj_hr_a0_f0; 361433708052SLuca Coelho } else { 361533708052SLuca Coelho /* 361633708052SLuca Coelho * a step no FPGA 361733708052SLuca Coelho */ 36182f7a3863SLuca Coelho trans->cfg = &iwl22000_2ac_cfg_hr; 3619f6586b69STzipi Peres } 362033708052SLuca Coelho } 3621f6586b69STzipi Peres #endif 36221afb0ae4SHaim Dreyfuss 36232e5d4a8fSHaim Dreyfuss iwl_pcie_set_interrupt_capa(pdev, trans); 3624e705c121SKalle Valo trans->hw_id = (pdev->device << 16) + pdev->subsystem_device; 3625e705c121SKalle Valo snprintf(trans->hw_id_str, sizeof(trans->hw_id_str), 3626e705c121SKalle Valo "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device); 3627e705c121SKalle Valo 3628e705c121SKalle Valo /* Initialize the wait queue for commands */ 3629e705c121SKalle Valo init_waitqueue_head(&trans_pcie->wait_command_queue); 3630e705c121SKalle Valo 36314cbb8e50SLuciano Coelho init_waitqueue_head(&trans_pcie->d0i3_waitq); 36324cbb8e50SLuciano Coelho 36332e5d4a8fSHaim Dreyfuss if (trans_pcie->msix_enabled) { 36342388bd7bSDan Carpenter ret = iwl_pcie_init_msix_handler(pdev, trans_pcie); 36352388bd7bSDan Carpenter if (ret) 36365a41a86cSSharon Dvir goto out_no_pci; 36372e5d4a8fSHaim Dreyfuss } else { 3638e705c121SKalle Valo ret = iwl_pcie_alloc_ict(trans); 3639e705c121SKalle Valo if (ret) 36405a41a86cSSharon Dvir goto out_no_pci; 3641e705c121SKalle Valo 36425a41a86cSSharon Dvir ret = devm_request_threaded_irq(&pdev->dev, pdev->irq, 36435a41a86cSSharon Dvir iwl_pcie_isr, 3644e705c121SKalle Valo iwl_pcie_irq_handler, 3645e705c121SKalle Valo IRQF_SHARED, DRV_NAME, trans); 3646e705c121SKalle Valo if (ret) { 3647e705c121SKalle Valo IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq); 3648e705c121SKalle Valo goto out_free_ict; 3649e705c121SKalle Valo } 3650e705c121SKalle Valo trans_pcie->inta_mask = CSR_INI_SET_MASK; 36512e5d4a8fSHaim Dreyfuss } 3652e705c121SKalle Valo 365310a54d81SLuca Coelho trans_pcie->rba.alloc_wq = alloc_workqueue("rb_allocator", 365410a54d81SLuca Coelho WQ_HIGHPRI | WQ_UNBOUND, 1); 365510a54d81SLuca Coelho INIT_WORK(&trans_pcie->rba.rx_alloc, iwl_pcie_rx_allocator_work); 365610a54d81SLuca Coelho 3657b3ff1270SLuca Coelho #ifdef CONFIG_IWLWIFI_PCIE_RTPM 3658b3ff1270SLuca Coelho trans->runtime_pm_mode = IWL_PLAT_PM_MODE_D0I3; 3659b3ff1270SLuca Coelho #else 3660b3ff1270SLuca Coelho trans->runtime_pm_mode = IWL_PLAT_PM_MODE_DISABLED; 3661b3ff1270SLuca Coelho #endif /* CONFIG_IWLWIFI_PCIE_RTPM */ 3662b3ff1270SLuca Coelho 3663f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS 3664f7805b33SLior Cohen trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_CLOSED; 3665f7805b33SLior Cohen mutex_init(&trans_pcie->fw_mon_data.mutex); 3666f7805b33SLior Cohen #endif 3667f7805b33SLior Cohen 3668e705c121SKalle Valo return trans; 3669e705c121SKalle Valo 3670e705c121SKalle Valo out_free_ict: 3671e705c121SKalle Valo iwl_pcie_free_ict(trans); 3672e705c121SKalle Valo out_no_pci: 36736eb5e529SEmmanuel Grumbach free_percpu(trans_pcie->tso_hdr_page); 3674e705c121SKalle Valo iwl_trans_free(trans); 3675e705c121SKalle Valo return ERR_PTR(ret); 3676e705c121SKalle Valo } 3677b8a7547dSShahar S Matityahu 3678d1967ce6SShahar S Matityahu void iwl_trans_pcie_sync_nmi(struct iwl_trans *trans) 3679b8a7547dSShahar S Matityahu { 36801c6bca6dSShahar S Matityahu struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 3681b8a7547dSShahar S Matityahu unsigned long timeout = jiffies + IWL_TRANS_NMI_TIMEOUT; 36821c6bca6dSShahar S Matityahu u32 inta_addr, sw_err_bit; 36831c6bca6dSShahar S Matityahu 36841c6bca6dSShahar S Matityahu if (trans_pcie->msix_enabled) { 36851c6bca6dSShahar S Matityahu inta_addr = CSR_MSIX_HW_INT_CAUSES_AD; 36861c6bca6dSShahar S Matityahu sw_err_bit = MSIX_HW_INT_CAUSES_REG_SW_ERR; 36871c6bca6dSShahar S Matityahu } else { 36881c6bca6dSShahar S Matityahu inta_addr = CSR_INT; 36891c6bca6dSShahar S Matityahu sw_err_bit = CSR_INT_BIT_SW_ERR; 36901c6bca6dSShahar S Matityahu } 3691b8a7547dSShahar S Matityahu 3692b8a7547dSShahar S Matityahu iwl_disable_interrupts(trans); 3693b8a7547dSShahar S Matityahu iwl_force_nmi(trans); 3694b8a7547dSShahar S Matityahu while (time_after(timeout, jiffies)) { 36951c6bca6dSShahar S Matityahu u32 inta_hw = iwl_read32(trans, inta_addr); 3696b8a7547dSShahar S Matityahu 3697b8a7547dSShahar S Matityahu /* Error detected by uCode */ 36981c6bca6dSShahar S Matityahu if (inta_hw & sw_err_bit) { 3699b8a7547dSShahar S Matityahu /* Clear causes register */ 37001c6bca6dSShahar S Matityahu iwl_write32(trans, inta_addr, inta_hw & sw_err_bit); 3701b8a7547dSShahar S Matityahu break; 3702b8a7547dSShahar S Matityahu } 3703b8a7547dSShahar S Matityahu 3704b8a7547dSShahar S Matityahu mdelay(1); 3705b8a7547dSShahar S Matityahu } 3706b8a7547dSShahar S Matityahu iwl_enable_interrupts(trans); 3707b8a7547dSShahar S Matityahu iwl_trans_fw_error(trans); 3708b8a7547dSShahar S Matityahu } 3709