xref: /freebsd/sys/contrib/dev/iwlwifi/pcie/trans-gen2.c (revision 9af1bba44e1ce9b0296ae56760b564d67ab7a1cf)
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*9af1bba4SBjoern A. Zeeb  * Copyright (C) 2018-2023 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) {
123*9af1bba4SBjoern A. Zeeb 		u32 inta_hw = iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD);
124*9af1bba4SBjoern A. Zeeb 
125*9af1bba4SBjoern A. Zeeb 		IWL_ERR(trans,
126*9af1bba4SBjoern A. Zeeb 			"timeout waiting for FW reset ACK (inta_hw=0x%x)\n",
127*9af1bba4SBjoern A. Zeeb 			inta_hw);
128*9af1bba4SBjoern A. Zeeb 
129*9af1bba4SBjoern 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*9af1bba4SBjoern A. Zeeb 		iwl_pcie_rx_napi_sync(trans);
168bfcc09ddSBjoern A. Zeeb 		iwl_txq_gen2_tx_free(trans);
169bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rx_stop(trans);
170bfcc09ddSBjoern A. Zeeb 	}
171bfcc09ddSBjoern A. Zeeb 
172bfcc09ddSBjoern A. Zeeb 	iwl_pcie_ctxt_info_free_paging(trans);
173bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
174bfcc09ddSBjoern A. Zeeb 		iwl_pcie_ctxt_info_gen3_free(trans, false);
175bfcc09ddSBjoern A. Zeeb 	else
176bfcc09ddSBjoern A. Zeeb 		iwl_pcie_ctxt_info_free(trans);
177bfcc09ddSBjoern A. Zeeb 
178bfcc09ddSBjoern A. Zeeb 	/* Stop the device, and put it in low power state */
179bfcc09ddSBjoern A. Zeeb 	iwl_pcie_gen2_apm_stop(trans, false);
180bfcc09ddSBjoern A. Zeeb 
181d9836fb4SBjoern A. Zeeb 	/* re-take ownership to prevent other users from stealing the device */
182d9836fb4SBjoern A. Zeeb 	iwl_trans_sw_reset(trans, true);
183bfcc09ddSBjoern A. Zeeb 
184bfcc09ddSBjoern A. Zeeb 	/*
185bfcc09ddSBjoern A. Zeeb 	 * Upon stop, the IVAR table gets erased, so msi-x won't
186bfcc09ddSBjoern A. Zeeb 	 * work. This causes a bug in RF-KILL flows, since the interrupt
187bfcc09ddSBjoern A. Zeeb 	 * that enables radio won't fire on the correct irq, and the
188bfcc09ddSBjoern A. Zeeb 	 * driver won't be able to handle the interrupt.
189bfcc09ddSBjoern A. Zeeb 	 * Configure the IVAR table again after reset.
190bfcc09ddSBjoern A. Zeeb 	 */
191bfcc09ddSBjoern A. Zeeb 	iwl_pcie_conf_msix_hw(trans_pcie);
192bfcc09ddSBjoern A. Zeeb 
193bfcc09ddSBjoern A. Zeeb 	/*
194bfcc09ddSBjoern A. Zeeb 	 * Upon stop, the APM issues an interrupt if HW RF kill is set.
195bfcc09ddSBjoern A. Zeeb 	 * This is a bug in certain verions of the hardware.
196bfcc09ddSBjoern A. Zeeb 	 * Certain devices also keep sending HW RF kill interrupt all
197bfcc09ddSBjoern A. Zeeb 	 * the time, unless the interrupt is ACKed even if the interrupt
198bfcc09ddSBjoern A. Zeeb 	 * should be masked. Re-ACK all the interrupts here.
199bfcc09ddSBjoern A. Zeeb 	 */
200bfcc09ddSBjoern A. Zeeb 	iwl_disable_interrupts(trans);
201bfcc09ddSBjoern A. Zeeb 
202bfcc09ddSBjoern A. Zeeb 	/* clear all status bits */
203bfcc09ddSBjoern A. Zeeb 	clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
204bfcc09ddSBjoern A. Zeeb 	clear_bit(STATUS_INT_ENABLED, &trans->status);
205bfcc09ddSBjoern A. Zeeb 	clear_bit(STATUS_TPOWER_PMI, &trans->status);
206bfcc09ddSBjoern A. Zeeb 
207bfcc09ddSBjoern A. Zeeb 	/*
208bfcc09ddSBjoern A. Zeeb 	 * Even if we stop the HW, we still want the RF kill
209bfcc09ddSBjoern A. Zeeb 	 * interrupt
210bfcc09ddSBjoern A. Zeeb 	 */
211bfcc09ddSBjoern A. Zeeb 	iwl_enable_rfkill_int(trans);
212bfcc09ddSBjoern A. Zeeb }
213bfcc09ddSBjoern A. Zeeb 
214bfcc09ddSBjoern A. Zeeb void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans)
215bfcc09ddSBjoern A. Zeeb {
216bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
217bfcc09ddSBjoern A. Zeeb 	bool was_in_rfkill;
218bfcc09ddSBjoern A. Zeeb 
219bfcc09ddSBjoern A. Zeeb 	iwl_op_mode_time_point(trans->op_mode,
220bfcc09ddSBjoern A. Zeeb 			       IWL_FW_INI_TIME_POINT_HOST_DEVICE_DISABLE,
221bfcc09ddSBjoern A. Zeeb 			       NULL);
222bfcc09ddSBjoern A. Zeeb 
223bfcc09ddSBjoern A. Zeeb 	mutex_lock(&trans_pcie->mutex);
224bfcc09ddSBjoern A. Zeeb 	trans_pcie->opmode_down = true;
225bfcc09ddSBjoern A. Zeeb 	was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
226bfcc09ddSBjoern A. Zeeb 	_iwl_trans_pcie_gen2_stop_device(trans);
227bfcc09ddSBjoern A. Zeeb 	iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill);
228bfcc09ddSBjoern A. Zeeb 	mutex_unlock(&trans_pcie->mutex);
229bfcc09ddSBjoern A. Zeeb }
230bfcc09ddSBjoern A. Zeeb 
231bfcc09ddSBjoern A. Zeeb static int iwl_pcie_gen2_nic_init(struct iwl_trans *trans)
232bfcc09ddSBjoern A. Zeeb {
233bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
234bfcc09ddSBjoern A. Zeeb 	int queue_size = max_t(u32, IWL_CMD_QUEUE_SIZE,
235bfcc09ddSBjoern A. Zeeb 			       trans->cfg->min_txq_size);
236bfcc09ddSBjoern A. Zeeb 
237bfcc09ddSBjoern A. Zeeb 	/* TODO: most of the logic can be removed in A0 - but not in Z0 */
238bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&trans_pcie->irq_lock);
239bfcc09ddSBjoern A. Zeeb 	iwl_pcie_gen2_apm_init(trans);
240bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&trans_pcie->irq_lock);
241bfcc09ddSBjoern A. Zeeb 
242bfcc09ddSBjoern A. Zeeb 	iwl_op_mode_nic_config(trans->op_mode);
243bfcc09ddSBjoern A. Zeeb 
244bfcc09ddSBjoern A. Zeeb 	/* Allocate the RX queue, or reset if it is already allocated */
245bfcc09ddSBjoern A. Zeeb 	if (iwl_pcie_gen2_rx_init(trans))
246bfcc09ddSBjoern A. Zeeb 		return -ENOMEM;
247bfcc09ddSBjoern A. Zeeb 
248bfcc09ddSBjoern A. Zeeb 	/* Allocate or reset and init all Tx and Command queues */
249bfcc09ddSBjoern A. Zeeb 	if (iwl_txq_gen2_init(trans, trans->txqs.cmd.q_id, queue_size))
250bfcc09ddSBjoern A. Zeeb 		return -ENOMEM;
251bfcc09ddSBjoern A. Zeeb 
252bfcc09ddSBjoern A. Zeeb 	/* enable shadow regs in HW */
253bfcc09ddSBjoern A. Zeeb 	iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
254bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
255bfcc09ddSBjoern A. Zeeb 
256bfcc09ddSBjoern A. Zeeb 	return 0;
257bfcc09ddSBjoern A. Zeeb }
258bfcc09ddSBjoern A. Zeeb 
259bfcc09ddSBjoern A. Zeeb static void iwl_pcie_get_rf_name(struct iwl_trans *trans)
260bfcc09ddSBjoern A. Zeeb {
261bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
262bfcc09ddSBjoern A. Zeeb 	char *buf = trans_pcie->rf_name;
263bfcc09ddSBjoern A. Zeeb 	size_t buflen = sizeof(trans_pcie->rf_name);
264bfcc09ddSBjoern A. Zeeb 	size_t pos;
265bfcc09ddSBjoern A. Zeeb 	u32 version;
266bfcc09ddSBjoern A. Zeeb 
267bfcc09ddSBjoern A. Zeeb 	if (buf[0])
268bfcc09ddSBjoern A. Zeeb 		return;
269bfcc09ddSBjoern A. Zeeb 
270bfcc09ddSBjoern A. Zeeb 	switch (CSR_HW_RFID_TYPE(trans->hw_rf_id)) {
271bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_JF):
272bfcc09ddSBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "JF");
273bfcc09ddSBjoern A. Zeeb 		break;
274bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_GF):
275bfcc09ddSBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "GF");
276bfcc09ddSBjoern A. Zeeb 		break;
277bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_GF4):
278bfcc09ddSBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "GF4");
279bfcc09ddSBjoern A. Zeeb 		break;
280bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR):
281bfcc09ddSBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "HR");
282bfcc09ddSBjoern A. Zeeb 		break;
283bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR1):
284bfcc09ddSBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "HR1");
285bfcc09ddSBjoern A. Zeeb 		break;
286bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HRCDB):
287bfcc09ddSBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "HRCDB");
288bfcc09ddSBjoern A. Zeeb 		break;
289*9af1bba4SBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_MS):
290*9af1bba4SBjoern A. Zeeb 		pos = scnprintf(buf, buflen, "MS");
291*9af1bba4SBjoern A. Zeeb 		break;
292bfcc09ddSBjoern A. Zeeb 	default:
293bfcc09ddSBjoern A. Zeeb 		return;
294bfcc09ddSBjoern A. Zeeb 	}
295bfcc09ddSBjoern A. Zeeb 
296bfcc09ddSBjoern A. Zeeb 	switch (CSR_HW_RFID_TYPE(trans->hw_rf_id)) {
297bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR):
298bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR1):
299bfcc09ddSBjoern A. Zeeb 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HRCDB):
300bfcc09ddSBjoern A. Zeeb 		version = iwl_read_prph(trans, CNVI_MBOX_C);
301bfcc09ddSBjoern A. Zeeb 		switch (version) {
302bfcc09ddSBjoern A. Zeeb 		case 0x20000:
303bfcc09ddSBjoern A. Zeeb 			pos += scnprintf(buf + pos, buflen - pos, " B3");
304bfcc09ddSBjoern A. Zeeb 			break;
305bfcc09ddSBjoern A. Zeeb 		case 0x120000:
306bfcc09ddSBjoern A. Zeeb 			pos += scnprintf(buf + pos, buflen - pos, " B5");
307bfcc09ddSBjoern A. Zeeb 			break;
308bfcc09ddSBjoern A. Zeeb 		default:
309bfcc09ddSBjoern A. Zeeb 			pos += scnprintf(buf + pos, buflen - pos,
310bfcc09ddSBjoern A. Zeeb 					 " (0x%x)", version);
311bfcc09ddSBjoern A. Zeeb 			break;
312bfcc09ddSBjoern A. Zeeb 		}
313bfcc09ddSBjoern A. Zeeb 		break;
314bfcc09ddSBjoern A. Zeeb 	default:
315bfcc09ddSBjoern A. Zeeb 		break;
316bfcc09ddSBjoern A. Zeeb 	}
317bfcc09ddSBjoern A. Zeeb 
318bfcc09ddSBjoern A. Zeeb 	pos += scnprintf(buf + pos, buflen - pos, ", rfid=0x%x",
319bfcc09ddSBjoern A. Zeeb 			 trans->hw_rf_id);
320bfcc09ddSBjoern A. Zeeb 
321bfcc09ddSBjoern A. Zeeb 	IWL_INFO(trans, "Detected RF %s\n", buf);
322bfcc09ddSBjoern A. Zeeb 
323bfcc09ddSBjoern A. Zeeb 	/*
324bfcc09ddSBjoern A. Zeeb 	 * also add a \n for debugfs - need to do it after printing
325bfcc09ddSBjoern A. Zeeb 	 * since our IWL_INFO machinery wants to see a static \n at
326bfcc09ddSBjoern A. Zeeb 	 * the end of the string
327bfcc09ddSBjoern A. Zeeb 	 */
328bfcc09ddSBjoern A. Zeeb 	pos += scnprintf(buf + pos, buflen - pos, "\n");
329bfcc09ddSBjoern A. Zeeb }
330bfcc09ddSBjoern A. Zeeb 
331bfcc09ddSBjoern A. Zeeb void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans, u32 scd_addr)
332bfcc09ddSBjoern A. Zeeb {
333bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
334bfcc09ddSBjoern A. Zeeb 
335bfcc09ddSBjoern A. Zeeb 	iwl_pcie_reset_ict(trans);
336bfcc09ddSBjoern A. Zeeb 
337bfcc09ddSBjoern A. Zeeb 	/* make sure all queue are not stopped/used */
338bfcc09ddSBjoern A. Zeeb 	memset(trans->txqs.queue_stopped, 0,
339bfcc09ddSBjoern A. Zeeb 	       sizeof(trans->txqs.queue_stopped));
340bfcc09ddSBjoern A. Zeeb 	memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
341bfcc09ddSBjoern A. Zeeb 
342bfcc09ddSBjoern A. Zeeb 	/* now that we got alive we can free the fw image & the context info.
343bfcc09ddSBjoern A. Zeeb 	 * paging memory cannot be freed included since FW will still use it
344bfcc09ddSBjoern A. Zeeb 	 */
345bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
346bfcc09ddSBjoern A. Zeeb 		iwl_pcie_ctxt_info_gen3_free(trans, true);
347bfcc09ddSBjoern A. Zeeb 	else
348bfcc09ddSBjoern A. Zeeb 		iwl_pcie_ctxt_info_free(trans);
349bfcc09ddSBjoern A. Zeeb 
350bfcc09ddSBjoern A. Zeeb 	/*
351bfcc09ddSBjoern A. Zeeb 	 * Re-enable all the interrupts, including the RF-Kill one, now that
352bfcc09ddSBjoern A. Zeeb 	 * the firmware is alive.
353bfcc09ddSBjoern A. Zeeb 	 */
354bfcc09ddSBjoern A. Zeeb 	iwl_enable_interrupts(trans);
355bfcc09ddSBjoern A. Zeeb 	mutex_lock(&trans_pcie->mutex);
356bfcc09ddSBjoern A. Zeeb 	iwl_pcie_check_hw_rf_kill(trans);
357bfcc09ddSBjoern A. Zeeb 
358bfcc09ddSBjoern A. Zeeb 	iwl_pcie_get_rf_name(trans);
359bfcc09ddSBjoern A. Zeeb 	mutex_unlock(&trans_pcie->mutex);
360bfcc09ddSBjoern A. Zeeb }
361bfcc09ddSBjoern A. Zeeb 
362*9af1bba4SBjoern A. Zeeb static bool iwl_pcie_set_ltr(struct iwl_trans *trans)
363bfcc09ddSBjoern A. Zeeb {
364bfcc09ddSBjoern A. Zeeb 	u32 ltr_val = CSR_LTR_LONG_VAL_AD_NO_SNOOP_REQ |
365bfcc09ddSBjoern A. Zeeb 		      u32_encode_bits(CSR_LTR_LONG_VAL_AD_SCALE_USEC,
366bfcc09ddSBjoern A. Zeeb 				      CSR_LTR_LONG_VAL_AD_NO_SNOOP_SCALE) |
367bfcc09ddSBjoern A. Zeeb 		      u32_encode_bits(250,
368bfcc09ddSBjoern A. Zeeb 				      CSR_LTR_LONG_VAL_AD_NO_SNOOP_VAL) |
369bfcc09ddSBjoern A. Zeeb 		      CSR_LTR_LONG_VAL_AD_SNOOP_REQ |
370bfcc09ddSBjoern A. Zeeb 		      u32_encode_bits(CSR_LTR_LONG_VAL_AD_SCALE_USEC,
371bfcc09ddSBjoern A. Zeeb 				      CSR_LTR_LONG_VAL_AD_SNOOP_SCALE) |
372bfcc09ddSBjoern A. Zeeb 		      u32_encode_bits(250, CSR_LTR_LONG_VAL_AD_SNOOP_VAL);
373bfcc09ddSBjoern A. Zeeb 
374bfcc09ddSBjoern A. Zeeb 	/*
375bfcc09ddSBjoern A. Zeeb 	 * To workaround hardware latency issues during the boot process,
376bfcc09ddSBjoern A. Zeeb 	 * initialize the LTR to ~250 usec (see ltr_val above).
377bfcc09ddSBjoern A. Zeeb 	 * The firmware initializes this again later (to a smaller value).
378bfcc09ddSBjoern A. Zeeb 	 */
379bfcc09ddSBjoern A. Zeeb 	if ((trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210 ||
380bfcc09ddSBjoern A. Zeeb 	     trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_22000) &&
381bfcc09ddSBjoern A. Zeeb 	    !trans->trans_cfg->integrated) {
382bfcc09ddSBjoern A. Zeeb 		iwl_write32(trans, CSR_LTR_LONG_VAL_AD, ltr_val);
383*9af1bba4SBjoern A. Zeeb 		return true;
384*9af1bba4SBjoern A. Zeeb 	}
385*9af1bba4SBjoern A. Zeeb 
386*9af1bba4SBjoern A. Zeeb 	if (trans->trans_cfg->integrated &&
387bfcc09ddSBjoern A. Zeeb 	    trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_22000) {
388bfcc09ddSBjoern A. Zeeb 		iwl_write_prph(trans, HPM_MAC_LTR_CSR, HPM_MAC_LRT_ENABLE_ALL);
389bfcc09ddSBjoern A. Zeeb 		iwl_write_prph(trans, HPM_UMAC_LTR, ltr_val);
390*9af1bba4SBjoern A. Zeeb 		return true;
391bfcc09ddSBjoern A. Zeeb 	}
392*9af1bba4SBjoern A. Zeeb 
393*9af1bba4SBjoern A. Zeeb 	if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210) {
394*9af1bba4SBjoern A. Zeeb 		/* First clear the interrupt, just in case */
395*9af1bba4SBjoern A. Zeeb 		iwl_write32(trans, CSR_MSIX_HW_INT_CAUSES_AD,
396*9af1bba4SBjoern A. Zeeb 			    MSIX_HW_INT_CAUSES_REG_IML);
397*9af1bba4SBjoern A. Zeeb 		/* In this case, unfortunately the same ROM bug exists in the
398*9af1bba4SBjoern A. Zeeb 		 * device (not setting LTR correctly), but we don't have control
399*9af1bba4SBjoern A. Zeeb 		 * over the settings from the host due to some hardware security
400*9af1bba4SBjoern A. Zeeb 		 * features. The only workaround we've been able to come up with
401*9af1bba4SBjoern A. Zeeb 		 * so far is to try to keep the CPU and device busy by polling
402*9af1bba4SBjoern A. Zeeb 		 * it and the IML (image loader) completed interrupt.
403*9af1bba4SBjoern A. Zeeb 		 */
404*9af1bba4SBjoern A. Zeeb 		return false;
405*9af1bba4SBjoern A. Zeeb 	}
406*9af1bba4SBjoern A. Zeeb 
407*9af1bba4SBjoern A. Zeeb 	/* nothing needs to be done on other devices */
408*9af1bba4SBjoern A. Zeeb 	return true;
409*9af1bba4SBjoern A. Zeeb }
410*9af1bba4SBjoern A. Zeeb 
411*9af1bba4SBjoern A. Zeeb static void iwl_pcie_spin_for_iml(struct iwl_trans *trans)
412*9af1bba4SBjoern A. Zeeb {
413*9af1bba4SBjoern A. Zeeb /* in practice, this seems to complete in around 20-30ms at most, wait 100 */
414*9af1bba4SBjoern A. Zeeb #define IML_WAIT_TIMEOUT	(HZ / 10)
415*9af1bba4SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
416*9af1bba4SBjoern A. Zeeb 	unsigned long end_time = jiffies + IML_WAIT_TIMEOUT;
417*9af1bba4SBjoern A. Zeeb 	u32 value, loops = 0;
418*9af1bba4SBjoern A. Zeeb 	bool irq = false;
419*9af1bba4SBjoern A. Zeeb 
420*9af1bba4SBjoern A. Zeeb 	if (WARN_ON(!trans_pcie->iml))
421*9af1bba4SBjoern A. Zeeb 		return;
422*9af1bba4SBjoern A. Zeeb 
423*9af1bba4SBjoern A. Zeeb 	value = iwl_read32(trans, CSR_LTR_LAST_MSG);
424*9af1bba4SBjoern A. Zeeb 	IWL_DEBUG_INFO(trans, "Polling for IML load - CSR_LTR_LAST_MSG=0x%x\n",
425*9af1bba4SBjoern A. Zeeb 		       value);
426*9af1bba4SBjoern A. Zeeb 
427*9af1bba4SBjoern A. Zeeb 	while (time_before(jiffies, end_time)) {
428*9af1bba4SBjoern A. Zeeb 		if (iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD) &
429*9af1bba4SBjoern A. Zeeb 				MSIX_HW_INT_CAUSES_REG_IML) {
430*9af1bba4SBjoern A. Zeeb 			irq = true;
431*9af1bba4SBjoern A. Zeeb 			break;
432*9af1bba4SBjoern A. Zeeb 		}
433*9af1bba4SBjoern A. Zeeb 		/* Keep the CPU and device busy. */
434*9af1bba4SBjoern A. Zeeb 		value = iwl_read32(trans, CSR_LTR_LAST_MSG);
435*9af1bba4SBjoern A. Zeeb 		loops++;
436*9af1bba4SBjoern A. Zeeb 	}
437*9af1bba4SBjoern A. Zeeb 
438*9af1bba4SBjoern A. Zeeb 	IWL_DEBUG_INFO(trans,
439*9af1bba4SBjoern A. Zeeb 		       "Polled for IML load: irq=%d, loops=%d, CSR_LTR_LAST_MSG=0x%x\n",
440*9af1bba4SBjoern A. Zeeb 		       irq, loops, value);
441*9af1bba4SBjoern A. Zeeb 
442*9af1bba4SBjoern A. Zeeb 	/* We don't fail here even if we timed out - maybe we get lucky and the
443*9af1bba4SBjoern A. Zeeb 	 * interrupt comes in later (and we get alive from firmware) and then
444*9af1bba4SBjoern A. Zeeb 	 * we're all happy - but if not we'll fail on alive timeout or get some
445*9af1bba4SBjoern A. Zeeb 	 * other error out.
446*9af1bba4SBjoern A. Zeeb 	 */
447bfcc09ddSBjoern A. Zeeb }
448bfcc09ddSBjoern A. Zeeb 
449bfcc09ddSBjoern A. Zeeb int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans,
450bfcc09ddSBjoern A. Zeeb 				 const struct fw_img *fw, bool run_in_rfkill)
451bfcc09ddSBjoern A. Zeeb {
452bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
453*9af1bba4SBjoern A. Zeeb 	bool hw_rfkill, keep_ram_busy;
454bfcc09ddSBjoern A. Zeeb 	int ret;
455bfcc09ddSBjoern A. Zeeb 
456bfcc09ddSBjoern A. Zeeb 	/* This may fail if AMT took ownership of the device */
457bfcc09ddSBjoern A. Zeeb 	if (iwl_pcie_prepare_card_hw(trans)) {
458bfcc09ddSBjoern A. Zeeb 		IWL_WARN(trans, "Exit HW not ready\n");
459fac1f593SBjoern A. Zeeb 		return -EIO;
460bfcc09ddSBjoern A. Zeeb 	}
461bfcc09ddSBjoern A. Zeeb 
462bfcc09ddSBjoern A. Zeeb 	iwl_enable_rfkill_int(trans);
463bfcc09ddSBjoern A. Zeeb 
464bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
465bfcc09ddSBjoern A. Zeeb 
466bfcc09ddSBjoern A. Zeeb 	/*
467bfcc09ddSBjoern A. Zeeb 	 * We enabled the RF-Kill interrupt and the handler may very
468bfcc09ddSBjoern A. Zeeb 	 * well be running. Disable the interrupts to make sure no other
469bfcc09ddSBjoern A. Zeeb 	 * interrupt can be fired.
470bfcc09ddSBjoern A. Zeeb 	 */
471bfcc09ddSBjoern A. Zeeb 	iwl_disable_interrupts(trans);
472bfcc09ddSBjoern A. Zeeb 
473bfcc09ddSBjoern A. Zeeb 	/* Make sure it finished running */
474bfcc09ddSBjoern A. Zeeb 	iwl_pcie_synchronize_irqs(trans);
475bfcc09ddSBjoern A. Zeeb 
476bfcc09ddSBjoern A. Zeeb 	mutex_lock(&trans_pcie->mutex);
477bfcc09ddSBjoern A. Zeeb 
478bfcc09ddSBjoern A. Zeeb 	/* If platform's RF_KILL switch is NOT set to KILL */
479bfcc09ddSBjoern A. Zeeb 	hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
480bfcc09ddSBjoern A. Zeeb 	if (hw_rfkill && !run_in_rfkill) {
481bfcc09ddSBjoern A. Zeeb 		ret = -ERFKILL;
482bfcc09ddSBjoern A. Zeeb 		goto out;
483bfcc09ddSBjoern A. Zeeb 	}
484bfcc09ddSBjoern A. Zeeb 
485bfcc09ddSBjoern A. Zeeb 	/* Someone called stop_device, don't try to start_fw */
486bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->is_down) {
487bfcc09ddSBjoern A. Zeeb 		IWL_WARN(trans,
488bfcc09ddSBjoern A. Zeeb 			 "Can't start_fw since the HW hasn't been started\n");
489bfcc09ddSBjoern A. Zeeb 		ret = -EIO;
490bfcc09ddSBjoern A. Zeeb 		goto out;
491bfcc09ddSBjoern A. Zeeb 	}
492bfcc09ddSBjoern A. Zeeb 
493bfcc09ddSBjoern A. Zeeb 	/* make sure rfkill handshake bits are cleared */
494bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
495bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
496bfcc09ddSBjoern A. Zeeb 		    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
497bfcc09ddSBjoern A. Zeeb 
498bfcc09ddSBjoern A. Zeeb 	/* clear (again), then enable host interrupts */
499bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
500bfcc09ddSBjoern A. Zeeb 
501bfcc09ddSBjoern A. Zeeb 	ret = iwl_pcie_gen2_nic_init(trans);
502bfcc09ddSBjoern A. Zeeb 	if (ret) {
503bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans, "Unable to init nic\n");
504bfcc09ddSBjoern A. Zeeb 		goto out;
505bfcc09ddSBjoern A. Zeeb 	}
506bfcc09ddSBjoern A. Zeeb 
507bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
508bfcc09ddSBjoern A. Zeeb 		ret = iwl_pcie_ctxt_info_gen3_init(trans, fw);
509bfcc09ddSBjoern A. Zeeb 	else
510bfcc09ddSBjoern A. Zeeb 		ret = iwl_pcie_ctxt_info_init(trans, fw);
511bfcc09ddSBjoern A. Zeeb 	if (ret)
512bfcc09ddSBjoern A. Zeeb 		goto out;
513bfcc09ddSBjoern A. Zeeb 
514*9af1bba4SBjoern A. Zeeb 	keep_ram_busy = !iwl_pcie_set_ltr(trans);
515bfcc09ddSBjoern A. Zeeb 
516bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
517bfcc09ddSBjoern A. Zeeb 		iwl_write32(trans, CSR_FUNC_SCRATCH, CSR_FUNC_SCRATCH_INIT_VALUE);
518bfcc09ddSBjoern A. Zeeb 		iwl_set_bit(trans, CSR_GP_CNTRL,
519bfcc09ddSBjoern A. Zeeb 			    CSR_GP_CNTRL_REG_FLAG_ROM_START);
520bfcc09ddSBjoern A. Zeeb 	} else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
521bfcc09ddSBjoern A. Zeeb 		iwl_write_umac_prph(trans, UREG_CPU_INIT_RUN, 1);
522bfcc09ddSBjoern A. Zeeb 	} else {
523bfcc09ddSBjoern A. Zeeb 		iwl_write_prph(trans, UREG_CPU_INIT_RUN, 1);
524bfcc09ddSBjoern A. Zeeb 	}
525bfcc09ddSBjoern A. Zeeb 
526*9af1bba4SBjoern A. Zeeb 	if (keep_ram_busy)
527*9af1bba4SBjoern A. Zeeb 		iwl_pcie_spin_for_iml(trans);
528*9af1bba4SBjoern A. Zeeb 
529bfcc09ddSBjoern A. Zeeb 	/* re-check RF-Kill state since we may have missed the interrupt */
530bfcc09ddSBjoern A. Zeeb 	hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
531bfcc09ddSBjoern A. Zeeb 	if (hw_rfkill && !run_in_rfkill)
532bfcc09ddSBjoern A. Zeeb 		ret = -ERFKILL;
533bfcc09ddSBjoern A. Zeeb 
534bfcc09ddSBjoern A. Zeeb out:
535bfcc09ddSBjoern A. Zeeb 	mutex_unlock(&trans_pcie->mutex);
536bfcc09ddSBjoern A. Zeeb 	return ret;
537bfcc09ddSBjoern A. Zeeb }
538