18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
38e99ea8dSJohannes Berg  * Copyright (C) 2017 Intel Deutschland GmbH
4c2a1c8c1SJohannes Berg  * Copyright (C) 2018-2023 Intel Corporation
58e99ea8dSJohannes Berg  */
6eda50cdeSSara Sharon #include "iwl-trans.h"
70232d2cdSSara Sharon #include "iwl-prph.h"
8eda50cdeSSara Sharon #include "iwl-context-info.h"
92ee82402SGolan Ben Ami #include "iwl-context-info-gen3.h"
10eda50cdeSSara Sharon #include "internal.h"
11ae17404eSShahar S Matityahu #include "fw/dbg.h"
12eda50cdeSSara Sharon 
1325df65aeSMatti Gottlieb #define FW_RESET_TIMEOUT (HZ / 5)
1425df65aeSMatti Gottlieb 
15eda50cdeSSara Sharon /*
16eda50cdeSSara Sharon  * Start up NIC's basic functionality after it has been reset
17eda50cdeSSara Sharon  * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
18eda50cdeSSara Sharon  * NOTE:  This does not load uCode nor start the embedded processor
19eda50cdeSSara Sharon  */
iwl_pcie_gen2_apm_init(struct iwl_trans * trans)20b6fe2757SGolan Ben Ami int iwl_pcie_gen2_apm_init(struct iwl_trans *trans)
21eda50cdeSSara Sharon {
22eda50cdeSSara Sharon 	int ret = 0;
23eda50cdeSSara Sharon 
24eda50cdeSSara Sharon 	IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
25eda50cdeSSara Sharon 
26eda50cdeSSara Sharon 	/*
27eda50cdeSSara Sharon 	 * Use "set_bit" below rather than "write", to preserve any hardware
28eda50cdeSSara Sharon 	 * bits already set by default after reset.
29eda50cdeSSara Sharon 	 */
30eda50cdeSSara Sharon 
31eda50cdeSSara Sharon 	/*
32eda50cdeSSara Sharon 	 * Disable L0s without affecting L1;
33eda50cdeSSara Sharon 	 * don't wait for ICH L0s (ICH bug W/A)
34eda50cdeSSara Sharon 	 */
35eda50cdeSSara Sharon 	iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
36eda50cdeSSara Sharon 		    CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
37eda50cdeSSara Sharon 
38eda50cdeSSara Sharon 	/* Set FH wait threshold to maximum (HW error during stress W/A) */
39eda50cdeSSara Sharon 	iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
40eda50cdeSSara Sharon 
41eda50cdeSSara Sharon 	/*
42eda50cdeSSara Sharon 	 * Enable HAP INTA (interrupt from management bus) to
43eda50cdeSSara Sharon 	 * wake device's PCI Express link L1a -> L0s
44eda50cdeSSara Sharon 	 */
45eda50cdeSSara Sharon 	iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
46eda50cdeSSara Sharon 		    CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
47eda50cdeSSara Sharon 
48eda50cdeSSara Sharon 	iwl_pcie_apm_config(trans);
49eda50cdeSSara Sharon 
50425d66d8SJohannes Berg 	ret = iwl_finish_nic_init(trans);
51c96b5eecSJohannes Berg 	if (ret)
52eda50cdeSSara Sharon 		return ret;
53eda50cdeSSara Sharon 
54eda50cdeSSara Sharon 	set_bit(STATUS_DEVICE_ENABLED, &trans->status);
55eda50cdeSSara Sharon 
56eda50cdeSSara Sharon 	return 0;
57eda50cdeSSara Sharon }
58eda50cdeSSara Sharon 
iwl_pcie_gen2_apm_stop(struct iwl_trans * trans,bool op_mode_leave)5977c09bc8SSara Sharon static void iwl_pcie_gen2_apm_stop(struct iwl_trans *trans, bool op_mode_leave)
6077c09bc8SSara Sharon {
6177c09bc8SSara Sharon 	IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
6277c09bc8SSara Sharon 
6377c09bc8SSara Sharon 	if (op_mode_leave) {
6477c09bc8SSara Sharon 		if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
6577c09bc8SSara Sharon 			iwl_pcie_gen2_apm_init(trans);
6677c09bc8SSara Sharon 
6777c09bc8SSara Sharon 		/* inform ME that we are leaving */
6877c09bc8SSara Sharon 		iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
6977c09bc8SSara Sharon 			    CSR_RESET_LINK_PWR_MGMT_DISABLED);
7077c09bc8SSara Sharon 		iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
7177c09bc8SSara Sharon 			    CSR_HW_IF_CONFIG_REG_PREPARE |
7277c09bc8SSara Sharon 			    CSR_HW_IF_CONFIG_REG_ENABLE_PME);
7377c09bc8SSara Sharon 		mdelay(1);
7477c09bc8SSara Sharon 		iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
7577c09bc8SSara Sharon 			      CSR_RESET_LINK_PWR_MGMT_DISABLED);
7677c09bc8SSara Sharon 		mdelay(5);
7777c09bc8SSara Sharon 	}
7877c09bc8SSara Sharon 
7977c09bc8SSara Sharon 	clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
8077c09bc8SSara Sharon 
8177c09bc8SSara Sharon 	/* Stop device's DMA activity */
8277c09bc8SSara Sharon 	iwl_pcie_apm_stop_master(trans);
8377c09bc8SSara Sharon 
8415bf5ac6SJohannes Berg 	iwl_trans_sw_reset(trans, false);
8577c09bc8SSara Sharon 
8677c09bc8SSara Sharon 	/*
8777c09bc8SSara Sharon 	 * Clear "initialization complete" bit to move adapter from
8877c09bc8SSara Sharon 	 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
8977c09bc8SSara Sharon 	 */
909ce041f5SJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
919ce041f5SJohannes Berg 		iwl_clear_bit(trans, CSR_GP_CNTRL,
929ce041f5SJohannes Berg 			      CSR_GP_CNTRL_REG_FLAG_MAC_INIT);
939ce041f5SJohannes Berg 	else
949ce041f5SJohannes Berg 		iwl_clear_bit(trans, CSR_GP_CNTRL,
959ce041f5SJohannes Berg 			      CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
9677c09bc8SSara Sharon }
9777c09bc8SSara Sharon 
iwl_trans_pcie_fw_reset_handshake(struct iwl_trans * trans)98906d4eb8SJohannes Berg static void iwl_trans_pcie_fw_reset_handshake(struct iwl_trans *trans)
99906d4eb8SJohannes Berg {
100906d4eb8SJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
101906d4eb8SJohannes Berg 	int ret;
102906d4eb8SJohannes Berg 
103e63aafeaSJohannes Berg 	trans_pcie->fw_reset_state = FW_RESET_REQUESTED;
104906d4eb8SJohannes Berg 
105906d4eb8SJohannes Berg 	if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
106906d4eb8SJohannes Berg 		iwl_write_umac_prph(trans, UREG_NIC_SET_NMI_DRIVER,
107906d4eb8SJohannes Berg 				    UREG_NIC_SET_NMI_DRIVER_RESET_HANDSHAKE);
10823a392a4SJohannes Berg 	else if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210)
109906d4eb8SJohannes Berg 		iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
110906d4eb8SJohannes Berg 				    UREG_DOORBELL_TO_ISR6_RESET_HANDSHAKE);
11123a392a4SJohannes Berg 	else
11223a392a4SJohannes Berg 		iwl_write32(trans, CSR_DOORBELL_VECTOR,
11323a392a4SJohannes Berg 			    UREG_DOORBELL_TO_ISR6_RESET_HANDSHAKE);
114906d4eb8SJohannes Berg 
115906d4eb8SJohannes Berg 	/* wait 200ms */
116906d4eb8SJohannes Berg 	ret = wait_event_timeout(trans_pcie->fw_reset_waitq,
117e63aafeaSJohannes Berg 				 trans_pcie->fw_reset_state != FW_RESET_REQUESTED,
118e63aafeaSJohannes Berg 				 FW_RESET_TIMEOUT);
119e63aafeaSJohannes Berg 	if (!ret || trans_pcie->fw_reset_state == FW_RESET_ERROR) {
120c2a1c8c1SJohannes Berg 		u32 inta_hw = iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD);
121c2a1c8c1SJohannes Berg 
122c2a1c8c1SJohannes Berg 		IWL_ERR(trans,
123c2a1c8c1SJohannes Berg 			"timeout waiting for FW reset ACK (inta_hw=0x%x)\n",
124c2a1c8c1SJohannes Berg 			inta_hw);
125c2a1c8c1SJohannes Berg 
126c2a1c8c1SJohannes Berg 		if (!(inta_hw & MSIX_HW_INT_CAUSES_REG_RESET_DONE))
127e63aafeaSJohannes Berg 			iwl_trans_fw_error(trans, true);
128e63aafeaSJohannes Berg 	}
129e63aafeaSJohannes Berg 
130e63aafeaSJohannes Berg 	trans_pcie->fw_reset_state = FW_RESET_IDLE;
131906d4eb8SJohannes Berg }
132906d4eb8SJohannes Berg 
_iwl_trans_pcie_gen2_stop_device(struct iwl_trans * trans)133bab3cb92SEmmanuel Grumbach void _iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans)
13477c09bc8SSara Sharon {
13577c09bc8SSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
13677c09bc8SSara Sharon 
13777c09bc8SSara Sharon 	lockdep_assert_held(&trans_pcie->mutex);
13877c09bc8SSara Sharon 
13977c09bc8SSara Sharon 	if (trans_pcie->is_down)
14077c09bc8SSara Sharon 		return;
14177c09bc8SSara Sharon 
14244b2dd40SRoee Goldfiner 	if (trans->state >= IWL_TRANS_FW_STARTED)
14344b2dd40SRoee Goldfiner 		if (trans_pcie->fw_reset_handshake)
144906d4eb8SJohannes Berg 			iwl_trans_pcie_fw_reset_handshake(trans);
145906d4eb8SJohannes Berg 
14677c09bc8SSara Sharon 	trans_pcie->is_down = true;
14777c09bc8SSara Sharon 
14877c09bc8SSara Sharon 	/* tell the device to stop sending interrupts */
14977c09bc8SSara Sharon 	iwl_disable_interrupts(trans);
15077c09bc8SSara Sharon 
15177c09bc8SSara Sharon 	/* device going down, Stop using ICT table */
15277c09bc8SSara Sharon 	iwl_pcie_disable_ict(trans);
15377c09bc8SSara Sharon 
15477c09bc8SSara Sharon 	/*
15577c09bc8SSara Sharon 	 * If a HW restart happens during firmware loading,
15677c09bc8SSara Sharon 	 * then the firmware loading might call this function
15777c09bc8SSara Sharon 	 * and later it might be called again due to the
15877c09bc8SSara Sharon 	 * restart. So don't process again if the device is
15977c09bc8SSara Sharon 	 * already dead.
16077c09bc8SSara Sharon 	 */
16177c09bc8SSara Sharon 	if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
16277c09bc8SSara Sharon 		IWL_DEBUG_INFO(trans,
16377c09bc8SSara Sharon 			       "DEVICE_ENABLED bit was set and is now cleared\n");
164*f1f2e068SJohannes Berg 		iwl_pcie_synchronize_irqs(trans);
1655af2bb31SGregory Greenman 		iwl_pcie_rx_napi_sync(trans);
1664cf2f590SMordechay Goodstein 		iwl_txq_gen2_tx_free(trans);
16777c09bc8SSara Sharon 		iwl_pcie_rx_stop(trans);
16877c09bc8SSara Sharon 	}
16977c09bc8SSara Sharon 
17077c09bc8SSara Sharon 	iwl_pcie_ctxt_info_free_paging(trans);
1713681021fSJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
17294768975SJohannes Berg 		iwl_pcie_ctxt_info_gen3_free(trans, false);
1732ee82402SGolan Ben Ami 	else
17477c09bc8SSara Sharon 		iwl_pcie_ctxt_info_free(trans);
17577c09bc8SSara Sharon 
17677c09bc8SSara Sharon 	/* Stop the device, and put it in low power state */
17777c09bc8SSara Sharon 	iwl_pcie_gen2_apm_stop(trans, false);
17877c09bc8SSara Sharon 
17915bf5ac6SJohannes Berg 	/* re-take ownership to prevent other users from stealing the device */
18015bf5ac6SJohannes Berg 	iwl_trans_sw_reset(trans, true);
18177c09bc8SSara Sharon 
18277c09bc8SSara Sharon 	/*
18377c09bc8SSara Sharon 	 * Upon stop, the IVAR table gets erased, so msi-x won't
18477c09bc8SSara Sharon 	 * work. This causes a bug in RF-KILL flows, since the interrupt
18577c09bc8SSara Sharon 	 * that enables radio won't fire on the correct irq, and the
18677c09bc8SSara Sharon 	 * driver won't be able to handle the interrupt.
18777c09bc8SSara Sharon 	 * Configure the IVAR table again after reset.
18877c09bc8SSara Sharon 	 */
18977c09bc8SSara Sharon 	iwl_pcie_conf_msix_hw(trans_pcie);
19077c09bc8SSara Sharon 
19177c09bc8SSara Sharon 	/*
19277c09bc8SSara Sharon 	 * Upon stop, the APM issues an interrupt if HW RF kill is set.
19377c09bc8SSara Sharon 	 * This is a bug in certain verions of the hardware.
19477c09bc8SSara Sharon 	 * Certain devices also keep sending HW RF kill interrupt all
19577c09bc8SSara Sharon 	 * the time, unless the interrupt is ACKed even if the interrupt
19677c09bc8SSara Sharon 	 * should be masked. Re-ACK all the interrupts here.
19777c09bc8SSara Sharon 	 */
19877c09bc8SSara Sharon 	iwl_disable_interrupts(trans);
19977c09bc8SSara Sharon 
20077c09bc8SSara Sharon 	/* clear all status bits */
20177c09bc8SSara Sharon 	clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
20277c09bc8SSara Sharon 	clear_bit(STATUS_INT_ENABLED, &trans->status);
20377c09bc8SSara Sharon 	clear_bit(STATUS_TPOWER_PMI, &trans->status);
20477c09bc8SSara Sharon 
20577c09bc8SSara Sharon 	/*
20677c09bc8SSara Sharon 	 * Even if we stop the HW, we still want the RF kill
20777c09bc8SSara Sharon 	 * interrupt
20877c09bc8SSara Sharon 	 */
20977c09bc8SSara Sharon 	iwl_enable_rfkill_int(trans);
21077c09bc8SSara Sharon }
21177c09bc8SSara Sharon 
iwl_trans_pcie_gen2_stop_device(struct iwl_trans * trans)212bab3cb92SEmmanuel Grumbach void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans)
21377c09bc8SSara Sharon {
21477c09bc8SSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
215326477e4SJohannes Berg 	bool was_in_rfkill;
21677c09bc8SSara Sharon 
217d0129315SMordechay Goodstein 	iwl_op_mode_time_point(trans->op_mode,
218d0129315SMordechay Goodstein 			       IWL_FW_INI_TIME_POINT_HOST_DEVICE_DISABLE,
219d0129315SMordechay Goodstein 			       NULL);
220d0129315SMordechay Goodstein 
22177c09bc8SSara Sharon 	mutex_lock(&trans_pcie->mutex);
222326477e4SJohannes Berg 	trans_pcie->opmode_down = true;
223326477e4SJohannes Berg 	was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
224bab3cb92SEmmanuel Grumbach 	_iwl_trans_pcie_gen2_stop_device(trans);
225326477e4SJohannes Berg 	iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill);
22677c09bc8SSara Sharon 	mutex_unlock(&trans_pcie->mutex);
22777c09bc8SSara Sharon }
22877c09bc8SSara Sharon 
iwl_pcie_gen2_nic_init(struct iwl_trans * trans)229eda50cdeSSara Sharon static int iwl_pcie_gen2_nic_init(struct iwl_trans *trans)
230eda50cdeSSara Sharon {
231eda50cdeSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
232718a8b23SShaul Triebitz 	int queue_size = max_t(u32, IWL_CMD_QUEUE_SIZE,
233718a8b23SShaul Triebitz 			       trans->cfg->min_txq_size);
234eda50cdeSSara Sharon 
235eda50cdeSSara Sharon 	/* TODO: most of the logic can be removed in A0 - but not in Z0 */
23625edc8f2SJohannes Berg 	spin_lock_bh(&trans_pcie->irq_lock);
237eda50cdeSSara Sharon 	iwl_pcie_gen2_apm_init(trans);
23825edc8f2SJohannes Berg 	spin_unlock_bh(&trans_pcie->irq_lock);
239eda50cdeSSara Sharon 
240eda50cdeSSara Sharon 	iwl_op_mode_nic_config(trans->op_mode);
241eda50cdeSSara Sharon 
242eda50cdeSSara Sharon 	/* Allocate the RX queue, or reset if it is already allocated */
243eda50cdeSSara Sharon 	if (iwl_pcie_gen2_rx_init(trans))
244eda50cdeSSara Sharon 		return -ENOMEM;
245eda50cdeSSara Sharon 
246eda50cdeSSara Sharon 	/* Allocate or reset and init all Tx and Command queues */
2470cd1ad2dSMordechay Goodstein 	if (iwl_txq_gen2_init(trans, trans->txqs.cmd.q_id, queue_size))
248eda50cdeSSara Sharon 		return -ENOMEM;
249eda50cdeSSara Sharon 
250eda50cdeSSara Sharon 	/* enable shadow regs in HW */
251eda50cdeSSara Sharon 	iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
252eda50cdeSSara Sharon 	IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
253eda50cdeSSara Sharon 
254eda50cdeSSara Sharon 	return 0;
255eda50cdeSSara Sharon }
256eda50cdeSSara Sharon 
iwl_pcie_get_rf_name(struct iwl_trans * trans)257aa899e68SJohannes Berg static void iwl_pcie_get_rf_name(struct iwl_trans *trans)
258aa899e68SJohannes Berg {
259aa899e68SJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
260aa899e68SJohannes Berg 	char *buf = trans_pcie->rf_name;
261aa899e68SJohannes Berg 	size_t buflen = sizeof(trans_pcie->rf_name);
262aa899e68SJohannes Berg 	size_t pos;
263aa899e68SJohannes Berg 	u32 version;
264aa899e68SJohannes Berg 
265aa899e68SJohannes Berg 	if (buf[0])
266aa899e68SJohannes Berg 		return;
267aa899e68SJohannes Berg 
268aa899e68SJohannes Berg 	switch (CSR_HW_RFID_TYPE(trans->hw_rf_id)) {
269aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_JF):
270aa899e68SJohannes Berg 		pos = scnprintf(buf, buflen, "JF");
271aa899e68SJohannes Berg 		break;
272aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_GF):
273aa899e68SJohannes Berg 		pos = scnprintf(buf, buflen, "GF");
274aa899e68SJohannes Berg 		break;
275aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_GF4):
276aa899e68SJohannes Berg 		pos = scnprintf(buf, buflen, "GF4");
277aa899e68SJohannes Berg 		break;
278aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR):
279aa899e68SJohannes Berg 		pos = scnprintf(buf, buflen, "HR");
280aa899e68SJohannes Berg 		break;
281aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR1):
282aa899e68SJohannes Berg 		pos = scnprintf(buf, buflen, "HR1");
283aa899e68SJohannes Berg 		break;
284aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HRCDB):
285aa899e68SJohannes Berg 		pos = scnprintf(buf, buflen, "HRCDB");
286aa899e68SJohannes Berg 		break;
2877f165fdfSMukesh Sisodiya 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_MS):
2887f165fdfSMukesh Sisodiya 		pos = scnprintf(buf, buflen, "MS");
2897f165fdfSMukesh Sisodiya 		break;
290aa899e68SJohannes Berg 	default:
291aa899e68SJohannes Berg 		return;
292aa899e68SJohannes Berg 	}
293aa899e68SJohannes Berg 
294aa899e68SJohannes Berg 	switch (CSR_HW_RFID_TYPE(trans->hw_rf_id)) {
295aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR):
296aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HR1):
297aa899e68SJohannes Berg 	case CSR_HW_RFID_TYPE(CSR_HW_RF_ID_TYPE_HRCDB):
298aa899e68SJohannes Berg 		version = iwl_read_prph(trans, CNVI_MBOX_C);
299aa899e68SJohannes Berg 		switch (version) {
300aa899e68SJohannes Berg 		case 0x20000:
301aa899e68SJohannes Berg 			pos += scnprintf(buf + pos, buflen - pos, " B3");
302aa899e68SJohannes Berg 			break;
303aa899e68SJohannes Berg 		case 0x120000:
304aa899e68SJohannes Berg 			pos += scnprintf(buf + pos, buflen - pos, " B5");
305aa899e68SJohannes Berg 			break;
306aa899e68SJohannes Berg 		default:
307aa899e68SJohannes Berg 			pos += scnprintf(buf + pos, buflen - pos,
308aa899e68SJohannes Berg 					 " (0x%x)", version);
309aa899e68SJohannes Berg 			break;
310aa899e68SJohannes Berg 		}
311aa899e68SJohannes Berg 		break;
312aa899e68SJohannes Berg 	default:
313aa899e68SJohannes Berg 		break;
314aa899e68SJohannes Berg 	}
315aa899e68SJohannes Berg 
316aa899e68SJohannes Berg 	pos += scnprintf(buf + pos, buflen - pos, ", rfid=0x%x",
317aa899e68SJohannes Berg 			 trans->hw_rf_id);
318aa899e68SJohannes Berg 
319aa899e68SJohannes Berg 	IWL_INFO(trans, "Detected RF %s\n", buf);
320aa899e68SJohannes Berg 
321aa899e68SJohannes Berg 	/*
322aa899e68SJohannes Berg 	 * also add a \n for debugfs - need to do it after printing
323aa899e68SJohannes Berg 	 * since our IWL_INFO machinery wants to see a static \n at
324aa899e68SJohannes Berg 	 * the end of the string
325aa899e68SJohannes Berg 	 */
326aa899e68SJohannes Berg 	pos += scnprintf(buf + pos, buflen - pos, "\n");
327aa899e68SJohannes Berg }
328aa899e68SJohannes Berg 
iwl_trans_pcie_gen2_fw_alive(struct iwl_trans * trans,u32 scd_addr)329eda50cdeSSara Sharon void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans, u32 scd_addr)
330eda50cdeSSara Sharon {
331eda50cdeSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
332eda50cdeSSara Sharon 
333eda50cdeSSara Sharon 	iwl_pcie_reset_ict(trans);
334eda50cdeSSara Sharon 
335eda50cdeSSara Sharon 	/* make sure all queue are not stopped/used */
3364f4822b7SMordechay Goodstein 	memset(trans->txqs.queue_stopped, 0,
3374f4822b7SMordechay Goodstein 	       sizeof(trans->txqs.queue_stopped));
3384f4822b7SMordechay Goodstein 	memset(trans->txqs.queue_used, 0, sizeof(trans->txqs.queue_used));
339eda50cdeSSara Sharon 
340eda50cdeSSara Sharon 	/* now that we got alive we can free the fw image & the context info.
341eda50cdeSSara Sharon 	 * paging memory cannot be freed included since FW will still use it
342eda50cdeSSara Sharon 	 */
34394768975SJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
34494768975SJohannes Berg 		iwl_pcie_ctxt_info_gen3_free(trans, true);
34594768975SJohannes Berg 	else
346eda50cdeSSara Sharon 		iwl_pcie_ctxt_info_free(trans);
347ed3e4c6dSEmmanuel Grumbach 
348ed3e4c6dSEmmanuel Grumbach 	/*
349ed3e4c6dSEmmanuel Grumbach 	 * Re-enable all the interrupts, including the RF-Kill one, now that
350ed3e4c6dSEmmanuel Grumbach 	 * the firmware is alive.
351ed3e4c6dSEmmanuel Grumbach 	 */
352ed3e4c6dSEmmanuel Grumbach 	iwl_enable_interrupts(trans);
353ed3e4c6dSEmmanuel Grumbach 	mutex_lock(&trans_pcie->mutex);
354ed3e4c6dSEmmanuel Grumbach 	iwl_pcie_check_hw_rf_kill(trans);
355aa899e68SJohannes Berg 
356aa899e68SJohannes Berg 	iwl_pcie_get_rf_name(trans);
357ed3e4c6dSEmmanuel Grumbach 	mutex_unlock(&trans_pcie->mutex);
358eda50cdeSSara Sharon }
359eda50cdeSSara Sharon 
iwl_pcie_set_ltr(struct iwl_trans * trans)36070582b82SJohannes Berg static bool iwl_pcie_set_ltr(struct iwl_trans *trans)
36125628bc0SJohannes Berg {
36225628bc0SJohannes Berg 	u32 ltr_val = CSR_LTR_LONG_VAL_AD_NO_SNOOP_REQ |
36325628bc0SJohannes Berg 		      u32_encode_bits(CSR_LTR_LONG_VAL_AD_SCALE_USEC,
36425628bc0SJohannes Berg 				      CSR_LTR_LONG_VAL_AD_NO_SNOOP_SCALE) |
36525628bc0SJohannes Berg 		      u32_encode_bits(250,
36625628bc0SJohannes Berg 				      CSR_LTR_LONG_VAL_AD_NO_SNOOP_VAL) |
36725628bc0SJohannes Berg 		      CSR_LTR_LONG_VAL_AD_SNOOP_REQ |
36825628bc0SJohannes Berg 		      u32_encode_bits(CSR_LTR_LONG_VAL_AD_SCALE_USEC,
36925628bc0SJohannes Berg 				      CSR_LTR_LONG_VAL_AD_SNOOP_SCALE) |
37025628bc0SJohannes Berg 		      u32_encode_bits(250, CSR_LTR_LONG_VAL_AD_SNOOP_VAL);
37125628bc0SJohannes Berg 
37225628bc0SJohannes Berg 	/*
37325628bc0SJohannes Berg 	 * To workaround hardware latency issues during the boot process,
37425628bc0SJohannes Berg 	 * initialize the LTR to ~250 usec (see ltr_val above).
37525628bc0SJohannes Berg 	 * The firmware initializes this again later (to a smaller value).
37625628bc0SJohannes Berg 	 */
37725628bc0SJohannes Berg 	if ((trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210 ||
37825628bc0SJohannes Berg 	     trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_22000) &&
37925628bc0SJohannes Berg 	    !trans->trans_cfg->integrated) {
38025628bc0SJohannes Berg 		iwl_write32(trans, CSR_LTR_LONG_VAL_AD, ltr_val);
38170582b82SJohannes Berg 		return true;
38270582b82SJohannes Berg 	}
38370582b82SJohannes Berg 
38470582b82SJohannes Berg 	if (trans->trans_cfg->integrated &&
38525628bc0SJohannes Berg 	    trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_22000) {
38625628bc0SJohannes Berg 		iwl_write_prph(trans, HPM_MAC_LTR_CSR, HPM_MAC_LRT_ENABLE_ALL);
38725628bc0SJohannes Berg 		iwl_write_prph(trans, HPM_UMAC_LTR, ltr_val);
38870582b82SJohannes Berg 		return true;
38925628bc0SJohannes Berg 	}
39070582b82SJohannes Berg 
39170582b82SJohannes Berg 	if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_AX210) {
39270582b82SJohannes Berg 		/* First clear the interrupt, just in case */
39370582b82SJohannes Berg 		iwl_write32(trans, CSR_MSIX_HW_INT_CAUSES_AD,
39470582b82SJohannes Berg 			    MSIX_HW_INT_CAUSES_REG_IML);
39570582b82SJohannes Berg 		/* In this case, unfortunately the same ROM bug exists in the
39670582b82SJohannes Berg 		 * device (not setting LTR correctly), but we don't have control
39770582b82SJohannes Berg 		 * over the settings from the host due to some hardware security
39870582b82SJohannes Berg 		 * features. The only workaround we've been able to come up with
39970582b82SJohannes Berg 		 * so far is to try to keep the CPU and device busy by polling
40070582b82SJohannes Berg 		 * it and the IML (image loader) completed interrupt.
40170582b82SJohannes Berg 		 */
40270582b82SJohannes Berg 		return false;
40370582b82SJohannes Berg 	}
40470582b82SJohannes Berg 
40570582b82SJohannes Berg 	/* nothing needs to be done on other devices */
40670582b82SJohannes Berg 	return true;
40770582b82SJohannes Berg }
40870582b82SJohannes Berg 
iwl_pcie_spin_for_iml(struct iwl_trans * trans)40970582b82SJohannes Berg static void iwl_pcie_spin_for_iml(struct iwl_trans *trans)
41070582b82SJohannes Berg {
41170582b82SJohannes Berg /* in practice, this seems to complete in around 20-30ms at most, wait 100 */
41270582b82SJohannes Berg #define IML_WAIT_TIMEOUT	(HZ / 10)
41370582b82SJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
41470582b82SJohannes Berg 	unsigned long end_time = jiffies + IML_WAIT_TIMEOUT;
41570582b82SJohannes Berg 	u32 value, loops = 0;
41670582b82SJohannes Berg 	bool irq = false;
41770582b82SJohannes Berg 
41870582b82SJohannes Berg 	if (WARN_ON(!trans_pcie->iml))
41970582b82SJohannes Berg 		return;
42070582b82SJohannes Berg 
42170582b82SJohannes Berg 	value = iwl_read32(trans, CSR_LTR_LAST_MSG);
42270582b82SJohannes Berg 	IWL_DEBUG_INFO(trans, "Polling for IML load - CSR_LTR_LAST_MSG=0x%x\n",
42370582b82SJohannes Berg 		       value);
42470582b82SJohannes Berg 
42570582b82SJohannes Berg 	while (time_before(jiffies, end_time)) {
42670582b82SJohannes Berg 		if (iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD) &
42770582b82SJohannes Berg 				MSIX_HW_INT_CAUSES_REG_IML) {
42870582b82SJohannes Berg 			irq = true;
42970582b82SJohannes Berg 			break;
43070582b82SJohannes Berg 		}
43170582b82SJohannes Berg 		/* Keep the CPU and device busy. */
43270582b82SJohannes Berg 		value = iwl_read32(trans, CSR_LTR_LAST_MSG);
43370582b82SJohannes Berg 		loops++;
43470582b82SJohannes Berg 	}
43570582b82SJohannes Berg 
43670582b82SJohannes Berg 	IWL_DEBUG_INFO(trans,
43770582b82SJohannes Berg 		       "Polled for IML load: irq=%d, loops=%d, CSR_LTR_LAST_MSG=0x%x\n",
43870582b82SJohannes Berg 		       irq, loops, value);
43970582b82SJohannes Berg 
44070582b82SJohannes Berg 	/* We don't fail here even if we timed out - maybe we get lucky and the
44170582b82SJohannes Berg 	 * interrupt comes in later (and we get alive from firmware) and then
44270582b82SJohannes Berg 	 * we're all happy - but if not we'll fail on alive timeout or get some
44370582b82SJohannes Berg 	 * other error out.
44470582b82SJohannes Berg 	 */
44525628bc0SJohannes Berg }
44625628bc0SJohannes Berg 
iwl_trans_pcie_gen2_start_fw(struct iwl_trans * trans,const struct fw_img * fw,bool run_in_rfkill)447eda50cdeSSara Sharon int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans,
448eda50cdeSSara Sharon 				 const struct fw_img *fw, bool run_in_rfkill)
449eda50cdeSSara Sharon {
450eda50cdeSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
45170582b82SJohannes Berg 	bool hw_rfkill, keep_ram_busy;
452eda50cdeSSara Sharon 	int ret;
453eda50cdeSSara Sharon 
454eda50cdeSSara Sharon 	/* This may fail if AMT took ownership of the device */
455eda50cdeSSara Sharon 	if (iwl_pcie_prepare_card_hw(trans)) {
456eda50cdeSSara Sharon 		IWL_WARN(trans, "Exit HW not ready\n");
4574c29c1e2SJohannes Berg 		return -EIO;
458eda50cdeSSara Sharon 	}
459eda50cdeSSara Sharon 
460eda50cdeSSara Sharon 	iwl_enable_rfkill_int(trans);
461eda50cdeSSara Sharon 
462eda50cdeSSara Sharon 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
463eda50cdeSSara Sharon 
464eda50cdeSSara Sharon 	/*
465eda50cdeSSara Sharon 	 * We enabled the RF-Kill interrupt and the handler may very
466eda50cdeSSara Sharon 	 * well be running. Disable the interrupts to make sure no other
467eda50cdeSSara Sharon 	 * interrupt can be fired.
468eda50cdeSSara Sharon 	 */
469eda50cdeSSara Sharon 	iwl_disable_interrupts(trans);
470eda50cdeSSara Sharon 
471eda50cdeSSara Sharon 	/* Make sure it finished running */
472eda50cdeSSara Sharon 	iwl_pcie_synchronize_irqs(trans);
473eda50cdeSSara Sharon 
474eda50cdeSSara Sharon 	mutex_lock(&trans_pcie->mutex);
475eda50cdeSSara Sharon 
476eda50cdeSSara Sharon 	/* If platform's RF_KILL switch is NOT set to KILL */
4779ad8fd0bSJohannes Berg 	hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
478eda50cdeSSara Sharon 	if (hw_rfkill && !run_in_rfkill) {
479eda50cdeSSara Sharon 		ret = -ERFKILL;
480eda50cdeSSara Sharon 		goto out;
481eda50cdeSSara Sharon 	}
482eda50cdeSSara Sharon 
483eda50cdeSSara Sharon 	/* Someone called stop_device, don't try to start_fw */
484eda50cdeSSara Sharon 	if (trans_pcie->is_down) {
485eda50cdeSSara Sharon 		IWL_WARN(trans,
486eda50cdeSSara Sharon 			 "Can't start_fw since the HW hasn't been started\n");
487eda50cdeSSara Sharon 		ret = -EIO;
488eda50cdeSSara Sharon 		goto out;
489eda50cdeSSara Sharon 	}
490eda50cdeSSara Sharon 
491eda50cdeSSara Sharon 	/* make sure rfkill handshake bits are cleared */
492eda50cdeSSara Sharon 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
493eda50cdeSSara Sharon 	iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
494eda50cdeSSara Sharon 		    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
495eda50cdeSSara Sharon 
496eda50cdeSSara Sharon 	/* clear (again), then enable host interrupts */
497eda50cdeSSara Sharon 	iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
498eda50cdeSSara Sharon 
499eda50cdeSSara Sharon 	ret = iwl_pcie_gen2_nic_init(trans);
500eda50cdeSSara Sharon 	if (ret) {
501eda50cdeSSara Sharon 		IWL_ERR(trans, "Unable to init nic\n");
502eda50cdeSSara Sharon 		goto out;
503eda50cdeSSara Sharon 	}
504eda50cdeSSara Sharon 
5053681021fSJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
5062ee82402SGolan Ben Ami 		ret = iwl_pcie_ctxt_info_gen3_init(trans, fw);
5072ee82402SGolan Ben Ami 	else
50897b00d87SJohannes Berg 		ret = iwl_pcie_ctxt_info_init(trans, fw);
50997b00d87SJohannes Berg 	if (ret)
51097b00d87SJohannes Berg 		goto out;
511eda50cdeSSara Sharon 
51270582b82SJohannes Berg 	keep_ram_busy = !iwl_pcie_set_ltr(trans);
51325628bc0SJohannes Berg 
514595c230bSMatti Gottlieb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
515595c230bSMatti Gottlieb 		iwl_write32(trans, CSR_FUNC_SCRATCH, CSR_FUNC_SCRATCH_INIT_VALUE);
5169ce041f5SJohannes Berg 		iwl_set_bit(trans, CSR_GP_CNTRL,
5179ce041f5SJohannes Berg 			    CSR_GP_CNTRL_REG_FLAG_ROM_START);
518595c230bSMatti Gottlieb 	} else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
51925628bc0SJohannes Berg 		iwl_write_umac_prph(trans, UREG_CPU_INIT_RUN, 1);
520595c230bSMatti Gottlieb 	} else {
52125628bc0SJohannes Berg 		iwl_write_prph(trans, UREG_CPU_INIT_RUN, 1);
522595c230bSMatti Gottlieb 	}
52325628bc0SJohannes Berg 
52470582b82SJohannes Berg 	if (keep_ram_busy)
52570582b82SJohannes Berg 		iwl_pcie_spin_for_iml(trans);
52670582b82SJohannes Berg 
527eda50cdeSSara Sharon 	/* re-check RF-Kill state since we may have missed the interrupt */
5289ad8fd0bSJohannes Berg 	hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
529eda50cdeSSara Sharon 	if (hw_rfkill && !run_in_rfkill)
530eda50cdeSSara Sharon 		ret = -ERFKILL;
531eda50cdeSSara Sharon 
532eda50cdeSSara Sharon out:
533eda50cdeSSara Sharon 	mutex_unlock(&trans_pcie->mutex);
534eda50cdeSSara Sharon 	return ret;
535eda50cdeSSara Sharon }
536