1bfcc09ddSBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2bfcc09ddSBjoern A. Zeeb /* 3bfcc09ddSBjoern A. Zeeb * Copyright (C) 2017 Intel Deutschland GmbH 4*a4128aadSBjoern A. Zeeb * Copyright (C) 2018-2024 Intel Corporation 5bfcc09ddSBjoern A. Zeeb */ 6bfcc09ddSBjoern A. Zeeb #if defined(__FreeBSD__) 7bfcc09ddSBjoern A. Zeeb #include <linux/delay.h> 8bfcc09ddSBjoern A. Zeeb #endif 9bfcc09ddSBjoern A. Zeeb #include "iwl-trans.h" 10bfcc09ddSBjoern A. Zeeb #include "iwl-prph.h" 11bfcc09ddSBjoern A. Zeeb #include "iwl-context-info.h" 12bfcc09ddSBjoern A. Zeeb #include "iwl-context-info-gen3.h" 13bfcc09ddSBjoern A. Zeeb #include "internal.h" 14bfcc09ddSBjoern A. Zeeb #include "fw/dbg.h" 15bfcc09ddSBjoern A. Zeeb 16bfcc09ddSBjoern A. Zeeb #define FW_RESET_TIMEOUT (HZ / 5) 17bfcc09ddSBjoern A. Zeeb 18bfcc09ddSBjoern A. Zeeb /* 19bfcc09ddSBjoern A. Zeeb * Start up NIC's basic functionality after it has been reset 20bfcc09ddSBjoern A. Zeeb * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop()) 21bfcc09ddSBjoern A. Zeeb * NOTE: This does not load uCode nor start the embedded processor 22bfcc09ddSBjoern A. Zeeb */ 23bfcc09ddSBjoern A. Zeeb int iwl_pcie_gen2_apm_init(struct iwl_trans *trans) 24bfcc09ddSBjoern A. Zeeb { 25bfcc09ddSBjoern A. Zeeb int ret = 0; 26bfcc09ddSBjoern A. Zeeb 27bfcc09ddSBjoern A. Zeeb IWL_DEBUG_INFO(trans, "Init card's basic functions\n"); 28bfcc09ddSBjoern A. Zeeb 29bfcc09ddSBjoern A. Zeeb /* 30bfcc09ddSBjoern A. Zeeb * Use "set_bit" below rather than "write", to preserve any hardware 31bfcc09ddSBjoern A. Zeeb * bits already set by default after reset. 32bfcc09ddSBjoern A. Zeeb */ 33bfcc09ddSBjoern A. Zeeb 34bfcc09ddSBjoern A. Zeeb /* 35bfcc09ddSBjoern A. Zeeb * Disable L0s without affecting L1; 36bfcc09ddSBjoern A. Zeeb * don't wait for ICH L0s (ICH bug W/A) 37bfcc09ddSBjoern A. Zeeb */ 38bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS, 39bfcc09ddSBjoern A. Zeeb CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX); 40bfcc09ddSBjoern A. Zeeb 41bfcc09ddSBjoern A. Zeeb /* Set FH wait threshold to maximum (HW error during stress W/A) */ 42bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL); 43bfcc09ddSBjoern A. Zeeb 44bfcc09ddSBjoern A. Zeeb /* 45bfcc09ddSBjoern A. Zeeb * Enable HAP INTA (interrupt from management bus) to 46bfcc09ddSBjoern A. Zeeb * wake device's PCI Express link L1a -> L0s 47bfcc09ddSBjoern A. Zeeb */ 48bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 49bfcc09ddSBjoern A. Zeeb CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A); 50bfcc09ddSBjoern A. Zeeb 51bfcc09ddSBjoern A. Zeeb iwl_pcie_apm_config(trans); 52bfcc09ddSBjoern A. Zeeb 53bfcc09ddSBjoern A. Zeeb ret = iwl_finish_nic_init(trans); 54bfcc09ddSBjoern A. Zeeb if (ret) 55bfcc09ddSBjoern A. Zeeb return ret; 56bfcc09ddSBjoern A. Zeeb 57bfcc09ddSBjoern A. Zeeb set_bit(STATUS_DEVICE_ENABLED, &trans->status); 58bfcc09ddSBjoern A. Zeeb 59bfcc09ddSBjoern A. Zeeb return 0; 60bfcc09ddSBjoern A. Zeeb } 61bfcc09ddSBjoern A. Zeeb 62bfcc09ddSBjoern A. Zeeb static void iwl_pcie_gen2_apm_stop(struct iwl_trans *trans, bool op_mode_leave) 63bfcc09ddSBjoern A. Zeeb { 64bfcc09ddSBjoern A. Zeeb IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n"); 65bfcc09ddSBjoern A. Zeeb 66bfcc09ddSBjoern A. Zeeb if (op_mode_leave) { 67bfcc09ddSBjoern A. Zeeb if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status)) 68bfcc09ddSBjoern A. Zeeb iwl_pcie_gen2_apm_init(trans); 69bfcc09ddSBjoern A. Zeeb 70bfcc09ddSBjoern A. Zeeb /* inform ME that we are leaving */ 71bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 72bfcc09ddSBjoern A. Zeeb CSR_RESET_LINK_PWR_MGMT_DISABLED); 73bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG, 74bfcc09ddSBjoern A. Zeeb CSR_HW_IF_CONFIG_REG_PREPARE | 75bfcc09ddSBjoern A. Zeeb CSR_HW_IF_CONFIG_REG_ENABLE_PME); 76bfcc09ddSBjoern A. Zeeb mdelay(1); 77bfcc09ddSBjoern A. Zeeb iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG, 78bfcc09ddSBjoern A. Zeeb CSR_RESET_LINK_PWR_MGMT_DISABLED); 79bfcc09ddSBjoern A. Zeeb mdelay(5); 80bfcc09ddSBjoern A. Zeeb } 81bfcc09ddSBjoern A. Zeeb 82bfcc09ddSBjoern A. Zeeb clear_bit(STATUS_DEVICE_ENABLED, &trans->status); 83bfcc09ddSBjoern A. Zeeb 84bfcc09ddSBjoern A. Zeeb /* Stop device's DMA activity */ 85bfcc09ddSBjoern A. Zeeb iwl_pcie_apm_stop_master(trans); 86bfcc09ddSBjoern A. Zeeb 87d9836fb4SBjoern A. Zeeb iwl_trans_sw_reset(trans, false); 88bfcc09ddSBjoern A. Zeeb 89bfcc09ddSBjoern A. Zeeb /* 90bfcc09ddSBjoern A. Zeeb * Clear "initialization complete" bit to move adapter from 91bfcc09ddSBjoern A. Zeeb * D0A* (powered-up Active) --> D0U* (Uninitialized) state. 92bfcc09ddSBjoern A. Zeeb */ 93bfcc09ddSBjoern A. Zeeb if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) 94bfcc09ddSBjoern A. Zeeb iwl_clear_bit(trans, CSR_GP_CNTRL, 95bfcc09ddSBjoern A. Zeeb CSR_GP_CNTRL_REG_FLAG_MAC_INIT); 96bfcc09ddSBjoern A. Zeeb else 97bfcc09ddSBjoern A. Zeeb iwl_clear_bit(trans, CSR_GP_CNTRL, 98bfcc09ddSBjoern A. Zeeb CSR_GP_CNTRL_REG_FLAG_INIT_DONE); 99bfcc09ddSBjoern A. Zeeb } 100bfcc09ddSBjoern A. Zeeb 101bfcc09ddSBjoern A. Zeeb static void iwl_trans_pcie_fw_reset_handshake(struct iwl_trans *trans) 102bfcc09ddSBjoern A. Zeeb { 103bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 104bfcc09ddSBjoern A. Zeeb int ret; 105bfcc09ddSBjoern A. Zeeb 106bfcc09ddSBjoern A. Zeeb trans_pcie->fw_reset_state = FW_RESET_REQUESTED; 107bfcc09ddSBjoern A. Zeeb 108bfcc09ddSBjoern A. Zeeb if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210) 109bfcc09ddSBjoern A. Zeeb iwl_write_umac_prph(trans, UREG_NIC_SET_NMI_DRIVER, 110bfcc09ddSBjoern A. Zeeb UREG_NIC_SET_NMI_DRIVER_RESET_HANDSHAKE); 111d9836fb4SBjoern A. Zeeb else if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210) 112bfcc09ddSBjoern A. Zeeb iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6, 113bfcc09ddSBjoern A. Zeeb UREG_DOORBELL_TO_ISR6_RESET_HANDSHAKE); 114d9836fb4SBjoern A. Zeeb else 115d9836fb4SBjoern A. Zeeb iwl_write32(trans, CSR_DOORBELL_VECTOR, 116d9836fb4SBjoern A. Zeeb UREG_DOORBELL_TO_ISR6_RESET_HANDSHAKE); 117bfcc09ddSBjoern A. Zeeb 118bfcc09ddSBjoern A. Zeeb /* wait 200ms */ 119bfcc09ddSBjoern A. Zeeb ret = wait_event_timeout(trans_pcie->fw_reset_waitq, 120bfcc09ddSBjoern A. Zeeb trans_pcie->fw_reset_state != FW_RESET_REQUESTED, 121bfcc09ddSBjoern A. Zeeb FW_RESET_TIMEOUT); 122bfcc09ddSBjoern A. Zeeb if (!ret || trans_pcie->fw_reset_state == FW_RESET_ERROR) { 1239af1bba4SBjoern A. Zeeb u32 inta_hw = iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD); 1249af1bba4SBjoern A. Zeeb 1259af1bba4SBjoern A. Zeeb IWL_ERR(trans, 1269af1bba4SBjoern A. Zeeb "timeout waiting for FW reset ACK (inta_hw=0x%x)\n", 1279af1bba4SBjoern A. Zeeb inta_hw); 1289af1bba4SBjoern A. Zeeb 1299af1bba4SBjoern A. Zeeb if (!(inta_hw & MSIX_HW_INT_CAUSES_REG_RESET_DONE)) 130bfcc09ddSBjoern A. Zeeb iwl_trans_fw_error(trans, true); 131bfcc09ddSBjoern A. Zeeb } 132bfcc09ddSBjoern A. Zeeb 133bfcc09ddSBjoern A. Zeeb trans_pcie->fw_reset_state = FW_RESET_IDLE; 134bfcc09ddSBjoern A. Zeeb } 135bfcc09ddSBjoern A. Zeeb 136bfcc09ddSBjoern A. Zeeb void _iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans) 137bfcc09ddSBjoern A. Zeeb { 138bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 139bfcc09ddSBjoern A. Zeeb 140bfcc09ddSBjoern A. Zeeb lockdep_assert_held(&trans_pcie->mutex); 141bfcc09ddSBjoern A. Zeeb 142bfcc09ddSBjoern A. Zeeb if (trans_pcie->is_down) 143bfcc09ddSBjoern A. Zeeb return; 144bfcc09ddSBjoern A. Zeeb 145bfcc09ddSBjoern A. Zeeb if (trans->state >= IWL_TRANS_FW_STARTED) 146bfcc09ddSBjoern A. Zeeb if (trans_pcie->fw_reset_handshake) 147bfcc09ddSBjoern A. Zeeb iwl_trans_pcie_fw_reset_handshake(trans); 148bfcc09ddSBjoern A. Zeeb 149bfcc09ddSBjoern A. Zeeb trans_pcie->is_down = true; 150bfcc09ddSBjoern A. Zeeb 151bfcc09ddSBjoern A. Zeeb /* tell the device to stop sending interrupts */ 152bfcc09ddSBjoern A. Zeeb iwl_disable_interrupts(trans); 153bfcc09ddSBjoern A. Zeeb 154bfcc09ddSBjoern A. Zeeb /* device going down, Stop using ICT table */ 155bfcc09ddSBjoern A. Zeeb iwl_pcie_disable_ict(trans); 156bfcc09ddSBjoern A. Zeeb 157bfcc09ddSBjoern A. Zeeb /* 158bfcc09ddSBjoern A. Zeeb * If a HW restart happens during firmware loading, 159bfcc09ddSBjoern A. Zeeb * then the firmware loading might call this function 160bfcc09ddSBjoern A. Zeeb * and later it might be called again due to the 161bfcc09ddSBjoern A. Zeeb * restart. So don't process again if the device is 162bfcc09ddSBjoern A. Zeeb * already dead. 163bfcc09ddSBjoern A. Zeeb */ 164bfcc09ddSBjoern A. Zeeb if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) { 165bfcc09ddSBjoern A. Zeeb IWL_DEBUG_INFO(trans, 166bfcc09ddSBjoern A. Zeeb "DEVICE_ENABLED bit was set and is now cleared\n"); 167*a4128aadSBjoern A. Zeeb iwl_pcie_synchronize_irqs(trans); 1689af1bba4SBjoern A. Zeeb iwl_pcie_rx_napi_sync(trans); 169bfcc09ddSBjoern A. Zeeb iwl_txq_gen2_tx_free(trans); 170bfcc09ddSBjoern A. Zeeb iwl_pcie_rx_stop(trans); 171bfcc09ddSBjoern A. Zeeb } 172bfcc09ddSBjoern A. Zeeb 173bfcc09ddSBjoern A. Zeeb iwl_pcie_ctxt_info_free_paging(trans); 174bfcc09ddSBjoern A. Zeeb if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 175bfcc09ddSBjoern A. Zeeb iwl_pcie_ctxt_info_gen3_free(trans, false); 176bfcc09ddSBjoern A. Zeeb else 177bfcc09ddSBjoern A. Zeeb iwl_pcie_ctxt_info_free(trans); 178bfcc09ddSBjoern A. Zeeb 179bfcc09ddSBjoern A. Zeeb /* Stop the device, and put it in low power state */ 180bfcc09ddSBjoern A. Zeeb iwl_pcie_gen2_apm_stop(trans, false); 181bfcc09ddSBjoern A. Zeeb 182d9836fb4SBjoern A. Zeeb /* re-take ownership to prevent other users from stealing the device */ 183d9836fb4SBjoern A. Zeeb iwl_trans_sw_reset(trans, true); 184bfcc09ddSBjoern A. Zeeb 185bfcc09ddSBjoern A. Zeeb /* 186bfcc09ddSBjoern A. Zeeb * Upon stop, the IVAR table gets erased, so msi-x won't 187bfcc09ddSBjoern A. Zeeb * work. This causes a bug in RF-KILL flows, since the interrupt 188bfcc09ddSBjoern A. Zeeb * that enables radio won't fire on the correct irq, and the 189bfcc09ddSBjoern A. Zeeb * driver won't be able to handle the interrupt. 190bfcc09ddSBjoern A. Zeeb * Configure the IVAR table again after reset. 191bfcc09ddSBjoern A. Zeeb */ 192bfcc09ddSBjoern A. Zeeb iwl_pcie_conf_msix_hw(trans_pcie); 193bfcc09ddSBjoern A. Zeeb 194bfcc09ddSBjoern A. Zeeb /* 195bfcc09ddSBjoern A. Zeeb * Upon stop, the APM issues an interrupt if HW RF kill is set. 196bfcc09ddSBjoern A. Zeeb * This is a bug in certain verions of the hardware. 197bfcc09ddSBjoern A. Zeeb * Certain devices also keep sending HW RF kill interrupt all 198bfcc09ddSBjoern A. Zeeb * the time, unless the interrupt is ACKed even if the interrupt 199bfcc09ddSBjoern A. Zeeb * should be masked. Re-ACK all the interrupts here. 200bfcc09ddSBjoern A. Zeeb */ 201bfcc09ddSBjoern A. Zeeb iwl_disable_interrupts(trans); 202bfcc09ddSBjoern A. Zeeb 203bfcc09ddSBjoern A. Zeeb /* clear all status bits */ 204bfcc09ddSBjoern A. Zeeb clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status); 205bfcc09ddSBjoern A. Zeeb clear_bit(STATUS_INT_ENABLED, &trans->status); 206bfcc09ddSBjoern A. Zeeb clear_bit(STATUS_TPOWER_PMI, &trans->status); 207bfcc09ddSBjoern A. Zeeb 208bfcc09ddSBjoern A. Zeeb /* 209bfcc09ddSBjoern A. Zeeb * Even if we stop the HW, we still want the RF kill 210bfcc09ddSBjoern A. Zeeb * interrupt 211bfcc09ddSBjoern A. Zeeb */ 212bfcc09ddSBjoern A. Zeeb iwl_enable_rfkill_int(trans); 213bfcc09ddSBjoern A. Zeeb } 214bfcc09ddSBjoern A. Zeeb 215bfcc09ddSBjoern A. Zeeb void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans) 216bfcc09ddSBjoern A. Zeeb { 217bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 218bfcc09ddSBjoern A. Zeeb bool was_in_rfkill; 219bfcc09ddSBjoern A. Zeeb 220bfcc09ddSBjoern A. Zeeb iwl_op_mode_time_point(trans->op_mode, 221bfcc09ddSBjoern A. Zeeb IWL_FW_INI_TIME_POINT_HOST_DEVICE_DISABLE, 222bfcc09ddSBjoern A. Zeeb NULL); 223bfcc09ddSBjoern A. Zeeb 224bfcc09ddSBjoern A. Zeeb mutex_lock(&trans_pcie->mutex); 225bfcc09ddSBjoern A. Zeeb trans_pcie->opmode_down = true; 226bfcc09ddSBjoern A. Zeeb was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status); 227bfcc09ddSBjoern A. Zeeb _iwl_trans_pcie_gen2_stop_device(trans); 228bfcc09ddSBjoern A. Zeeb iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill); 229bfcc09ddSBjoern A. Zeeb mutex_unlock(&trans_pcie->mutex); 230bfcc09ddSBjoern A. Zeeb } 231bfcc09ddSBjoern A. Zeeb 232bfcc09ddSBjoern A. Zeeb static int iwl_pcie_gen2_nic_init(struct iwl_trans *trans) 233bfcc09ddSBjoern A. Zeeb { 234bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 235bfcc09ddSBjoern A. Zeeb int queue_size = max_t(u32, IWL_CMD_QUEUE_SIZE, 236bfcc09ddSBjoern A. Zeeb trans->cfg->min_txq_size); 237*a4128aadSBjoern A. Zeeb int ret; 238bfcc09ddSBjoern A. Zeeb 239bfcc09ddSBjoern A. Zeeb /* TODO: most of the logic can be removed in A0 - but not in Z0 */ 240bfcc09ddSBjoern A. Zeeb spin_lock_bh(&trans_pcie->irq_lock); 241*a4128aadSBjoern A. Zeeb ret = iwl_pcie_gen2_apm_init(trans); 242bfcc09ddSBjoern A. Zeeb spin_unlock_bh(&trans_pcie->irq_lock); 243*a4128aadSBjoern A. Zeeb if (ret) 244*a4128aadSBjoern A. Zeeb return ret; 245bfcc09ddSBjoern A. Zeeb 246bfcc09ddSBjoern A. Zeeb iwl_op_mode_nic_config(trans->op_mode); 247bfcc09ddSBjoern A. Zeeb 248bfcc09ddSBjoern A. Zeeb /* Allocate the RX queue, or reset if it is already allocated */ 249bfcc09ddSBjoern A. Zeeb if (iwl_pcie_gen2_rx_init(trans)) 250bfcc09ddSBjoern A. Zeeb return -ENOMEM; 251bfcc09ddSBjoern A. Zeeb 252bfcc09ddSBjoern A. Zeeb /* Allocate or reset and init all Tx and Command queues */ 253*a4128aadSBjoern A. Zeeb if (iwl_txq_gen2_init(trans, trans_pcie->txqs.cmd.q_id, queue_size)) 254bfcc09ddSBjoern A. Zeeb return -ENOMEM; 255bfcc09ddSBjoern A. Zeeb 256bfcc09ddSBjoern A. Zeeb /* enable shadow regs in HW */ 257bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF); 258bfcc09ddSBjoern A. Zeeb IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n"); 259bfcc09ddSBjoern A. Zeeb 260bfcc09ddSBjoern A. Zeeb return 0; 261bfcc09ddSBjoern A. Zeeb } 262bfcc09ddSBjoern A. Zeeb 263bfcc09ddSBjoern A. Zeeb static void iwl_pcie_get_rf_name(struct iwl_trans *trans) 264bfcc09ddSBjoern A. Zeeb { 265bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 266bfcc09ddSBjoern A. Zeeb char *buf = trans_pcie->rf_name; 267bfcc09ddSBjoern A. Zeeb size_t buflen = sizeof(trans_pcie->rf_name); 268bfcc09ddSBjoern A. Zeeb size_t pos; 269bfcc09ddSBjoern A. Zeeb u32 version; 270bfcc09ddSBjoern A. Zeeb 271bfcc09ddSBjoern A. Zeeb if (buf[0]) 272bfcc09ddSBjoern A. Zeeb return; 273bfcc09ddSBjoern A. Zeeb 274bfcc09ddSBjoern A. Zeeb switch (CSR_HW_RFID_TYPE(trans->hw_rf_id)) { 275bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_JF): 276bfcc09ddSBjoern A. Zeeb pos = scnprintf(buf, buflen, "JF"); 277bfcc09ddSBjoern A. Zeeb break; 278bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_GF): 279bfcc09ddSBjoern A. Zeeb pos = scnprintf(buf, buflen, "GF"); 280bfcc09ddSBjoern A. Zeeb break; 281bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_GF4): 282bfcc09ddSBjoern A. Zeeb pos = scnprintf(buf, buflen, "GF4"); 283bfcc09ddSBjoern A. Zeeb break; 284bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR): 285bfcc09ddSBjoern A. Zeeb pos = scnprintf(buf, buflen, "HR"); 286bfcc09ddSBjoern A. Zeeb break; 287bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR1): 288bfcc09ddSBjoern A. Zeeb pos = scnprintf(buf, buflen, "HR1"); 289bfcc09ddSBjoern A. Zeeb break; 290bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HRCDB): 291bfcc09ddSBjoern A. Zeeb pos = scnprintf(buf, buflen, "HRCDB"); 292bfcc09ddSBjoern A. Zeeb break; 2939af1bba4SBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_MS): 2949af1bba4SBjoern A. Zeeb pos = scnprintf(buf, buflen, "MS"); 2959af1bba4SBjoern A. Zeeb break; 296*a4128aadSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_FM): 297*a4128aadSBjoern A. Zeeb pos = scnprintf(buf, buflen, "FM"); 298*a4128aadSBjoern A. Zeeb break; 299*a4128aadSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_WP): 300*a4128aadSBjoern A. Zeeb if (SILICON_Z_STEP == 301*a4128aadSBjoern A. Zeeb CSR_HW_RFID_STEP(trans->hw_rf_id)) 302*a4128aadSBjoern A. Zeeb pos = scnprintf(buf, buflen, "WHTC"); 303*a4128aadSBjoern A. Zeeb else 304*a4128aadSBjoern A. Zeeb pos = scnprintf(buf, buflen, "WH"); 305*a4128aadSBjoern A. Zeeb break; 306bfcc09ddSBjoern A. Zeeb default: 307bfcc09ddSBjoern A. Zeeb return; 308bfcc09ddSBjoern A. Zeeb } 309bfcc09ddSBjoern A. Zeeb 310bfcc09ddSBjoern A. Zeeb switch (CSR_HW_RFID_TYPE(trans->hw_rf_id)) { 311bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR): 312bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR1): 313bfcc09ddSBjoern A. Zeeb case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HRCDB): 314bfcc09ddSBjoern A. Zeeb version = iwl_read_prph(trans, CNVI_MBOX_C); 315bfcc09ddSBjoern A. Zeeb switch (version) { 316bfcc09ddSBjoern A. Zeeb case 0x20000: 317bfcc09ddSBjoern A. Zeeb pos += scnprintf(buf + pos, buflen - pos, " B3"); 318bfcc09ddSBjoern A. Zeeb break; 319bfcc09ddSBjoern A. Zeeb case 0x120000: 320bfcc09ddSBjoern A. Zeeb pos += scnprintf(buf + pos, buflen - pos, " B5"); 321bfcc09ddSBjoern A. Zeeb break; 322bfcc09ddSBjoern A. Zeeb default: 323bfcc09ddSBjoern A. Zeeb pos += scnprintf(buf + pos, buflen - pos, 324bfcc09ddSBjoern A. Zeeb " (0x%x)", version); 325bfcc09ddSBjoern A. Zeeb break; 326bfcc09ddSBjoern A. Zeeb } 327bfcc09ddSBjoern A. Zeeb break; 328bfcc09ddSBjoern A. Zeeb default: 329bfcc09ddSBjoern A. Zeeb break; 330bfcc09ddSBjoern A. Zeeb } 331bfcc09ddSBjoern A. Zeeb 332bfcc09ddSBjoern A. Zeeb pos += scnprintf(buf + pos, buflen - pos, ", rfid=0x%x", 333bfcc09ddSBjoern A. Zeeb trans->hw_rf_id); 334bfcc09ddSBjoern A. Zeeb 335bfcc09ddSBjoern A. Zeeb IWL_INFO(trans, "Detected RF %s\n", buf); 336bfcc09ddSBjoern A. Zeeb 337bfcc09ddSBjoern A. Zeeb /* 338bfcc09ddSBjoern A. Zeeb * also add a \n for debugfs - need to do it after printing 339bfcc09ddSBjoern A. Zeeb * since our IWL_INFO machinery wants to see a static \n at 340bfcc09ddSBjoern A. Zeeb * the end of the string 341bfcc09ddSBjoern A. Zeeb */ 342bfcc09ddSBjoern A. Zeeb pos += scnprintf(buf + pos, buflen - pos, "\n"); 343bfcc09ddSBjoern A. Zeeb } 344bfcc09ddSBjoern A. Zeeb 345*a4128aadSBjoern A. Zeeb void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans) 346bfcc09ddSBjoern A. Zeeb { 347bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 348bfcc09ddSBjoern A. Zeeb 349bfcc09ddSBjoern A. Zeeb iwl_pcie_reset_ict(trans); 350bfcc09ddSBjoern A. Zeeb 351bfcc09ddSBjoern A. Zeeb /* make sure all queue are not stopped/used */ 352*a4128aadSBjoern A. Zeeb memset(trans_pcie->txqs.queue_stopped, 0, 353*a4128aadSBjoern A. Zeeb sizeof(trans_pcie->txqs.queue_stopped)); 354*a4128aadSBjoern A. Zeeb memset(trans_pcie->txqs.queue_used, 0, 355*a4128aadSBjoern A. Zeeb sizeof(trans_pcie->txqs.queue_used)); 356bfcc09ddSBjoern A. Zeeb 357bfcc09ddSBjoern A. Zeeb /* now that we got alive we can free the fw image & the context info. 358bfcc09ddSBjoern A. Zeeb * paging memory cannot be freed included since FW will still use it 359bfcc09ddSBjoern A. Zeeb */ 360bfcc09ddSBjoern A. Zeeb if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 361bfcc09ddSBjoern A. Zeeb iwl_pcie_ctxt_info_gen3_free(trans, true); 362bfcc09ddSBjoern A. Zeeb else 363bfcc09ddSBjoern A. Zeeb iwl_pcie_ctxt_info_free(trans); 364bfcc09ddSBjoern A. Zeeb 365bfcc09ddSBjoern A. Zeeb /* 366bfcc09ddSBjoern A. Zeeb * Re-enable all the interrupts, including the RF-Kill one, now that 367bfcc09ddSBjoern A. Zeeb * the firmware is alive. 368bfcc09ddSBjoern A. Zeeb */ 369bfcc09ddSBjoern A. Zeeb iwl_enable_interrupts(trans); 370bfcc09ddSBjoern A. Zeeb mutex_lock(&trans_pcie->mutex); 371bfcc09ddSBjoern A. Zeeb iwl_pcie_check_hw_rf_kill(trans); 372bfcc09ddSBjoern A. Zeeb 373bfcc09ddSBjoern A. Zeeb iwl_pcie_get_rf_name(trans); 374bfcc09ddSBjoern A. Zeeb mutex_unlock(&trans_pcie->mutex); 375bfcc09ddSBjoern A. Zeeb } 376bfcc09ddSBjoern A. Zeeb 3779af1bba4SBjoern A. Zeeb static bool iwl_pcie_set_ltr(struct iwl_trans *trans) 378bfcc09ddSBjoern A. Zeeb { 379bfcc09ddSBjoern A. Zeeb u32 ltr_val = CSR_LTR_LONG_VAL_AD_NO_SNOOP_REQ | 380bfcc09ddSBjoern A. Zeeb u32_encode_bits(CSR_LTR_LONG_VAL_AD_SCALE_USEC, 381bfcc09ddSBjoern A. Zeeb CSR_LTR_LONG_VAL_AD_NO_SNOOP_SCALE) | 382bfcc09ddSBjoern A. Zeeb u32_encode_bits(250, 383bfcc09ddSBjoern A. Zeeb CSR_LTR_LONG_VAL_AD_NO_SNOOP_VAL) | 384bfcc09ddSBjoern A. Zeeb CSR_LTR_LONG_VAL_AD_SNOOP_REQ | 385bfcc09ddSBjoern A. Zeeb u32_encode_bits(CSR_LTR_LONG_VAL_AD_SCALE_USEC, 386bfcc09ddSBjoern A. Zeeb CSR_LTR_LONG_VAL_AD_SNOOP_SCALE) | 387bfcc09ddSBjoern A. Zeeb u32_encode_bits(250, CSR_LTR_LONG_VAL_AD_SNOOP_VAL); 388bfcc09ddSBjoern A. Zeeb 389bfcc09ddSBjoern A. Zeeb /* 390bfcc09ddSBjoern A. Zeeb * To workaround hardware latency issues during the boot process, 391bfcc09ddSBjoern A. Zeeb * initialize the LTR to ~250 usec (see ltr_val above). 392bfcc09ddSBjoern A. Zeeb * The firmware initializes this again later (to a smaller value). 393bfcc09ddSBjoern A. Zeeb */ 394bfcc09ddSBjoern A. Zeeb if ((trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210 || 395bfcc09ddSBjoern A. Zeeb trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_22000) && 396bfcc09ddSBjoern A. Zeeb !trans->trans_cfg->integrated) { 397bfcc09ddSBjoern A. Zeeb iwl_write32(trans, CSR_LTR_LONG_VAL_AD, ltr_val); 3989af1bba4SBjoern A. Zeeb return true; 3999af1bba4SBjoern A. Zeeb } 4009af1bba4SBjoern A. Zeeb 4019af1bba4SBjoern A. Zeeb if (trans->trans_cfg->integrated && 402bfcc09ddSBjoern A. Zeeb trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_22000) { 403bfcc09ddSBjoern A. Zeeb iwl_write_prph(trans, HPM_MAC_LTR_CSR, HPM_MAC_LRT_ENABLE_ALL); 404bfcc09ddSBjoern A. Zeeb iwl_write_prph(trans, HPM_UMAC_LTR, ltr_val); 4059af1bba4SBjoern A. Zeeb return true; 406bfcc09ddSBjoern A. Zeeb } 4079af1bba4SBjoern A. Zeeb 4089af1bba4SBjoern A. Zeeb if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210) { 4099af1bba4SBjoern A. Zeeb /* First clear the interrupt, just in case */ 4109af1bba4SBjoern A. Zeeb iwl_write32(trans, CSR_MSIX_HW_INT_CAUSES_AD, 4119af1bba4SBjoern A. Zeeb MSIX_HW_INT_CAUSES_REG_IML); 4129af1bba4SBjoern A. Zeeb /* In this case, unfortunately the same ROM bug exists in the 4139af1bba4SBjoern A. Zeeb * device (not setting LTR correctly), but we don't have control 4149af1bba4SBjoern A. Zeeb * over the settings from the host due to some hardware security 4159af1bba4SBjoern A. Zeeb * features. The only workaround we've been able to come up with 4169af1bba4SBjoern A. Zeeb * so far is to try to keep the CPU and device busy by polling 4179af1bba4SBjoern A. Zeeb * it and the IML (image loader) completed interrupt. 4189af1bba4SBjoern A. Zeeb */ 4199af1bba4SBjoern A. Zeeb return false; 4209af1bba4SBjoern A. Zeeb } 4219af1bba4SBjoern A. Zeeb 4229af1bba4SBjoern A. Zeeb /* nothing needs to be done on other devices */ 4239af1bba4SBjoern A. Zeeb return true; 4249af1bba4SBjoern A. Zeeb } 4259af1bba4SBjoern A. Zeeb 4269af1bba4SBjoern A. Zeeb static void iwl_pcie_spin_for_iml(struct iwl_trans *trans) 4279af1bba4SBjoern A. Zeeb { 4289af1bba4SBjoern A. Zeeb /* in practice, this seems to complete in around 20-30ms at most, wait 100 */ 4299af1bba4SBjoern A. Zeeb #define IML_WAIT_TIMEOUT (HZ / 10) 4309af1bba4SBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 4319af1bba4SBjoern A. Zeeb unsigned long end_time = jiffies + IML_WAIT_TIMEOUT; 4329af1bba4SBjoern A. Zeeb u32 value, loops = 0; 4339af1bba4SBjoern A. Zeeb bool irq = false; 4349af1bba4SBjoern A. Zeeb 4359af1bba4SBjoern A. Zeeb if (WARN_ON(!trans_pcie->iml)) 4369af1bba4SBjoern A. Zeeb return; 4379af1bba4SBjoern A. Zeeb 4389af1bba4SBjoern A. Zeeb value = iwl_read32(trans, CSR_LTR_LAST_MSG); 4399af1bba4SBjoern A. Zeeb IWL_DEBUG_INFO(trans, "Polling for IML load - CSR_LTR_LAST_MSG=0x%x\n", 4409af1bba4SBjoern A. Zeeb value); 4419af1bba4SBjoern A. Zeeb 4429af1bba4SBjoern A. Zeeb while (time_before(jiffies, end_time)) { 4439af1bba4SBjoern A. Zeeb if (iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD) & 4449af1bba4SBjoern A. Zeeb MSIX_HW_INT_CAUSES_REG_IML) { 4459af1bba4SBjoern A. Zeeb irq = true; 4469af1bba4SBjoern A. Zeeb break; 4479af1bba4SBjoern A. Zeeb } 4489af1bba4SBjoern A. Zeeb /* Keep the CPU and device busy. */ 4499af1bba4SBjoern A. Zeeb value = iwl_read32(trans, CSR_LTR_LAST_MSG); 4509af1bba4SBjoern A. Zeeb loops++; 4519af1bba4SBjoern A. Zeeb } 4529af1bba4SBjoern A. Zeeb 4539af1bba4SBjoern A. Zeeb IWL_DEBUG_INFO(trans, 4549af1bba4SBjoern A. Zeeb "Polled for IML load: irq=%d, loops=%d, CSR_LTR_LAST_MSG=0x%x\n", 4559af1bba4SBjoern A. Zeeb irq, loops, value); 4569af1bba4SBjoern A. Zeeb 4579af1bba4SBjoern A. Zeeb /* We don't fail here even if we timed out - maybe we get lucky and the 4589af1bba4SBjoern A. Zeeb * interrupt comes in later (and we get alive from firmware) and then 4599af1bba4SBjoern A. Zeeb * we're all happy - but if not we'll fail on alive timeout or get some 4609af1bba4SBjoern A. Zeeb * other error out. 4619af1bba4SBjoern A. Zeeb */ 462bfcc09ddSBjoern A. Zeeb } 463bfcc09ddSBjoern A. Zeeb 464bfcc09ddSBjoern A. Zeeb int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans, 465bfcc09ddSBjoern A. Zeeb const struct fw_img *fw, bool run_in_rfkill) 466bfcc09ddSBjoern A. Zeeb { 467bfcc09ddSBjoern A. Zeeb struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); 4689af1bba4SBjoern A. Zeeb bool hw_rfkill, keep_ram_busy; 469bfcc09ddSBjoern A. Zeeb int ret; 470bfcc09ddSBjoern A. Zeeb 471bfcc09ddSBjoern A. Zeeb /* This may fail if AMT took ownership of the device */ 472bfcc09ddSBjoern A. Zeeb if (iwl_pcie_prepare_card_hw(trans)) { 473bfcc09ddSBjoern A. Zeeb IWL_WARN(trans, "Exit HW not ready\n"); 474fac1f593SBjoern A. Zeeb return -EIO; 475bfcc09ddSBjoern A. Zeeb } 476bfcc09ddSBjoern A. Zeeb 477bfcc09ddSBjoern A. Zeeb iwl_enable_rfkill_int(trans); 478bfcc09ddSBjoern A. Zeeb 479bfcc09ddSBjoern A. Zeeb iwl_write32(trans, CSR_INT, 0xFFFFFFFF); 480bfcc09ddSBjoern A. Zeeb 481bfcc09ddSBjoern A. Zeeb /* 482bfcc09ddSBjoern A. Zeeb * We enabled the RF-Kill interrupt and the handler may very 483bfcc09ddSBjoern A. Zeeb * well be running. Disable the interrupts to make sure no other 484bfcc09ddSBjoern A. Zeeb * interrupt can be fired. 485bfcc09ddSBjoern A. Zeeb */ 486bfcc09ddSBjoern A. Zeeb iwl_disable_interrupts(trans); 487bfcc09ddSBjoern A. Zeeb 488bfcc09ddSBjoern A. Zeeb /* Make sure it finished running */ 489bfcc09ddSBjoern A. Zeeb iwl_pcie_synchronize_irqs(trans); 490bfcc09ddSBjoern A. Zeeb 491bfcc09ddSBjoern A. Zeeb mutex_lock(&trans_pcie->mutex); 492bfcc09ddSBjoern A. Zeeb 493bfcc09ddSBjoern A. Zeeb /* If platform's RF_KILL switch is NOT set to KILL */ 494bfcc09ddSBjoern A. Zeeb hw_rfkill = iwl_pcie_check_hw_rf_kill(trans); 495bfcc09ddSBjoern A. Zeeb if (hw_rfkill && !run_in_rfkill) { 496bfcc09ddSBjoern A. Zeeb ret = -ERFKILL; 497bfcc09ddSBjoern A. Zeeb goto out; 498bfcc09ddSBjoern A. Zeeb } 499bfcc09ddSBjoern A. Zeeb 500bfcc09ddSBjoern A. Zeeb /* Someone called stop_device, don't try to start_fw */ 501bfcc09ddSBjoern A. Zeeb if (trans_pcie->is_down) { 502bfcc09ddSBjoern A. Zeeb IWL_WARN(trans, 503bfcc09ddSBjoern A. Zeeb "Can't start_fw since the HW hasn't been started\n"); 504bfcc09ddSBjoern A. Zeeb ret = -EIO; 505bfcc09ddSBjoern A. Zeeb goto out; 506bfcc09ddSBjoern A. Zeeb } 507bfcc09ddSBjoern A. Zeeb 508bfcc09ddSBjoern A. Zeeb /* make sure rfkill handshake bits are cleared */ 509bfcc09ddSBjoern A. Zeeb iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL); 510bfcc09ddSBjoern A. Zeeb iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, 511bfcc09ddSBjoern A. Zeeb CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED); 512bfcc09ddSBjoern A. Zeeb 513bfcc09ddSBjoern A. Zeeb /* clear (again), then enable host interrupts */ 514bfcc09ddSBjoern A. Zeeb iwl_write32(trans, CSR_INT, 0xFFFFFFFF); 515bfcc09ddSBjoern A. Zeeb 516bfcc09ddSBjoern A. Zeeb ret = iwl_pcie_gen2_nic_init(trans); 517bfcc09ddSBjoern A. Zeeb if (ret) { 518bfcc09ddSBjoern A. Zeeb IWL_ERR(trans, "Unable to init nic\n"); 519bfcc09ddSBjoern A. Zeeb goto out; 520bfcc09ddSBjoern A. Zeeb } 521bfcc09ddSBjoern A. Zeeb 522bfcc09ddSBjoern A. Zeeb if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) 523bfcc09ddSBjoern A. Zeeb ret = iwl_pcie_ctxt_info_gen3_init(trans, fw); 524bfcc09ddSBjoern A. Zeeb else 525bfcc09ddSBjoern A. Zeeb ret = iwl_pcie_ctxt_info_init(trans, fw); 526bfcc09ddSBjoern A. Zeeb if (ret) 527bfcc09ddSBjoern A. Zeeb goto out; 528bfcc09ddSBjoern A. Zeeb 5299af1bba4SBjoern A. Zeeb keep_ram_busy = !iwl_pcie_set_ltr(trans); 530bfcc09ddSBjoern A. Zeeb 531bfcc09ddSBjoern A. Zeeb if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) { 532bfcc09ddSBjoern A. Zeeb iwl_write32(trans, CSR_FUNC_SCRATCH, CSR_FUNC_SCRATCH_INIT_VALUE); 533bfcc09ddSBjoern A. Zeeb iwl_set_bit(trans, CSR_GP_CNTRL, 534bfcc09ddSBjoern A. Zeeb CSR_GP_CNTRL_REG_FLAG_ROM_START); 535bfcc09ddSBjoern A. Zeeb } else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) { 536bfcc09ddSBjoern A. Zeeb iwl_write_umac_prph(trans, UREG_CPU_INIT_RUN, 1); 537bfcc09ddSBjoern A. Zeeb } else { 538bfcc09ddSBjoern A. Zeeb iwl_write_prph(trans, UREG_CPU_INIT_RUN, 1); 539bfcc09ddSBjoern A. Zeeb } 540bfcc09ddSBjoern A. Zeeb 5419af1bba4SBjoern A. Zeeb if (keep_ram_busy) 5429af1bba4SBjoern A. Zeeb iwl_pcie_spin_for_iml(trans); 5439af1bba4SBjoern A. Zeeb 544bfcc09ddSBjoern A. Zeeb /* re-check RF-Kill state since we may have missed the interrupt */ 545bfcc09ddSBjoern A. Zeeb hw_rfkill = iwl_pcie_check_hw_rf_kill(trans); 546bfcc09ddSBjoern A. Zeeb if (hw_rfkill && !run_in_rfkill) 547bfcc09ddSBjoern A. Zeeb ret = -ERFKILL; 548bfcc09ddSBjoern A. Zeeb 549bfcc09ddSBjoern A. Zeeb out: 550bfcc09ddSBjoern A. Zeeb mutex_unlock(&trans_pcie->mutex); 551bfcc09ddSBjoern A. Zeeb return ret; 552bfcc09ddSBjoern A. Zeeb } 553