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/interrupt.h> 66e705c121SKalle Valo #include <linux/debugfs.h> 67e705c121SKalle Valo #include <linux/sched.h> 68e705c121SKalle Valo #include <linux/bitops.h> 69e705c121SKalle Valo #include <linux/gfp.h> 70e705c121SKalle Valo #include <linux/vmalloc.h> 7149564a80SLuca Coelho #include <linux/module.h> 72f7805b33SLior Cohen #include <linux/wait.h> 73e705c121SKalle Valo 74e705c121SKalle Valo #include "iwl-drv.h" 75e705c121SKalle Valo #include "iwl-trans.h" 76e705c121SKalle Valo #include "iwl-csr.h" 77e705c121SKalle Valo #include "iwl-prph.h" 78e705c121SKalle Valo #include "iwl-scd.h" 79e705c121SKalle Valo #include "iwl-agn-hw.h" 80d962f9b1SJohannes Berg #include "fw/error-dump.h" 81520f03eaSShahar S Matityahu #include "fw/dbg.h" 82e705c121SKalle Valo #include "internal.h" 83e705c121SKalle Valo #include "iwl-fh.h" 84e705c121SKalle Valo 85e705c121SKalle Valo /* extended range in FW SRAM */ 86e705c121SKalle Valo #define IWL_FW_MEM_EXTENDED_START 0x40000 87e705c121SKalle Valo #define IWL_FW_MEM_EXTENDED_END 0x57FFF 88e705c121SKalle Valo 894290eaadSJohannes Berg void iwl_trans_pcie_dump_regs(struct iwl_trans *trans) 90a6d24fadSRajat Jain { 91c4d3f2eeSLuca Coelho #define PCI_DUMP_SIZE 352 92c4d3f2eeSLuca Coelho #define PCI_MEM_DUMP_SIZE 64 93c4d3f2eeSLuca Coelho #define PCI_PARENT_DUMP_SIZE 524 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); 105c4d3f2eeSLuca Coelho BUILD_BUG_ON(PCI_MEM_DUMP_SIZE > 4096 || PCI_MEM_DUMP_SIZE & 0x3); 106c4d3f2eeSLuca Coelho BUILD_BUG_ON(PCI_PARENT_DUMP_SIZE > 4096 || PCI_PARENT_DUMP_SIZE & 0x3); 107c4d3f2eeSLuca Coelho 108a6d24fadSRajat Jain /* Alloc a max size buffer */ 109a6d24fadSRajat Jain alloc_size = PCI_ERR_ROOT_ERR_SRC + 4 + PREFIX_LEN; 110c4d3f2eeSLuca Coelho alloc_size = max_t(u32, alloc_size, PCI_DUMP_SIZE + PREFIX_LEN); 111c4d3f2eeSLuca Coelho alloc_size = max_t(u32, alloc_size, PCI_MEM_DUMP_SIZE + PREFIX_LEN); 112c4d3f2eeSLuca Coelho alloc_size = max_t(u32, alloc_size, PCI_PARENT_DUMP_SIZE + PREFIX_LEN); 113c4d3f2eeSLuca Coelho 114a6d24fadSRajat Jain buf = kmalloc(alloc_size, GFP_ATOMIC); 115a6d24fadSRajat Jain if (!buf) 116a6d24fadSRajat Jain return; 117a6d24fadSRajat Jain prefix = (char *)buf + alloc_size - PREFIX_LEN; 118a6d24fadSRajat Jain 119a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi transaction failed, dumping registers\n"); 120a6d24fadSRajat Jain 121a6d24fadSRajat Jain /* Print wifi device registers */ 122a6d24fadSRajat Jain sprintf(prefix, "iwlwifi %s: ", pci_name(pdev)); 123a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi device config registers:\n"); 124a6d24fadSRajat Jain for (i = 0, ptr = buf; i < PCI_DUMP_SIZE; i += 4, ptr++) 125a6d24fadSRajat Jain if (pci_read_config_dword(pdev, i, ptr)) 126a6d24fadSRajat Jain goto err_read; 127a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 128a6d24fadSRajat Jain 129a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi device memory mapped registers:\n"); 130c4d3f2eeSLuca Coelho for (i = 0, ptr = buf; i < PCI_MEM_DUMP_SIZE; i += 4, ptr++) 131a6d24fadSRajat Jain *ptr = iwl_read32(trans, i); 132a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 133a6d24fadSRajat Jain 134a6d24fadSRajat Jain pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); 135a6d24fadSRajat Jain if (pos) { 136a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi device AER capability structure:\n"); 137a6d24fadSRajat Jain for (i = 0, ptr = buf; i < PCI_ERR_ROOT_COMMAND; i += 4, ptr++) 138a6d24fadSRajat Jain if (pci_read_config_dword(pdev, pos + i, ptr)) 139a6d24fadSRajat Jain goto err_read; 140a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 141a6d24fadSRajat Jain 32, 4, buf, i, 0); 142a6d24fadSRajat Jain } 143a6d24fadSRajat Jain 144a6d24fadSRajat Jain /* Print parent device registers next */ 145a6d24fadSRajat Jain if (!pdev->bus->self) 146a6d24fadSRajat Jain goto out; 147a6d24fadSRajat Jain 148a6d24fadSRajat Jain pdev = pdev->bus->self; 149a6d24fadSRajat Jain sprintf(prefix, "iwlwifi %s: ", pci_name(pdev)); 150a6d24fadSRajat Jain 151a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi parent port (%s) config registers:\n", 152a6d24fadSRajat Jain pci_name(pdev)); 153c4d3f2eeSLuca Coelho for (i = 0, ptr = buf; i < PCI_PARENT_DUMP_SIZE; i += 4, ptr++) 154a6d24fadSRajat Jain if (pci_read_config_dword(pdev, i, ptr)) 155a6d24fadSRajat Jain goto err_read; 156a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 157a6d24fadSRajat Jain 158a6d24fadSRajat Jain /* Print root port AER registers */ 159a6d24fadSRajat Jain pos = 0; 160a6d24fadSRajat Jain pdev = pcie_find_root_port(pdev); 161a6d24fadSRajat Jain if (pdev) 162a6d24fadSRajat Jain pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); 163a6d24fadSRajat Jain if (pos) { 164a6d24fadSRajat Jain IWL_ERR(trans, "iwlwifi root port (%s) AER cap structure:\n", 165a6d24fadSRajat Jain pci_name(pdev)); 166a6d24fadSRajat Jain sprintf(prefix, "iwlwifi %s: ", pci_name(pdev)); 167a6d24fadSRajat Jain for (i = 0, ptr = buf; i <= PCI_ERR_ROOT_ERR_SRC; i += 4, ptr++) 168a6d24fadSRajat Jain if (pci_read_config_dword(pdev, pos + i, ptr)) 169a6d24fadSRajat Jain goto err_read; 170a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 171a6d24fadSRajat Jain 4, buf, i, 0); 172a6d24fadSRajat Jain } 173f3402d6dSSara Sharon goto out; 174a6d24fadSRajat Jain 175a6d24fadSRajat Jain err_read: 176a6d24fadSRajat Jain print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 32, 4, buf, i, 0); 177a6d24fadSRajat Jain IWL_ERR(trans, "Read failed at 0x%X\n", i); 178a6d24fadSRajat Jain out: 179a6d24fadSRajat Jain trans_pcie->pcie_dbg_dumped_once = 1; 180a6d24fadSRajat Jain kfree(buf); 181a6d24fadSRajat Jain } 182a6d24fadSRajat Jain 183870c2a11SGolan Ben Ami static void iwl_trans_pcie_sw_reset(struct iwl_trans *trans) 184870c2a11SGolan Ben Ami { 185870c2a11SGolan Ben Ami /* Reset entire device - do controller reset (results in SHRD_HW_RST) */ 186286ca8ebSLuca Coelho iwl_set_bit(trans, trans->trans_cfg->csr->addr_sw_reset, 187286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_sw_reset)); 188870c2a11SGolan Ben Ami usleep_range(5000, 6000); 189870c2a11SGolan Ben Ami } 190870c2a11SGolan Ben Ami 191e705c121SKalle Valo static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans) 192e705c121SKalle Valo { 19369f0e505SShahar S Matityahu struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon; 194e705c121SKalle Valo 19569f0e505SShahar S Matityahu if (!fw_mon->size) 19669f0e505SShahar S Matityahu return; 19769f0e505SShahar S Matityahu 19869f0e505SShahar S Matityahu dma_free_coherent(trans->dev, fw_mon->size, fw_mon->block, 19969f0e505SShahar S Matityahu fw_mon->physical); 20069f0e505SShahar S Matityahu 20169f0e505SShahar S Matityahu fw_mon->block = NULL; 20269f0e505SShahar S Matityahu fw_mon->physical = 0; 20369f0e505SShahar S Matityahu fw_mon->size = 0; 204e705c121SKalle Valo } 205e705c121SKalle Valo 20688964b2eSSara Sharon static void iwl_pcie_alloc_fw_monitor_block(struct iwl_trans *trans, 20788964b2eSSara Sharon u8 max_power, u8 min_power) 208e705c121SKalle Valo { 20969f0e505SShahar S Matityahu struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon; 21069f0e505SShahar S Matityahu void *block = NULL; 21169f0e505SShahar S Matityahu dma_addr_t physical = 0; 212e705c121SKalle Valo u32 size = 0; 213e705c121SKalle Valo u8 power; 214e705c121SKalle Valo 21569f0e505SShahar S Matityahu if (fw_mon->size) 21669f0e505SShahar S Matityahu return; 21769f0e505SShahar S Matityahu 21888964b2eSSara Sharon for (power = max_power; power >= min_power; power--) { 219e705c121SKalle Valo size = BIT(power); 22069f0e505SShahar S Matityahu block = dma_alloc_coherent(trans->dev, size, &physical, 2212d46f7afSChristoph Hellwig GFP_KERNEL | __GFP_NOWARN); 22269f0e505SShahar S Matityahu if (!block) 223e705c121SKalle Valo continue; 224e705c121SKalle Valo 225e705c121SKalle Valo IWL_INFO(trans, 226c5f97542SShahar S Matityahu "Allocated 0x%08x bytes for firmware monitor.\n", 227c5f97542SShahar S Matityahu size); 228e705c121SKalle Valo break; 229e705c121SKalle Valo } 230e705c121SKalle Valo 23169f0e505SShahar S Matityahu if (WARN_ON_ONCE(!block)) 232e705c121SKalle Valo return; 233e705c121SKalle Valo 234e705c121SKalle Valo if (power != max_power) 235e705c121SKalle Valo IWL_ERR(trans, 236e705c121SKalle Valo "Sorry - debug buffer is only %luK while you requested %luK\n", 237e705c121SKalle Valo (unsigned long)BIT(power - 10), 238e705c121SKalle Valo (unsigned long)BIT(max_power - 10)); 239e705c121SKalle Valo 24069f0e505SShahar S Matityahu fw_mon->block = block; 24169f0e505SShahar S Matityahu fw_mon->physical = physical; 24269f0e505SShahar S Matityahu fw_mon->size = size; 24388964b2eSSara Sharon } 24488964b2eSSara Sharon 24588964b2eSSara Sharon void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power) 24688964b2eSSara Sharon { 24788964b2eSSara Sharon if (!max_power) { 24888964b2eSSara Sharon /* default max_power is maximum */ 24988964b2eSSara Sharon max_power = 26; 25088964b2eSSara Sharon } else { 25188964b2eSSara Sharon max_power += 11; 25288964b2eSSara Sharon } 25388964b2eSSara Sharon 25488964b2eSSara Sharon if (WARN(max_power > 26, 25588964b2eSSara Sharon "External buffer size for monitor is too big %d, check the FW TLV\n", 25688964b2eSSara Sharon max_power)) 25788964b2eSSara Sharon return; 25888964b2eSSara Sharon 25969f0e505SShahar S Matityahu if (trans->dbg.fw_mon.size) 26088964b2eSSara Sharon return; 26188964b2eSSara Sharon 26288964b2eSSara Sharon iwl_pcie_alloc_fw_monitor_block(trans, max_power, 11); 263e705c121SKalle Valo } 264e705c121SKalle Valo 265e705c121SKalle Valo static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg) 266e705c121SKalle Valo { 267e705c121SKalle Valo iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG, 268e705c121SKalle Valo ((reg & 0x0000ffff) | (2 << 28))); 269e705c121SKalle Valo return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG); 270e705c121SKalle Valo } 271e705c121SKalle Valo 272e705c121SKalle Valo static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val) 273e705c121SKalle Valo { 274e705c121SKalle Valo iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val); 275e705c121SKalle Valo iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG, 276e705c121SKalle Valo ((reg & 0x0000ffff) | (3 << 28))); 277e705c121SKalle Valo } 278e705c121SKalle Valo 279e705c121SKalle Valo static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux) 280e705c121SKalle Valo { 281e705c121SKalle Valo if (trans->cfg->apmg_not_supported) 282e705c121SKalle Valo return; 283e705c121SKalle Valo 284e705c121SKalle Valo if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold)) 285e705c121SKalle Valo iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG, 286e705c121SKalle Valo APMG_PS_CTRL_VAL_PWR_SRC_VAUX, 287e705c121SKalle Valo ~APMG_PS_CTRL_MSK_PWR_SRC); 288e705c121SKalle Valo else 289e705c121SKalle Valo iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG, 290e705c121SKalle Valo APMG_PS_CTRL_VAL_PWR_SRC_VMAIN, 291e705c121SKalle Valo ~APMG_PS_CTRL_MSK_PWR_SRC); 292e705c121SKalle Valo } 293e705c121SKalle Valo 294e705c121SKalle Valo /* PCI registers */ 295e705c121SKalle Valo #define PCI_CFG_RETRY_TIMEOUT 0x041 296e705c121SKalle Valo 297eda50cdeSSara Sharon void iwl_pcie_apm_config(struct iwl_trans *trans) 298e705c121SKalle Valo { 299e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 300e705c121SKalle Valo u16 lctl; 301e705c121SKalle Valo u16 cap; 302e705c121SKalle Valo 303e705c121SKalle Valo /* 304e705c121SKalle Valo * HW bug W/A for instability in PCIe bus L0S->L1 transition. 305e705c121SKalle Valo * Check if BIOS (or OS) enabled L1-ASPM on this device. 306e705c121SKalle Valo * If so (likely), disable L0S, so device moves directly L0->L1; 307e705c121SKalle Valo * costs negligible amount of power savings. 308e705c121SKalle Valo * If not (unlikely), enable L0S, so there is at least some 309e705c121SKalle Valo * power savings, even without L1. 310e705c121SKalle Valo */ 311e705c121SKalle Valo pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl); 312e705c121SKalle Valo if (lctl & PCI_EXP_LNKCTL_ASPM_L1) 313e705c121SKalle Valo iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED); 314e705c121SKalle Valo else 315e705c121SKalle Valo iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED); 316e705c121SKalle Valo trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S); 317e705c121SKalle Valo 318e705c121SKalle Valo pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap); 319e705c121SKalle Valo trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN; 320d74a61fcSLuca Coelho IWL_DEBUG_POWER(trans, "L1 %sabled - LTR %sabled\n", 321e705c121SKalle Valo (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis", 322e705c121SKalle Valo trans->ltr_enabled ? "En" : "Dis"); 323e705c121SKalle Valo } 324e705c121SKalle Valo 325e705c121SKalle Valo /* 326e705c121SKalle Valo * Start up NIC's basic functionality after it has been reset 327e705c121SKalle Valo * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop()) 328e705c121SKalle Valo * NOTE: This does not load uCode nor start the embedded processor 329e705c121SKalle Valo */ 330e705c121SKalle Valo static int iwl_pcie_apm_init(struct iwl_trans *trans) 331e705c121SKalle Valo { 33252b6e168SEmmanuel Grumbach int ret; 33352b6e168SEmmanuel Grumbach 334e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Init card's basic functions\n"); 335e705c121SKalle Valo 336e705c121SKalle Valo /* 337e705c121SKalle Valo * Use "set_bit" below rather than "write", to preserve any hardware 338e705c121SKalle Valo * bits already set by default after reset. 339e705c121SKalle Valo */ 340e705c121SKalle Valo 341e705c121SKalle Valo /* Disable L0S exit timer (platform NMI Work/Around) */ 342286ca8ebSLuca Coelho if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_8000) 343e705c121SKalle Valo iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS, 344e705c121SKalle Valo CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER); 345e705c121SKalle Valo 346e705c121SKalle Valo /* 347e705c121SKalle Valo * Disable L0s without affecting L1; 348e705c121SKalle Valo * don't wait for ICH L0s (ICH bug W/A) 349e705c121SKalle Valo */ 350e705c121SKalle Valo iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS, 351e705c121SKalle Valo CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX); 352e705c121SKalle Valo 353e705c121SKalle Valo /* Set FH wait threshold to maximum (HW error during stress W/A) */ 354e705c121SKalle Valo iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL); 355e705c121SKalle Valo 356e705c121SKalle Valo /* 357e705c121SKalle Valo * Enable HAP INTA (interrupt from management bus) to 358e705c121SKalle Valo * wake device's PCI Express link L1a -> L0s 359e705c121SKalle Valo */ 360e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 361e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A); 362e705c121SKalle Valo 363e705c121SKalle Valo iwl_pcie_apm_config(trans); 364e705c121SKalle Valo 365e705c121SKalle Valo /* Configure analog phase-lock-loop before activating to D0A */ 366286ca8ebSLuca Coelho if (trans->trans_cfg->base_params->pll_cfg) 36777d76931SJohannes Berg iwl_set_bit(trans, CSR_ANA_PLL_CFG, CSR50_ANA_PLL_CFG_VAL); 368e705c121SKalle Valo 3697d34a7d7SLuca Coelho ret = iwl_finish_nic_init(trans, trans->trans_cfg); 370c96b5eecSJohannes Berg if (ret) 37152b6e168SEmmanuel Grumbach return ret; 372e705c121SKalle Valo 373e705c121SKalle Valo if (trans->cfg->host_interrupt_operation_mode) { 374e705c121SKalle Valo /* 375e705c121SKalle Valo * This is a bit of an abuse - This is needed for 7260 / 3160 376e705c121SKalle Valo * only check host_interrupt_operation_mode even if this is 377e705c121SKalle Valo * not related to host_interrupt_operation_mode. 378e705c121SKalle Valo * 379e705c121SKalle Valo * Enable the oscillator to count wake up time for L1 exit. This 380e705c121SKalle Valo * consumes slightly more power (100uA) - but allows to be sure 381e705c121SKalle Valo * that we wake up from L1 on time. 382e705c121SKalle Valo * 383e705c121SKalle Valo * This looks weird: read twice the same register, discard the 384e705c121SKalle Valo * value, set a bit, and yet again, read that same register 385e705c121SKalle Valo * just to discard the value. But that's the way the hardware 386e705c121SKalle Valo * seems to like it. 387e705c121SKalle Valo */ 388e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 389e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 390e705c121SKalle Valo iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL); 391e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 392e705c121SKalle Valo iwl_read_prph(trans, OSC_CLK); 393e705c121SKalle Valo } 394e705c121SKalle Valo 395e705c121SKalle Valo /* 396e705c121SKalle Valo * Enable DMA clock and wait for it to stabilize. 397e705c121SKalle Valo * 398e705c121SKalle Valo * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" 399e705c121SKalle Valo * bits do not disable clocks. This preserves any hardware 400e705c121SKalle Valo * bits already set by default in "CLK_CTRL_REG" after reset. 401e705c121SKalle Valo */ 402e705c121SKalle Valo if (!trans->cfg->apmg_not_supported) { 403e705c121SKalle Valo iwl_write_prph(trans, APMG_CLK_EN_REG, 404e705c121SKalle Valo APMG_CLK_VAL_DMA_CLK_RQT); 405e705c121SKalle Valo udelay(20); 406e705c121SKalle Valo 407e705c121SKalle Valo /* Disable L1-Active */ 408e705c121SKalle Valo iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG, 409e705c121SKalle Valo APMG_PCIDEV_STT_VAL_L1_ACT_DIS); 410e705c121SKalle Valo 411e705c121SKalle Valo /* Clear the interrupt in APMG if the NIC is in RFKILL */ 412e705c121SKalle Valo iwl_write_prph(trans, APMG_RTC_INT_STT_REG, 413e705c121SKalle Valo APMG_RTC_INT_STT_RFKILL); 414e705c121SKalle Valo } 415e705c121SKalle Valo 416e705c121SKalle Valo set_bit(STATUS_DEVICE_ENABLED, &trans->status); 417e705c121SKalle Valo 41852b6e168SEmmanuel Grumbach return 0; 419e705c121SKalle Valo } 420e705c121SKalle Valo 421e705c121SKalle Valo /* 422e705c121SKalle Valo * Enable LP XTAL to avoid HW bug where device may consume much power if 423e705c121SKalle Valo * FW is not loaded after device reset. LP XTAL is disabled by default 424e705c121SKalle Valo * after device HW reset. Do it only if XTAL is fed by internal source. 425e705c121SKalle Valo * Configure device's "persistence" mode to avoid resetting XTAL again when 426e705c121SKalle Valo * SHRD_HW_RST occurs in S3. 427e705c121SKalle Valo */ 428e705c121SKalle Valo static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans) 429e705c121SKalle Valo { 430e705c121SKalle Valo int ret; 431e705c121SKalle Valo u32 apmg_gp1_reg; 432e705c121SKalle Valo u32 apmg_xtal_cfg_reg; 433e705c121SKalle Valo u32 dl_cfg_reg; 434e705c121SKalle Valo 435e705c121SKalle Valo /* Force XTAL ON */ 436e705c121SKalle Valo __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL, 437e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_XTAL_ON); 438e705c121SKalle Valo 439870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 440e705c121SKalle Valo 4417d34a7d7SLuca Coelho ret = iwl_finish_nic_init(trans, trans->trans_cfg); 442c96b5eecSJohannes Berg if (WARN_ON(ret)) { 443e705c121SKalle Valo /* Release XTAL ON request */ 444e705c121SKalle Valo __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL, 445e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_XTAL_ON); 446e705c121SKalle Valo return; 447e705c121SKalle Valo } 448e705c121SKalle Valo 449e705c121SKalle Valo /* 450e705c121SKalle Valo * Clear "disable persistence" to avoid LP XTAL resetting when 451e705c121SKalle Valo * SHRD_HW_RST is applied in S3. 452e705c121SKalle Valo */ 453e705c121SKalle Valo iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG, 454e705c121SKalle Valo APMG_PCIDEV_STT_VAL_PERSIST_DIS); 455e705c121SKalle Valo 456e705c121SKalle Valo /* 457e705c121SKalle Valo * Force APMG XTAL to be active to prevent its disabling by HW 458e705c121SKalle Valo * caused by APMG idle state. 459e705c121SKalle Valo */ 460e705c121SKalle Valo apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans, 461e705c121SKalle Valo SHR_APMG_XTAL_CFG_REG); 462e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG, 463e705c121SKalle Valo apmg_xtal_cfg_reg | 464e705c121SKalle Valo SHR_APMG_XTAL_CFG_XTAL_ON_REQ); 465e705c121SKalle Valo 466870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 467e705c121SKalle Valo 468e705c121SKalle Valo /* Enable LP XTAL by indirect access through CSR */ 469e705c121SKalle Valo apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG); 470e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg | 471e705c121SKalle Valo SHR_APMG_GP1_WF_XTAL_LP_EN | 472e705c121SKalle Valo SHR_APMG_GP1_CHICKEN_BIT_SELECT); 473e705c121SKalle Valo 474e705c121SKalle Valo /* Clear delay line clock power up */ 475e705c121SKalle Valo dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG); 476e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg & 477e705c121SKalle Valo ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP); 478e705c121SKalle Valo 479e705c121SKalle Valo /* 480e705c121SKalle Valo * Enable persistence mode to avoid LP XTAL resetting when 481e705c121SKalle Valo * SHRD_HW_RST is applied in S3. 482e705c121SKalle Valo */ 483e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 484e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PERSIST_MODE); 485e705c121SKalle Valo 486e705c121SKalle Valo /* 487e705c121SKalle Valo * Clear "initialization complete" bit to move adapter from 488e705c121SKalle Valo * D0A* (powered-up Active) --> D0U* (Uninitialized) state. 489e705c121SKalle Valo */ 490e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 491286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_init_done)); 492e705c121SKalle Valo 493e705c121SKalle Valo /* Activates XTAL resources monitor */ 494e705c121SKalle Valo __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG, 495e705c121SKalle Valo CSR_MONITOR_XTAL_RESOURCES); 496e705c121SKalle Valo 497e705c121SKalle Valo /* Release XTAL ON request */ 498e705c121SKalle Valo __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL, 499e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_XTAL_ON); 500e705c121SKalle Valo udelay(10); 501e705c121SKalle Valo 502e705c121SKalle Valo /* Release APMG XTAL */ 503e705c121SKalle Valo iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG, 504e705c121SKalle Valo apmg_xtal_cfg_reg & 505e705c121SKalle Valo ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ); 506e705c121SKalle Valo } 507e705c121SKalle Valo 508e8c8935eSJohannes Berg void iwl_pcie_apm_stop_master(struct iwl_trans *trans) 509e705c121SKalle Valo { 510e8c8935eSJohannes Berg int ret; 511e705c121SKalle Valo 512e705c121SKalle Valo /* stop device's busmaster DMA activity */ 513286ca8ebSLuca Coelho iwl_set_bit(trans, trans->trans_cfg->csr->addr_sw_reset, 514286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_stop_master)); 515e705c121SKalle Valo 516286ca8ebSLuca Coelho ret = iwl_poll_bit(trans, trans->trans_cfg->csr->addr_sw_reset, 517286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_master_dis), 518286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_master_dis), 100); 519e705c121SKalle Valo if (ret < 0) 520e705c121SKalle Valo IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n"); 521e705c121SKalle Valo 522e705c121SKalle Valo IWL_DEBUG_INFO(trans, "stop master\n"); 523e705c121SKalle Valo } 524e705c121SKalle Valo 525e705c121SKalle Valo static void iwl_pcie_apm_stop(struct iwl_trans *trans, bool op_mode_leave) 526e705c121SKalle Valo { 527e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n"); 528e705c121SKalle Valo 529e705c121SKalle Valo if (op_mode_leave) { 530e705c121SKalle Valo if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 531e705c121SKalle Valo iwl_pcie_apm_init(trans); 532e705c121SKalle Valo 533e705c121SKalle Valo /* inform ME that we are leaving */ 534286ca8ebSLuca Coelho if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_7000) 535e705c121SKalle Valo iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG, 536e705c121SKalle Valo APMG_PCIDEV_STT_VAL_WAKE_ME); 537286ca8ebSLuca Coelho else if (trans->trans_cfg->device_family >= 53879b6c8feSLuca Coelho IWL_DEVICE_FAMILY_8000) { 539e705c121SKalle Valo iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 540e705c121SKalle Valo CSR_RESET_LINK_PWR_MGMT_DISABLED); 541e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 542e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PREPARE | 543e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_ENABLE_PME); 544e705c121SKalle Valo mdelay(1); 545e705c121SKalle Valo iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 546e705c121SKalle Valo CSR_RESET_LINK_PWR_MGMT_DISABLED); 547e705c121SKalle Valo } 548e705c121SKalle Valo mdelay(5); 549e705c121SKalle Valo } 550e705c121SKalle Valo 551e705c121SKalle Valo clear_bit(STATUS_DEVICE_ENABLED, &trans->status); 552e705c121SKalle Valo 553e705c121SKalle Valo /* Stop device's DMA activity */ 554e705c121SKalle Valo iwl_pcie_apm_stop_master(trans); 555e705c121SKalle Valo 556e705c121SKalle Valo if (trans->cfg->lp_xtal_workaround) { 557e705c121SKalle Valo iwl_pcie_apm_lp_xtal_enable(trans); 558e705c121SKalle Valo return; 559e705c121SKalle Valo } 560e705c121SKalle Valo 561870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 562e705c121SKalle Valo 563e705c121SKalle Valo /* 564e705c121SKalle Valo * Clear "initialization complete" bit to move adapter from 565e705c121SKalle Valo * D0A* (powered-up Active) --> D0U* (Uninitialized) state. 566e705c121SKalle Valo */ 567e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 568286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_init_done)); 569e705c121SKalle Valo } 570e705c121SKalle Valo 571e705c121SKalle Valo static int iwl_pcie_nic_init(struct iwl_trans *trans) 572e705c121SKalle Valo { 573e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 57452b6e168SEmmanuel Grumbach int ret; 575e705c121SKalle Valo 576e705c121SKalle Valo /* nic_init */ 577e705c121SKalle Valo spin_lock(&trans_pcie->irq_lock); 57852b6e168SEmmanuel Grumbach ret = iwl_pcie_apm_init(trans); 579e705c121SKalle Valo spin_unlock(&trans_pcie->irq_lock); 580e705c121SKalle Valo 58152b6e168SEmmanuel Grumbach if (ret) 58252b6e168SEmmanuel Grumbach return ret; 58352b6e168SEmmanuel Grumbach 584e705c121SKalle Valo iwl_pcie_set_pwr(trans, false); 585e705c121SKalle Valo 586e705c121SKalle Valo iwl_op_mode_nic_config(trans->op_mode); 587e705c121SKalle Valo 588e705c121SKalle Valo /* Allocate the RX queue, or reset if it is already allocated */ 589e705c121SKalle Valo iwl_pcie_rx_init(trans); 590e705c121SKalle Valo 591e705c121SKalle Valo /* Allocate or reset and init all Tx and Command queues */ 592e705c121SKalle Valo if (iwl_pcie_tx_init(trans)) 593e705c121SKalle Valo return -ENOMEM; 594e705c121SKalle Valo 595286ca8ebSLuca Coelho if (trans->trans_cfg->base_params->shadow_reg_enable) { 596e705c121SKalle Valo /* enable shadow regs in HW */ 597e705c121SKalle Valo iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF); 598e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n"); 599e705c121SKalle Valo } 600e705c121SKalle Valo 601e705c121SKalle Valo return 0; 602e705c121SKalle Valo } 603e705c121SKalle Valo 604e705c121SKalle Valo #define HW_READY_TIMEOUT (50) 605e705c121SKalle Valo 606e705c121SKalle Valo /* Note: returns poll_bit return value, which is >= 0 if success */ 607e705c121SKalle Valo static int iwl_pcie_set_hw_ready(struct iwl_trans *trans) 608e705c121SKalle Valo { 609e705c121SKalle Valo int ret; 610e705c121SKalle Valo 611e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 612e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_NIC_READY); 613e705c121SKalle Valo 614e705c121SKalle Valo /* See if we got it */ 615e705c121SKalle Valo ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG, 616e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, 617e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, 618e705c121SKalle Valo HW_READY_TIMEOUT); 619e705c121SKalle Valo 620e705c121SKalle Valo if (ret >= 0) 621e705c121SKalle Valo iwl_set_bit(trans, CSR_MBOX_SET_REG, CSR_MBOX_SET_REG_OS_ALIVE); 622e705c121SKalle Valo 623e705c121SKalle Valo IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : ""); 624e705c121SKalle Valo return ret; 625e705c121SKalle Valo } 626e705c121SKalle Valo 627e705c121SKalle Valo /* Note: returns standard 0/-ERROR code */ 628eda50cdeSSara Sharon int iwl_pcie_prepare_card_hw(struct iwl_trans *trans) 629e705c121SKalle Valo { 630e705c121SKalle Valo int ret; 631e705c121SKalle Valo int t = 0; 632e705c121SKalle Valo int iter; 633e705c121SKalle Valo 634e705c121SKalle Valo IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n"); 635e705c121SKalle Valo 636e705c121SKalle Valo ret = iwl_pcie_set_hw_ready(trans); 637e705c121SKalle Valo /* If the card is ready, exit 0 */ 638e705c121SKalle Valo if (ret >= 0) 639e705c121SKalle Valo return 0; 640e705c121SKalle Valo 641e705c121SKalle Valo iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 642e705c121SKalle Valo CSR_RESET_LINK_PWR_MGMT_DISABLED); 643192185d6SJohannes Berg usleep_range(1000, 2000); 644e705c121SKalle Valo 645e705c121SKalle Valo for (iter = 0; iter < 10; iter++) { 646e705c121SKalle Valo /* If HW is not ready, prepare the conditions to check again */ 647e705c121SKalle Valo iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 648e705c121SKalle Valo CSR_HW_IF_CONFIG_REG_PREPARE); 649e705c121SKalle Valo 650e705c121SKalle Valo do { 651e705c121SKalle Valo ret = iwl_pcie_set_hw_ready(trans); 652e705c121SKalle Valo if (ret >= 0) 653e705c121SKalle Valo return 0; 654e705c121SKalle Valo 655e705c121SKalle Valo usleep_range(200, 1000); 656e705c121SKalle Valo t += 200; 657e705c121SKalle Valo } while (t < 150000); 658e705c121SKalle Valo msleep(25); 659e705c121SKalle Valo } 660e705c121SKalle Valo 661e705c121SKalle Valo IWL_ERR(trans, "Couldn't prepare the card\n"); 662e705c121SKalle Valo 663e705c121SKalle Valo return ret; 664e705c121SKalle Valo } 665e705c121SKalle Valo 666e705c121SKalle Valo /* 667e705c121SKalle Valo * ucode 668e705c121SKalle Valo */ 669564cdce7SSara Sharon static void iwl_pcie_load_firmware_chunk_fh(struct iwl_trans *trans, 670564cdce7SSara Sharon u32 dst_addr, dma_addr_t phy_addr, 671564cdce7SSara Sharon u32 byte_cnt) 672e705c121SKalle Valo { 673bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL), 674e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE); 675e705c121SKalle Valo 676bac842daSEmmanuel Grumbach iwl_write32(trans, FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL), 677e705c121SKalle Valo dst_addr); 678e705c121SKalle Valo 679bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL), 680e705c121SKalle Valo phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK); 681e705c121SKalle Valo 682bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL), 683e705c121SKalle Valo (iwl_get_dma_hi_addr(phy_addr) 684e705c121SKalle Valo << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt); 685e705c121SKalle Valo 686bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL), 687bac842daSEmmanuel Grumbach BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM) | 688bac842daSEmmanuel Grumbach BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX) | 689e705c121SKalle Valo FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID); 690e705c121SKalle Valo 691bac842daSEmmanuel Grumbach iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL), 692e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE | 693e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE | 694e705c121SKalle Valo FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD); 695564cdce7SSara Sharon } 696e705c121SKalle Valo 697564cdce7SSara Sharon static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans, 698564cdce7SSara Sharon u32 dst_addr, dma_addr_t phy_addr, 699564cdce7SSara Sharon u32 byte_cnt) 700564cdce7SSara Sharon { 701564cdce7SSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 702564cdce7SSara Sharon unsigned long flags; 703564cdce7SSara Sharon int ret; 704564cdce7SSara Sharon 705564cdce7SSara Sharon trans_pcie->ucode_write_complete = false; 706564cdce7SSara Sharon 707564cdce7SSara Sharon if (!iwl_trans_grab_nic_access(trans, &flags)) 708564cdce7SSara Sharon return -EIO; 709564cdce7SSara Sharon 710564cdce7SSara Sharon iwl_pcie_load_firmware_chunk_fh(trans, dst_addr, phy_addr, 711564cdce7SSara Sharon byte_cnt); 712bac842daSEmmanuel Grumbach iwl_trans_release_nic_access(trans, &flags); 713bac842daSEmmanuel Grumbach 714e705c121SKalle Valo ret = wait_event_timeout(trans_pcie->ucode_write_waitq, 715e705c121SKalle Valo trans_pcie->ucode_write_complete, 5 * HZ); 716e705c121SKalle Valo if (!ret) { 717e705c121SKalle Valo IWL_ERR(trans, "Failed to load firmware chunk!\n"); 718fb12777aSKirtika Ruchandani iwl_trans_pcie_dump_regs(trans); 719e705c121SKalle Valo return -ETIMEDOUT; 720e705c121SKalle Valo } 721e705c121SKalle Valo 722e705c121SKalle Valo return 0; 723e705c121SKalle Valo } 724e705c121SKalle Valo 725e705c121SKalle Valo static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num, 726e705c121SKalle Valo const struct fw_desc *section) 727e705c121SKalle Valo { 728e705c121SKalle Valo u8 *v_addr; 729e705c121SKalle Valo dma_addr_t p_addr; 730e705c121SKalle Valo u32 offset, chunk_sz = min_t(u32, FH_MEM_TB_MAX_LENGTH, section->len); 731e705c121SKalle Valo int ret = 0; 732e705c121SKalle Valo 733e705c121SKalle Valo IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n", 734e705c121SKalle Valo section_num); 735e705c121SKalle Valo 736e705c121SKalle Valo v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr, 737e705c121SKalle Valo GFP_KERNEL | __GFP_NOWARN); 738e705c121SKalle Valo if (!v_addr) { 739e705c121SKalle Valo IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n"); 740e705c121SKalle Valo chunk_sz = PAGE_SIZE; 741e705c121SKalle Valo v_addr = dma_alloc_coherent(trans->dev, chunk_sz, 742e705c121SKalle Valo &p_addr, GFP_KERNEL); 743e705c121SKalle Valo if (!v_addr) 744e705c121SKalle Valo return -ENOMEM; 745e705c121SKalle Valo } 746e705c121SKalle Valo 747e705c121SKalle Valo for (offset = 0; offset < section->len; offset += chunk_sz) { 748e705c121SKalle Valo u32 copy_size, dst_addr; 749e705c121SKalle Valo bool extended_addr = false; 750e705c121SKalle Valo 751e705c121SKalle Valo copy_size = min_t(u32, chunk_sz, section->len - offset); 752e705c121SKalle Valo dst_addr = section->offset + offset; 753e705c121SKalle Valo 754e705c121SKalle Valo if (dst_addr >= IWL_FW_MEM_EXTENDED_START && 755e705c121SKalle Valo dst_addr <= IWL_FW_MEM_EXTENDED_END) 756e705c121SKalle Valo extended_addr = true; 757e705c121SKalle Valo 758e705c121SKalle Valo if (extended_addr) 759e705c121SKalle Valo iwl_set_bits_prph(trans, LMPM_CHICK, 760e705c121SKalle Valo LMPM_CHICK_EXTENDED_ADDR_SPACE); 761e705c121SKalle Valo 762e705c121SKalle Valo memcpy(v_addr, (u8 *)section->data + offset, copy_size); 763e705c121SKalle Valo ret = iwl_pcie_load_firmware_chunk(trans, dst_addr, p_addr, 764e705c121SKalle Valo copy_size); 765e705c121SKalle Valo 766e705c121SKalle Valo if (extended_addr) 767e705c121SKalle Valo iwl_clear_bits_prph(trans, LMPM_CHICK, 768e705c121SKalle Valo LMPM_CHICK_EXTENDED_ADDR_SPACE); 769e705c121SKalle Valo 770e705c121SKalle Valo if (ret) { 771e705c121SKalle Valo IWL_ERR(trans, 772e705c121SKalle Valo "Could not load the [%d] uCode section\n", 773e705c121SKalle Valo section_num); 774e705c121SKalle Valo break; 775e705c121SKalle Valo } 776e705c121SKalle Valo } 777e705c121SKalle Valo 778e705c121SKalle Valo dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr); 779e705c121SKalle Valo return ret; 780e705c121SKalle Valo } 781e705c121SKalle Valo 782e705c121SKalle Valo static int iwl_pcie_load_cpu_sections_8000(struct iwl_trans *trans, 783e705c121SKalle Valo const struct fw_img *image, 784e705c121SKalle Valo int cpu, 785e705c121SKalle Valo int *first_ucode_section) 786e705c121SKalle Valo { 787e705c121SKalle Valo int shift_param; 788e705c121SKalle Valo int i, ret = 0, sec_num = 0x1; 789e705c121SKalle Valo u32 val, last_read_idx = 0; 790e705c121SKalle Valo 791e705c121SKalle Valo if (cpu == 1) { 792e705c121SKalle Valo shift_param = 0; 793e705c121SKalle Valo *first_ucode_section = 0; 794e705c121SKalle Valo } else { 795e705c121SKalle Valo shift_param = 16; 796e705c121SKalle Valo (*first_ucode_section)++; 797e705c121SKalle Valo } 798e705c121SKalle Valo 799eef187a7SSara Sharon for (i = *first_ucode_section; i < image->num_sec; i++) { 800e705c121SKalle Valo last_read_idx = i; 801e705c121SKalle Valo 802e705c121SKalle Valo /* 803e705c121SKalle Valo * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between 804e705c121SKalle Valo * CPU1 to CPU2. 805e705c121SKalle Valo * PAGING_SEPARATOR_SECTION delimiter - separate between 806e705c121SKalle Valo * CPU2 non paged to CPU2 paging sec. 807e705c121SKalle Valo */ 808e705c121SKalle Valo if (!image->sec[i].data || 809e705c121SKalle Valo image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION || 810e705c121SKalle Valo image->sec[i].offset == PAGING_SEPARATOR_SECTION) { 811e705c121SKalle Valo IWL_DEBUG_FW(trans, 812e705c121SKalle Valo "Break since Data not valid or Empty section, sec = %d\n", 813e705c121SKalle Valo i); 814e705c121SKalle Valo break; 815e705c121SKalle Valo } 816e705c121SKalle Valo 817e705c121SKalle Valo ret = iwl_pcie_load_section(trans, i, &image->sec[i]); 818e705c121SKalle Valo if (ret) 819e705c121SKalle Valo return ret; 820e705c121SKalle Valo 821d6a2c5c7SSara Sharon /* Notify ucode of loaded section number and status */ 822e705c121SKalle Valo val = iwl_read_direct32(trans, FH_UCODE_LOAD_STATUS); 823e705c121SKalle Valo val = val | (sec_num << shift_param); 824e705c121SKalle Valo iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, val); 825eda50cdeSSara Sharon 826e705c121SKalle Valo sec_num = (sec_num << 1) | 0x1; 827e705c121SKalle Valo } 828e705c121SKalle Valo 829e705c121SKalle Valo *first_ucode_section = last_read_idx; 830e705c121SKalle Valo 8312aabdbdcSEmmanuel Grumbach iwl_enable_interrupts(trans); 8322aabdbdcSEmmanuel Grumbach 833286ca8ebSLuca Coelho if (trans->trans_cfg->use_tfh) { 834e705c121SKalle Valo if (cpu == 1) 835d6a2c5c7SSara Sharon iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS, 836d6a2c5c7SSara Sharon 0xFFFF); 837e705c121SKalle Valo else 838d6a2c5c7SSara Sharon iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS, 839d6a2c5c7SSara Sharon 0xFFFFFFFF); 840d6a2c5c7SSara Sharon } else { 841d6a2c5c7SSara Sharon if (cpu == 1) 842d6a2c5c7SSara Sharon iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, 843d6a2c5c7SSara Sharon 0xFFFF); 844d6a2c5c7SSara Sharon else 845d6a2c5c7SSara Sharon iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, 846d6a2c5c7SSara Sharon 0xFFFFFFFF); 847d6a2c5c7SSara Sharon } 848e705c121SKalle Valo 849e705c121SKalle Valo return 0; 850e705c121SKalle Valo } 851e705c121SKalle Valo 852e705c121SKalle Valo static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans, 853e705c121SKalle Valo const struct fw_img *image, 854e705c121SKalle Valo int cpu, 855e705c121SKalle Valo int *first_ucode_section) 856e705c121SKalle Valo { 857e705c121SKalle Valo int i, ret = 0; 858e705c121SKalle Valo u32 last_read_idx = 0; 859e705c121SKalle Valo 8603ce4a038SKirtika Ruchandani if (cpu == 1) 861e705c121SKalle Valo *first_ucode_section = 0; 8623ce4a038SKirtika Ruchandani else 863e705c121SKalle Valo (*first_ucode_section)++; 864e705c121SKalle Valo 865eef187a7SSara Sharon for (i = *first_ucode_section; i < image->num_sec; i++) { 866e705c121SKalle Valo last_read_idx = i; 867e705c121SKalle Valo 868e705c121SKalle Valo /* 869e705c121SKalle Valo * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between 870e705c121SKalle Valo * CPU1 to CPU2. 871e705c121SKalle Valo * PAGING_SEPARATOR_SECTION delimiter - separate between 872e705c121SKalle Valo * CPU2 non paged to CPU2 paging sec. 873e705c121SKalle Valo */ 874e705c121SKalle Valo if (!image->sec[i].data || 875e705c121SKalle Valo image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION || 876e705c121SKalle Valo image->sec[i].offset == PAGING_SEPARATOR_SECTION) { 877e705c121SKalle Valo IWL_DEBUG_FW(trans, 878e705c121SKalle Valo "Break since Data not valid or Empty section, sec = %d\n", 879e705c121SKalle Valo i); 880e705c121SKalle Valo break; 881e705c121SKalle Valo } 882e705c121SKalle Valo 883e705c121SKalle Valo ret = iwl_pcie_load_section(trans, i, &image->sec[i]); 884e705c121SKalle Valo if (ret) 885e705c121SKalle Valo return ret; 886e705c121SKalle Valo } 887e705c121SKalle Valo 888e705c121SKalle Valo *first_ucode_section = last_read_idx; 889e705c121SKalle Valo 890e705c121SKalle Valo return 0; 891e705c121SKalle Valo } 892e705c121SKalle Valo 893593fae3eSShahar S Matityahu static void iwl_pcie_apply_destination_ini(struct iwl_trans *trans) 894593fae3eSShahar S Matityahu { 895593fae3eSShahar S Matityahu enum iwl_fw_ini_allocation_id alloc_id = IWL_FW_INI_ALLOCATION_ID_DBGC1; 896593fae3eSShahar S Matityahu struct iwl_fw_ini_allocation_tlv *fw_mon_cfg = 897593fae3eSShahar S Matityahu &trans->dbg.fw_mon_cfg[alloc_id]; 898593fae3eSShahar S Matityahu struct iwl_dram_data *frag; 899593fae3eSShahar S Matityahu 900593fae3eSShahar S Matityahu if (!iwl_trans_dbg_ini_valid(trans)) 901593fae3eSShahar S Matityahu return; 902593fae3eSShahar S Matityahu 903593fae3eSShahar S Matityahu if (le32_to_cpu(fw_mon_cfg->buf_location) == 904593fae3eSShahar S Matityahu IWL_FW_INI_LOCATION_SRAM_PATH) { 905593fae3eSShahar S Matityahu IWL_DEBUG_FW(trans, "WRT: Applying SMEM buffer destination\n"); 906593fae3eSShahar S Matityahu /* set sram monitor by enabling bit 7 */ 907593fae3eSShahar S Matityahu iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 908593fae3eSShahar S Matityahu CSR_HW_IF_CONFIG_REG_BIT_MONITOR_SRAM); 909593fae3eSShahar S Matityahu 910593fae3eSShahar S Matityahu return; 911593fae3eSShahar S Matityahu } 912593fae3eSShahar S Matityahu 913593fae3eSShahar S Matityahu if (le32_to_cpu(fw_mon_cfg->buf_location) != 914593fae3eSShahar S Matityahu IWL_FW_INI_LOCATION_DRAM_PATH || 915593fae3eSShahar S Matityahu !trans->dbg.fw_mon_ini[alloc_id].num_frags) 916593fae3eSShahar S Matityahu return; 917593fae3eSShahar S Matityahu 918593fae3eSShahar S Matityahu frag = &trans->dbg.fw_mon_ini[alloc_id].frags[0]; 919593fae3eSShahar S Matityahu 920593fae3eSShahar S Matityahu IWL_DEBUG_FW(trans, "WRT: Applying DRAM destination (alloc_id=%u)\n", 921593fae3eSShahar S Matityahu alloc_id); 922593fae3eSShahar S Matityahu 923593fae3eSShahar S Matityahu iwl_write_umac_prph(trans, MON_BUFF_BASE_ADDR_VER2, 924593fae3eSShahar S Matityahu frag->physical >> MON_BUFF_SHIFT_VER2); 925593fae3eSShahar S Matityahu iwl_write_umac_prph(trans, MON_BUFF_END_ADDR_VER2, 926593fae3eSShahar S Matityahu (frag->physical + frag->size - 256) >> 927593fae3eSShahar S Matityahu MON_BUFF_SHIFT_VER2); 928593fae3eSShahar S Matityahu } 929593fae3eSShahar S Matityahu 930c9be849dSLiad Kaufman void iwl_pcie_apply_destination(struct iwl_trans *trans) 931e705c121SKalle Valo { 93291c28b83SShahar S Matityahu const struct iwl_fw_dbg_dest_tlv_v1 *dest = trans->dbg.dest_tlv; 93369f0e505SShahar S Matityahu const struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon; 934e705c121SKalle Valo int i; 935e705c121SKalle Valo 936a1af4c48SShahar S Matityahu if (iwl_trans_dbg_ini_valid(trans)) { 937593fae3eSShahar S Matityahu iwl_pcie_apply_destination_ini(trans); 9387a14c23dSSara Sharon return; 9397a14c23dSSara Sharon } 9407a14c23dSSara Sharon 941e705c121SKalle Valo IWL_INFO(trans, "Applying debug destination %s\n", 942e705c121SKalle Valo get_fw_dbg_mode_string(dest->monitor_mode)); 943e705c121SKalle Valo 944e705c121SKalle Valo if (dest->monitor_mode == EXTERNAL_MODE) 945e705c121SKalle Valo iwl_pcie_alloc_fw_monitor(trans, dest->size_power); 946e705c121SKalle Valo else 947e705c121SKalle Valo IWL_WARN(trans, "PCI should have external buffer debug\n"); 948e705c121SKalle Valo 94991c28b83SShahar S Matityahu for (i = 0; i < trans->dbg.n_dest_reg; i++) { 950e705c121SKalle Valo u32 addr = le32_to_cpu(dest->reg_ops[i].addr); 951e705c121SKalle Valo u32 val = le32_to_cpu(dest->reg_ops[i].val); 952e705c121SKalle Valo 953e705c121SKalle Valo switch (dest->reg_ops[i].op) { 954e705c121SKalle Valo case CSR_ASSIGN: 955e705c121SKalle Valo iwl_write32(trans, addr, val); 956e705c121SKalle Valo break; 957e705c121SKalle Valo case CSR_SETBIT: 958e705c121SKalle Valo iwl_set_bit(trans, addr, BIT(val)); 959e705c121SKalle Valo break; 960e705c121SKalle Valo case CSR_CLEARBIT: 961e705c121SKalle Valo iwl_clear_bit(trans, addr, BIT(val)); 962e705c121SKalle Valo break; 963e705c121SKalle Valo case PRPH_ASSIGN: 964e705c121SKalle Valo iwl_write_prph(trans, addr, val); 965e705c121SKalle Valo break; 966e705c121SKalle Valo case PRPH_SETBIT: 967e705c121SKalle Valo iwl_set_bits_prph(trans, addr, BIT(val)); 968e705c121SKalle Valo break; 969e705c121SKalle Valo case PRPH_CLEARBIT: 970e705c121SKalle Valo iwl_clear_bits_prph(trans, addr, BIT(val)); 971e705c121SKalle Valo break; 972e705c121SKalle Valo case PRPH_BLOCKBIT: 973e705c121SKalle Valo if (iwl_read_prph(trans, addr) & BIT(val)) { 974e705c121SKalle Valo IWL_ERR(trans, 975e705c121SKalle Valo "BIT(%u) in address 0x%x is 1, stopping FW configuration\n", 976e705c121SKalle Valo val, addr); 977e705c121SKalle Valo goto monitor; 978e705c121SKalle Valo } 979e705c121SKalle Valo break; 980e705c121SKalle Valo default: 981e705c121SKalle Valo IWL_ERR(trans, "FW debug - unknown OP %d\n", 982e705c121SKalle Valo dest->reg_ops[i].op); 983e705c121SKalle Valo break; 984e705c121SKalle Valo } 985e705c121SKalle Valo } 986e705c121SKalle Valo 987e705c121SKalle Valo monitor: 98869f0e505SShahar S Matityahu if (dest->monitor_mode == EXTERNAL_MODE && fw_mon->size) { 989e705c121SKalle Valo iwl_write_prph(trans, le32_to_cpu(dest->base_reg), 99069f0e505SShahar S Matityahu fw_mon->physical >> dest->base_shift); 991286ca8ebSLuca Coelho if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_8000) 992e705c121SKalle Valo iwl_write_prph(trans, le32_to_cpu(dest->end_reg), 99369f0e505SShahar S Matityahu (fw_mon->physical + fw_mon->size - 99469f0e505SShahar S Matityahu 256) >> dest->end_shift); 99562d7476dSEmmanuel Grumbach else 99662d7476dSEmmanuel Grumbach iwl_write_prph(trans, le32_to_cpu(dest->end_reg), 99769f0e505SShahar S Matityahu (fw_mon->physical + fw_mon->size) >> 99862d7476dSEmmanuel Grumbach dest->end_shift); 999e705c121SKalle Valo } 1000e705c121SKalle Valo } 1001e705c121SKalle Valo 1002e705c121SKalle Valo static int iwl_pcie_load_given_ucode(struct iwl_trans *trans, 1003e705c121SKalle Valo const struct fw_img *image) 1004e705c121SKalle Valo { 1005e705c121SKalle Valo int ret = 0; 1006e705c121SKalle Valo int first_ucode_section; 1007e705c121SKalle Valo 1008e705c121SKalle Valo IWL_DEBUG_FW(trans, "working with %s CPU\n", 1009e705c121SKalle Valo image->is_dual_cpus ? "Dual" : "Single"); 1010e705c121SKalle Valo 1011e705c121SKalle Valo /* load to FW the binary non secured sections of CPU1 */ 1012e705c121SKalle Valo ret = iwl_pcie_load_cpu_sections(trans, image, 1, &first_ucode_section); 1013e705c121SKalle Valo if (ret) 1014e705c121SKalle Valo return ret; 1015e705c121SKalle Valo 1016e705c121SKalle Valo if (image->is_dual_cpus) { 1017e705c121SKalle Valo /* set CPU2 header address */ 1018e705c121SKalle Valo iwl_write_prph(trans, 1019e705c121SKalle Valo LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR, 1020e705c121SKalle Valo LMPM_SECURE_CPU2_HDR_MEM_SPACE); 1021e705c121SKalle Valo 1022e705c121SKalle Valo /* load to FW the binary sections of CPU2 */ 1023e705c121SKalle Valo ret = iwl_pcie_load_cpu_sections(trans, image, 2, 1024e705c121SKalle Valo &first_ucode_section); 1025e705c121SKalle Valo if (ret) 1026e705c121SKalle Valo return ret; 1027e705c121SKalle Valo } 1028e705c121SKalle Valo 1029e705c121SKalle Valo /* supported for 7000 only for the moment */ 1030e705c121SKalle Valo if (iwlwifi_mod_params.fw_monitor && 1031286ca8ebSLuca Coelho trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_7000) { 103269f0e505SShahar S Matityahu struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon; 1033e705c121SKalle Valo 103469f0e505SShahar S Matityahu iwl_pcie_alloc_fw_monitor(trans, 0); 103569f0e505SShahar S Matityahu if (fw_mon->size) { 1036e705c121SKalle Valo iwl_write_prph(trans, MON_BUFF_BASE_ADDR, 103769f0e505SShahar S Matityahu fw_mon->physical >> 4); 1038e705c121SKalle Valo iwl_write_prph(trans, MON_BUFF_END_ADDR, 103969f0e505SShahar S Matityahu (fw_mon->physical + fw_mon->size) >> 4); 1040e705c121SKalle Valo } 10417a14c23dSSara Sharon } else if (iwl_pcie_dbg_on(trans)) { 1042e705c121SKalle Valo iwl_pcie_apply_destination(trans); 1043e705c121SKalle Valo } 1044e705c121SKalle Valo 10452aabdbdcSEmmanuel Grumbach iwl_enable_interrupts(trans); 10462aabdbdcSEmmanuel Grumbach 1047e705c121SKalle Valo /* release CPU reset */ 1048e705c121SKalle Valo iwl_write32(trans, CSR_RESET, 0); 1049e705c121SKalle Valo 1050e705c121SKalle Valo return 0; 1051e705c121SKalle Valo } 1052e705c121SKalle Valo 1053e705c121SKalle Valo static int iwl_pcie_load_given_ucode_8000(struct iwl_trans *trans, 1054e705c121SKalle Valo const struct fw_img *image) 1055e705c121SKalle Valo { 1056e705c121SKalle Valo int ret = 0; 1057e705c121SKalle Valo int first_ucode_section; 1058e705c121SKalle Valo 1059e705c121SKalle Valo IWL_DEBUG_FW(trans, "working with %s CPU\n", 1060e705c121SKalle Valo image->is_dual_cpus ? "Dual" : "Single"); 1061e705c121SKalle Valo 10627a14c23dSSara Sharon if (iwl_pcie_dbg_on(trans)) 1063e705c121SKalle Valo iwl_pcie_apply_destination(trans); 1064e705c121SKalle Valo 106582ea7966SSara Sharon IWL_DEBUG_POWER(trans, "Original WFPM value = 0x%08X\n", 106682ea7966SSara Sharon iwl_read_prph(trans, WFPM_GP2)); 106782ea7966SSara Sharon 106882ea7966SSara Sharon /* 106982ea7966SSara Sharon * Set default value. On resume reading the values that were 107082ea7966SSara Sharon * zeored can provide debug data on the resume flow. 107182ea7966SSara Sharon * This is for debugging only and has no functional impact. 107282ea7966SSara Sharon */ 107382ea7966SSara Sharon iwl_write_prph(trans, WFPM_GP2, 0x01010101); 107482ea7966SSara Sharon 1075e705c121SKalle Valo /* configure the ucode to be ready to get the secured image */ 1076e705c121SKalle Valo /* release CPU reset */ 1077e705c121SKalle Valo iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT); 1078e705c121SKalle Valo 1079e705c121SKalle Valo /* load to FW the binary Secured sections of CPU1 */ 1080e705c121SKalle Valo ret = iwl_pcie_load_cpu_sections_8000(trans, image, 1, 1081e705c121SKalle Valo &first_ucode_section); 1082e705c121SKalle Valo if (ret) 1083e705c121SKalle Valo return ret; 1084e705c121SKalle Valo 1085e705c121SKalle Valo /* load to FW the binary sections of CPU2 */ 1086e705c121SKalle Valo return iwl_pcie_load_cpu_sections_8000(trans, image, 2, 1087e705c121SKalle Valo &first_ucode_section); 1088e705c121SKalle Valo } 1089e705c121SKalle Valo 10909ad8fd0bSJohannes Berg bool iwl_pcie_check_hw_rf_kill(struct iwl_trans *trans) 1091727c02dfSSara Sharon { 1092326477e4SJohannes Berg struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1093727c02dfSSara Sharon bool hw_rfkill = iwl_is_rfkill_set(trans); 1094326477e4SJohannes Berg bool prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 1095326477e4SJohannes Berg bool report; 1096727c02dfSSara Sharon 1097326477e4SJohannes Berg if (hw_rfkill) { 1098326477e4SJohannes Berg set_bit(STATUS_RFKILL_HW, &trans->status); 1099326477e4SJohannes Berg set_bit(STATUS_RFKILL_OPMODE, &trans->status); 1100326477e4SJohannes Berg } else { 1101326477e4SJohannes Berg clear_bit(STATUS_RFKILL_HW, &trans->status); 1102326477e4SJohannes Berg if (trans_pcie->opmode_down) 1103326477e4SJohannes Berg clear_bit(STATUS_RFKILL_OPMODE, &trans->status); 1104326477e4SJohannes Berg } 1105727c02dfSSara Sharon 1106326477e4SJohannes Berg report = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 1107326477e4SJohannes Berg 1108326477e4SJohannes Berg if (prev != report) 1109326477e4SJohannes Berg iwl_trans_pcie_rf_kill(trans, report); 1110727c02dfSSara Sharon 1111727c02dfSSara Sharon return hw_rfkill; 1112727c02dfSSara Sharon } 1113727c02dfSSara Sharon 11147ca00409SHaim Dreyfuss struct iwl_causes_list { 11157ca00409SHaim Dreyfuss u32 cause_num; 11167ca00409SHaim Dreyfuss u32 mask_reg; 11177ca00409SHaim Dreyfuss u8 addr; 11187ca00409SHaim Dreyfuss }; 11197ca00409SHaim Dreyfuss 11207ca00409SHaim Dreyfuss static struct iwl_causes_list causes_list[] = { 11217ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_D2S_CH0_NUM, CSR_MSIX_FH_INT_MASK_AD, 0}, 11227ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_D2S_CH1_NUM, CSR_MSIX_FH_INT_MASK_AD, 0x1}, 11237ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_S2D, CSR_MSIX_FH_INT_MASK_AD, 0x3}, 11247ca00409SHaim Dreyfuss {MSIX_FH_INT_CAUSES_FH_ERR, CSR_MSIX_FH_INT_MASK_AD, 0x5}, 11257ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_ALIVE, CSR_MSIX_HW_INT_MASK_AD, 0x10}, 11267ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_WAKEUP, CSR_MSIX_HW_INT_MASK_AD, 0x11}, 1127ff911dcaSShaul Triebitz {MSIX_HW_INT_CAUSES_REG_IML, CSR_MSIX_HW_INT_MASK_AD, 0x12}, 11287ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_CT_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x16}, 11297ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_RF_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x17}, 11307ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_PERIODIC, CSR_MSIX_HW_INT_MASK_AD, 0x18}, 11317ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_SW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x29}, 11327ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_SCD, CSR_MSIX_HW_INT_MASK_AD, 0x2A}, 11337ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_FH_TX, CSR_MSIX_HW_INT_MASK_AD, 0x2B}, 11347ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_HW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x2D}, 11357ca00409SHaim Dreyfuss {MSIX_HW_INT_CAUSES_REG_HAP, CSR_MSIX_HW_INT_MASK_AD, 0x2E}, 11367ca00409SHaim Dreyfuss }; 11377ca00409SHaim Dreyfuss 11387ca00409SHaim Dreyfuss static void iwl_pcie_map_non_rx_causes(struct iwl_trans *trans) 11397ca00409SHaim Dreyfuss { 11407ca00409SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 11417ca00409SHaim Dreyfuss int val = trans_pcie->def_irq | MSIX_NON_AUTO_CLEAR_CAUSE; 11423681021fSJohannes Berg int i, arr_size = ARRAY_SIZE(causes_list); 11433681021fSJohannes Berg struct iwl_causes_list *causes = causes_list; 11447ca00409SHaim Dreyfuss 11457ca00409SHaim Dreyfuss /* 11467ca00409SHaim Dreyfuss * Access all non RX causes and map them to the default irq. 11477ca00409SHaim Dreyfuss * In case we are missing at least one interrupt vector, 11487ca00409SHaim Dreyfuss * the first interrupt vector will serve non-RX and FBQ causes. 11497ca00409SHaim Dreyfuss */ 11509b58419eSGolan Ben Ami for (i = 0; i < arr_size; i++) { 11519b58419eSGolan Ben Ami iwl_write8(trans, CSR_MSIX_IVAR(causes[i].addr), val); 11529b58419eSGolan Ben Ami iwl_clear_bit(trans, causes[i].mask_reg, 11539b58419eSGolan Ben Ami causes[i].cause_num); 11547ca00409SHaim Dreyfuss } 11557ca00409SHaim Dreyfuss } 11567ca00409SHaim Dreyfuss 11577ca00409SHaim Dreyfuss static void iwl_pcie_map_rx_causes(struct iwl_trans *trans) 11587ca00409SHaim Dreyfuss { 11597ca00409SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 11607ca00409SHaim Dreyfuss u32 offset = 11617ca00409SHaim Dreyfuss trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0; 11627ca00409SHaim Dreyfuss u32 val, idx; 11637ca00409SHaim Dreyfuss 11647ca00409SHaim Dreyfuss /* 11657ca00409SHaim Dreyfuss * The first RX queue - fallback queue, which is designated for 11667ca00409SHaim Dreyfuss * management frame, command responses etc, is always mapped to the 11677ca00409SHaim Dreyfuss * first interrupt vector. The other RX queues are mapped to 11687ca00409SHaim Dreyfuss * the other (N - 2) interrupt vectors. 11697ca00409SHaim Dreyfuss */ 11707ca00409SHaim Dreyfuss val = BIT(MSIX_FH_INT_CAUSES_Q(0)); 11717ca00409SHaim Dreyfuss for (idx = 1; idx < trans->num_rx_queues; idx++) { 11727ca00409SHaim Dreyfuss iwl_write8(trans, CSR_MSIX_RX_IVAR(idx), 11737ca00409SHaim Dreyfuss MSIX_FH_INT_CAUSES_Q(idx - offset)); 11747ca00409SHaim Dreyfuss val |= BIT(MSIX_FH_INT_CAUSES_Q(idx)); 11757ca00409SHaim Dreyfuss } 11767ca00409SHaim Dreyfuss iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~val); 11777ca00409SHaim Dreyfuss 11787ca00409SHaim Dreyfuss val = MSIX_FH_INT_CAUSES_Q(0); 11797ca00409SHaim Dreyfuss if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX) 11807ca00409SHaim Dreyfuss val |= MSIX_NON_AUTO_CLEAR_CAUSE; 11817ca00409SHaim Dreyfuss iwl_write8(trans, CSR_MSIX_RX_IVAR(0), val); 11827ca00409SHaim Dreyfuss 11837ca00409SHaim Dreyfuss if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS) 11847ca00409SHaim Dreyfuss iwl_write8(trans, CSR_MSIX_RX_IVAR(1), val); 11857ca00409SHaim Dreyfuss } 11867ca00409SHaim Dreyfuss 118777c09bc8SSara Sharon void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie) 11887ca00409SHaim Dreyfuss { 11897ca00409SHaim Dreyfuss struct iwl_trans *trans = trans_pcie->trans; 11907ca00409SHaim Dreyfuss 11917ca00409SHaim Dreyfuss if (!trans_pcie->msix_enabled) { 1192286ca8ebSLuca Coelho if (trans->trans_cfg->mq_rx_supported && 1193d7270d61SHaim Dreyfuss test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 1194ea695b7cSShaul Triebitz iwl_write_umac_prph(trans, UREG_CHICK, 11957ca00409SHaim Dreyfuss UREG_CHICK_MSI_ENABLE); 11967ca00409SHaim Dreyfuss return; 11977ca00409SHaim Dreyfuss } 1198d7270d61SHaim Dreyfuss /* 1199d7270d61SHaim Dreyfuss * The IVAR table needs to be configured again after reset, 1200d7270d61SHaim Dreyfuss * but if the device is disabled, we can't write to 1201d7270d61SHaim Dreyfuss * prph. 1202d7270d61SHaim Dreyfuss */ 1203d7270d61SHaim Dreyfuss if (test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 1204ea695b7cSShaul Triebitz iwl_write_umac_prph(trans, UREG_CHICK, UREG_CHICK_MSIX_ENABLE); 12057ca00409SHaim Dreyfuss 12067ca00409SHaim Dreyfuss /* 12077ca00409SHaim Dreyfuss * Each cause from the causes list above and the RX causes is 12087ca00409SHaim Dreyfuss * represented as a byte in the IVAR table. The first nibble 12097ca00409SHaim Dreyfuss * represents the bound interrupt vector of the cause, the second 12107ca00409SHaim Dreyfuss * represents no auto clear for this cause. This will be set if its 12117ca00409SHaim Dreyfuss * interrupt vector is bound to serve other causes. 12127ca00409SHaim Dreyfuss */ 12137ca00409SHaim Dreyfuss iwl_pcie_map_rx_causes(trans); 12147ca00409SHaim Dreyfuss 12157ca00409SHaim Dreyfuss iwl_pcie_map_non_rx_causes(trans); 121683730058SHaim Dreyfuss } 12177ca00409SHaim Dreyfuss 121883730058SHaim Dreyfuss static void iwl_pcie_init_msix(struct iwl_trans_pcie *trans_pcie) 121983730058SHaim Dreyfuss { 122083730058SHaim Dreyfuss struct iwl_trans *trans = trans_pcie->trans; 122183730058SHaim Dreyfuss 122283730058SHaim Dreyfuss iwl_pcie_conf_msix_hw(trans_pcie); 122383730058SHaim Dreyfuss 122483730058SHaim Dreyfuss if (!trans_pcie->msix_enabled) 122583730058SHaim Dreyfuss return; 122683730058SHaim Dreyfuss 122783730058SHaim Dreyfuss trans_pcie->fh_init_mask = ~iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD); 12287ca00409SHaim Dreyfuss trans_pcie->fh_mask = trans_pcie->fh_init_mask; 122983730058SHaim Dreyfuss trans_pcie->hw_init_mask = ~iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD); 12307ca00409SHaim Dreyfuss trans_pcie->hw_mask = trans_pcie->hw_init_mask; 12317ca00409SHaim Dreyfuss } 12327ca00409SHaim Dreyfuss 1233bab3cb92SEmmanuel Grumbach static void _iwl_trans_pcie_stop_device(struct iwl_trans *trans) 1234e705c121SKalle Valo { 1235e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1236e705c121SKalle Valo 1237e705c121SKalle Valo lockdep_assert_held(&trans_pcie->mutex); 1238e705c121SKalle Valo 1239e705c121SKalle Valo if (trans_pcie->is_down) 1240e705c121SKalle Valo return; 1241e705c121SKalle Valo 1242e705c121SKalle Valo trans_pcie->is_down = true; 1243e705c121SKalle Valo 1244e705c121SKalle Valo /* tell the device to stop sending interrupts */ 1245e705c121SKalle Valo iwl_disable_interrupts(trans); 1246e705c121SKalle Valo 1247e705c121SKalle Valo /* device going down, Stop using ICT table */ 1248e705c121SKalle Valo iwl_pcie_disable_ict(trans); 1249e705c121SKalle Valo 1250e705c121SKalle Valo /* 1251e705c121SKalle Valo * If a HW restart happens during firmware loading, 1252e705c121SKalle Valo * then the firmware loading might call this function 1253e705c121SKalle Valo * and later it might be called again due to the 1254e705c121SKalle Valo * restart. So don't process again if the device is 1255e705c121SKalle Valo * already dead. 1256e705c121SKalle Valo */ 1257e705c121SKalle Valo if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) { 1258a6bd005fSEmmanuel Grumbach IWL_DEBUG_INFO(trans, 1259a6bd005fSEmmanuel Grumbach "DEVICE_ENABLED bit was set and is now cleared\n"); 1260e705c121SKalle Valo iwl_pcie_tx_stop(trans); 1261e705c121SKalle Valo iwl_pcie_rx_stop(trans); 1262e705c121SKalle Valo 1263e705c121SKalle Valo /* Power-down device's busmaster DMA clocks */ 1264e705c121SKalle Valo if (!trans->cfg->apmg_not_supported) { 1265e705c121SKalle Valo iwl_write_prph(trans, APMG_CLK_DIS_REG, 1266e705c121SKalle Valo APMG_CLK_VAL_DMA_CLK_RQT); 1267e705c121SKalle Valo udelay(5); 1268e705c121SKalle Valo } 1269e705c121SKalle Valo } 1270e705c121SKalle Valo 1271e705c121SKalle Valo /* Make sure (redundant) we've released our request to stay awake */ 1272e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1273286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_mac_access_req)); 1274e705c121SKalle Valo 1275e705c121SKalle Valo /* Stop the device, and put it in low power state */ 1276e705c121SKalle Valo iwl_pcie_apm_stop(trans, false); 1277e705c121SKalle Valo 1278870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 1279e705c121SKalle Valo 1280e705c121SKalle Valo /* 1281f4a1f04aSGolan Ben Ami * Upon stop, the IVAR table gets erased, so msi-x won't 1282f4a1f04aSGolan Ben Ami * work. This causes a bug in RF-KILL flows, since the interrupt 1283f4a1f04aSGolan Ben Ami * that enables radio won't fire on the correct irq, and the 1284f4a1f04aSGolan Ben Ami * driver won't be able to handle the interrupt. 1285f4a1f04aSGolan Ben Ami * Configure the IVAR table again after reset. 1286f4a1f04aSGolan Ben Ami */ 1287f4a1f04aSGolan Ben Ami iwl_pcie_conf_msix_hw(trans_pcie); 1288f4a1f04aSGolan Ben Ami 1289f4a1f04aSGolan Ben Ami /* 1290e705c121SKalle Valo * Upon stop, the APM issues an interrupt if HW RF kill is set. 1291e705c121SKalle Valo * This is a bug in certain verions of the hardware. 1292e705c121SKalle Valo * Certain devices also keep sending HW RF kill interrupt all 1293e705c121SKalle Valo * the time, unless the interrupt is ACKed even if the interrupt 1294e705c121SKalle Valo * should be masked. Re-ACK all the interrupts here. 1295e705c121SKalle Valo */ 1296e705c121SKalle Valo iwl_disable_interrupts(trans); 1297e705c121SKalle Valo 1298e705c121SKalle Valo /* clear all status bits */ 1299e705c121SKalle Valo clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status); 1300e705c121SKalle Valo clear_bit(STATUS_INT_ENABLED, &trans->status); 1301e705c121SKalle Valo clear_bit(STATUS_TPOWER_PMI, &trans->status); 1302e705c121SKalle Valo 1303e705c121SKalle Valo /* 1304e705c121SKalle Valo * Even if we stop the HW, we still want the RF kill 1305e705c121SKalle Valo * interrupt 1306e705c121SKalle Valo */ 1307e705c121SKalle Valo iwl_enable_rfkill_int(trans); 1308e705c121SKalle Valo 1309a6bd005fSEmmanuel Grumbach /* re-take ownership to prevent other users from stealing the device */ 1310e705c121SKalle Valo iwl_pcie_prepare_card_hw(trans); 1311e705c121SKalle Valo } 1312e705c121SKalle Valo 1313eda50cdeSSara Sharon void iwl_pcie_synchronize_irqs(struct iwl_trans *trans) 13142e5d4a8fSHaim Dreyfuss { 13152e5d4a8fSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 13162e5d4a8fSHaim Dreyfuss 13172e5d4a8fSHaim Dreyfuss if (trans_pcie->msix_enabled) { 13182e5d4a8fSHaim Dreyfuss int i; 13192e5d4a8fSHaim Dreyfuss 1320496d83caSHaim Dreyfuss for (i = 0; i < trans_pcie->alloc_vecs; i++) 13212e5d4a8fSHaim Dreyfuss synchronize_irq(trans_pcie->msix_entries[i].vector); 13222e5d4a8fSHaim Dreyfuss } else { 13232e5d4a8fSHaim Dreyfuss synchronize_irq(trans_pcie->pci_dev->irq); 13242e5d4a8fSHaim Dreyfuss } 13252e5d4a8fSHaim Dreyfuss } 13262e5d4a8fSHaim Dreyfuss 1327a6bd005fSEmmanuel Grumbach static int iwl_trans_pcie_start_fw(struct iwl_trans *trans, 1328a6bd005fSEmmanuel Grumbach const struct fw_img *fw, bool run_in_rfkill) 1329a6bd005fSEmmanuel Grumbach { 1330a6bd005fSEmmanuel Grumbach struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1331a6bd005fSEmmanuel Grumbach bool hw_rfkill; 1332a6bd005fSEmmanuel Grumbach int ret; 1333a6bd005fSEmmanuel Grumbach 1334a6bd005fSEmmanuel Grumbach /* This may fail if AMT took ownership of the device */ 1335a6bd005fSEmmanuel Grumbach if (iwl_pcie_prepare_card_hw(trans)) { 1336a6bd005fSEmmanuel Grumbach IWL_WARN(trans, "Exit HW not ready\n"); 1337a6bd005fSEmmanuel Grumbach ret = -EIO; 1338a6bd005fSEmmanuel Grumbach goto out; 1339a6bd005fSEmmanuel Grumbach } 1340a6bd005fSEmmanuel Grumbach 1341a6bd005fSEmmanuel Grumbach iwl_enable_rfkill_int(trans); 1342a6bd005fSEmmanuel Grumbach 1343a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_INT, 0xFFFFFFFF); 1344a6bd005fSEmmanuel Grumbach 1345a6bd005fSEmmanuel Grumbach /* 1346a6bd005fSEmmanuel Grumbach * We enabled the RF-Kill interrupt and the handler may very 1347a6bd005fSEmmanuel Grumbach * well be running. Disable the interrupts to make sure no other 1348a6bd005fSEmmanuel Grumbach * interrupt can be fired. 1349a6bd005fSEmmanuel Grumbach */ 1350a6bd005fSEmmanuel Grumbach iwl_disable_interrupts(trans); 1351a6bd005fSEmmanuel Grumbach 1352a6bd005fSEmmanuel Grumbach /* Make sure it finished running */ 13532e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1354a6bd005fSEmmanuel Grumbach 1355a6bd005fSEmmanuel Grumbach mutex_lock(&trans_pcie->mutex); 1356a6bd005fSEmmanuel Grumbach 1357a6bd005fSEmmanuel Grumbach /* If platform's RF_KILL switch is NOT set to KILL */ 13589ad8fd0bSJohannes Berg hw_rfkill = iwl_pcie_check_hw_rf_kill(trans); 1359a6bd005fSEmmanuel Grumbach if (hw_rfkill && !run_in_rfkill) { 1360a6bd005fSEmmanuel Grumbach ret = -ERFKILL; 1361a6bd005fSEmmanuel Grumbach goto out; 1362a6bd005fSEmmanuel Grumbach } 1363a6bd005fSEmmanuel Grumbach 1364a6bd005fSEmmanuel Grumbach /* Someone called stop_device, don't try to start_fw */ 1365a6bd005fSEmmanuel Grumbach if (trans_pcie->is_down) { 1366a6bd005fSEmmanuel Grumbach IWL_WARN(trans, 1367a6bd005fSEmmanuel Grumbach "Can't start_fw since the HW hasn't been started\n"); 136820aa99bbSAnton Protopopov ret = -EIO; 1369a6bd005fSEmmanuel Grumbach goto out; 1370a6bd005fSEmmanuel Grumbach } 1371a6bd005fSEmmanuel Grumbach 1372a6bd005fSEmmanuel Grumbach /* make sure rfkill handshake bits are cleared */ 1373a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 1374a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, 1375a6bd005fSEmmanuel Grumbach CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED); 1376a6bd005fSEmmanuel Grumbach 1377a6bd005fSEmmanuel Grumbach /* clear (again), then enable host interrupts */ 1378a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_INT, 0xFFFFFFFF); 1379a6bd005fSEmmanuel Grumbach 1380a6bd005fSEmmanuel Grumbach ret = iwl_pcie_nic_init(trans); 1381a6bd005fSEmmanuel Grumbach if (ret) { 1382a6bd005fSEmmanuel Grumbach IWL_ERR(trans, "Unable to init nic\n"); 1383a6bd005fSEmmanuel Grumbach goto out; 1384a6bd005fSEmmanuel Grumbach } 1385a6bd005fSEmmanuel Grumbach 1386a6bd005fSEmmanuel Grumbach /* 1387a6bd005fSEmmanuel Grumbach * Now, we load the firmware and don't want to be interrupted, even 1388a6bd005fSEmmanuel Grumbach * by the RF-Kill interrupt (hence mask all the interrupt besides the 1389a6bd005fSEmmanuel Grumbach * FH_TX interrupt which is needed to load the firmware). If the 1390a6bd005fSEmmanuel Grumbach * RF-Kill switch is toggled, we will find out after having loaded 1391a6bd005fSEmmanuel Grumbach * the firmware and return the proper value to the caller. 1392a6bd005fSEmmanuel Grumbach */ 1393a6bd005fSEmmanuel Grumbach iwl_enable_fw_load_int(trans); 1394a6bd005fSEmmanuel Grumbach 1395a6bd005fSEmmanuel Grumbach /* really make sure rfkill handshake bits are cleared */ 1396a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 1397a6bd005fSEmmanuel Grumbach iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 1398a6bd005fSEmmanuel Grumbach 1399a6bd005fSEmmanuel Grumbach /* Load the given image to the HW */ 1400286ca8ebSLuca Coelho if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_8000) 1401a6bd005fSEmmanuel Grumbach ret = iwl_pcie_load_given_ucode_8000(trans, fw); 1402a6bd005fSEmmanuel Grumbach else 1403a6bd005fSEmmanuel Grumbach ret = iwl_pcie_load_given_ucode(trans, fw); 1404a6bd005fSEmmanuel Grumbach 1405a6bd005fSEmmanuel Grumbach /* re-check RF-Kill state since we may have missed the interrupt */ 14069ad8fd0bSJohannes Berg hw_rfkill = iwl_pcie_check_hw_rf_kill(trans); 1407a6bd005fSEmmanuel Grumbach if (hw_rfkill && !run_in_rfkill) 1408a6bd005fSEmmanuel Grumbach ret = -ERFKILL; 1409a6bd005fSEmmanuel Grumbach 1410a6bd005fSEmmanuel Grumbach out: 1411a6bd005fSEmmanuel Grumbach mutex_unlock(&trans_pcie->mutex); 1412a6bd005fSEmmanuel Grumbach return ret; 1413a6bd005fSEmmanuel Grumbach } 1414a6bd005fSEmmanuel Grumbach 1415a6bd005fSEmmanuel Grumbach static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr) 1416a6bd005fSEmmanuel Grumbach { 1417a6bd005fSEmmanuel Grumbach iwl_pcie_reset_ict(trans); 1418a6bd005fSEmmanuel Grumbach iwl_pcie_tx_start(trans, scd_addr); 1419a6bd005fSEmmanuel Grumbach } 1420a6bd005fSEmmanuel Grumbach 1421326477e4SJohannes Berg void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans, 1422326477e4SJohannes Berg bool was_in_rfkill) 1423326477e4SJohannes Berg { 1424326477e4SJohannes Berg bool hw_rfkill; 1425326477e4SJohannes Berg 1426326477e4SJohannes Berg /* 1427326477e4SJohannes Berg * Check again since the RF kill state may have changed while 1428326477e4SJohannes Berg * all the interrupts were disabled, in this case we couldn't 1429326477e4SJohannes Berg * receive the RF kill interrupt and update the state in the 1430326477e4SJohannes Berg * op_mode. 1431326477e4SJohannes Berg * Don't call the op_mode if the rkfill state hasn't changed. 1432326477e4SJohannes Berg * This allows the op_mode to call stop_device from the rfkill 1433326477e4SJohannes Berg * notification without endless recursion. Under very rare 1434326477e4SJohannes Berg * circumstances, we might have a small recursion if the rfkill 1435326477e4SJohannes Berg * state changed exactly now while we were called from stop_device. 1436326477e4SJohannes Berg * This is very unlikely but can happen and is supported. 1437326477e4SJohannes Berg */ 1438326477e4SJohannes Berg hw_rfkill = iwl_is_rfkill_set(trans); 1439326477e4SJohannes Berg if (hw_rfkill) { 1440326477e4SJohannes Berg set_bit(STATUS_RFKILL_HW, &trans->status); 1441326477e4SJohannes Berg set_bit(STATUS_RFKILL_OPMODE, &trans->status); 1442326477e4SJohannes Berg } else { 1443326477e4SJohannes Berg clear_bit(STATUS_RFKILL_HW, &trans->status); 1444326477e4SJohannes Berg clear_bit(STATUS_RFKILL_OPMODE, &trans->status); 1445326477e4SJohannes Berg } 1446326477e4SJohannes Berg if (hw_rfkill != was_in_rfkill) 1447326477e4SJohannes Berg iwl_trans_pcie_rf_kill(trans, hw_rfkill); 1448326477e4SJohannes Berg } 1449326477e4SJohannes Berg 1450bab3cb92SEmmanuel Grumbach static void iwl_trans_pcie_stop_device(struct iwl_trans *trans) 1451e705c121SKalle Valo { 1452e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1453326477e4SJohannes Berg bool was_in_rfkill; 1454e705c121SKalle Valo 1455e705c121SKalle Valo mutex_lock(&trans_pcie->mutex); 1456326477e4SJohannes Berg trans_pcie->opmode_down = true; 1457326477e4SJohannes Berg was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 1458bab3cb92SEmmanuel Grumbach _iwl_trans_pcie_stop_device(trans); 1459326477e4SJohannes Berg iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill); 1460e705c121SKalle Valo mutex_unlock(&trans_pcie->mutex); 1461e705c121SKalle Valo } 1462e705c121SKalle Valo 1463e705c121SKalle Valo void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state) 1464e705c121SKalle Valo { 1465e705c121SKalle Valo struct iwl_trans_pcie __maybe_unused *trans_pcie = 1466e705c121SKalle Valo IWL_TRANS_GET_PCIE_TRANS(trans); 1467e705c121SKalle Valo 1468e705c121SKalle Valo lockdep_assert_held(&trans_pcie->mutex); 1469e705c121SKalle Valo 1470326477e4SJohannes Berg IWL_WARN(trans, "reporting RF_KILL (radio %s)\n", 1471326477e4SJohannes Berg state ? "disabled" : "enabled"); 147277c09bc8SSara Sharon if (iwl_op_mode_hw_rf_kill(trans->op_mode, state)) { 1473286ca8ebSLuca Coelho if (trans->trans_cfg->gen2) 1474bab3cb92SEmmanuel Grumbach _iwl_trans_pcie_gen2_stop_device(trans); 147577c09bc8SSara Sharon else 1476bab3cb92SEmmanuel Grumbach _iwl_trans_pcie_stop_device(trans); 1477e705c121SKalle Valo } 147877c09bc8SSara Sharon } 1479e705c121SKalle Valo 1480e5f3f215SHaim Dreyfuss void iwl_pcie_d3_complete_suspend(struct iwl_trans *trans, 1481e5f3f215SHaim Dreyfuss bool test, bool reset) 1482e705c121SKalle Valo { 1483e705c121SKalle Valo iwl_disable_interrupts(trans); 1484e705c121SKalle Valo 1485e705c121SKalle Valo /* 1486e705c121SKalle Valo * in testing mode, the host stays awake and the 1487e705c121SKalle Valo * hardware won't be reset (not even partially) 1488e705c121SKalle Valo */ 1489e705c121SKalle Valo if (test) 1490e705c121SKalle Valo return; 1491e705c121SKalle Valo 1492e705c121SKalle Valo iwl_pcie_disable_ict(trans); 1493e705c121SKalle Valo 14942e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1495e705c121SKalle Valo 1496e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1497286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_mac_access_req)); 1498e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1499286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_init_done)); 1500e705c121SKalle Valo 150123ae6128SMatti Gottlieb if (reset) { 1502e705c121SKalle Valo /* 1503e705c121SKalle Valo * reset TX queues -- some of their registers reset during S3 1504e705c121SKalle Valo * so if we don't reset everything here the D3 image would try 1505e705c121SKalle Valo * to execute some invalid memory upon resume 1506e705c121SKalle Valo */ 1507e705c121SKalle Valo iwl_trans_pcie_tx_reset(trans); 1508e705c121SKalle Valo } 1509e705c121SKalle Valo 1510e705c121SKalle Valo iwl_pcie_set_pwr(trans, true); 1511e705c121SKalle Valo } 1512e705c121SKalle Valo 1513e5f3f215SHaim Dreyfuss static int iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test, 1514e5f3f215SHaim Dreyfuss bool reset) 1515e5f3f215SHaim Dreyfuss { 1516e5f3f215SHaim Dreyfuss int ret; 1517e5f3f215SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1518e5f3f215SHaim Dreyfuss 1519e5f3f215SHaim Dreyfuss /* 1520e5f3f215SHaim Dreyfuss * Family IWL_DEVICE_FAMILY_AX210 and above persist mode is set by FW. 1521e5f3f215SHaim Dreyfuss */ 1522e5f3f215SHaim Dreyfuss if (!reset && trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210) { 1523e5f3f215SHaim Dreyfuss /* Enable persistence mode to avoid reset */ 1524e5f3f215SHaim Dreyfuss iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 1525e5f3f215SHaim Dreyfuss CSR_HW_IF_CONFIG_REG_PERSIST_MODE); 1526e5f3f215SHaim Dreyfuss } 1527e5f3f215SHaim Dreyfuss 1528e5f3f215SHaim Dreyfuss if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 1529e5f3f215SHaim Dreyfuss iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 1530e5f3f215SHaim Dreyfuss UREG_DOORBELL_TO_ISR6_SUSPEND); 1531e5f3f215SHaim Dreyfuss 1532e5f3f215SHaim Dreyfuss ret = wait_event_timeout(trans_pcie->sx_waitq, 1533e5f3f215SHaim Dreyfuss trans_pcie->sx_complete, 2 * HZ); 1534e5f3f215SHaim Dreyfuss /* 1535e5f3f215SHaim Dreyfuss * Invalidate it toward resume. 1536e5f3f215SHaim Dreyfuss */ 1537e5f3f215SHaim Dreyfuss trans_pcie->sx_complete = false; 1538e5f3f215SHaim Dreyfuss 1539e5f3f215SHaim Dreyfuss if (!ret) { 1540e5f3f215SHaim Dreyfuss IWL_ERR(trans, "Timeout entering D3\n"); 1541e5f3f215SHaim Dreyfuss return -ETIMEDOUT; 1542e5f3f215SHaim Dreyfuss } 1543e5f3f215SHaim Dreyfuss } 1544e5f3f215SHaim Dreyfuss iwl_pcie_d3_complete_suspend(trans, test, reset); 1545e5f3f215SHaim Dreyfuss 1546e5f3f215SHaim Dreyfuss return 0; 1547e5f3f215SHaim Dreyfuss } 1548e5f3f215SHaim Dreyfuss 1549e705c121SKalle Valo static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans, 1550e705c121SKalle Valo enum iwl_d3_status *status, 155123ae6128SMatti Gottlieb bool test, bool reset) 1552e705c121SKalle Valo { 1553d7270d61SHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1554e705c121SKalle Valo u32 val; 1555e705c121SKalle Valo int ret; 1556e705c121SKalle Valo 1557e705c121SKalle Valo if (test) { 1558e705c121SKalle Valo iwl_enable_interrupts(trans); 1559e705c121SKalle Valo *status = IWL_D3_STATUS_ALIVE; 1560e5f3f215SHaim Dreyfuss goto out; 1561e705c121SKalle Valo } 1562e705c121SKalle Valo 1563a8cbb46fSGolan Ben Ami iwl_set_bit(trans, CSR_GP_CNTRL, 1564286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_mac_access_req)); 1565e705c121SKalle Valo 15667d34a7d7SLuca Coelho ret = iwl_finish_nic_init(trans, trans->trans_cfg); 1567c96b5eecSJohannes Berg if (ret) 1568e705c121SKalle Valo return ret; 1569e705c121SKalle Valo 1570f98ad635SEmmanuel Grumbach /* 1571f98ad635SEmmanuel Grumbach * Reconfigure IVAR table in case of MSIX or reset ict table in 1572f98ad635SEmmanuel Grumbach * MSI mode since HW reset erased it. 1573f98ad635SEmmanuel Grumbach * Also enables interrupts - none will happen as 1574f98ad635SEmmanuel Grumbach * the device doesn't know we're waking it up, only when 1575f98ad635SEmmanuel Grumbach * the opmode actually tells it after this call. 1576f98ad635SEmmanuel Grumbach */ 1577f98ad635SEmmanuel Grumbach iwl_pcie_conf_msix_hw(trans_pcie); 1578f98ad635SEmmanuel Grumbach if (!trans_pcie->msix_enabled) 1579f98ad635SEmmanuel Grumbach iwl_pcie_reset_ict(trans); 1580f98ad635SEmmanuel Grumbach iwl_enable_interrupts(trans); 1581f98ad635SEmmanuel Grumbach 1582e705c121SKalle Valo iwl_pcie_set_pwr(trans, false); 1583e705c121SKalle Valo 158423ae6128SMatti Gottlieb if (!reset) { 1585e705c121SKalle Valo iwl_clear_bit(trans, CSR_GP_CNTRL, 1586286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_mac_access_req)); 1587e705c121SKalle Valo } else { 1588e705c121SKalle Valo iwl_trans_pcie_tx_reset(trans); 1589e705c121SKalle Valo 1590e705c121SKalle Valo ret = iwl_pcie_rx_init(trans); 1591e705c121SKalle Valo if (ret) { 1592e705c121SKalle Valo IWL_ERR(trans, 1593e705c121SKalle Valo "Failed to resume the device (RX reset)\n"); 1594e705c121SKalle Valo return ret; 1595e705c121SKalle Valo } 1596e705c121SKalle Valo } 1597e705c121SKalle Valo 159882ea7966SSara Sharon IWL_DEBUG_POWER(trans, "WFPM value upon resume = 0x%08X\n", 1599ea695b7cSShaul Triebitz iwl_read_umac_prph(trans, WFPM_GP2)); 160082ea7966SSara Sharon 1601e705c121SKalle Valo val = iwl_read32(trans, CSR_RESET); 1602e705c121SKalle Valo if (val & CSR_RESET_REG_FLAG_NEVO_RESET) 1603e705c121SKalle Valo *status = IWL_D3_STATUS_RESET; 1604e705c121SKalle Valo else 1605e705c121SKalle Valo *status = IWL_D3_STATUS_ALIVE; 1606e705c121SKalle Valo 1607e5f3f215SHaim Dreyfuss out: 1608e5f3f215SHaim Dreyfuss if (*status == IWL_D3_STATUS_ALIVE && 1609e5f3f215SHaim Dreyfuss trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 1610e5f3f215SHaim Dreyfuss trans_pcie->sx_complete = false; 1611e5f3f215SHaim Dreyfuss iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 1612e5f3f215SHaim Dreyfuss UREG_DOORBELL_TO_ISR6_RESUME); 1613e5f3f215SHaim Dreyfuss 1614e5f3f215SHaim Dreyfuss ret = wait_event_timeout(trans_pcie->sx_waitq, 1615e5f3f215SHaim Dreyfuss trans_pcie->sx_complete, 2 * HZ); 1616e5f3f215SHaim Dreyfuss /* 1617e5f3f215SHaim Dreyfuss * Invalidate it toward next suspend. 1618e5f3f215SHaim Dreyfuss */ 1619e5f3f215SHaim Dreyfuss trans_pcie->sx_complete = false; 1620e5f3f215SHaim Dreyfuss 1621e5f3f215SHaim Dreyfuss if (!ret) { 1622e5f3f215SHaim Dreyfuss IWL_ERR(trans, "Timeout exiting D3\n"); 1623e5f3f215SHaim Dreyfuss return -ETIMEDOUT; 1624e5f3f215SHaim Dreyfuss } 1625e5f3f215SHaim Dreyfuss } 1626e705c121SKalle Valo return 0; 1627e705c121SKalle Valo } 1628e705c121SKalle Valo 16290c18714aSLuca Coelho static void 16300c18714aSLuca Coelho iwl_pcie_set_interrupt_capa(struct pci_dev *pdev, 16310c18714aSLuca Coelho struct iwl_trans *trans, 16320c18714aSLuca Coelho const struct iwl_cfg_trans_params *cfg_trans) 16332e5d4a8fSHaim Dreyfuss { 16342e5d4a8fSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1635ab1068d6SHao Wei Tee int max_irqs, num_irqs, i, ret; 16362e5d4a8fSHaim Dreyfuss u16 pci_cmd; 16372e5d4a8fSHaim Dreyfuss 16380c18714aSLuca Coelho if (!cfg_trans->mq_rx_supported) 163906f4b081SSara Sharon goto enable_msi; 164006f4b081SSara Sharon 1641ab1068d6SHao Wei Tee max_irqs = min_t(u32, num_online_cpus() + 2, IWL_MAX_RX_HW_QUEUES); 164206f4b081SSara Sharon for (i = 0; i < max_irqs; i++) 16432e5d4a8fSHaim Dreyfuss trans_pcie->msix_entries[i].entry = i; 16442e5d4a8fSHaim Dreyfuss 164506f4b081SSara Sharon num_irqs = pci_enable_msix_range(pdev, trans_pcie->msix_entries, 16462e5d4a8fSHaim Dreyfuss MSIX_MIN_INTERRUPT_VECTORS, 164706f4b081SSara Sharon max_irqs); 164806f4b081SSara Sharon if (num_irqs < 0) { 1649496d83caSHaim Dreyfuss IWL_DEBUG_INFO(trans, 165006f4b081SSara Sharon "Failed to enable msi-x mode (ret %d). Moving to msi mode.\n", 165106f4b081SSara Sharon num_irqs); 165206f4b081SSara Sharon goto enable_msi; 1653496d83caSHaim Dreyfuss } 165406f4b081SSara Sharon trans_pcie->def_irq = (num_irqs == max_irqs) ? num_irqs - 1 : 0; 1655496d83caSHaim Dreyfuss 16562e5d4a8fSHaim Dreyfuss IWL_DEBUG_INFO(trans, 165706f4b081SSara Sharon "MSI-X enabled. %d interrupt vectors were allocated\n", 165806f4b081SSara Sharon num_irqs); 165906f4b081SSara Sharon 1660496d83caSHaim Dreyfuss /* 166106f4b081SSara Sharon * In case the OS provides fewer interrupts than requested, different 166206f4b081SSara Sharon * causes will share the same interrupt vector as follows: 1663496d83caSHaim Dreyfuss * One interrupt less: non rx causes shared with FBQ. 1664496d83caSHaim Dreyfuss * Two interrupts less: non rx causes shared with FBQ and RSS. 1665496d83caSHaim Dreyfuss * More than two interrupts: we will use fewer RSS queues. 1666496d83caSHaim Dreyfuss */ 1667ab1068d6SHao Wei Tee if (num_irqs <= max_irqs - 2) { 166806f4b081SSara Sharon trans_pcie->trans->num_rx_queues = num_irqs + 1; 1669496d83caSHaim Dreyfuss trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX | 1670496d83caSHaim Dreyfuss IWL_SHARED_IRQ_FIRST_RSS; 1671ab1068d6SHao Wei Tee } else if (num_irqs == max_irqs - 1) { 167206f4b081SSara Sharon trans_pcie->trans->num_rx_queues = num_irqs; 1673496d83caSHaim Dreyfuss trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX; 1674496d83caSHaim Dreyfuss } else { 167506f4b081SSara Sharon trans_pcie->trans->num_rx_queues = num_irqs - 1; 1676496d83caSHaim Dreyfuss } 1677ab1068d6SHao Wei Tee WARN_ON(trans_pcie->trans->num_rx_queues > IWL_MAX_RX_HW_QUEUES); 16782e5d4a8fSHaim Dreyfuss 167906f4b081SSara Sharon trans_pcie->alloc_vecs = num_irqs; 1680496d83caSHaim Dreyfuss trans_pcie->msix_enabled = true; 16812e5d4a8fSHaim Dreyfuss return; 16822e5d4a8fSHaim Dreyfuss 168306f4b081SSara Sharon enable_msi: 168406f4b081SSara Sharon ret = pci_enable_msi(pdev); 168506f4b081SSara Sharon if (ret) { 168606f4b081SSara Sharon dev_err(&pdev->dev, "pci_enable_msi failed - %d\n", ret); 16872e5d4a8fSHaim Dreyfuss /* enable rfkill interrupt: hw bug w/a */ 16882e5d4a8fSHaim Dreyfuss pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd); 16892e5d4a8fSHaim Dreyfuss if (pci_cmd & PCI_COMMAND_INTX_DISABLE) { 16902e5d4a8fSHaim Dreyfuss pci_cmd &= ~PCI_COMMAND_INTX_DISABLE; 16912e5d4a8fSHaim Dreyfuss pci_write_config_word(pdev, PCI_COMMAND, pci_cmd); 16922e5d4a8fSHaim Dreyfuss } 16932e5d4a8fSHaim Dreyfuss } 16942e5d4a8fSHaim Dreyfuss } 16952e5d4a8fSHaim Dreyfuss 16967c8d91ebSHaim Dreyfuss static void iwl_pcie_irq_set_affinity(struct iwl_trans *trans) 16977c8d91ebSHaim Dreyfuss { 16987c8d91ebSHaim Dreyfuss int iter_rx_q, i, ret, cpu, offset; 16997c8d91ebSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 17007c8d91ebSHaim Dreyfuss 17017c8d91ebSHaim Dreyfuss i = trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 0 : 1; 17027c8d91ebSHaim Dreyfuss iter_rx_q = trans_pcie->trans->num_rx_queues - 1 + i; 17037c8d91ebSHaim Dreyfuss offset = 1 + i; 17047c8d91ebSHaim Dreyfuss for (; i < iter_rx_q ; i++) { 17057c8d91ebSHaim Dreyfuss /* 17067c8d91ebSHaim Dreyfuss * Get the cpu prior to the place to search 17077c8d91ebSHaim Dreyfuss * (i.e. return will be > i - 1). 17087c8d91ebSHaim Dreyfuss */ 17097c8d91ebSHaim Dreyfuss cpu = cpumask_next(i - offset, cpu_online_mask); 17107c8d91ebSHaim Dreyfuss cpumask_set_cpu(cpu, &trans_pcie->affinity_mask[i]); 17117c8d91ebSHaim Dreyfuss ret = irq_set_affinity_hint(trans_pcie->msix_entries[i].vector, 17127c8d91ebSHaim Dreyfuss &trans_pcie->affinity_mask[i]); 17137c8d91ebSHaim Dreyfuss if (ret) 17147c8d91ebSHaim Dreyfuss IWL_ERR(trans_pcie->trans, 17157c8d91ebSHaim Dreyfuss "Failed to set affinity mask for IRQ %d\n", 17167c8d91ebSHaim Dreyfuss i); 17177c8d91ebSHaim Dreyfuss } 17187c8d91ebSHaim Dreyfuss } 17197c8d91ebSHaim Dreyfuss 17202e5d4a8fSHaim Dreyfuss static int iwl_pcie_init_msix_handler(struct pci_dev *pdev, 17212e5d4a8fSHaim Dreyfuss struct iwl_trans_pcie *trans_pcie) 17222e5d4a8fSHaim Dreyfuss { 1723496d83caSHaim Dreyfuss int i; 17242e5d4a8fSHaim Dreyfuss 1725496d83caSHaim Dreyfuss for (i = 0; i < trans_pcie->alloc_vecs; i++) { 17262e5d4a8fSHaim Dreyfuss int ret; 17275a41a86cSSharon Dvir struct msix_entry *msix_entry; 172864fa3affSSharon Dvir const char *qname = queue_name(&pdev->dev, trans_pcie, i); 172964fa3affSSharon Dvir 173064fa3affSSharon Dvir if (!qname) 173164fa3affSSharon Dvir return -ENOMEM; 17322e5d4a8fSHaim Dreyfuss 17335a41a86cSSharon Dvir msix_entry = &trans_pcie->msix_entries[i]; 17345a41a86cSSharon Dvir ret = devm_request_threaded_irq(&pdev->dev, 17355a41a86cSSharon Dvir msix_entry->vector, 17362e5d4a8fSHaim Dreyfuss iwl_pcie_msix_isr, 1737496d83caSHaim Dreyfuss (i == trans_pcie->def_irq) ? 17382e5d4a8fSHaim Dreyfuss iwl_pcie_irq_msix_handler : 17392e5d4a8fSHaim Dreyfuss iwl_pcie_irq_rx_msix_handler, 17402e5d4a8fSHaim Dreyfuss IRQF_SHARED, 174164fa3affSSharon Dvir qname, 17425a41a86cSSharon Dvir msix_entry); 17432e5d4a8fSHaim Dreyfuss if (ret) { 17442e5d4a8fSHaim Dreyfuss IWL_ERR(trans_pcie->trans, 17452e5d4a8fSHaim Dreyfuss "Error allocating IRQ %d\n", i); 17465a41a86cSSharon Dvir 17472e5d4a8fSHaim Dreyfuss return ret; 17482e5d4a8fSHaim Dreyfuss } 17492e5d4a8fSHaim Dreyfuss } 17507c8d91ebSHaim Dreyfuss iwl_pcie_irq_set_affinity(trans_pcie->trans); 17512e5d4a8fSHaim Dreyfuss 17522e5d4a8fSHaim Dreyfuss return 0; 17532e5d4a8fSHaim Dreyfuss } 17542e5d4a8fSHaim Dreyfuss 175544f61b5cSShahar S Matityahu static int iwl_trans_pcie_clear_persistence_bit(struct iwl_trans *trans) 175644f61b5cSShahar S Matityahu { 175744f61b5cSShahar S Matityahu u32 hpm, wprot; 175844f61b5cSShahar S Matityahu 1759286ca8ebSLuca Coelho switch (trans->trans_cfg->device_family) { 176044f61b5cSShahar S Matityahu case IWL_DEVICE_FAMILY_9000: 176144f61b5cSShahar S Matityahu wprot = PREG_PRPH_WPROT_9000; 176244f61b5cSShahar S Matityahu break; 176344f61b5cSShahar S Matityahu case IWL_DEVICE_FAMILY_22000: 176444f61b5cSShahar S Matityahu wprot = PREG_PRPH_WPROT_22000; 176544f61b5cSShahar S Matityahu break; 176644f61b5cSShahar S Matityahu default: 176744f61b5cSShahar S Matityahu return 0; 176844f61b5cSShahar S Matityahu } 176944f61b5cSShahar S Matityahu 177044f61b5cSShahar S Matityahu hpm = iwl_read_umac_prph_no_grab(trans, HPM_DEBUG); 177144f61b5cSShahar S Matityahu if (hpm != 0xa5a5a5a0 && (hpm & PERSISTENCE_BIT)) { 177244f61b5cSShahar S Matityahu u32 wprot_val = iwl_read_umac_prph_no_grab(trans, wprot); 177344f61b5cSShahar S Matityahu 177444f61b5cSShahar S Matityahu if (wprot_val & PREG_WFPM_ACCESS) { 177544f61b5cSShahar S Matityahu IWL_ERR(trans, 177644f61b5cSShahar S Matityahu "Error, can not clear persistence bit\n"); 177744f61b5cSShahar S Matityahu return -EPERM; 177844f61b5cSShahar S Matityahu } 177944f61b5cSShahar S Matityahu iwl_write_umac_prph_no_grab(trans, HPM_DEBUG, 178044f61b5cSShahar S Matityahu hpm & ~PERSISTENCE_BIT); 178144f61b5cSShahar S Matityahu } 178244f61b5cSShahar S Matityahu 178344f61b5cSShahar S Matityahu return 0; 178444f61b5cSShahar S Matityahu } 178544f61b5cSShahar S Matityahu 1786bab3cb92SEmmanuel Grumbach static int _iwl_trans_pcie_start_hw(struct iwl_trans *trans) 1787e705c121SKalle Valo { 1788e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1789e705c121SKalle Valo int err; 1790e705c121SKalle Valo 1791e705c121SKalle Valo lockdep_assert_held(&trans_pcie->mutex); 1792e705c121SKalle Valo 1793e705c121SKalle Valo err = iwl_pcie_prepare_card_hw(trans); 1794e705c121SKalle Valo if (err) { 1795e705c121SKalle Valo IWL_ERR(trans, "Error while preparing HW: %d\n", err); 1796e705c121SKalle Valo return err; 1797e705c121SKalle Valo } 1798e705c121SKalle Valo 179944f61b5cSShahar S Matityahu err = iwl_trans_pcie_clear_persistence_bit(trans); 180044f61b5cSShahar S Matityahu if (err) 180144f61b5cSShahar S Matityahu return err; 18028954e1ebSShahar S Matityahu 1803870c2a11SGolan Ben Ami iwl_trans_pcie_sw_reset(trans); 1804e705c121SKalle Valo 180552b6e168SEmmanuel Grumbach err = iwl_pcie_apm_init(trans); 180652b6e168SEmmanuel Grumbach if (err) 180752b6e168SEmmanuel Grumbach return err; 1808e705c121SKalle Valo 18092e5d4a8fSHaim Dreyfuss iwl_pcie_init_msix(trans_pcie); 181083730058SHaim Dreyfuss 1811e705c121SKalle Valo /* From now on, the op_mode will be kept updated about RF kill state */ 1812e705c121SKalle Valo iwl_enable_rfkill_int(trans); 1813e705c121SKalle Valo 1814326477e4SJohannes Berg trans_pcie->opmode_down = false; 1815326477e4SJohannes Berg 1816e705c121SKalle Valo /* Set is_down to false here so that...*/ 1817e705c121SKalle Valo trans_pcie->is_down = false; 1818e705c121SKalle Valo 1819e705c121SKalle Valo /* ...rfkill can call stop_device and set it false if needed */ 18209ad8fd0bSJohannes Berg iwl_pcie_check_hw_rf_kill(trans); 1821e705c121SKalle Valo 1822e705c121SKalle Valo return 0; 1823e705c121SKalle Valo } 1824e705c121SKalle Valo 1825bab3cb92SEmmanuel Grumbach static int iwl_trans_pcie_start_hw(struct iwl_trans *trans) 1826e705c121SKalle Valo { 1827e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1828e705c121SKalle Valo int ret; 1829e705c121SKalle Valo 1830e705c121SKalle Valo mutex_lock(&trans_pcie->mutex); 1831bab3cb92SEmmanuel Grumbach ret = _iwl_trans_pcie_start_hw(trans); 1832e705c121SKalle Valo mutex_unlock(&trans_pcie->mutex); 1833e705c121SKalle Valo 1834e705c121SKalle Valo return ret; 1835e705c121SKalle Valo } 1836e705c121SKalle Valo 1837e705c121SKalle Valo static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans) 1838e705c121SKalle Valo { 1839e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1840e705c121SKalle Valo 1841e705c121SKalle Valo mutex_lock(&trans_pcie->mutex); 1842e705c121SKalle Valo 1843e705c121SKalle Valo /* disable interrupts - don't enable HW RF kill interrupt */ 1844e705c121SKalle Valo iwl_disable_interrupts(trans); 1845e705c121SKalle Valo 1846e705c121SKalle Valo iwl_pcie_apm_stop(trans, true); 1847e705c121SKalle Valo 1848e705c121SKalle Valo iwl_disable_interrupts(trans); 1849e705c121SKalle Valo 1850e705c121SKalle Valo iwl_pcie_disable_ict(trans); 1851e705c121SKalle Valo 1852e705c121SKalle Valo mutex_unlock(&trans_pcie->mutex); 1853e705c121SKalle Valo 18542e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1855e705c121SKalle Valo } 1856e705c121SKalle Valo 1857e705c121SKalle Valo static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val) 1858e705c121SKalle Valo { 1859e705c121SKalle Valo writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs); 1860e705c121SKalle Valo } 1861e705c121SKalle Valo 1862e705c121SKalle Valo static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val) 1863e705c121SKalle Valo { 1864e705c121SKalle Valo writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs); 1865e705c121SKalle Valo } 1866e705c121SKalle Valo 1867e705c121SKalle Valo static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs) 1868e705c121SKalle Valo { 1869e705c121SKalle Valo return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs); 1870e705c121SKalle Valo } 1871e705c121SKalle Valo 187284fb372cSSara Sharon static u32 iwl_trans_pcie_prph_msk(struct iwl_trans *trans) 187384fb372cSSara Sharon { 18743681021fSJohannes Berg if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 187584fb372cSSara Sharon return 0x00FFFFFF; 187684fb372cSSara Sharon else 187784fb372cSSara Sharon return 0x000FFFFF; 187884fb372cSSara Sharon } 187984fb372cSSara Sharon 1880e705c121SKalle Valo static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg) 1881e705c121SKalle Valo { 188284fb372cSSara Sharon u32 mask = iwl_trans_pcie_prph_msk(trans); 188384fb372cSSara Sharon 1884e705c121SKalle Valo iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR, 188584fb372cSSara Sharon ((reg & mask) | (3 << 24))); 1886e705c121SKalle Valo return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT); 1887e705c121SKalle Valo } 1888e705c121SKalle Valo 1889e705c121SKalle Valo static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr, 1890e705c121SKalle Valo u32 val) 1891e705c121SKalle Valo { 189284fb372cSSara Sharon u32 mask = iwl_trans_pcie_prph_msk(trans); 189384fb372cSSara Sharon 1894e705c121SKalle Valo iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR, 189584fb372cSSara Sharon ((addr & mask) | (3 << 24))); 1896e705c121SKalle Valo iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val); 1897e705c121SKalle Valo } 1898e705c121SKalle Valo 1899e705c121SKalle Valo static void iwl_trans_pcie_configure(struct iwl_trans *trans, 1900e705c121SKalle Valo const struct iwl_trans_config *trans_cfg) 1901e705c121SKalle Valo { 1902e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 1903e705c121SKalle Valo 1904e705c121SKalle Valo trans_pcie->cmd_queue = trans_cfg->cmd_queue; 1905e705c121SKalle Valo trans_pcie->cmd_fifo = trans_cfg->cmd_fifo; 1906e705c121SKalle Valo trans_pcie->cmd_q_wdg_timeout = trans_cfg->cmd_q_wdg_timeout; 1907e705c121SKalle Valo if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS)) 1908e705c121SKalle Valo trans_pcie->n_no_reclaim_cmds = 0; 1909e705c121SKalle Valo else 1910e705c121SKalle Valo trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds; 1911e705c121SKalle Valo if (trans_pcie->n_no_reclaim_cmds) 1912e705c121SKalle Valo memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds, 1913e705c121SKalle Valo trans_pcie->n_no_reclaim_cmds * sizeof(u8)); 1914e705c121SKalle Valo 19156c4fbcbcSEmmanuel Grumbach trans_pcie->rx_buf_size = trans_cfg->rx_buf_size; 19166c4fbcbcSEmmanuel Grumbach trans_pcie->rx_page_order = 19176c4fbcbcSEmmanuel Grumbach iwl_trans_get_rb_size_order(trans_pcie->rx_buf_size); 191880084e35SJohannes Berg trans_pcie->rx_buf_bytes = 191980084e35SJohannes Berg iwl_trans_get_rb_size(trans_pcie->rx_buf_size); 1920cfdc20efSJohannes Berg trans_pcie->supported_dma_mask = DMA_BIT_MASK(12); 1921cfdc20efSJohannes Berg if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 1922cfdc20efSJohannes Berg trans_pcie->supported_dma_mask = DMA_BIT_MASK(11); 1923e705c121SKalle Valo 1924e705c121SKalle Valo trans_pcie->bc_table_dword = trans_cfg->bc_table_dword; 1925e705c121SKalle Valo trans_pcie->scd_set_active = trans_cfg->scd_set_active; 192641837ca9SEmmanuel Grumbach trans_pcie->sw_csum_tx = trans_cfg->sw_csum_tx; 1927e705c121SKalle Valo 192821cb3222SJohannes Berg trans_pcie->page_offs = trans_cfg->cb_data_offs; 192921cb3222SJohannes Berg trans_pcie->dev_cmd_offs = trans_cfg->cb_data_offs + sizeof(void *); 193021cb3222SJohannes Berg 193139bdb17eSSharon Dvir trans->command_groups = trans_cfg->command_groups; 193239bdb17eSSharon Dvir trans->command_groups_size = trans_cfg->command_groups_size; 193339bdb17eSSharon Dvir 1934e705c121SKalle Valo /* Initialize NAPI here - it should be before registering to mac80211 1935e705c121SKalle Valo * in the opmode but after the HW struct is allocated. 1936e705c121SKalle Valo * As this function may be called again in some corner cases don't 1937e705c121SKalle Valo * do anything if NAPI was already initialized. 1938e705c121SKalle Valo */ 1939bce97731SSara Sharon if (trans_pcie->napi_dev.reg_state != NETREG_DUMMY) 1940e705c121SKalle Valo init_dummy_netdev(&trans_pcie->napi_dev); 1941e705c121SKalle Valo } 1942e705c121SKalle Valo 1943e705c121SKalle Valo void iwl_trans_pcie_free(struct iwl_trans *trans) 1944e705c121SKalle Valo { 1945e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 19466eb5e529SEmmanuel Grumbach int i; 1947e705c121SKalle Valo 19482e5d4a8fSHaim Dreyfuss iwl_pcie_synchronize_irqs(trans); 1949e705c121SKalle Valo 1950286ca8ebSLuca Coelho if (trans->trans_cfg->gen2) 195113a3a390SSara Sharon iwl_pcie_gen2_tx_free(trans); 195213a3a390SSara Sharon else 1953e705c121SKalle Valo iwl_pcie_tx_free(trans); 1954e705c121SKalle Valo iwl_pcie_rx_free(trans); 1955e705c121SKalle Valo 195610a54d81SLuca Coelho if (trans_pcie->rba.alloc_wq) { 195710a54d81SLuca Coelho destroy_workqueue(trans_pcie->rba.alloc_wq); 195810a54d81SLuca Coelho trans_pcie->rba.alloc_wq = NULL; 195910a54d81SLuca Coelho } 196010a54d81SLuca Coelho 19612e5d4a8fSHaim Dreyfuss if (trans_pcie->msix_enabled) { 19627c8d91ebSHaim Dreyfuss for (i = 0; i < trans_pcie->alloc_vecs; i++) { 19637c8d91ebSHaim Dreyfuss irq_set_affinity_hint( 19647c8d91ebSHaim Dreyfuss trans_pcie->msix_entries[i].vector, 19657c8d91ebSHaim Dreyfuss NULL); 19667c8d91ebSHaim Dreyfuss } 19672e5d4a8fSHaim Dreyfuss 19682e5d4a8fSHaim Dreyfuss trans_pcie->msix_enabled = false; 19692e5d4a8fSHaim Dreyfuss } else { 1970e705c121SKalle Valo iwl_pcie_free_ict(trans); 19712e5d4a8fSHaim Dreyfuss } 1972e705c121SKalle Valo 1973e705c121SKalle Valo iwl_pcie_free_fw_monitor(trans); 1974e705c121SKalle Valo 19756eb5e529SEmmanuel Grumbach for_each_possible_cpu(i) { 19766eb5e529SEmmanuel Grumbach struct iwl_tso_hdr_page *p = 19776eb5e529SEmmanuel Grumbach per_cpu_ptr(trans_pcie->tso_hdr_page, i); 19786eb5e529SEmmanuel Grumbach 19796eb5e529SEmmanuel Grumbach if (p->page) 19806eb5e529SEmmanuel Grumbach __free_page(p->page); 19816eb5e529SEmmanuel Grumbach } 19826eb5e529SEmmanuel Grumbach 19836eb5e529SEmmanuel Grumbach free_percpu(trans_pcie->tso_hdr_page); 1984a2a57a35SEmmanuel Grumbach mutex_destroy(&trans_pcie->mutex); 1985e705c121SKalle Valo iwl_trans_free(trans); 1986e705c121SKalle Valo } 1987e705c121SKalle Valo 1988e705c121SKalle Valo static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state) 1989e705c121SKalle Valo { 1990e705c121SKalle Valo if (state) 1991e705c121SKalle Valo set_bit(STATUS_TPOWER_PMI, &trans->status); 1992e705c121SKalle Valo else 1993e705c121SKalle Valo clear_bit(STATUS_TPOWER_PMI, &trans->status); 1994e705c121SKalle Valo } 1995e705c121SKalle Valo 199649564a80SLuca Coelho struct iwl_trans_pcie_removal { 199749564a80SLuca Coelho struct pci_dev *pdev; 199849564a80SLuca Coelho struct work_struct work; 199949564a80SLuca Coelho }; 200049564a80SLuca Coelho 200149564a80SLuca Coelho static void iwl_trans_pcie_removal_wk(struct work_struct *wk) 200249564a80SLuca Coelho { 200349564a80SLuca Coelho struct iwl_trans_pcie_removal *removal = 200449564a80SLuca Coelho container_of(wk, struct iwl_trans_pcie_removal, work); 200549564a80SLuca Coelho struct pci_dev *pdev = removal->pdev; 2006aba1e632SColin Ian King static char *prop[] = {"EVENT=INACCESSIBLE", NULL}; 200749564a80SLuca Coelho 200849564a80SLuca Coelho dev_err(&pdev->dev, "Device gone - attempting removal\n"); 200949564a80SLuca Coelho kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, prop); 201049564a80SLuca Coelho pci_lock_rescan_remove(); 201149564a80SLuca Coelho pci_dev_put(pdev); 201249564a80SLuca Coelho pci_stop_and_remove_bus_device(pdev); 201349564a80SLuca Coelho pci_unlock_rescan_remove(); 201449564a80SLuca Coelho 201549564a80SLuca Coelho kfree(removal); 201649564a80SLuca Coelho module_put(THIS_MODULE); 201749564a80SLuca Coelho } 201849564a80SLuca Coelho 201923ba9340SEmmanuel Grumbach static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, 2020e705c121SKalle Valo unsigned long *flags) 2021e705c121SKalle Valo { 2022e705c121SKalle Valo int ret; 2023e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2024e705c121SKalle Valo 2025e705c121SKalle Valo spin_lock_irqsave(&trans_pcie->reg_lock, *flags); 2026e705c121SKalle Valo 2027e705c121SKalle Valo if (trans_pcie->cmd_hold_nic_awake) 2028e705c121SKalle Valo goto out; 2029e705c121SKalle Valo 2030e705c121SKalle Valo /* this bit wakes up the NIC */ 2031e705c121SKalle Valo __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL, 2032286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_mac_access_req)); 2033286ca8ebSLuca Coelho if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_8000) 2034e705c121SKalle Valo udelay(2); 2035e705c121SKalle Valo 2036e705c121SKalle Valo /* 2037e705c121SKalle Valo * These bits say the device is running, and should keep running for 2038e705c121SKalle Valo * at least a short while (at least as long as MAC_ACCESS_REQ stays 1), 2039e705c121SKalle Valo * but they do not indicate that embedded SRAM is restored yet; 2040fb70d49fSLuca Coelho * HW with volatile SRAM must save/restore contents to/from 2041fb70d49fSLuca Coelho * host DRAM when sleeping/waking for power-saving. 2042e705c121SKalle Valo * Each direction takes approximately 1/4 millisecond; with this 2043e705c121SKalle Valo * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a 2044e705c121SKalle Valo * series of register accesses are expected (e.g. reading Event Log), 2045e705c121SKalle Valo * to keep device from sleeping. 2046e705c121SKalle Valo * 2047e705c121SKalle Valo * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that 2048e705c121SKalle Valo * SRAM is okay/restored. We don't check that here because this call 2049fb70d49fSLuca Coelho * is just for hardware register access; but GP1 MAC_SLEEP 2050fb70d49fSLuca Coelho * check is a good idea before accessing the SRAM of HW with 2051fb70d49fSLuca Coelho * volatile SRAM (e.g. reading Event Log). 2052e705c121SKalle Valo * 2053e705c121SKalle Valo * 5000 series and later (including 1000 series) have non-volatile SRAM, 2054e705c121SKalle Valo * and do not save/restore SRAM when power cycling. 2055e705c121SKalle Valo */ 2056e705c121SKalle Valo ret = iwl_poll_bit(trans, CSR_GP_CNTRL, 2057286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_val_mac_access_en), 2058286ca8ebSLuca Coelho (BIT(trans->trans_cfg->csr->flag_mac_clock_ready) | 2059e705c121SKalle Valo CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000); 2060e705c121SKalle Valo if (unlikely(ret < 0)) { 206149564a80SLuca Coelho u32 cntrl = iwl_read32(trans, CSR_GP_CNTRL); 206249564a80SLuca Coelho 2063e705c121SKalle Valo WARN_ONCE(1, 2064e705c121SKalle Valo "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n", 206549564a80SLuca Coelho cntrl); 206649564a80SLuca Coelho 206749564a80SLuca Coelho iwl_trans_pcie_dump_regs(trans); 206849564a80SLuca Coelho 206949564a80SLuca Coelho if (iwlwifi_mod_params.remove_when_gone && cntrl == ~0U) { 207049564a80SLuca Coelho struct iwl_trans_pcie_removal *removal; 207149564a80SLuca Coelho 2072f60c9e59SEmmanuel Grumbach if (test_bit(STATUS_TRANS_DEAD, &trans->status)) 207349564a80SLuca Coelho goto err; 207449564a80SLuca Coelho 207549564a80SLuca Coelho IWL_ERR(trans, "Device gone - scheduling removal!\n"); 207649564a80SLuca Coelho 207749564a80SLuca Coelho /* 207849564a80SLuca Coelho * get a module reference to avoid doing this 207949564a80SLuca Coelho * while unloading anyway and to avoid 208049564a80SLuca Coelho * scheduling a work with code that's being 208149564a80SLuca Coelho * removed. 208249564a80SLuca Coelho */ 208349564a80SLuca Coelho if (!try_module_get(THIS_MODULE)) { 208449564a80SLuca Coelho IWL_ERR(trans, 208549564a80SLuca Coelho "Module is being unloaded - abort\n"); 208649564a80SLuca Coelho goto err; 208749564a80SLuca Coelho } 208849564a80SLuca Coelho 208949564a80SLuca Coelho removal = kzalloc(sizeof(*removal), GFP_ATOMIC); 209049564a80SLuca Coelho if (!removal) { 209149564a80SLuca Coelho module_put(THIS_MODULE); 209249564a80SLuca Coelho goto err; 209349564a80SLuca Coelho } 209449564a80SLuca Coelho /* 209549564a80SLuca Coelho * we don't need to clear this flag, because 209649564a80SLuca Coelho * the trans will be freed and reallocated. 209749564a80SLuca Coelho */ 2098f60c9e59SEmmanuel Grumbach set_bit(STATUS_TRANS_DEAD, &trans->status); 209949564a80SLuca Coelho 210049564a80SLuca Coelho removal->pdev = to_pci_dev(trans->dev); 210149564a80SLuca Coelho INIT_WORK(&removal->work, iwl_trans_pcie_removal_wk); 210249564a80SLuca Coelho pci_dev_get(removal->pdev); 210349564a80SLuca Coelho schedule_work(&removal->work); 210449564a80SLuca Coelho } else { 210549564a80SLuca Coelho iwl_write32(trans, CSR_RESET, 210649564a80SLuca Coelho CSR_RESET_REG_FLAG_FORCE_NMI); 210749564a80SLuca Coelho } 210849564a80SLuca Coelho 210949564a80SLuca Coelho err: 2110e705c121SKalle Valo spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags); 2111e705c121SKalle Valo return false; 2112e705c121SKalle Valo } 2113e705c121SKalle Valo 2114e705c121SKalle Valo out: 2115e705c121SKalle Valo /* 2116e705c121SKalle Valo * Fool sparse by faking we release the lock - sparse will 2117e705c121SKalle Valo * track nic_access anyway. 2118e705c121SKalle Valo */ 2119e705c121SKalle Valo __release(&trans_pcie->reg_lock); 2120e705c121SKalle Valo return true; 2121e705c121SKalle Valo } 2122e705c121SKalle Valo 2123e705c121SKalle Valo static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans, 2124e705c121SKalle Valo unsigned long *flags) 2125e705c121SKalle Valo { 2126e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2127e705c121SKalle Valo 2128e705c121SKalle Valo lockdep_assert_held(&trans_pcie->reg_lock); 2129e705c121SKalle Valo 2130e705c121SKalle Valo /* 2131e705c121SKalle Valo * Fool sparse by faking we acquiring the lock - sparse will 2132e705c121SKalle Valo * track nic_access anyway. 2133e705c121SKalle Valo */ 2134e705c121SKalle Valo __acquire(&trans_pcie->reg_lock); 2135e705c121SKalle Valo 2136e705c121SKalle Valo if (trans_pcie->cmd_hold_nic_awake) 2137e705c121SKalle Valo goto out; 2138e705c121SKalle Valo 2139e705c121SKalle Valo __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL, 2140286ca8ebSLuca Coelho BIT(trans->trans_cfg->csr->flag_mac_access_req)); 2141e705c121SKalle Valo /* 2142e705c121SKalle Valo * Above we read the CSR_GP_CNTRL register, which will flush 2143e705c121SKalle Valo * any previous writes, but we need the write that clears the 2144e705c121SKalle Valo * MAC_ACCESS_REQ bit to be performed before any other writes 2145e705c121SKalle Valo * scheduled on different CPUs (after we drop reg_lock). 2146e705c121SKalle Valo */ 2147e705c121SKalle Valo out: 2148e705c121SKalle Valo spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags); 2149e705c121SKalle Valo } 2150e705c121SKalle Valo 2151e705c121SKalle Valo static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr, 2152e705c121SKalle Valo void *buf, int dwords) 2153e705c121SKalle Valo { 2154e705c121SKalle Valo unsigned long flags; 2155e705c121SKalle Valo int offs, ret = 0; 2156e705c121SKalle Valo u32 *vals = buf; 2157e705c121SKalle Valo 215823ba9340SEmmanuel Grumbach if (iwl_trans_grab_nic_access(trans, &flags)) { 2159e705c121SKalle Valo iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr); 2160e705c121SKalle Valo for (offs = 0; offs < dwords; offs++) 2161e705c121SKalle Valo vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT); 2162e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 2163e705c121SKalle Valo } else { 2164e705c121SKalle Valo ret = -EBUSY; 2165e705c121SKalle Valo } 2166e705c121SKalle Valo return ret; 2167e705c121SKalle Valo } 2168e705c121SKalle Valo 2169e705c121SKalle Valo static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr, 2170e705c121SKalle Valo const void *buf, int dwords) 2171e705c121SKalle Valo { 2172e705c121SKalle Valo unsigned long flags; 2173e705c121SKalle Valo int offs, ret = 0; 2174e705c121SKalle Valo const u32 *vals = buf; 2175e705c121SKalle Valo 217623ba9340SEmmanuel Grumbach if (iwl_trans_grab_nic_access(trans, &flags)) { 2177e705c121SKalle Valo iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr); 2178e705c121SKalle Valo for (offs = 0; offs < dwords; offs++) 2179e705c121SKalle Valo iwl_write32(trans, HBUS_TARG_MEM_WDAT, 2180e705c121SKalle Valo vals ? vals[offs] : 0); 2181e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 2182e705c121SKalle Valo } else { 2183e705c121SKalle Valo ret = -EBUSY; 2184e705c121SKalle Valo } 2185e705c121SKalle Valo return ret; 2186e705c121SKalle Valo } 2187e705c121SKalle Valo 2188e705c121SKalle Valo static void iwl_trans_pcie_freeze_txq_timer(struct iwl_trans *trans, 2189e705c121SKalle Valo unsigned long txqs, 2190e705c121SKalle Valo bool freeze) 2191e705c121SKalle Valo { 2192e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2193e705c121SKalle Valo int queue; 2194e705c121SKalle Valo 2195e705c121SKalle Valo for_each_set_bit(queue, &txqs, BITS_PER_LONG) { 2196b2a3b1c1SSara Sharon struct iwl_txq *txq = trans_pcie->txq[queue]; 2197e705c121SKalle Valo unsigned long now; 2198e705c121SKalle Valo 2199e705c121SKalle Valo spin_lock_bh(&txq->lock); 2200e705c121SKalle Valo 2201e705c121SKalle Valo now = jiffies; 2202e705c121SKalle Valo 2203e705c121SKalle Valo if (txq->frozen == freeze) 2204e705c121SKalle Valo goto next_queue; 2205e705c121SKalle Valo 2206e705c121SKalle Valo IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n", 2207e705c121SKalle Valo freeze ? "Freezing" : "Waking", queue); 2208e705c121SKalle Valo 2209e705c121SKalle Valo txq->frozen = freeze; 2210e705c121SKalle Valo 2211bb98ecd4SSara Sharon if (txq->read_ptr == txq->write_ptr) 2212e705c121SKalle Valo goto next_queue; 2213e705c121SKalle Valo 2214e705c121SKalle Valo if (freeze) { 2215e705c121SKalle Valo if (unlikely(time_after(now, 2216e705c121SKalle Valo txq->stuck_timer.expires))) { 2217e705c121SKalle Valo /* 2218e705c121SKalle Valo * The timer should have fired, maybe it is 2219e705c121SKalle Valo * spinning right now on the lock. 2220e705c121SKalle Valo */ 2221e705c121SKalle Valo goto next_queue; 2222e705c121SKalle Valo } 2223e705c121SKalle Valo /* remember how long until the timer fires */ 2224e705c121SKalle Valo txq->frozen_expiry_remainder = 2225e705c121SKalle Valo txq->stuck_timer.expires - now; 2226e705c121SKalle Valo del_timer(&txq->stuck_timer); 2227e705c121SKalle Valo goto next_queue; 2228e705c121SKalle Valo } 2229e705c121SKalle Valo 2230e705c121SKalle Valo /* 2231e705c121SKalle Valo * Wake a non-empty queue -> arm timer with the 2232e705c121SKalle Valo * remainder before it froze 2233e705c121SKalle Valo */ 2234e705c121SKalle Valo mod_timer(&txq->stuck_timer, 2235e705c121SKalle Valo now + txq->frozen_expiry_remainder); 2236e705c121SKalle Valo 2237e705c121SKalle Valo next_queue: 2238e705c121SKalle Valo spin_unlock_bh(&txq->lock); 2239e705c121SKalle Valo } 2240e705c121SKalle Valo } 2241e705c121SKalle Valo 22420cd58eaaSEmmanuel Grumbach static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block) 22430cd58eaaSEmmanuel Grumbach { 22440cd58eaaSEmmanuel Grumbach struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 22450cd58eaaSEmmanuel Grumbach int i; 22460cd58eaaSEmmanuel Grumbach 2247286ca8ebSLuca Coelho for (i = 0; i < trans->trans_cfg->base_params->num_of_queues; i++) { 2248b2a3b1c1SSara Sharon struct iwl_txq *txq = trans_pcie->txq[i]; 22490cd58eaaSEmmanuel Grumbach 22500cd58eaaSEmmanuel Grumbach if (i == trans_pcie->cmd_queue) 22510cd58eaaSEmmanuel Grumbach continue; 22520cd58eaaSEmmanuel Grumbach 22530cd58eaaSEmmanuel Grumbach spin_lock_bh(&txq->lock); 22540cd58eaaSEmmanuel Grumbach 22550cd58eaaSEmmanuel Grumbach if (!block && !(WARN_ON_ONCE(!txq->block))) { 22560cd58eaaSEmmanuel Grumbach txq->block--; 22570cd58eaaSEmmanuel Grumbach if (!txq->block) { 22580cd58eaaSEmmanuel Grumbach iwl_write32(trans, HBUS_TARG_WRPTR, 2259bb98ecd4SSara Sharon txq->write_ptr | (i << 8)); 22600cd58eaaSEmmanuel Grumbach } 22610cd58eaaSEmmanuel Grumbach } else if (block) { 22620cd58eaaSEmmanuel Grumbach txq->block++; 22630cd58eaaSEmmanuel Grumbach } 22640cd58eaaSEmmanuel Grumbach 22650cd58eaaSEmmanuel Grumbach spin_unlock_bh(&txq->lock); 22660cd58eaaSEmmanuel Grumbach } 22670cd58eaaSEmmanuel Grumbach } 22680cd58eaaSEmmanuel Grumbach 2269e705c121SKalle Valo #define IWL_FLUSH_WAIT_MS 2000 2270e705c121SKalle Valo 227138398efbSSara Sharon void iwl_trans_pcie_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq) 227238398efbSSara Sharon { 2273afb84431SEmmanuel Grumbach u32 txq_id = txq->id; 2274afb84431SEmmanuel Grumbach u32 status; 2275afb84431SEmmanuel Grumbach bool active; 2276afb84431SEmmanuel Grumbach u8 fifo; 227738398efbSSara Sharon 2278286ca8ebSLuca Coelho if (trans->trans_cfg->use_tfh) { 2279afb84431SEmmanuel Grumbach IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id, 2280bb98ecd4SSara Sharon txq->read_ptr, txq->write_ptr); 2281ae79785fSSara Sharon /* TODO: access new SCD registers and dump them */ 2282ae79785fSSara Sharon return; 2283afb84431SEmmanuel Grumbach } 2284ae79785fSSara Sharon 2285afb84431SEmmanuel Grumbach status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id)); 2286afb84431SEmmanuel Grumbach fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7; 2287afb84431SEmmanuel Grumbach active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE)); 228838398efbSSara Sharon 228938398efbSSara Sharon IWL_ERR(trans, 2290afb84431SEmmanuel Grumbach "Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n", 2291afb84431SEmmanuel Grumbach txq_id, active ? "" : "in", fifo, 2292afb84431SEmmanuel Grumbach jiffies_to_msecs(txq->wd_timeout), 2293afb84431SEmmanuel Grumbach txq->read_ptr, txq->write_ptr, 2294afb84431SEmmanuel Grumbach iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) & 2295286ca8ebSLuca Coelho (trans->trans_cfg->base_params->max_tfd_queue_size - 1), 2296afb84431SEmmanuel Grumbach iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) & 2297286ca8ebSLuca Coelho (trans->trans_cfg->base_params->max_tfd_queue_size - 1), 2298afb84431SEmmanuel Grumbach iwl_read_direct32(trans, FH_TX_TRB_REG(fifo))); 229938398efbSSara Sharon } 230038398efbSSara Sharon 230192536c96SSara Sharon static int iwl_trans_pcie_rxq_dma_data(struct iwl_trans *trans, int queue, 230292536c96SSara Sharon struct iwl_trans_rxq_dma_data *data) 230392536c96SSara Sharon { 230492536c96SSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 230592536c96SSara Sharon 230692536c96SSara Sharon if (queue >= trans->num_rx_queues || !trans_pcie->rxq) 230792536c96SSara Sharon return -EINVAL; 230892536c96SSara Sharon 230992536c96SSara Sharon data->fr_bd_cb = trans_pcie->rxq[queue].bd_dma; 231092536c96SSara Sharon data->urbd_stts_wrptr = trans_pcie->rxq[queue].rb_stts_dma; 231192536c96SSara Sharon data->ur_bd_cb = trans_pcie->rxq[queue].used_bd_dma; 231292536c96SSara Sharon data->fr_bd_wid = 0; 231392536c96SSara Sharon 231492536c96SSara Sharon return 0; 231592536c96SSara Sharon } 231692536c96SSara Sharon 2317d6d517b7SSara Sharon static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, int txq_idx) 2318e705c121SKalle Valo { 2319e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2320e705c121SKalle Valo struct iwl_txq *txq; 2321e705c121SKalle Valo unsigned long now = jiffies; 23222ae48edcSSara Sharon bool overflow_tx; 2323e705c121SKalle Valo u8 wr_ptr; 2324e705c121SKalle Valo 23252b3fae66SMatt Chen /* Make sure the NIC is still alive in the bus */ 2326f60c9e59SEmmanuel Grumbach if (test_bit(STATUS_TRANS_DEAD, &trans->status)) 2327f60c9e59SEmmanuel Grumbach return -ENODEV; 23282b3fae66SMatt Chen 2329d6d517b7SSara Sharon if (!test_bit(txq_idx, trans_pcie->queue_used)) 2330d6d517b7SSara Sharon return -EINVAL; 2331e705c121SKalle Valo 2332d6d517b7SSara Sharon IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", txq_idx); 2333d6d517b7SSara Sharon txq = trans_pcie->txq[txq_idx]; 23342ae48edcSSara Sharon 23352ae48edcSSara Sharon spin_lock_bh(&txq->lock); 23362ae48edcSSara Sharon overflow_tx = txq->overflow_tx || 23372ae48edcSSara Sharon !skb_queue_empty(&txq->overflow_q); 23382ae48edcSSara Sharon spin_unlock_bh(&txq->lock); 23392ae48edcSSara Sharon 23406aa7de05SMark Rutland wr_ptr = READ_ONCE(txq->write_ptr); 2341e705c121SKalle Valo 23422ae48edcSSara Sharon while ((txq->read_ptr != READ_ONCE(txq->write_ptr) || 23432ae48edcSSara Sharon overflow_tx) && 2344e705c121SKalle Valo !time_after(jiffies, 2345e705c121SKalle Valo now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) { 23466aa7de05SMark Rutland u8 write_ptr = READ_ONCE(txq->write_ptr); 2347e705c121SKalle Valo 23482ae48edcSSara Sharon /* 23492ae48edcSSara Sharon * If write pointer moved during the wait, warn only 23502ae48edcSSara Sharon * if the TX came from op mode. In case TX came from 23512ae48edcSSara Sharon * trans layer (overflow TX) don't warn. 23522ae48edcSSara Sharon */ 23532ae48edcSSara Sharon if (WARN_ONCE(wr_ptr != write_ptr && !overflow_tx, 2354e705c121SKalle Valo "WR pointer moved while flushing %d -> %d\n", 2355e705c121SKalle Valo wr_ptr, write_ptr)) 2356e705c121SKalle Valo return -ETIMEDOUT; 23572ae48edcSSara Sharon wr_ptr = write_ptr; 23582ae48edcSSara Sharon 2359192185d6SJohannes Berg usleep_range(1000, 2000); 23602ae48edcSSara Sharon 23612ae48edcSSara Sharon spin_lock_bh(&txq->lock); 23622ae48edcSSara Sharon overflow_tx = txq->overflow_tx || 23632ae48edcSSara Sharon !skb_queue_empty(&txq->overflow_q); 23642ae48edcSSara Sharon spin_unlock_bh(&txq->lock); 2365e705c121SKalle Valo } 2366e705c121SKalle Valo 2367bb98ecd4SSara Sharon if (txq->read_ptr != txq->write_ptr) { 2368e705c121SKalle Valo IWL_ERR(trans, 2369d6d517b7SSara Sharon "fail to flush all tx fifo queues Q %d\n", txq_idx); 2370d6d517b7SSara Sharon iwl_trans_pcie_log_scd_error(trans, txq); 2371d6d517b7SSara Sharon return -ETIMEDOUT; 2372e705c121SKalle Valo } 2373e705c121SKalle Valo 2374d6d517b7SSara Sharon IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", txq_idx); 2375d6d517b7SSara Sharon 2376d6d517b7SSara Sharon return 0; 2377d6d517b7SSara Sharon } 2378d6d517b7SSara Sharon 2379d6d517b7SSara Sharon static int iwl_trans_pcie_wait_txqs_empty(struct iwl_trans *trans, u32 txq_bm) 2380d6d517b7SSara Sharon { 2381d6d517b7SSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2382d6d517b7SSara Sharon int cnt; 2383d6d517b7SSara Sharon int ret = 0; 2384d6d517b7SSara Sharon 2385d6d517b7SSara Sharon /* waiting for all the tx frames complete might take a while */ 238679b6c8feSLuca Coelho for (cnt = 0; 2387286ca8ebSLuca Coelho cnt < trans->trans_cfg->base_params->num_of_queues; 238879b6c8feSLuca Coelho cnt++) { 2389d6d517b7SSara Sharon 2390d6d517b7SSara Sharon if (cnt == trans_pcie->cmd_queue) 2391d6d517b7SSara Sharon continue; 2392d6d517b7SSara Sharon if (!test_bit(cnt, trans_pcie->queue_used)) 2393d6d517b7SSara Sharon continue; 2394d6d517b7SSara Sharon if (!(BIT(cnt) & txq_bm)) 2395d6d517b7SSara Sharon continue; 2396d6d517b7SSara Sharon 2397d6d517b7SSara Sharon ret = iwl_trans_pcie_wait_txq_empty(trans, cnt); 239838398efbSSara Sharon if (ret) 2399d6d517b7SSara Sharon break; 2400d6d517b7SSara Sharon } 2401e705c121SKalle Valo 2402e705c121SKalle Valo return ret; 2403e705c121SKalle Valo } 2404e705c121SKalle Valo 2405e705c121SKalle Valo static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg, 2406e705c121SKalle Valo u32 mask, u32 value) 2407e705c121SKalle Valo { 2408e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2409e705c121SKalle Valo unsigned long flags; 2410e705c121SKalle Valo 2411e705c121SKalle Valo spin_lock_irqsave(&trans_pcie->reg_lock, flags); 2412e705c121SKalle Valo __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value); 2413e705c121SKalle Valo spin_unlock_irqrestore(&trans_pcie->reg_lock, flags); 2414e705c121SKalle Valo } 2415e705c121SKalle Valo 2416e705c121SKalle Valo static const char *get_csr_string(int cmd) 2417e705c121SKalle Valo { 2418e705c121SKalle Valo #define IWL_CMD(x) case x: return #x 2419e705c121SKalle Valo switch (cmd) { 2420e705c121SKalle Valo IWL_CMD(CSR_HW_IF_CONFIG_REG); 2421e705c121SKalle Valo IWL_CMD(CSR_INT_COALESCING); 2422e705c121SKalle Valo IWL_CMD(CSR_INT); 2423e705c121SKalle Valo IWL_CMD(CSR_INT_MASK); 2424e705c121SKalle Valo IWL_CMD(CSR_FH_INT_STATUS); 2425e705c121SKalle Valo IWL_CMD(CSR_GPIO_IN); 2426e705c121SKalle Valo IWL_CMD(CSR_RESET); 2427e705c121SKalle Valo IWL_CMD(CSR_GP_CNTRL); 2428e705c121SKalle Valo IWL_CMD(CSR_HW_REV); 2429e705c121SKalle Valo IWL_CMD(CSR_EEPROM_REG); 2430e705c121SKalle Valo IWL_CMD(CSR_EEPROM_GP); 2431e705c121SKalle Valo IWL_CMD(CSR_OTP_GP_REG); 2432e705c121SKalle Valo IWL_CMD(CSR_GIO_REG); 2433e705c121SKalle Valo IWL_CMD(CSR_GP_UCODE_REG); 2434e705c121SKalle Valo IWL_CMD(CSR_GP_DRIVER_REG); 2435e705c121SKalle Valo IWL_CMD(CSR_UCODE_DRV_GP1); 2436e705c121SKalle Valo IWL_CMD(CSR_UCODE_DRV_GP2); 2437e705c121SKalle Valo IWL_CMD(CSR_LED_REG); 2438e705c121SKalle Valo IWL_CMD(CSR_DRAM_INT_TBL_REG); 2439e705c121SKalle Valo IWL_CMD(CSR_GIO_CHICKEN_BITS); 2440e705c121SKalle Valo IWL_CMD(CSR_ANA_PLL_CFG); 2441e705c121SKalle Valo IWL_CMD(CSR_HW_REV_WA_REG); 2442e705c121SKalle Valo IWL_CMD(CSR_MONITOR_STATUS_REG); 2443e705c121SKalle Valo IWL_CMD(CSR_DBG_HPET_MEM_REG); 2444e705c121SKalle Valo default: 2445e705c121SKalle Valo return "UNKNOWN"; 2446e705c121SKalle Valo } 2447e705c121SKalle Valo #undef IWL_CMD 2448e705c121SKalle Valo } 2449e705c121SKalle Valo 2450e705c121SKalle Valo void iwl_pcie_dump_csr(struct iwl_trans *trans) 2451e705c121SKalle Valo { 2452e705c121SKalle Valo int i; 2453e705c121SKalle Valo static const u32 csr_tbl[] = { 2454e705c121SKalle Valo CSR_HW_IF_CONFIG_REG, 2455e705c121SKalle Valo CSR_INT_COALESCING, 2456e705c121SKalle Valo CSR_INT, 2457e705c121SKalle Valo CSR_INT_MASK, 2458e705c121SKalle Valo CSR_FH_INT_STATUS, 2459e705c121SKalle Valo CSR_GPIO_IN, 2460e705c121SKalle Valo CSR_RESET, 2461e705c121SKalle Valo CSR_GP_CNTRL, 2462e705c121SKalle Valo CSR_HW_REV, 2463e705c121SKalle Valo CSR_EEPROM_REG, 2464e705c121SKalle Valo CSR_EEPROM_GP, 2465e705c121SKalle Valo CSR_OTP_GP_REG, 2466e705c121SKalle Valo CSR_GIO_REG, 2467e705c121SKalle Valo CSR_GP_UCODE_REG, 2468e705c121SKalle Valo CSR_GP_DRIVER_REG, 2469e705c121SKalle Valo CSR_UCODE_DRV_GP1, 2470e705c121SKalle Valo CSR_UCODE_DRV_GP2, 2471e705c121SKalle Valo CSR_LED_REG, 2472e705c121SKalle Valo CSR_DRAM_INT_TBL_REG, 2473e705c121SKalle Valo CSR_GIO_CHICKEN_BITS, 2474e705c121SKalle Valo CSR_ANA_PLL_CFG, 2475e705c121SKalle Valo CSR_MONITOR_STATUS_REG, 2476e705c121SKalle Valo CSR_HW_REV_WA_REG, 2477e705c121SKalle Valo CSR_DBG_HPET_MEM_REG 2478e705c121SKalle Valo }; 2479e705c121SKalle Valo IWL_ERR(trans, "CSR values:\n"); 2480e705c121SKalle Valo IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is " 2481e705c121SKalle Valo "CSR_INT_PERIODIC_REG)\n"); 2482e705c121SKalle Valo for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) { 2483e705c121SKalle Valo IWL_ERR(trans, " %25s: 0X%08x\n", 2484e705c121SKalle Valo get_csr_string(csr_tbl[i]), 2485e705c121SKalle Valo iwl_read32(trans, csr_tbl[i])); 2486e705c121SKalle Valo } 2487e705c121SKalle Valo } 2488e705c121SKalle Valo 2489e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS 2490e705c121SKalle Valo /* create and remove of files */ 2491e705c121SKalle Valo #define DEBUGFS_ADD_FILE(name, parent, mode) do { \ 2492cf5d5663SGreg Kroah-Hartman debugfs_create_file(#name, mode, parent, trans, \ 2493cf5d5663SGreg Kroah-Hartman &iwl_dbgfs_##name##_ops); \ 2494e705c121SKalle Valo } while (0) 2495e705c121SKalle Valo 2496e705c121SKalle Valo /* file operation */ 2497e705c121SKalle Valo #define DEBUGFS_READ_FILE_OPS(name) \ 2498e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = { \ 2499e705c121SKalle Valo .read = iwl_dbgfs_##name##_read, \ 2500e705c121SKalle Valo .open = simple_open, \ 2501e705c121SKalle Valo .llseek = generic_file_llseek, \ 2502e705c121SKalle Valo }; 2503e705c121SKalle Valo 2504e705c121SKalle Valo #define DEBUGFS_WRITE_FILE_OPS(name) \ 2505e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = { \ 2506e705c121SKalle Valo .write = iwl_dbgfs_##name##_write, \ 2507e705c121SKalle Valo .open = simple_open, \ 2508e705c121SKalle Valo .llseek = generic_file_llseek, \ 2509e705c121SKalle Valo }; 2510e705c121SKalle Valo 2511e705c121SKalle Valo #define DEBUGFS_READ_WRITE_FILE_OPS(name) \ 2512e705c121SKalle Valo static const struct file_operations iwl_dbgfs_##name##_ops = { \ 2513e705c121SKalle Valo .write = iwl_dbgfs_##name##_write, \ 2514e705c121SKalle Valo .read = iwl_dbgfs_##name##_read, \ 2515e705c121SKalle Valo .open = simple_open, \ 2516e705c121SKalle Valo .llseek = generic_file_llseek, \ 2517e705c121SKalle Valo }; 2518e705c121SKalle Valo 2519e705c121SKalle Valo static ssize_t iwl_dbgfs_tx_queue_read(struct file *file, 2520e705c121SKalle Valo char __user *user_buf, 2521e705c121SKalle Valo size_t count, loff_t *ppos) 2522e705c121SKalle Valo { 2523e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2524e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2525e705c121SKalle Valo struct iwl_txq *txq; 2526e705c121SKalle Valo char *buf; 2527e705c121SKalle Valo int pos = 0; 2528e705c121SKalle Valo int cnt; 2529e705c121SKalle Valo int ret; 2530e705c121SKalle Valo size_t bufsz; 2531e705c121SKalle Valo 253279b6c8feSLuca Coelho bufsz = sizeof(char) * 75 * 2533286ca8ebSLuca Coelho trans->trans_cfg->base_params->num_of_queues; 2534e705c121SKalle Valo 2535b2a3b1c1SSara Sharon if (!trans_pcie->txq_memory) 2536e705c121SKalle Valo return -EAGAIN; 2537e705c121SKalle Valo 2538e705c121SKalle Valo buf = kzalloc(bufsz, GFP_KERNEL); 2539e705c121SKalle Valo if (!buf) 2540e705c121SKalle Valo return -ENOMEM; 2541e705c121SKalle Valo 254279b6c8feSLuca Coelho for (cnt = 0; 2543286ca8ebSLuca Coelho cnt < trans->trans_cfg->base_params->num_of_queues; 254479b6c8feSLuca Coelho cnt++) { 2545b2a3b1c1SSara Sharon txq = trans_pcie->txq[cnt]; 2546e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2547e705c121SKalle Valo "hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d frozen=%d%s\n", 2548bb98ecd4SSara Sharon cnt, txq->read_ptr, txq->write_ptr, 2549e705c121SKalle Valo !!test_bit(cnt, trans_pcie->queue_used), 2550e705c121SKalle Valo !!test_bit(cnt, trans_pcie->queue_stopped), 2551e705c121SKalle Valo txq->need_update, txq->frozen, 2552e705c121SKalle Valo (cnt == trans_pcie->cmd_queue ? " HCMD" : "")); 2553e705c121SKalle Valo } 2554e705c121SKalle Valo ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos); 2555e705c121SKalle Valo kfree(buf); 2556e705c121SKalle Valo return ret; 2557e705c121SKalle Valo } 2558e705c121SKalle Valo 2559e705c121SKalle Valo static ssize_t iwl_dbgfs_rx_queue_read(struct file *file, 2560e705c121SKalle Valo char __user *user_buf, 2561e705c121SKalle Valo size_t count, loff_t *ppos) 2562e705c121SKalle Valo { 2563e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2564e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 256578485054SSara Sharon char *buf; 256678485054SSara Sharon int pos = 0, i, ret; 2567eb3dc36eSColin Ian King size_t bufsz; 2568e705c121SKalle Valo 256978485054SSara Sharon bufsz = sizeof(char) * 121 * trans->num_rx_queues; 257078485054SSara Sharon 257178485054SSara Sharon if (!trans_pcie->rxq) 257278485054SSara Sharon return -EAGAIN; 257378485054SSara Sharon 257478485054SSara Sharon buf = kzalloc(bufsz, GFP_KERNEL); 257578485054SSara Sharon if (!buf) 257678485054SSara Sharon return -ENOMEM; 257778485054SSara Sharon 257878485054SSara Sharon for (i = 0; i < trans->num_rx_queues && pos < bufsz; i++) { 257978485054SSara Sharon struct iwl_rxq *rxq = &trans_pcie->rxq[i]; 258078485054SSara Sharon 258178485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "queue#: %2d\n", 258278485054SSara Sharon i); 258378485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\tread: %u\n", 2584e705c121SKalle Valo rxq->read); 258578485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\twrite: %u\n", 2586e705c121SKalle Valo rxq->write); 258778485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\twrite_actual: %u\n", 2588e705c121SKalle Valo rxq->write_actual); 258978485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\tneed_update: %2d\n", 2590e705c121SKalle Valo rxq->need_update); 259178485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, "\tfree_count: %u\n", 2592e705c121SKalle Valo rxq->free_count); 2593e705c121SKalle Valo if (rxq->rb_stts) { 25940307c839SGolan Ben Ami u32 r = __le16_to_cpu(iwl_get_closed_rb_stts(trans, 25950307c839SGolan Ben Ami rxq)); 259678485054SSara Sharon pos += scnprintf(buf + pos, bufsz - pos, 259778485054SSara Sharon "\tclosed_rb_num: %u\n", 25980307c839SGolan Ben Ami r & 0x0FFF); 2599e705c121SKalle Valo } else { 2600e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 260178485054SSara Sharon "\tclosed_rb_num: Not Allocated\n"); 2602e705c121SKalle Valo } 260378485054SSara Sharon } 260478485054SSara Sharon ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos); 260578485054SSara Sharon kfree(buf); 260678485054SSara Sharon 260778485054SSara Sharon return ret; 2608e705c121SKalle Valo } 2609e705c121SKalle Valo 2610e705c121SKalle Valo static ssize_t iwl_dbgfs_interrupt_read(struct file *file, 2611e705c121SKalle Valo char __user *user_buf, 2612e705c121SKalle Valo size_t count, loff_t *ppos) 2613e705c121SKalle Valo { 2614e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2615e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2616e705c121SKalle Valo struct isr_statistics *isr_stats = &trans_pcie->isr_stats; 2617e705c121SKalle Valo 2618e705c121SKalle Valo int pos = 0; 2619e705c121SKalle Valo char *buf; 2620e705c121SKalle Valo int bufsz = 24 * 64; /* 24 items * 64 char per item */ 2621e705c121SKalle Valo ssize_t ret; 2622e705c121SKalle Valo 2623e705c121SKalle Valo buf = kzalloc(bufsz, GFP_KERNEL); 2624e705c121SKalle Valo if (!buf) 2625e705c121SKalle Valo return -ENOMEM; 2626e705c121SKalle Valo 2627e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2628e705c121SKalle Valo "Interrupt Statistics Report:\n"); 2629e705c121SKalle Valo 2630e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n", 2631e705c121SKalle Valo isr_stats->hw); 2632e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n", 2633e705c121SKalle Valo isr_stats->sw); 2634e705c121SKalle Valo if (isr_stats->sw || isr_stats->hw) { 2635e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2636e705c121SKalle Valo "\tLast Restarting Code: 0x%X\n", 2637e705c121SKalle Valo isr_stats->err_code); 2638e705c121SKalle Valo } 2639e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUG 2640e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n", 2641e705c121SKalle Valo isr_stats->sch); 2642e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n", 2643e705c121SKalle Valo isr_stats->alive); 2644e705c121SKalle Valo #endif 2645e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2646e705c121SKalle Valo "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill); 2647e705c121SKalle Valo 2648e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n", 2649e705c121SKalle Valo isr_stats->ctkill); 2650e705c121SKalle Valo 2651e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n", 2652e705c121SKalle Valo isr_stats->wakeup); 2653e705c121SKalle Valo 2654e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, 2655e705c121SKalle Valo "Rx command responses:\t\t %u\n", isr_stats->rx); 2656e705c121SKalle Valo 2657e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n", 2658e705c121SKalle Valo isr_stats->tx); 2659e705c121SKalle Valo 2660e705c121SKalle Valo pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n", 2661e705c121SKalle Valo isr_stats->unhandled); 2662e705c121SKalle Valo 2663e705c121SKalle Valo ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos); 2664e705c121SKalle Valo kfree(buf); 2665e705c121SKalle Valo return ret; 2666e705c121SKalle Valo } 2667e705c121SKalle Valo 2668e705c121SKalle Valo static ssize_t iwl_dbgfs_interrupt_write(struct file *file, 2669e705c121SKalle Valo const char __user *user_buf, 2670e705c121SKalle Valo size_t count, loff_t *ppos) 2671e705c121SKalle Valo { 2672e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2673e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2674e705c121SKalle Valo struct isr_statistics *isr_stats = &trans_pcie->isr_stats; 2675e705c121SKalle Valo u32 reset_flag; 2676078f1131SJohannes Berg int ret; 2677e705c121SKalle Valo 2678078f1131SJohannes Berg ret = kstrtou32_from_user(user_buf, count, 16, &reset_flag); 2679078f1131SJohannes Berg if (ret) 2680078f1131SJohannes Berg return ret; 2681e705c121SKalle Valo if (reset_flag == 0) 2682e705c121SKalle Valo memset(isr_stats, 0, sizeof(*isr_stats)); 2683e705c121SKalle Valo 2684e705c121SKalle Valo return count; 2685e705c121SKalle Valo } 2686e705c121SKalle Valo 2687e705c121SKalle Valo static ssize_t iwl_dbgfs_csr_write(struct file *file, 2688e705c121SKalle Valo const char __user *user_buf, 2689e705c121SKalle Valo size_t count, loff_t *ppos) 2690e705c121SKalle Valo { 2691e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2692e705c121SKalle Valo 2693e705c121SKalle Valo iwl_pcie_dump_csr(trans); 2694e705c121SKalle Valo 2695e705c121SKalle Valo return count; 2696e705c121SKalle Valo } 2697e705c121SKalle Valo 2698e705c121SKalle Valo static ssize_t iwl_dbgfs_fh_reg_read(struct file *file, 2699e705c121SKalle Valo char __user *user_buf, 2700e705c121SKalle Valo size_t count, loff_t *ppos) 2701e705c121SKalle Valo { 2702e705c121SKalle Valo struct iwl_trans *trans = file->private_data; 2703e705c121SKalle Valo char *buf = NULL; 2704e705c121SKalle Valo ssize_t ret; 2705e705c121SKalle Valo 2706e705c121SKalle Valo ret = iwl_dump_fh(trans, &buf); 2707e705c121SKalle Valo if (ret < 0) 2708e705c121SKalle Valo return ret; 2709e705c121SKalle Valo if (!buf) 2710e705c121SKalle Valo return -EINVAL; 2711e705c121SKalle Valo ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 2712e705c121SKalle Valo kfree(buf); 2713e705c121SKalle Valo return ret; 2714e705c121SKalle Valo } 2715e705c121SKalle Valo 2716fa4de7f7SJohannes Berg static ssize_t iwl_dbgfs_rfkill_read(struct file *file, 2717fa4de7f7SJohannes Berg char __user *user_buf, 2718fa4de7f7SJohannes Berg size_t count, loff_t *ppos) 2719fa4de7f7SJohannes Berg { 2720fa4de7f7SJohannes Berg struct iwl_trans *trans = file->private_data; 2721fa4de7f7SJohannes Berg struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2722fa4de7f7SJohannes Berg char buf[100]; 2723fa4de7f7SJohannes Berg int pos; 2724fa4de7f7SJohannes Berg 2725fa4de7f7SJohannes Berg pos = scnprintf(buf, sizeof(buf), "debug: %d\nhw: %d\n", 2726fa4de7f7SJohannes Berg trans_pcie->debug_rfkill, 2727fa4de7f7SJohannes Berg !(iwl_read32(trans, CSR_GP_CNTRL) & 2728fa4de7f7SJohannes Berg CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)); 2729fa4de7f7SJohannes Berg 2730fa4de7f7SJohannes Berg return simple_read_from_buffer(user_buf, count, ppos, buf, pos); 2731fa4de7f7SJohannes Berg } 2732fa4de7f7SJohannes Berg 2733fa4de7f7SJohannes Berg static ssize_t iwl_dbgfs_rfkill_write(struct file *file, 2734fa4de7f7SJohannes Berg const char __user *user_buf, 2735fa4de7f7SJohannes Berg size_t count, loff_t *ppos) 2736fa4de7f7SJohannes Berg { 2737fa4de7f7SJohannes Berg struct iwl_trans *trans = file->private_data; 2738fa4de7f7SJohannes Berg struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2739c5bf4fa1SJohannes Berg bool new_value; 2740fa4de7f7SJohannes Berg int ret; 2741fa4de7f7SJohannes Berg 2742c5bf4fa1SJohannes Berg ret = kstrtobool_from_user(user_buf, count, &new_value); 2743fa4de7f7SJohannes Berg if (ret) 2744fa4de7f7SJohannes Berg return ret; 2745c5bf4fa1SJohannes Berg if (new_value == trans_pcie->debug_rfkill) 2746fa4de7f7SJohannes Berg return count; 2747fa4de7f7SJohannes Berg IWL_WARN(trans, "changing debug rfkill %d->%d\n", 2748c5bf4fa1SJohannes Berg trans_pcie->debug_rfkill, new_value); 2749c5bf4fa1SJohannes Berg trans_pcie->debug_rfkill = new_value; 2750fa4de7f7SJohannes Berg iwl_pcie_handle_rfkill_irq(trans); 2751fa4de7f7SJohannes Berg 2752fa4de7f7SJohannes Berg return count; 2753fa4de7f7SJohannes Berg } 2754fa4de7f7SJohannes Berg 2755f7805b33SLior Cohen static int iwl_dbgfs_monitor_data_open(struct inode *inode, 2756f7805b33SLior Cohen struct file *file) 2757f7805b33SLior Cohen { 2758f7805b33SLior Cohen struct iwl_trans *trans = inode->i_private; 2759f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2760f7805b33SLior Cohen 276191c28b83SShahar S Matityahu if (!trans->dbg.dest_tlv || 276291c28b83SShahar S Matityahu trans->dbg.dest_tlv->monitor_mode != EXTERNAL_MODE) { 2763f7805b33SLior Cohen IWL_ERR(trans, "Debug destination is not set to DRAM\n"); 2764f7805b33SLior Cohen return -ENOENT; 2765f7805b33SLior Cohen } 2766f7805b33SLior Cohen 2767f7805b33SLior Cohen if (trans_pcie->fw_mon_data.state != IWL_FW_MON_DBGFS_STATE_CLOSED) 2768f7805b33SLior Cohen return -EBUSY; 2769f7805b33SLior Cohen 2770f7805b33SLior Cohen trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_OPEN; 2771f7805b33SLior Cohen return simple_open(inode, file); 2772f7805b33SLior Cohen } 2773f7805b33SLior Cohen 2774f7805b33SLior Cohen static int iwl_dbgfs_monitor_data_release(struct inode *inode, 2775f7805b33SLior Cohen struct file *file) 2776f7805b33SLior Cohen { 2777f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = 2778f7805b33SLior Cohen IWL_TRANS_GET_PCIE_TRANS(inode->i_private); 2779f7805b33SLior Cohen 2780f7805b33SLior Cohen if (trans_pcie->fw_mon_data.state == IWL_FW_MON_DBGFS_STATE_OPEN) 2781f7805b33SLior Cohen trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_CLOSED; 2782f7805b33SLior Cohen return 0; 2783f7805b33SLior Cohen } 2784f7805b33SLior Cohen 2785f7805b33SLior Cohen static bool iwl_write_to_user_buf(char __user *user_buf, ssize_t count, 2786f7805b33SLior Cohen void *buf, ssize_t *size, 2787f7805b33SLior Cohen ssize_t *bytes_copied) 2788f7805b33SLior Cohen { 2789f7805b33SLior Cohen int buf_size_left = count - *bytes_copied; 2790f7805b33SLior Cohen 2791f7805b33SLior Cohen buf_size_left = buf_size_left - (buf_size_left % sizeof(u32)); 2792f7805b33SLior Cohen if (*size > buf_size_left) 2793f7805b33SLior Cohen *size = buf_size_left; 2794f7805b33SLior Cohen 2795f7805b33SLior Cohen *size -= copy_to_user(user_buf, buf, *size); 2796f7805b33SLior Cohen *bytes_copied += *size; 2797f7805b33SLior Cohen 2798f7805b33SLior Cohen if (buf_size_left == *size) 2799f7805b33SLior Cohen return true; 2800f7805b33SLior Cohen return false; 2801f7805b33SLior Cohen } 2802f7805b33SLior Cohen 2803f7805b33SLior Cohen static ssize_t iwl_dbgfs_monitor_data_read(struct file *file, 2804f7805b33SLior Cohen char __user *user_buf, 2805f7805b33SLior Cohen size_t count, loff_t *ppos) 2806f7805b33SLior Cohen { 2807f7805b33SLior Cohen struct iwl_trans *trans = file->private_data; 2808f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 280969f0e505SShahar S Matityahu void *cpu_addr = (void *)trans->dbg.fw_mon.block, *curr_buf; 2810f7805b33SLior Cohen struct cont_rec *data = &trans_pcie->fw_mon_data; 2811f7805b33SLior Cohen u32 write_ptr_addr, wrap_cnt_addr, write_ptr, wrap_cnt; 2812f7805b33SLior Cohen ssize_t size, bytes_copied = 0; 2813f7805b33SLior Cohen bool b_full; 2814f7805b33SLior Cohen 281591c28b83SShahar S Matityahu if (trans->dbg.dest_tlv) { 2816f7805b33SLior Cohen write_ptr_addr = 281791c28b83SShahar S Matityahu le32_to_cpu(trans->dbg.dest_tlv->write_ptr_reg); 281891c28b83SShahar S Matityahu wrap_cnt_addr = le32_to_cpu(trans->dbg.dest_tlv->wrap_count); 2819f7805b33SLior Cohen } else { 2820f7805b33SLior Cohen write_ptr_addr = MON_BUFF_WRPTR; 2821f7805b33SLior Cohen wrap_cnt_addr = MON_BUFF_CYCLE_CNT; 2822f7805b33SLior Cohen } 2823f7805b33SLior Cohen 282491c28b83SShahar S Matityahu if (unlikely(!trans->dbg.rec_on)) 2825f7805b33SLior Cohen return 0; 2826f7805b33SLior Cohen 2827f7805b33SLior Cohen mutex_lock(&data->mutex); 2828f7805b33SLior Cohen if (data->state == 2829f7805b33SLior Cohen IWL_FW_MON_DBGFS_STATE_DISABLED) { 2830f7805b33SLior Cohen mutex_unlock(&data->mutex); 2831f7805b33SLior Cohen return 0; 2832f7805b33SLior Cohen } 2833f7805b33SLior Cohen 2834f7805b33SLior Cohen /* write_ptr position in bytes rather then DW */ 2835f7805b33SLior Cohen write_ptr = iwl_read_prph(trans, write_ptr_addr) * sizeof(u32); 2836f7805b33SLior Cohen wrap_cnt = iwl_read_prph(trans, wrap_cnt_addr); 2837f7805b33SLior Cohen 2838f7805b33SLior Cohen if (data->prev_wrap_cnt == wrap_cnt) { 2839f7805b33SLior Cohen size = write_ptr - data->prev_wr_ptr; 2840f7805b33SLior Cohen curr_buf = cpu_addr + data->prev_wr_ptr; 2841f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2842f7805b33SLior Cohen curr_buf, &size, 2843f7805b33SLior Cohen &bytes_copied); 2844f7805b33SLior Cohen data->prev_wr_ptr += size; 2845f7805b33SLior Cohen 2846f7805b33SLior Cohen } else if (data->prev_wrap_cnt == wrap_cnt - 1 && 2847f7805b33SLior Cohen write_ptr < data->prev_wr_ptr) { 284869f0e505SShahar S Matityahu size = trans->dbg.fw_mon.size - data->prev_wr_ptr; 2849f7805b33SLior Cohen curr_buf = cpu_addr + data->prev_wr_ptr; 2850f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2851f7805b33SLior Cohen curr_buf, &size, 2852f7805b33SLior Cohen &bytes_copied); 2853f7805b33SLior Cohen data->prev_wr_ptr += size; 2854f7805b33SLior Cohen 2855f7805b33SLior Cohen if (!b_full) { 2856f7805b33SLior Cohen size = write_ptr; 2857f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2858f7805b33SLior Cohen cpu_addr, &size, 2859f7805b33SLior Cohen &bytes_copied); 2860f7805b33SLior Cohen data->prev_wr_ptr = size; 2861f7805b33SLior Cohen data->prev_wrap_cnt++; 2862f7805b33SLior Cohen } 2863f7805b33SLior Cohen } else { 2864f7805b33SLior Cohen if (data->prev_wrap_cnt == wrap_cnt - 1 && 2865f7805b33SLior Cohen write_ptr > data->prev_wr_ptr) 2866f7805b33SLior Cohen IWL_WARN(trans, 2867f7805b33SLior Cohen "write pointer passed previous write pointer, start copying from the beginning\n"); 2868f7805b33SLior Cohen else if (!unlikely(data->prev_wrap_cnt == 0 && 2869f7805b33SLior Cohen data->prev_wr_ptr == 0)) 2870f7805b33SLior Cohen IWL_WARN(trans, 2871f7805b33SLior Cohen "monitor data is out of sync, start copying from the beginning\n"); 2872f7805b33SLior Cohen 2873f7805b33SLior Cohen size = write_ptr; 2874f7805b33SLior Cohen b_full = iwl_write_to_user_buf(user_buf, count, 2875f7805b33SLior Cohen cpu_addr, &size, 2876f7805b33SLior Cohen &bytes_copied); 2877f7805b33SLior Cohen data->prev_wr_ptr = size; 2878f7805b33SLior Cohen data->prev_wrap_cnt = wrap_cnt; 2879f7805b33SLior Cohen } 2880f7805b33SLior Cohen 2881f7805b33SLior Cohen mutex_unlock(&data->mutex); 2882f7805b33SLior Cohen 2883f7805b33SLior Cohen return bytes_copied; 2884f7805b33SLior Cohen } 2885f7805b33SLior Cohen 2886e705c121SKalle Valo DEBUGFS_READ_WRITE_FILE_OPS(interrupt); 2887e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(fh_reg); 2888e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(rx_queue); 2889e705c121SKalle Valo DEBUGFS_READ_FILE_OPS(tx_queue); 2890e705c121SKalle Valo DEBUGFS_WRITE_FILE_OPS(csr); 2891fa4de7f7SJohannes Berg DEBUGFS_READ_WRITE_FILE_OPS(rfkill); 2892e705c121SKalle Valo 2893f7805b33SLior Cohen static const struct file_operations iwl_dbgfs_monitor_data_ops = { 2894f7805b33SLior Cohen .read = iwl_dbgfs_monitor_data_read, 2895f7805b33SLior Cohen .open = iwl_dbgfs_monitor_data_open, 2896f7805b33SLior Cohen .release = iwl_dbgfs_monitor_data_release, 2897f7805b33SLior Cohen }; 2898f7805b33SLior Cohen 2899f8a1edb7SJohannes Berg /* Create the debugfs files and directories */ 2900cf5d5663SGreg Kroah-Hartman void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans) 2901e705c121SKalle Valo { 2902f8a1edb7SJohannes Berg struct dentry *dir = trans->dbgfs_dir; 2903f8a1edb7SJohannes Berg 29042ef00c53SJoe Perches DEBUGFS_ADD_FILE(rx_queue, dir, 0400); 29052ef00c53SJoe Perches DEBUGFS_ADD_FILE(tx_queue, dir, 0400); 29062ef00c53SJoe Perches DEBUGFS_ADD_FILE(interrupt, dir, 0600); 29072ef00c53SJoe Perches DEBUGFS_ADD_FILE(csr, dir, 0200); 29082ef00c53SJoe Perches DEBUGFS_ADD_FILE(fh_reg, dir, 0400); 29092ef00c53SJoe Perches DEBUGFS_ADD_FILE(rfkill, dir, 0600); 2910f7805b33SLior Cohen DEBUGFS_ADD_FILE(monitor_data, dir, 0400); 2911e705c121SKalle Valo } 2912f7805b33SLior Cohen 2913f7805b33SLior Cohen static void iwl_trans_pcie_debugfs_cleanup(struct iwl_trans *trans) 2914f7805b33SLior Cohen { 2915f7805b33SLior Cohen struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2916f7805b33SLior Cohen struct cont_rec *data = &trans_pcie->fw_mon_data; 2917f7805b33SLior Cohen 2918f7805b33SLior Cohen mutex_lock(&data->mutex); 2919f7805b33SLior Cohen data->state = IWL_FW_MON_DBGFS_STATE_DISABLED; 2920f7805b33SLior Cohen mutex_unlock(&data->mutex); 2921f7805b33SLior Cohen } 2922e705c121SKalle Valo #endif /*CONFIG_IWLWIFI_DEBUGFS */ 2923e705c121SKalle Valo 29246983ba69SSara Sharon static u32 iwl_trans_pcie_get_cmdlen(struct iwl_trans *trans, void *tfd) 2925e705c121SKalle Valo { 29263cd1980bSSara Sharon struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 2927e705c121SKalle Valo u32 cmdlen = 0; 2928e705c121SKalle Valo int i; 2929e705c121SKalle Valo 29303cd1980bSSara Sharon for (i = 0; i < trans_pcie->max_tbs; i++) 29316983ba69SSara Sharon cmdlen += iwl_pcie_tfd_tb_get_len(trans, tfd, i); 2932e705c121SKalle Valo 2933e705c121SKalle Valo return cmdlen; 2934e705c121SKalle Valo } 2935e705c121SKalle Valo 2936e705c121SKalle Valo static u32 iwl_trans_pcie_dump_rbs(struct iwl_trans *trans, 2937e705c121SKalle Valo struct iwl_fw_error_dump_data **data, 2938e705c121SKalle Valo int allocated_rb_nums) 2939e705c121SKalle Valo { 2940e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 294180084e35SJohannes Berg int max_len = trans_pcie->rx_buf_bytes; 294278485054SSara Sharon /* Dump RBs is supported only for pre-9000 devices (1 queue) */ 294378485054SSara Sharon struct iwl_rxq *rxq = &trans_pcie->rxq[0]; 2944e705c121SKalle Valo u32 i, r, j, rb_len = 0; 2945e705c121SKalle Valo 2946e705c121SKalle Valo spin_lock(&rxq->lock); 2947e705c121SKalle Valo 29480307c839SGolan Ben Ami r = le16_to_cpu(iwl_get_closed_rb_stts(trans, rxq)) & 0x0FFF; 2949e705c121SKalle Valo 2950e705c121SKalle Valo for (i = rxq->read, j = 0; 2951e705c121SKalle Valo i != r && j < allocated_rb_nums; 2952e705c121SKalle Valo i = (i + 1) & RX_QUEUE_MASK, j++) { 2953e705c121SKalle Valo struct iwl_rx_mem_buffer *rxb = rxq->queue[i]; 2954e705c121SKalle Valo struct iwl_fw_error_dump_rb *rb; 2955e705c121SKalle Valo 2956e705c121SKalle Valo dma_unmap_page(trans->dev, rxb->page_dma, max_len, 2957e705c121SKalle Valo DMA_FROM_DEVICE); 2958e705c121SKalle Valo 2959e705c121SKalle Valo rb_len += sizeof(**data) + sizeof(*rb) + max_len; 2960e705c121SKalle Valo 2961e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RB); 2962e705c121SKalle Valo (*data)->len = cpu_to_le32(sizeof(*rb) + max_len); 2963e705c121SKalle Valo rb = (void *)(*data)->data; 2964e705c121SKalle Valo rb->index = cpu_to_le32(i); 2965e705c121SKalle Valo memcpy(rb->data, page_address(rxb->page), max_len); 2966e705c121SKalle Valo /* remap the page for the free benefit */ 2967cfdc20efSJohannes Berg rxb->page_dma = dma_map_page(trans->dev, rxb->page, 2968cfdc20efSJohannes Berg rxb->offset, max_len, 2969e705c121SKalle Valo DMA_FROM_DEVICE); 2970e705c121SKalle Valo 2971e705c121SKalle Valo *data = iwl_fw_error_next_data(*data); 2972e705c121SKalle Valo } 2973e705c121SKalle Valo 2974e705c121SKalle Valo spin_unlock(&rxq->lock); 2975e705c121SKalle Valo 2976e705c121SKalle Valo return rb_len; 2977e705c121SKalle Valo } 2978e705c121SKalle Valo #define IWL_CSR_TO_DUMP (0x250) 2979e705c121SKalle Valo 2980e705c121SKalle Valo static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans, 2981e705c121SKalle Valo struct iwl_fw_error_dump_data **data) 2982e705c121SKalle Valo { 2983e705c121SKalle Valo u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP; 2984e705c121SKalle Valo __le32 *val; 2985e705c121SKalle Valo int i; 2986e705c121SKalle Valo 2987e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR); 2988e705c121SKalle Valo (*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP); 2989e705c121SKalle Valo val = (void *)(*data)->data; 2990e705c121SKalle Valo 2991e705c121SKalle Valo for (i = 0; i < IWL_CSR_TO_DUMP; i += 4) 2992e705c121SKalle Valo *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i)); 2993e705c121SKalle Valo 2994e705c121SKalle Valo *data = iwl_fw_error_next_data(*data); 2995e705c121SKalle Valo 2996e705c121SKalle Valo return csr_len; 2997e705c121SKalle Valo } 2998e705c121SKalle Valo 2999e705c121SKalle Valo static u32 iwl_trans_pcie_fh_regs_dump(struct iwl_trans *trans, 3000e705c121SKalle Valo struct iwl_fw_error_dump_data **data) 3001e705c121SKalle Valo { 3002e705c121SKalle Valo u32 fh_regs_len = FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND; 3003e705c121SKalle Valo unsigned long flags; 3004e705c121SKalle Valo __le32 *val; 3005e705c121SKalle Valo int i; 3006e705c121SKalle Valo 300723ba9340SEmmanuel Grumbach if (!iwl_trans_grab_nic_access(trans, &flags)) 3008e705c121SKalle Valo return 0; 3009e705c121SKalle Valo 3010e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FH_REGS); 3011e705c121SKalle Valo (*data)->len = cpu_to_le32(fh_regs_len); 3012e705c121SKalle Valo val = (void *)(*data)->data; 3013e705c121SKalle Valo 3014286ca8ebSLuca Coelho if (!trans->trans_cfg->gen2) 3015723b45e2SLiad Kaufman for (i = FH_MEM_LOWER_BOUND; i < FH_MEM_UPPER_BOUND; 3016723b45e2SLiad Kaufman i += sizeof(u32)) 3017e705c121SKalle Valo *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i)); 3018723b45e2SLiad Kaufman else 3019ea695b7cSShaul Triebitz for (i = iwl_umac_prph(trans, FH_MEM_LOWER_BOUND_GEN2); 3020ea695b7cSShaul Triebitz i < iwl_umac_prph(trans, FH_MEM_UPPER_BOUND_GEN2); 3021723b45e2SLiad Kaufman i += sizeof(u32)) 3022723b45e2SLiad Kaufman *val++ = cpu_to_le32(iwl_trans_pcie_read_prph(trans, 3023723b45e2SLiad Kaufman i)); 3024e705c121SKalle Valo 3025e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 3026e705c121SKalle Valo 3027e705c121SKalle Valo *data = iwl_fw_error_next_data(*data); 3028e705c121SKalle Valo 3029e705c121SKalle Valo return sizeof(**data) + fh_regs_len; 3030e705c121SKalle Valo } 3031e705c121SKalle Valo 3032e705c121SKalle Valo static u32 3033e705c121SKalle Valo iwl_trans_pci_dump_marbh_monitor(struct iwl_trans *trans, 3034e705c121SKalle Valo struct iwl_fw_error_dump_fw_mon *fw_mon_data, 3035e705c121SKalle Valo u32 monitor_len) 3036e705c121SKalle Valo { 3037e705c121SKalle Valo u32 buf_size_in_dwords = (monitor_len >> 2); 3038e705c121SKalle Valo u32 *buffer = (u32 *)fw_mon_data->data; 3039e705c121SKalle Valo unsigned long flags; 3040e705c121SKalle Valo u32 i; 3041e705c121SKalle Valo 304223ba9340SEmmanuel Grumbach if (!iwl_trans_grab_nic_access(trans, &flags)) 3043e705c121SKalle Valo return 0; 3044e705c121SKalle Valo 3045ea695b7cSShaul Triebitz iwl_write_umac_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x1); 3046e705c121SKalle Valo for (i = 0; i < buf_size_in_dwords; i++) 3047ea695b7cSShaul Triebitz buffer[i] = iwl_read_umac_prph_no_grab(trans, 304814ef1b43SGolan Ben-Ami MON_DMARB_RD_DATA_ADDR); 3049ea695b7cSShaul Triebitz iwl_write_umac_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x0); 3050e705c121SKalle Valo 3051e705c121SKalle Valo iwl_trans_release_nic_access(trans, &flags); 3052e705c121SKalle Valo 3053e705c121SKalle Valo return monitor_len; 3054e705c121SKalle Valo } 3055e705c121SKalle Valo 30567a14c23dSSara Sharon static void 30577a14c23dSSara Sharon iwl_trans_pcie_dump_pointers(struct iwl_trans *trans, 30587a14c23dSSara Sharon struct iwl_fw_error_dump_fw_mon *fw_mon_data) 30597a14c23dSSara Sharon { 3060c88580e1SShahar S Matityahu u32 base, base_high, write_ptr, write_ptr_val, wrap_cnt; 30617a14c23dSSara Sharon 3062286ca8ebSLuca Coelho if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 3063c88580e1SShahar S Matityahu base = DBGC_CUR_DBGBUF_BASE_ADDR_LSB; 3064c88580e1SShahar S Matityahu base_high = DBGC_CUR_DBGBUF_BASE_ADDR_MSB; 3065c88580e1SShahar S Matityahu write_ptr = DBGC_CUR_DBGBUF_STATUS; 3066c88580e1SShahar S Matityahu wrap_cnt = DBGC_DBGBUF_WRAP_AROUND; 306791c28b83SShahar S Matityahu } else if (trans->dbg.dest_tlv) { 306891c28b83SShahar S Matityahu write_ptr = le32_to_cpu(trans->dbg.dest_tlv->write_ptr_reg); 306991c28b83SShahar S Matityahu wrap_cnt = le32_to_cpu(trans->dbg.dest_tlv->wrap_count); 307091c28b83SShahar S Matityahu base = le32_to_cpu(trans->dbg.dest_tlv->base_reg); 30717a14c23dSSara Sharon } else { 30727a14c23dSSara Sharon base = MON_BUFF_BASE_ADDR; 30737a14c23dSSara Sharon write_ptr = MON_BUFF_WRPTR; 30747a14c23dSSara Sharon wrap_cnt = MON_BUFF_CYCLE_CNT; 30757a14c23dSSara Sharon } 3076c88580e1SShahar S Matityahu 3077c88580e1SShahar S Matityahu write_ptr_val = iwl_read_prph(trans, write_ptr); 30787a14c23dSSara Sharon fw_mon_data->fw_mon_cycle_cnt = 30797a14c23dSSara Sharon cpu_to_le32(iwl_read_prph(trans, wrap_cnt)); 30807a14c23dSSara Sharon fw_mon_data->fw_mon_base_ptr = 30817a14c23dSSara Sharon cpu_to_le32(iwl_read_prph(trans, base)); 3082286ca8ebSLuca Coelho if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 3083c88580e1SShahar S Matityahu fw_mon_data->fw_mon_base_high_ptr = 3084c88580e1SShahar S Matityahu cpu_to_le32(iwl_read_prph(trans, base_high)); 3085c88580e1SShahar S Matityahu write_ptr_val &= DBGC_CUR_DBGBUF_STATUS_OFFSET_MSK; 3086c88580e1SShahar S Matityahu } 3087c88580e1SShahar S Matityahu fw_mon_data->fw_mon_wr_ptr = cpu_to_le32(write_ptr_val); 30887a14c23dSSara Sharon } 30897a14c23dSSara Sharon 3090e705c121SKalle Valo static u32 3091e705c121SKalle Valo iwl_trans_pcie_dump_monitor(struct iwl_trans *trans, 3092e705c121SKalle Valo struct iwl_fw_error_dump_data **data, 3093e705c121SKalle Valo u32 monitor_len) 3094e705c121SKalle Valo { 309569f0e505SShahar S Matityahu struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon; 3096e705c121SKalle Valo u32 len = 0; 3097e705c121SKalle Valo 309891c28b83SShahar S Matityahu if (trans->dbg.dest_tlv || 309969f0e505SShahar S Matityahu (fw_mon->size && 3100286ca8ebSLuca Coelho (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_7000 || 3101286ca8ebSLuca Coelho trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210))) { 3102e705c121SKalle Valo struct iwl_fw_error_dump_fw_mon *fw_mon_data; 3103e705c121SKalle Valo 3104e705c121SKalle Valo (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR); 3105e705c121SKalle Valo fw_mon_data = (void *)(*data)->data; 31067a14c23dSSara Sharon 31077a14c23dSSara Sharon iwl_trans_pcie_dump_pointers(trans, fw_mon_data); 3108e705c121SKalle Valo 3109e705c121SKalle Valo len += sizeof(**data) + sizeof(*fw_mon_data); 311069f0e505SShahar S Matityahu if (fw_mon->size) { 311169f0e505SShahar S Matityahu memcpy(fw_mon_data->data, fw_mon->block, fw_mon->size); 311269f0e505SShahar S Matityahu monitor_len = fw_mon->size; 311391c28b83SShahar S Matityahu } else if (trans->dbg.dest_tlv->monitor_mode == SMEM_MODE) { 31147a14c23dSSara Sharon u32 base = le32_to_cpu(fw_mon_data->fw_mon_base_ptr); 3115e705c121SKalle Valo /* 3116e705c121SKalle Valo * Update pointers to reflect actual values after 3117e705c121SKalle Valo * shifting 3118e705c121SKalle Valo */ 311991c28b83SShahar S Matityahu if (trans->dbg.dest_tlv->version) { 3120fd527eb5SGolan Ben Ami base = (iwl_read_prph(trans, base) & 3121fd527eb5SGolan Ben Ami IWL_LDBG_M2S_BUF_BA_MSK) << 312291c28b83SShahar S Matityahu trans->dbg.dest_tlv->base_shift; 3123fd527eb5SGolan Ben Ami base *= IWL_M2S_UNIT_SIZE; 3124fd527eb5SGolan Ben Ami base += trans->cfg->smem_offset; 3125fd527eb5SGolan Ben Ami } else { 3126e705c121SKalle Valo base = iwl_read_prph(trans, base) << 312791c28b83SShahar S Matityahu trans->dbg.dest_tlv->base_shift; 3128fd527eb5SGolan Ben Ami } 3129fd527eb5SGolan Ben Ami 3130e705c121SKalle Valo iwl_trans_read_mem(trans, base, fw_mon_data->data, 3131e705c121SKalle Valo monitor_len / sizeof(u32)); 313291c28b83SShahar S Matityahu } else if (trans->dbg.dest_tlv->monitor_mode == MARBH_MODE) { 3133e705c121SKalle Valo monitor_len = 3134e705c121SKalle Valo iwl_trans_pci_dump_marbh_monitor(trans, 3135e705c121SKalle Valo fw_mon_data, 3136e705c121SKalle Valo monitor_len); 3137e705c121SKalle Valo } else { 3138e705c121SKalle Valo /* Didn't match anything - output no monitor data */ 3139e705c121SKalle Valo monitor_len = 0; 3140e705c121SKalle Valo } 3141e705c121SKalle Valo 3142e705c121SKalle Valo len += monitor_len; 3143e705c121SKalle Valo (*data)->len = cpu_to_le32(monitor_len + sizeof(*fw_mon_data)); 3144e705c121SKalle Valo } 3145e705c121SKalle Valo 3146e705c121SKalle Valo return len; 3147e705c121SKalle Valo } 3148e705c121SKalle Valo 314993079fd5SJohannes Berg static int iwl_trans_get_fw_monitor_len(struct iwl_trans *trans, u32 *len) 3150e705c121SKalle Valo { 315169f0e505SShahar S Matityahu if (trans->dbg.fw_mon.size) { 3152da752717SShahar S Matityahu *len += sizeof(struct iwl_fw_error_dump_data) + 3153da752717SShahar S Matityahu sizeof(struct iwl_fw_error_dump_fw_mon) + 315469f0e505SShahar S Matityahu trans->dbg.fw_mon.size; 315569f0e505SShahar S Matityahu return trans->dbg.fw_mon.size; 315691c28b83SShahar S Matityahu } else if (trans->dbg.dest_tlv) { 3157da752717SShahar S Matityahu u32 base, end, cfg_reg, monitor_len; 3158e705c121SKalle Valo 315991c28b83SShahar S Matityahu if (trans->dbg.dest_tlv->version == 1) { 316091c28b83SShahar S Matityahu cfg_reg = le32_to_cpu(trans->dbg.dest_tlv->base_reg); 3161fd527eb5SGolan Ben Ami cfg_reg = iwl_read_prph(trans, cfg_reg); 3162fd527eb5SGolan Ben Ami base = (cfg_reg & IWL_LDBG_M2S_BUF_BA_MSK) << 316391c28b83SShahar S Matityahu trans->dbg.dest_tlv->base_shift; 3164fd527eb5SGolan Ben Ami base *= IWL_M2S_UNIT_SIZE; 3165fd527eb5SGolan Ben Ami base += trans->cfg->smem_offset; 3166fd527eb5SGolan Ben Ami 3167fd527eb5SGolan Ben Ami monitor_len = 3168fd527eb5SGolan Ben Ami (cfg_reg & IWL_LDBG_M2S_BUF_SIZE_MSK) >> 316991c28b83SShahar S Matityahu trans->dbg.dest_tlv->end_shift; 3170fd527eb5SGolan Ben Ami monitor_len *= IWL_M2S_UNIT_SIZE; 3171fd527eb5SGolan Ben Ami } else { 317291c28b83SShahar S Matityahu base = le32_to_cpu(trans->dbg.dest_tlv->base_reg); 317391c28b83SShahar S Matityahu end = le32_to_cpu(trans->dbg.dest_tlv->end_reg); 3174e705c121SKalle Valo 3175e705c121SKalle Valo base = iwl_read_prph(trans, base) << 317691c28b83SShahar S Matityahu trans->dbg.dest_tlv->base_shift; 3177e705c121SKalle Valo end = iwl_read_prph(trans, end) << 317891c28b83SShahar S Matityahu trans->dbg.dest_tlv->end_shift; 3179e705c121SKalle Valo 3180e705c121SKalle Valo /* Make "end" point to the actual end */ 3181286ca8ebSLuca Coelho if (trans->trans_cfg->device_family >= 3182fd527eb5SGolan Ben Ami IWL_DEVICE_FAMILY_8000 || 318391c28b83SShahar S Matityahu trans->dbg.dest_tlv->monitor_mode == MARBH_MODE) 318491c28b83SShahar S Matityahu end += (1 << trans->dbg.dest_tlv->end_shift); 3185e705c121SKalle Valo monitor_len = end - base; 3186fd527eb5SGolan Ben Ami } 3187da752717SShahar S Matityahu *len += sizeof(struct iwl_fw_error_dump_data) + 3188da752717SShahar S Matityahu sizeof(struct iwl_fw_error_dump_fw_mon) + 3189e705c121SKalle Valo monitor_len; 3190da752717SShahar S Matityahu return monitor_len; 3191e705c121SKalle Valo } 3192da752717SShahar S Matityahu return 0; 3193da752717SShahar S Matityahu } 3194da752717SShahar S Matityahu 3195da752717SShahar S Matityahu static struct iwl_trans_dump_data 3196da752717SShahar S Matityahu *iwl_trans_pcie_dump_data(struct iwl_trans *trans, 319779f033f6SSara Sharon u32 dump_mask) 3198da752717SShahar S Matityahu { 3199da752717SShahar S Matityahu struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 3200da752717SShahar S Matityahu struct iwl_fw_error_dump_data *data; 3201da752717SShahar S Matityahu struct iwl_txq *cmdq = trans_pcie->txq[trans_pcie->cmd_queue]; 3202da752717SShahar S Matityahu struct iwl_fw_error_dump_txcmd *txcmd; 3203da752717SShahar S Matityahu struct iwl_trans_dump_data *dump_data; 3204fefbf853SShahar S Matityahu u32 len, num_rbs = 0, monitor_len = 0; 3205da752717SShahar S Matityahu int i, ptr; 3206da752717SShahar S Matityahu bool dump_rbs = test_bit(STATUS_FW_ERROR, &trans->status) && 3207286ca8ebSLuca Coelho !trans->trans_cfg->mq_rx_supported && 320879f033f6SSara Sharon dump_mask & BIT(IWL_FW_ERROR_DUMP_RB); 320979f033f6SSara Sharon 321079f033f6SSara Sharon if (!dump_mask) 321179f033f6SSara Sharon return NULL; 3212da752717SShahar S Matityahu 3213da752717SShahar S Matityahu /* transport dump header */ 3214da752717SShahar S Matityahu len = sizeof(*dump_data); 3215da752717SShahar S Matityahu 3216da752717SShahar S Matityahu /* host commands */ 3217e4eee943SShahar S Matityahu if (dump_mask & BIT(IWL_FW_ERROR_DUMP_TXCMD) && cmdq) 3218da752717SShahar S Matityahu len += sizeof(*data) + 32198672aad3SShahar S Matityahu cmdq->n_window * (sizeof(*txcmd) + 32208672aad3SShahar S Matityahu TFD_MAX_PAYLOAD_SIZE); 3221da752717SShahar S Matityahu 3222da752717SShahar S Matityahu /* FW monitor */ 3223fefbf853SShahar S Matityahu if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FW_MONITOR)) 3224da752717SShahar S Matityahu monitor_len = iwl_trans_get_fw_monitor_len(trans, &len); 3225e705c121SKalle Valo 3226e705c121SKalle Valo /* CSR registers */ 322779f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_CSR)) 3228e705c121SKalle Valo len += sizeof(*data) + IWL_CSR_TO_DUMP; 3229e705c121SKalle Valo 3230e705c121SKalle Valo /* FH registers */ 323179f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FH_REGS)) { 3232286ca8ebSLuca Coelho if (trans->trans_cfg->gen2) 3233723b45e2SLiad Kaufman len += sizeof(*data) + 3234ea695b7cSShaul Triebitz (iwl_umac_prph(trans, FH_MEM_UPPER_BOUND_GEN2) - 3235ea695b7cSShaul Triebitz iwl_umac_prph(trans, FH_MEM_LOWER_BOUND_GEN2)); 3236723b45e2SLiad Kaufman else 3237723b45e2SLiad Kaufman len += sizeof(*data) + 3238520f03eaSShahar S Matityahu (FH_MEM_UPPER_BOUND - 3239520f03eaSShahar S Matityahu FH_MEM_LOWER_BOUND); 3240520f03eaSShahar S Matityahu } 3241e705c121SKalle Valo 3242e705c121SKalle Valo if (dump_rbs) { 324378485054SSara Sharon /* Dump RBs is supported only for pre-9000 devices (1 queue) */ 324478485054SSara Sharon struct iwl_rxq *rxq = &trans_pcie->rxq[0]; 3245e705c121SKalle Valo /* RBs */ 32460307c839SGolan Ben Ami num_rbs = 32470307c839SGolan Ben Ami le16_to_cpu(iwl_get_closed_rb_stts(trans, rxq)) 3248e705c121SKalle Valo & 0x0FFF; 324978485054SSara Sharon num_rbs = (num_rbs - rxq->read) & RX_QUEUE_MASK; 3250e705c121SKalle Valo len += num_rbs * (sizeof(*data) + 3251e705c121SKalle Valo sizeof(struct iwl_fw_error_dump_rb) + 3252e705c121SKalle Valo (PAGE_SIZE << trans_pcie->rx_page_order)); 3253e705c121SKalle Valo } 3254e705c121SKalle Valo 32555538409bSLiad Kaufman /* Paged memory for gen2 HW */ 3256286ca8ebSLuca Coelho if (trans->trans_cfg->gen2 && dump_mask & BIT(IWL_FW_ERROR_DUMP_PAGING)) 3257505a00c0SShahar S Matityahu for (i = 0; i < trans->init_dram.paging_cnt; i++) 32585538409bSLiad Kaufman len += sizeof(*data) + 32595538409bSLiad Kaufman sizeof(struct iwl_fw_error_dump_paging) + 3260505a00c0SShahar S Matityahu trans->init_dram.paging[i].size; 32615538409bSLiad Kaufman 3262e705c121SKalle Valo dump_data = vzalloc(len); 3263e705c121SKalle Valo if (!dump_data) 3264e705c121SKalle Valo return NULL; 3265e705c121SKalle Valo 3266e705c121SKalle Valo len = 0; 3267e705c121SKalle Valo data = (void *)dump_data->data; 3268520f03eaSShahar S Matityahu 3269e4eee943SShahar S Matityahu if (dump_mask & BIT(IWL_FW_ERROR_DUMP_TXCMD) && cmdq) { 3270520f03eaSShahar S Matityahu u16 tfd_size = trans_pcie->tfd_size; 3271520f03eaSShahar S Matityahu 3272e705c121SKalle Valo data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD); 3273e705c121SKalle Valo txcmd = (void *)data->data; 3274e705c121SKalle Valo spin_lock_bh(&cmdq->lock); 3275bb98ecd4SSara Sharon ptr = cmdq->write_ptr; 3276bb98ecd4SSara Sharon for (i = 0; i < cmdq->n_window; i++) { 32774ecab561SEmmanuel Grumbach u8 idx = iwl_pcie_get_cmd_index(cmdq, ptr); 327808326a97SJohannes Berg u8 tfdidx; 3279e705c121SKalle Valo u32 caplen, cmdlen; 3280e705c121SKalle Valo 328108326a97SJohannes Berg if (trans->trans_cfg->use_tfh) 328208326a97SJohannes Berg tfdidx = idx; 328308326a97SJohannes Berg else 328408326a97SJohannes Berg tfdidx = ptr; 328508326a97SJohannes Berg 3286520f03eaSShahar S Matityahu cmdlen = iwl_trans_pcie_get_cmdlen(trans, 328708326a97SJohannes Berg (u8 *)cmdq->tfds + 328808326a97SJohannes Berg tfd_size * tfdidx); 3289e705c121SKalle Valo caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen); 3290e705c121SKalle Valo 3291e705c121SKalle Valo if (cmdlen) { 3292e705c121SKalle Valo len += sizeof(*txcmd) + caplen; 3293e705c121SKalle Valo txcmd->cmdlen = cpu_to_le32(cmdlen); 3294e705c121SKalle Valo txcmd->caplen = cpu_to_le32(caplen); 3295520f03eaSShahar S Matityahu memcpy(txcmd->data, cmdq->entries[idx].cmd, 3296520f03eaSShahar S Matityahu caplen); 3297e705c121SKalle Valo txcmd = (void *)((u8 *)txcmd->data + caplen); 3298e705c121SKalle Valo } 3299e705c121SKalle Valo 33007b3e42eaSGolan Ben Ami ptr = iwl_queue_dec_wrap(trans, ptr); 3301e705c121SKalle Valo } 3302e705c121SKalle Valo spin_unlock_bh(&cmdq->lock); 3303e705c121SKalle Valo 3304e705c121SKalle Valo data->len = cpu_to_le32(len); 3305e705c121SKalle Valo len += sizeof(*data); 3306e705c121SKalle Valo data = iwl_fw_error_next_data(data); 3307520f03eaSShahar S Matityahu } 3308e705c121SKalle Valo 330979f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_CSR)) 3310e705c121SKalle Valo len += iwl_trans_pcie_dump_csr(trans, &data); 331179f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FH_REGS)) 3312e705c121SKalle Valo len += iwl_trans_pcie_fh_regs_dump(trans, &data); 3313e705c121SKalle Valo if (dump_rbs) 3314e705c121SKalle Valo len += iwl_trans_pcie_dump_rbs(trans, &data, num_rbs); 3315e705c121SKalle Valo 33165538409bSLiad Kaufman /* Paged memory for gen2 HW */ 3317286ca8ebSLuca Coelho if (trans->trans_cfg->gen2 && 331879b6c8feSLuca Coelho dump_mask & BIT(IWL_FW_ERROR_DUMP_PAGING)) { 3319505a00c0SShahar S Matityahu for (i = 0; i < trans->init_dram.paging_cnt; i++) { 33205538409bSLiad Kaufman struct iwl_fw_error_dump_paging *paging; 3321505a00c0SShahar S Matityahu u32 page_len = trans->init_dram.paging[i].size; 33225538409bSLiad Kaufman 33235538409bSLiad Kaufman data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PAGING); 33245538409bSLiad Kaufman data->len = cpu_to_le32(sizeof(*paging) + page_len); 33255538409bSLiad Kaufman paging = (void *)data->data; 33265538409bSLiad Kaufman paging->index = cpu_to_le32(i); 33275538409bSLiad Kaufman memcpy(paging->data, 3328505a00c0SShahar S Matityahu trans->init_dram.paging[i].block, page_len); 33295538409bSLiad Kaufman data = iwl_fw_error_next_data(data); 33305538409bSLiad Kaufman 33315538409bSLiad Kaufman len += sizeof(*data) + sizeof(*paging) + page_len; 33325538409bSLiad Kaufman } 33335538409bSLiad Kaufman } 333479f033f6SSara Sharon if (dump_mask & BIT(IWL_FW_ERROR_DUMP_FW_MONITOR)) 3335e705c121SKalle Valo len += iwl_trans_pcie_dump_monitor(trans, &data, monitor_len); 3336e705c121SKalle Valo 3337e705c121SKalle Valo dump_data->len = len; 3338e705c121SKalle Valo 3339e705c121SKalle Valo return dump_data; 3340e705c121SKalle Valo } 3341e705c121SKalle Valo 33424cbb8e50SLuciano Coelho #ifdef CONFIG_PM_SLEEP 33434cbb8e50SLuciano Coelho static int iwl_trans_pcie_suspend(struct iwl_trans *trans) 33444cbb8e50SLuciano Coelho { 33454cbb8e50SLuciano Coelho return 0; 33464cbb8e50SLuciano Coelho } 33474cbb8e50SLuciano Coelho 33484cbb8e50SLuciano Coelho static void iwl_trans_pcie_resume(struct iwl_trans *trans) 33494cbb8e50SLuciano Coelho { 33504cbb8e50SLuciano Coelho } 33514cbb8e50SLuciano Coelho #endif /* CONFIG_PM_SLEEP */ 33524cbb8e50SLuciano Coelho 3353623e7766SSara Sharon #define IWL_TRANS_COMMON_OPS \ 3354623e7766SSara Sharon .op_mode_leave = iwl_trans_pcie_op_mode_leave, \ 3355623e7766SSara Sharon .write8 = iwl_trans_pcie_write8, \ 3356623e7766SSara Sharon .write32 = iwl_trans_pcie_write32, \ 3357623e7766SSara Sharon .read32 = iwl_trans_pcie_read32, \ 3358623e7766SSara Sharon .read_prph = iwl_trans_pcie_read_prph, \ 3359623e7766SSara Sharon .write_prph = iwl_trans_pcie_write_prph, \ 3360623e7766SSara Sharon .read_mem = iwl_trans_pcie_read_mem, \ 3361623e7766SSara Sharon .write_mem = iwl_trans_pcie_write_mem, \ 3362623e7766SSara Sharon .configure = iwl_trans_pcie_configure, \ 3363623e7766SSara Sharon .set_pmi = iwl_trans_pcie_set_pmi, \ 3364870c2a11SGolan Ben Ami .sw_reset = iwl_trans_pcie_sw_reset, \ 3365623e7766SSara Sharon .grab_nic_access = iwl_trans_pcie_grab_nic_access, \ 3366623e7766SSara Sharon .release_nic_access = iwl_trans_pcie_release_nic_access, \ 3367623e7766SSara Sharon .set_bits_mask = iwl_trans_pcie_set_bits_mask, \ 3368623e7766SSara Sharon .dump_data = iwl_trans_pcie_dump_data, \ 3369623e7766SSara Sharon .d3_suspend = iwl_trans_pcie_d3_suspend, \ 3370d1967ce6SShahar S Matityahu .d3_resume = iwl_trans_pcie_d3_resume, \ 3371d1967ce6SShahar S Matityahu .sync_nmi = iwl_trans_pcie_sync_nmi 3372623e7766SSara Sharon 3373623e7766SSara Sharon #ifdef CONFIG_PM_SLEEP 3374623e7766SSara Sharon #define IWL_TRANS_PM_OPS \ 3375623e7766SSara Sharon .suspend = iwl_trans_pcie_suspend, \ 3376623e7766SSara Sharon .resume = iwl_trans_pcie_resume, 3377623e7766SSara Sharon #else 3378623e7766SSara Sharon #define IWL_TRANS_PM_OPS 3379623e7766SSara Sharon #endif /* CONFIG_PM_SLEEP */ 3380623e7766SSara Sharon 3381e705c121SKalle Valo static const struct iwl_trans_ops trans_ops_pcie = { 3382623e7766SSara Sharon IWL_TRANS_COMMON_OPS, 3383623e7766SSara Sharon IWL_TRANS_PM_OPS 3384e705c121SKalle Valo .start_hw = iwl_trans_pcie_start_hw, 3385e705c121SKalle Valo .fw_alive = iwl_trans_pcie_fw_alive, 3386e705c121SKalle Valo .start_fw = iwl_trans_pcie_start_fw, 3387e705c121SKalle Valo .stop_device = iwl_trans_pcie_stop_device, 3388e705c121SKalle Valo 3389e705c121SKalle Valo .send_cmd = iwl_trans_pcie_send_hcmd, 3390e705c121SKalle Valo 3391e705c121SKalle Valo .tx = iwl_trans_pcie_tx, 3392e705c121SKalle Valo .reclaim = iwl_trans_pcie_reclaim, 3393e705c121SKalle Valo 3394e705c121SKalle Valo .txq_disable = iwl_trans_pcie_txq_disable, 3395e705c121SKalle Valo .txq_enable = iwl_trans_pcie_txq_enable, 3396e705c121SKalle Valo 339742db09c1SLiad Kaufman .txq_set_shared_mode = iwl_trans_pcie_txq_set_shared_mode, 339842db09c1SLiad Kaufman 3399d6d517b7SSara Sharon .wait_tx_queues_empty = iwl_trans_pcie_wait_txqs_empty, 3400d6d517b7SSara Sharon 3401e705c121SKalle Valo .freeze_txq_timer = iwl_trans_pcie_freeze_txq_timer, 34020cd58eaaSEmmanuel Grumbach .block_txq_ptrs = iwl_trans_pcie_block_txq_ptrs, 3403f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS 3404f7805b33SLior Cohen .debugfs_cleanup = iwl_trans_pcie_debugfs_cleanup, 3405f7805b33SLior Cohen #endif 3406623e7766SSara Sharon }; 3407e705c121SKalle Valo 3408623e7766SSara Sharon static const struct iwl_trans_ops trans_ops_pcie_gen2 = { 3409623e7766SSara Sharon IWL_TRANS_COMMON_OPS, 3410623e7766SSara Sharon IWL_TRANS_PM_OPS 3411623e7766SSara Sharon .start_hw = iwl_trans_pcie_start_hw, 3412eda50cdeSSara Sharon .fw_alive = iwl_trans_pcie_gen2_fw_alive, 3413eda50cdeSSara Sharon .start_fw = iwl_trans_pcie_gen2_start_fw, 341477c09bc8SSara Sharon .stop_device = iwl_trans_pcie_gen2_stop_device, 3415e705c121SKalle Valo 3416ca60da2eSSara Sharon .send_cmd = iwl_trans_pcie_gen2_send_hcmd, 3417e705c121SKalle Valo 3418ab6c6445SSara Sharon .tx = iwl_trans_pcie_gen2_tx, 3419623e7766SSara Sharon .reclaim = iwl_trans_pcie_reclaim, 3420623e7766SSara Sharon 3421ba7136f3SAlex Malamud .set_q_ptrs = iwl_trans_pcie_set_q_ptrs, 3422ba7136f3SAlex Malamud 34236b35ff91SSara Sharon .txq_alloc = iwl_trans_pcie_dyn_txq_alloc, 34246b35ff91SSara Sharon .txq_free = iwl_trans_pcie_dyn_txq_free, 3425d6d517b7SSara Sharon .wait_txq_empty = iwl_trans_pcie_wait_txq_empty, 342692536c96SSara Sharon .rxq_dma_data = iwl_trans_pcie_rxq_dma_data, 3427f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS 3428f7805b33SLior Cohen .debugfs_cleanup = iwl_trans_pcie_debugfs_cleanup, 3429f7805b33SLior Cohen #endif 3430e705c121SKalle Valo }; 3431e705c121SKalle Valo 3432e705c121SKalle Valo struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev, 3433e705c121SKalle Valo const struct pci_device_id *ent, 34347e8258c0SLuca Coelho const struct iwl_cfg_trans_params *cfg_trans) 3435e705c121SKalle Valo { 3436e705c121SKalle Valo struct iwl_trans_pcie *trans_pcie; 3437e705c121SKalle Valo struct iwl_trans *trans; 343896a6497bSSara Sharon int ret, addr_size; 3439e705c121SKalle Valo 34405a41a86cSSharon Dvir ret = pcim_enable_device(pdev); 34415a41a86cSSharon Dvir if (ret) 34425a41a86cSSharon Dvir return ERR_PTR(ret); 34435a41a86cSSharon Dvir 34447e8258c0SLuca Coelho if (cfg_trans->gen2) 3445623e7766SSara Sharon trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie), 34460c18714aSLuca Coelho &pdev->dev, &trans_ops_pcie_gen2); 3447623e7766SSara Sharon else 3448e705c121SKalle Valo trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie), 34490c18714aSLuca Coelho &pdev->dev, &trans_ops_pcie); 34500c18714aSLuca Coelho 3451e705c121SKalle Valo if (!trans) 3452e705c121SKalle Valo return ERR_PTR(-ENOMEM); 3453e705c121SKalle Valo 3454e705c121SKalle Valo trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 3455e705c121SKalle Valo 3456e705c121SKalle Valo trans_pcie->trans = trans; 3457326477e4SJohannes Berg trans_pcie->opmode_down = true; 3458e705c121SKalle Valo spin_lock_init(&trans_pcie->irq_lock); 3459e705c121SKalle Valo spin_lock_init(&trans_pcie->reg_lock); 3460cfdc20efSJohannes Berg spin_lock_init(&trans_pcie->alloc_page_lock); 3461e705c121SKalle Valo mutex_init(&trans_pcie->mutex); 3462e705c121SKalle Valo init_waitqueue_head(&trans_pcie->ucode_write_waitq); 34638188a18eSJohannes Berg 34648188a18eSJohannes Berg trans_pcie->rba.alloc_wq = alloc_workqueue("rb_allocator", 34658188a18eSJohannes Berg WQ_HIGHPRI | WQ_UNBOUND, 1); 34668188a18eSJohannes Berg if (!trans_pcie->rba.alloc_wq) { 34678188a18eSJohannes Berg ret = -ENOMEM; 34688188a18eSJohannes Berg goto out_free_trans; 34698188a18eSJohannes Berg } 34708188a18eSJohannes Berg INIT_WORK(&trans_pcie->rba.rx_alloc, iwl_pcie_rx_allocator_work); 34718188a18eSJohannes Berg 34726eb5e529SEmmanuel Grumbach trans_pcie->tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page); 34736eb5e529SEmmanuel Grumbach if (!trans_pcie->tso_hdr_page) { 34746eb5e529SEmmanuel Grumbach ret = -ENOMEM; 34756eb5e529SEmmanuel Grumbach goto out_no_pci; 34766eb5e529SEmmanuel Grumbach } 3477c5bf4fa1SJohannes Berg trans_pcie->debug_rfkill = -1; 3478e705c121SKalle Valo 34797e8258c0SLuca Coelho if (!cfg_trans->base_params->pcie_l1_allowed) { 3480e705c121SKalle Valo /* 3481e705c121SKalle Valo * W/A - seems to solve weird behavior. We need to remove this 3482e705c121SKalle Valo * if we don't want to stay in L1 all the time. This wastes a 3483e705c121SKalle Valo * lot of power. 3484e705c121SKalle Valo */ 3485e705c121SKalle Valo pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | 3486e705c121SKalle Valo PCIE_LINK_STATE_L1 | 3487e705c121SKalle Valo PCIE_LINK_STATE_CLKPM); 3488e705c121SKalle Valo } 3489e705c121SKalle Valo 34909416560eSGolan Ben Ami trans_pcie->def_rx_queue = 0; 34919416560eSGolan Ben Ami 34927e8258c0SLuca Coelho if (cfg_trans->use_tfh) { 34932c6262b7SSara Sharon addr_size = 64; 34943cd1980bSSara Sharon trans_pcie->max_tbs = IWL_TFH_NUM_TBS; 34958352e62aSSara Sharon trans_pcie->tfd_size = sizeof(struct iwl_tfh_tfd); 34966983ba69SSara Sharon } else { 34972c6262b7SSara Sharon addr_size = 36; 34983cd1980bSSara Sharon trans_pcie->max_tbs = IWL_NUM_OF_TBS; 34996983ba69SSara Sharon trans_pcie->tfd_size = sizeof(struct iwl_tfd); 35006983ba69SSara Sharon } 35013cd1980bSSara Sharon trans->max_skb_frags = IWL_PCIE_MAX_FRAGS(trans_pcie); 35023cd1980bSSara Sharon 3503e705c121SKalle Valo pci_set_master(pdev); 3504e705c121SKalle Valo 350596a6497bSSara Sharon ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(addr_size)); 3506e705c121SKalle Valo if (!ret) 350796a6497bSSara Sharon ret = pci_set_consistent_dma_mask(pdev, 350896a6497bSSara Sharon DMA_BIT_MASK(addr_size)); 3509e705c121SKalle Valo if (ret) { 3510e705c121SKalle Valo ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 3511e705c121SKalle Valo if (!ret) 3512e705c121SKalle Valo ret = pci_set_consistent_dma_mask(pdev, 3513e705c121SKalle Valo DMA_BIT_MASK(32)); 3514e705c121SKalle Valo /* both attempts failed: */ 3515e705c121SKalle Valo if (ret) { 3516e705c121SKalle Valo dev_err(&pdev->dev, "No suitable DMA available\n"); 35175a41a86cSSharon Dvir goto out_no_pci; 3518e705c121SKalle Valo } 3519e705c121SKalle Valo } 3520e705c121SKalle Valo 35215a41a86cSSharon Dvir ret = pcim_iomap_regions_request_all(pdev, BIT(0), DRV_NAME); 3522e705c121SKalle Valo if (ret) { 35235a41a86cSSharon Dvir dev_err(&pdev->dev, "pcim_iomap_regions_request_all failed\n"); 35245a41a86cSSharon Dvir goto out_no_pci; 3525e705c121SKalle Valo } 3526e705c121SKalle Valo 35275a41a86cSSharon Dvir trans_pcie->hw_base = pcim_iomap_table(pdev)[0]; 3528e705c121SKalle Valo if (!trans_pcie->hw_base) { 35295a41a86cSSharon Dvir dev_err(&pdev->dev, "pcim_iomap_table failed\n"); 3530e705c121SKalle Valo ret = -ENODEV; 35315a41a86cSSharon Dvir goto out_no_pci; 3532e705c121SKalle Valo } 3533e705c121SKalle Valo 3534e705c121SKalle Valo /* We disable the RETRY_TIMEOUT register (0x41) to keep 3535e705c121SKalle Valo * PCI Tx retries from interfering with C3 CPU state */ 3536e705c121SKalle Valo pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00); 3537e705c121SKalle Valo 3538e705c121SKalle Valo trans_pcie->pci_dev = pdev; 3539e705c121SKalle Valo iwl_disable_interrupts(trans); 3540e705c121SKalle Valo 3541e705c121SKalle Valo trans->hw_rev = iwl_read32(trans, CSR_HW_REV); 35429a098a89SRajat Jain if (trans->hw_rev == 0xffffffff) { 35439a098a89SRajat Jain dev_err(&pdev->dev, "HW_REV=0xFFFFFFFF, PCI issues?\n"); 35449a098a89SRajat Jain ret = -EIO; 35459a098a89SRajat Jain goto out_no_pci; 35469a098a89SRajat Jain } 35479a098a89SRajat Jain 3548e705c121SKalle Valo /* 3549e705c121SKalle Valo * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have 3550e705c121SKalle Valo * changed, and now the revision step also includes bit 0-1 (no more 3551e705c121SKalle Valo * "dash" value). To keep hw_rev backwards compatible - we'll store it 3552e705c121SKalle Valo * in the old format. 3553e705c121SKalle Valo */ 35547e8258c0SLuca Coelho if (cfg_trans->device_family >= IWL_DEVICE_FAMILY_8000) { 3555e705c121SKalle Valo trans->hw_rev = (trans->hw_rev & 0xfff0) | 3556e705c121SKalle Valo (CSR_HW_REV_STEP(trans->hw_rev << 2) << 2); 3557e705c121SKalle Valo 3558e705c121SKalle Valo ret = iwl_pcie_prepare_card_hw(trans); 3559e705c121SKalle Valo if (ret) { 3560e705c121SKalle Valo IWL_WARN(trans, "Exit HW not ready\n"); 35615a41a86cSSharon Dvir goto out_no_pci; 3562e705c121SKalle Valo } 3563e705c121SKalle Valo 3564e705c121SKalle Valo /* 3565e705c121SKalle Valo * in-order to recognize C step driver should read chip version 3566e705c121SKalle Valo * id located at the AUX bus MISC address space. 3567e705c121SKalle Valo */ 35687e8258c0SLuca Coelho ret = iwl_finish_nic_init(trans, cfg_trans); 3569c96b5eecSJohannes Berg if (ret) 35705a41a86cSSharon Dvir goto out_no_pci; 3571e705c121SKalle Valo 3572e705c121SKalle Valo } 3573e705c121SKalle Valo 357499be6166SLuca Coelho IWL_DEBUG_INFO(trans, "HW REV: 0x%0x\n", trans->hw_rev); 357599be6166SLuca Coelho 35767e8258c0SLuca Coelho iwl_pcie_set_interrupt_capa(pdev, trans, cfg_trans); 3577e705c121SKalle Valo trans->hw_id = (pdev->device << 16) + pdev->subsystem_device; 3578e705c121SKalle Valo snprintf(trans->hw_id_str, sizeof(trans->hw_id_str), 3579e705c121SKalle Valo "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device); 3580e705c121SKalle Valo 3581e705c121SKalle Valo /* Initialize the wait queue for commands */ 3582e705c121SKalle Valo init_waitqueue_head(&trans_pcie->wait_command_queue); 3583e705c121SKalle Valo 3584e5f3f215SHaim Dreyfuss init_waitqueue_head(&trans_pcie->sx_waitq); 3585e5f3f215SHaim Dreyfuss 35862e5d4a8fSHaim Dreyfuss if (trans_pcie->msix_enabled) { 35872388bd7bSDan Carpenter ret = iwl_pcie_init_msix_handler(pdev, trans_pcie); 35882388bd7bSDan Carpenter if (ret) 35895a41a86cSSharon Dvir goto out_no_pci; 35902e5d4a8fSHaim Dreyfuss } else { 3591e705c121SKalle Valo ret = iwl_pcie_alloc_ict(trans); 3592e705c121SKalle Valo if (ret) 35935a41a86cSSharon Dvir goto out_no_pci; 3594e705c121SKalle Valo 35955a41a86cSSharon Dvir ret = devm_request_threaded_irq(&pdev->dev, pdev->irq, 35965a41a86cSSharon Dvir iwl_pcie_isr, 3597e705c121SKalle Valo iwl_pcie_irq_handler, 3598e705c121SKalle Valo IRQF_SHARED, DRV_NAME, trans); 3599e705c121SKalle Valo if (ret) { 3600e705c121SKalle Valo IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq); 3601e705c121SKalle Valo goto out_free_ict; 3602e705c121SKalle Valo } 3603e705c121SKalle Valo trans_pcie->inta_mask = CSR_INI_SET_MASK; 36042e5d4a8fSHaim Dreyfuss } 3605e705c121SKalle Valo 3606f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS 3607f7805b33SLior Cohen trans_pcie->fw_mon_data.state = IWL_FW_MON_DBGFS_STATE_CLOSED; 3608f7805b33SLior Cohen mutex_init(&trans_pcie->fw_mon_data.mutex); 3609f7805b33SLior Cohen #endif 3610f7805b33SLior Cohen 3611a9248de4SShahar S Matityahu iwl_dbg_tlv_init(trans); 3612a9248de4SShahar S Matityahu 3613e705c121SKalle Valo return trans; 3614e705c121SKalle Valo 3615e705c121SKalle Valo out_free_ict: 3616e705c121SKalle Valo iwl_pcie_free_ict(trans); 3617e705c121SKalle Valo out_no_pci: 36186eb5e529SEmmanuel Grumbach free_percpu(trans_pcie->tso_hdr_page); 36198188a18eSJohannes Berg destroy_workqueue(trans_pcie->rba.alloc_wq); 36208188a18eSJohannes Berg out_free_trans: 3621e705c121SKalle Valo iwl_trans_free(trans); 3622e705c121SKalle Valo return ERR_PTR(ret); 3623e705c121SKalle Valo } 3624b8a7547dSShahar S Matityahu 3625d1967ce6SShahar S Matityahu void iwl_trans_pcie_sync_nmi(struct iwl_trans *trans) 3626b8a7547dSShahar S Matityahu { 36271c6bca6dSShahar S Matityahu struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 3628b8a7547dSShahar S Matityahu unsigned long timeout = jiffies + IWL_TRANS_NMI_TIMEOUT; 3629e4eee943SShahar S Matityahu bool interrupts_enabled = test_bit(STATUS_INT_ENABLED, &trans->status); 36301c6bca6dSShahar S Matityahu u32 inta_addr, sw_err_bit; 36311c6bca6dSShahar S Matityahu 36321c6bca6dSShahar S Matityahu if (trans_pcie->msix_enabled) { 36331c6bca6dSShahar S Matityahu inta_addr = CSR_MSIX_HW_INT_CAUSES_AD; 36341c6bca6dSShahar S Matityahu sw_err_bit = MSIX_HW_INT_CAUSES_REG_SW_ERR; 36351c6bca6dSShahar S Matityahu } else { 36361c6bca6dSShahar S Matityahu inta_addr = CSR_INT; 36371c6bca6dSShahar S Matityahu sw_err_bit = CSR_INT_BIT_SW_ERR; 36381c6bca6dSShahar S Matityahu } 3639b8a7547dSShahar S Matityahu 3640e4eee943SShahar S Matityahu /* if the interrupts were already disabled, there is no point in 3641e4eee943SShahar S Matityahu * calling iwl_disable_interrupts 3642e4eee943SShahar S Matityahu */ 3643e4eee943SShahar S Matityahu if (interrupts_enabled) 3644b8a7547dSShahar S Matityahu iwl_disable_interrupts(trans); 3645e4eee943SShahar S Matityahu 3646b8a7547dSShahar S Matityahu iwl_force_nmi(trans); 3647b8a7547dSShahar S Matityahu while (time_after(timeout, jiffies)) { 36481c6bca6dSShahar S Matityahu u32 inta_hw = iwl_read32(trans, inta_addr); 3649b8a7547dSShahar S Matityahu 3650b8a7547dSShahar S Matityahu /* Error detected by uCode */ 36511c6bca6dSShahar S Matityahu if (inta_hw & sw_err_bit) { 3652b8a7547dSShahar S Matityahu /* Clear causes register */ 36531c6bca6dSShahar S Matityahu iwl_write32(trans, inta_addr, inta_hw & sw_err_bit); 3654b8a7547dSShahar S Matityahu break; 3655b8a7547dSShahar S Matityahu } 3656b8a7547dSShahar S Matityahu 3657b8a7547dSShahar S Matityahu mdelay(1); 3658b8a7547dSShahar S Matityahu } 3659e4eee943SShahar S Matityahu 3660e4eee943SShahar S Matityahu /* enable interrupts only if there were already enabled before this 3661e4eee943SShahar S Matityahu * function to avoid a case were the driver enable interrupts before 3662e4eee943SShahar S Matityahu * proper configurations were made 3663e4eee943SShahar S Matityahu */ 3664e4eee943SShahar S Matityahu if (interrupts_enabled) 3665b8a7547dSShahar S Matityahu iwl_enable_interrupts(trans); 3666e4eee943SShahar S Matityahu 3667b8a7547dSShahar S Matityahu iwl_trans_fw_error(trans); 3668b8a7547dSShahar S Matityahu } 3669