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