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