xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/pci.c (revision 6fe6f68fef7f7d5f6b5b62fde78de91cdc528c58)
16e0355afSGovind Singh // SPDX-License-Identifier: BSD-3-Clause-Clear
26e0355afSGovind Singh /*
36e0355afSGovind Singh  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
46e0355afSGovind Singh  */
56e0355afSGovind Singh 
66e0355afSGovind Singh #include <linux/module.h>
75697a564SGovind Singh #include <linux/msi.h>
86e0355afSGovind Singh #include <linux/pci.h>
96e0355afSGovind Singh 
105762613eSGovind Singh #include "pci.h"
116e0355afSGovind Singh #include "core.h"
121399fb87SGovind Singh #include "hif.h"
131399fb87SGovind Singh #include "mhi.h"
146e0355afSGovind Singh #include "debug.h"
156e0355afSGovind Singh 
165762613eSGovind Singh #define ATH11K_PCI_BAR_NUM		0
175762613eSGovind Singh #define ATH11K_PCI_DMA_MASK		32
185762613eSGovind Singh 
197f4beda2SGovind Singh #define ATH11K_PCI_IRQ_CE0_OFFSET		3
207f4beda2SGovind Singh 
21654e959aSGovind Singh #define WINDOW_ENABLE_BIT		0x40000000
22654e959aSGovind Singh #define WINDOW_REG_ADDRESS		0x310c
23654e959aSGovind Singh #define WINDOW_VALUE_MASK		GENMASK(24, 19)
24654e959aSGovind Singh #define WINDOW_START			0x80000
25654e959aSGovind Singh #define WINDOW_RANGE_MASK		GENMASK(18, 0)
26654e959aSGovind Singh 
2718ac1665SKalle Valo #define TCSR_SOC_HW_VERSION		0x0224
2818ac1665SKalle Valo #define TCSR_SOC_HW_VERSION_MAJOR_MASK	GENMASK(16, 8)
2918ac1665SKalle Valo #define TCSR_SOC_HW_VERSION_MINOR_MASK	GENMASK(7, 0)
3018ac1665SKalle Valo 
31a05bd851SCarl Huang /* BAR0 + 4k is always accessible, and no
32a05bd851SCarl Huang  * need to force wakeup.
33a05bd851SCarl Huang  * 4K - 32 = 0xFE0
34a05bd851SCarl Huang  */
35a05bd851SCarl Huang #define ACCESS_ALWAYS_OFF 0xFE0
36a05bd851SCarl Huang 
376e0355afSGovind Singh #define QCA6390_DEVICE_ID		0x1101
386e0355afSGovind Singh 
396e0355afSGovind Singh static const struct pci_device_id ath11k_pci_id_table[] = {
406e0355afSGovind Singh 	{ PCI_VDEVICE(QCOM, QCA6390_DEVICE_ID) },
416e0355afSGovind Singh 	{0}
426e0355afSGovind Singh };
436e0355afSGovind Singh 
446e0355afSGovind Singh MODULE_DEVICE_TABLE(pci, ath11k_pci_id_table);
456e0355afSGovind Singh 
461ff8ed78SGovind Singh static const struct ath11k_bus_params ath11k_pci_bus_params = {
471ff8ed78SGovind Singh 	.mhi_support = true,
4856970454SGovind Singh 	.m3_fw_support = true,
496eb6ea51SGovind Singh 	.fixed_bdf_addr = false,
506eb6ea51SGovind Singh 	.fixed_mem_region = false,
511ff8ed78SGovind Singh };
521ff8ed78SGovind Singh 
537a3aed0cSAnilkumar Kolli static const struct ath11k_msi_config ath11k_msi_config[] = {
547a3aed0cSAnilkumar Kolli 	{
555697a564SGovind Singh 		.total_vectors = 32,
565697a564SGovind Singh 		.total_users = 4,
575697a564SGovind Singh 		.users = (struct ath11k_msi_user[]) {
585697a564SGovind Singh 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
595697a564SGovind Singh 			{ .name = "CE", .num_vectors = 10, .base_vector = 3 },
605697a564SGovind Singh 			{ .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
615697a564SGovind Singh 			{ .name = "DP", .num_vectors = 18, .base_vector = 14 },
625697a564SGovind Singh 		},
637a3aed0cSAnilkumar Kolli 	},
645697a564SGovind Singh };
655697a564SGovind Singh 
667f4beda2SGovind Singh static const char *irq_name[ATH11K_IRQ_NUM_MAX] = {
677f4beda2SGovind Singh 	"bhi",
687f4beda2SGovind Singh 	"mhi-er0",
697f4beda2SGovind Singh 	"mhi-er1",
707f4beda2SGovind Singh 	"ce0",
717f4beda2SGovind Singh 	"ce1",
727f4beda2SGovind Singh 	"ce2",
737f4beda2SGovind Singh 	"ce3",
747f4beda2SGovind Singh 	"ce4",
757f4beda2SGovind Singh 	"ce5",
767f4beda2SGovind Singh 	"ce6",
777f4beda2SGovind Singh 	"ce7",
787f4beda2SGovind Singh 	"ce8",
797f4beda2SGovind Singh 	"ce9",
807f4beda2SGovind Singh 	"ce10",
817f4beda2SGovind Singh 	"ce11",
827f4beda2SGovind Singh 	"host2wbm-desc-feed",
837f4beda2SGovind Singh 	"host2reo-re-injection",
847f4beda2SGovind Singh 	"host2reo-command",
857f4beda2SGovind Singh 	"host2rxdma-monitor-ring3",
867f4beda2SGovind Singh 	"host2rxdma-monitor-ring2",
877f4beda2SGovind Singh 	"host2rxdma-monitor-ring1",
887f4beda2SGovind Singh 	"reo2ost-exception",
897f4beda2SGovind Singh 	"wbm2host-rx-release",
907f4beda2SGovind Singh 	"reo2host-status",
917f4beda2SGovind Singh 	"reo2host-destination-ring4",
927f4beda2SGovind Singh 	"reo2host-destination-ring3",
937f4beda2SGovind Singh 	"reo2host-destination-ring2",
947f4beda2SGovind Singh 	"reo2host-destination-ring1",
957f4beda2SGovind Singh 	"rxdma2host-monitor-destination-mac3",
967f4beda2SGovind Singh 	"rxdma2host-monitor-destination-mac2",
977f4beda2SGovind Singh 	"rxdma2host-monitor-destination-mac1",
987f4beda2SGovind Singh 	"ppdu-end-interrupts-mac3",
997f4beda2SGovind Singh 	"ppdu-end-interrupts-mac2",
1007f4beda2SGovind Singh 	"ppdu-end-interrupts-mac1",
1017f4beda2SGovind Singh 	"rxdma2host-monitor-status-ring-mac3",
1027f4beda2SGovind Singh 	"rxdma2host-monitor-status-ring-mac2",
1037f4beda2SGovind Singh 	"rxdma2host-monitor-status-ring-mac1",
1047f4beda2SGovind Singh 	"host2rxdma-host-buf-ring-mac3",
1057f4beda2SGovind Singh 	"host2rxdma-host-buf-ring-mac2",
1067f4beda2SGovind Singh 	"host2rxdma-host-buf-ring-mac1",
1077f4beda2SGovind Singh 	"rxdma2host-destination-ring-mac3",
1087f4beda2SGovind Singh 	"rxdma2host-destination-ring-mac2",
1097f4beda2SGovind Singh 	"rxdma2host-destination-ring-mac1",
1107f4beda2SGovind Singh 	"host2tcl-input-ring4",
1117f4beda2SGovind Singh 	"host2tcl-input-ring3",
1127f4beda2SGovind Singh 	"host2tcl-input-ring2",
1137f4beda2SGovind Singh 	"host2tcl-input-ring1",
1147f4beda2SGovind Singh 	"wbm2host-tx-completions-ring3",
1157f4beda2SGovind Singh 	"wbm2host-tx-completions-ring2",
1167f4beda2SGovind Singh 	"wbm2host-tx-completions-ring1",
1177f4beda2SGovind Singh 	"tcl2host-status-ring",
1187f4beda2SGovind Singh };
1197f4beda2SGovind Singh 
120654e959aSGovind Singh static inline void ath11k_pci_select_window(struct ath11k_pci *ab_pci, u32 offset)
121654e959aSGovind Singh {
122654e959aSGovind Singh 	struct ath11k_base *ab = ab_pci->ab;
123654e959aSGovind Singh 
124654e959aSGovind Singh 	u32 window = FIELD_GET(WINDOW_VALUE_MASK, offset);
125654e959aSGovind Singh 
126654e959aSGovind Singh 	lockdep_assert_held(&ab_pci->window_lock);
127654e959aSGovind Singh 
128654e959aSGovind Singh 	if (window != ab_pci->register_window) {
129654e959aSGovind Singh 		iowrite32(WINDOW_ENABLE_BIT | window,
130654e959aSGovind Singh 			  ab->mem + WINDOW_REG_ADDRESS);
131f6fa37a4SCarl Huang 		ioread32(ab->mem + WINDOW_REG_ADDRESS);
132654e959aSGovind Singh 		ab_pci->register_window = window;
133654e959aSGovind Singh 	}
134654e959aSGovind Singh }
135654e959aSGovind Singh 
136480a7361SKarthikeyan Periyasamy static inline void ath11k_pci_select_static_window(struct ath11k_pci *ab_pci)
137480a7361SKarthikeyan Periyasamy {
138480a7361SKarthikeyan Periyasamy 	u32 umac_window = FIELD_GET(WINDOW_VALUE_MASK, HAL_SEQ_WCSS_UMAC_OFFSET);
139480a7361SKarthikeyan Periyasamy 	u32 ce_window = FIELD_GET(WINDOW_VALUE_MASK, HAL_CE_WFSS_CE_REG_BASE);
140480a7361SKarthikeyan Periyasamy 	u32 window;
141480a7361SKarthikeyan Periyasamy 
142480a7361SKarthikeyan Periyasamy 	window = (umac_window << 12) | (ce_window << 6);
143480a7361SKarthikeyan Periyasamy 
144480a7361SKarthikeyan Periyasamy 	iowrite32(WINDOW_ENABLE_BIT | window, ab_pci->ab->mem + WINDOW_REG_ADDRESS);
145480a7361SKarthikeyan Periyasamy }
146480a7361SKarthikeyan Periyasamy 
147480a7361SKarthikeyan Periyasamy static inline u32 ath11k_pci_get_window_start(struct ath11k_base *ab,
148480a7361SKarthikeyan Periyasamy 					      u32 offset)
149480a7361SKarthikeyan Periyasamy {
150480a7361SKarthikeyan Periyasamy 	u32 window_start;
151480a7361SKarthikeyan Periyasamy 
152480a7361SKarthikeyan Periyasamy 	/* If offset lies within DP register range, use 3rd window */
153480a7361SKarthikeyan Periyasamy 	if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < WINDOW_RANGE_MASK)
154480a7361SKarthikeyan Periyasamy 		window_start = 3 * WINDOW_START;
155480a7361SKarthikeyan Periyasamy 	/* If offset lies within CE register range, use 2nd window */
156480a7361SKarthikeyan Periyasamy 	else if ((offset ^ HAL_CE_WFSS_CE_REG_BASE) < WINDOW_RANGE_MASK)
157480a7361SKarthikeyan Periyasamy 		window_start = 2 * WINDOW_START;
158480a7361SKarthikeyan Periyasamy 	else
159480a7361SKarthikeyan Periyasamy 		window_start = WINDOW_START;
160480a7361SKarthikeyan Periyasamy 
161480a7361SKarthikeyan Periyasamy 	return window_start;
162480a7361SKarthikeyan Periyasamy }
163480a7361SKarthikeyan Periyasamy 
164f3c603d4SCarl Huang void ath11k_pci_write32(struct ath11k_base *ab, u32 offset, u32 value)
165654e959aSGovind Singh {
166654e959aSGovind Singh 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
167480a7361SKarthikeyan Periyasamy 	u32 window_start;
168654e959aSGovind Singh 
169a05bd851SCarl Huang 	/* for offset beyond BAR + 4K - 32, may
170a05bd851SCarl Huang 	 * need to wakeup MHI to access.
171a05bd851SCarl Huang 	 */
172a05bd851SCarl Huang 	if (test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
173a05bd851SCarl Huang 	    offset >= ACCESS_ALWAYS_OFF)
174a05bd851SCarl Huang 		mhi_device_get_sync(ab_pci->mhi_ctrl->mhi_dev);
175a05bd851SCarl Huang 
176654e959aSGovind Singh 	if (offset < WINDOW_START) {
177654e959aSGovind Singh 		iowrite32(value, ab->mem  + offset);
178654e959aSGovind Singh 	} else {
179480a7361SKarthikeyan Periyasamy 		if (ab->bus_params.static_window_map)
180480a7361SKarthikeyan Periyasamy 			window_start = ath11k_pci_get_window_start(ab, offset);
181480a7361SKarthikeyan Periyasamy 		else
182480a7361SKarthikeyan Periyasamy 			window_start = WINDOW_START;
183480a7361SKarthikeyan Periyasamy 
184480a7361SKarthikeyan Periyasamy 		if (window_start == WINDOW_START) {
185654e959aSGovind Singh 			spin_lock_bh(&ab_pci->window_lock);
186654e959aSGovind Singh 			ath11k_pci_select_window(ab_pci, offset);
187480a7361SKarthikeyan Periyasamy 			iowrite32(value, ab->mem + window_start +
188480a7361SKarthikeyan Periyasamy 				  (offset & WINDOW_RANGE_MASK));
189654e959aSGovind Singh 			spin_unlock_bh(&ab_pci->window_lock);
190480a7361SKarthikeyan Periyasamy 		} else {
191480a7361SKarthikeyan Periyasamy 			iowrite32(value, ab->mem + window_start +
192480a7361SKarthikeyan Periyasamy 				  (offset & WINDOW_RANGE_MASK));
193480a7361SKarthikeyan Periyasamy 		}
194654e959aSGovind Singh 	}
195a05bd851SCarl Huang 
196a05bd851SCarl Huang 	if (test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
197a05bd851SCarl Huang 	    offset >= ACCESS_ALWAYS_OFF)
198a05bd851SCarl Huang 		mhi_device_put(ab_pci->mhi_ctrl->mhi_dev);
199654e959aSGovind Singh }
200654e959aSGovind Singh 
201f3c603d4SCarl Huang u32 ath11k_pci_read32(struct ath11k_base *ab, u32 offset)
202654e959aSGovind Singh {
203654e959aSGovind Singh 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
204480a7361SKarthikeyan Periyasamy 	u32 val, window_start;
205654e959aSGovind Singh 
206a05bd851SCarl Huang 	/* for offset beyond BAR + 4K - 32, may
207a05bd851SCarl Huang 	 * need to wakeup MHI to access.
208a05bd851SCarl Huang 	 */
209a05bd851SCarl Huang 	if (test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
210a05bd851SCarl Huang 	    offset >= ACCESS_ALWAYS_OFF)
211a05bd851SCarl Huang 		mhi_device_get_sync(ab_pci->mhi_ctrl->mhi_dev);
212a05bd851SCarl Huang 
213654e959aSGovind Singh 	if (offset < WINDOW_START) {
214654e959aSGovind Singh 		val = ioread32(ab->mem + offset);
215654e959aSGovind Singh 	} else {
216480a7361SKarthikeyan Periyasamy 		if (ab->bus_params.static_window_map)
217480a7361SKarthikeyan Periyasamy 			window_start = ath11k_pci_get_window_start(ab, offset);
218480a7361SKarthikeyan Periyasamy 		else
219480a7361SKarthikeyan Periyasamy 			window_start = WINDOW_START;
220480a7361SKarthikeyan Periyasamy 
221480a7361SKarthikeyan Periyasamy 		if (window_start == WINDOW_START) {
222654e959aSGovind Singh 			spin_lock_bh(&ab_pci->window_lock);
223654e959aSGovind Singh 			ath11k_pci_select_window(ab_pci, offset);
224480a7361SKarthikeyan Periyasamy 			val = ioread32(ab->mem + window_start +
225480a7361SKarthikeyan Periyasamy 				       (offset & WINDOW_RANGE_MASK));
226654e959aSGovind Singh 			spin_unlock_bh(&ab_pci->window_lock);
227480a7361SKarthikeyan Periyasamy 		} else {
228480a7361SKarthikeyan Periyasamy 			val = ioread32(ab->mem + window_start +
229480a7361SKarthikeyan Periyasamy 				       (offset & WINDOW_RANGE_MASK));
230480a7361SKarthikeyan Periyasamy 		}
231654e959aSGovind Singh 	}
232654e959aSGovind Singh 
233a05bd851SCarl Huang 	if (test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
234a05bd851SCarl Huang 	    offset >= ACCESS_ALWAYS_OFF)
235a05bd851SCarl Huang 		mhi_device_put(ab_pci->mhi_ctrl->mhi_dev);
236a05bd851SCarl Huang 
237654e959aSGovind Singh 	return val;
238654e959aSGovind Singh }
239654e959aSGovind Singh 
240f3c603d4SCarl Huang static void ath11k_pci_soc_global_reset(struct ath11k_base *ab)
241f3c603d4SCarl Huang {
242f3c603d4SCarl Huang 	u32 val, delay;
243f3c603d4SCarl Huang 
244f3c603d4SCarl Huang 	val = ath11k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
245f3c603d4SCarl Huang 
246f3c603d4SCarl Huang 	val |= PCIE_SOC_GLOBAL_RESET_V;
247f3c603d4SCarl Huang 
248f3c603d4SCarl Huang 	ath11k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
249f3c603d4SCarl Huang 
250f3c603d4SCarl Huang 	/* TODO: exact time to sleep is uncertain */
251f3c603d4SCarl Huang 	delay = 10;
252f3c603d4SCarl Huang 	mdelay(delay);
253f3c603d4SCarl Huang 
254f3c603d4SCarl Huang 	/* Need to toggle V bit back otherwise stuck in reset status */
255f3c603d4SCarl Huang 	val &= ~PCIE_SOC_GLOBAL_RESET_V;
256f3c603d4SCarl Huang 
257f3c603d4SCarl Huang 	ath11k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
258f3c603d4SCarl Huang 
259f3c603d4SCarl Huang 	mdelay(delay);
260f3c603d4SCarl Huang 
261f3c603d4SCarl Huang 	val = ath11k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
262f3c603d4SCarl Huang 	if (val == 0xffffffff)
263f3c603d4SCarl Huang 		ath11k_warn(ab, "link down error during global reset\n");
264f3c603d4SCarl Huang }
265f3c603d4SCarl Huang 
266f3c603d4SCarl Huang static void ath11k_pci_clear_dbg_registers(struct ath11k_base *ab)
267f3c603d4SCarl Huang {
268f3c603d4SCarl Huang 	u32 val;
269f3c603d4SCarl Huang 
270f3c603d4SCarl Huang 	/* read cookie */
271f3c603d4SCarl Huang 	val = ath11k_pci_read32(ab, PCIE_Q6_COOKIE_ADDR);
272f3c603d4SCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "cookie:0x%x\n", val);
273f3c603d4SCarl Huang 
274f3c603d4SCarl Huang 	val = ath11k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
275f3c603d4SCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
276f3c603d4SCarl Huang 
277f3c603d4SCarl Huang 	/* TODO: exact time to sleep is uncertain */
278f3c603d4SCarl Huang 	mdelay(10);
279f3c603d4SCarl Huang 
280f3c603d4SCarl Huang 	/* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
281f3c603d4SCarl Huang 	 * continuing warm path and entering dead loop.
282f3c603d4SCarl Huang 	 */
283f3c603d4SCarl Huang 	ath11k_pci_write32(ab, WLAON_WARM_SW_ENTRY, 0);
284f3c603d4SCarl Huang 	mdelay(10);
285f3c603d4SCarl Huang 
286f3c603d4SCarl Huang 	val = ath11k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
287f3c603d4SCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
288f3c603d4SCarl Huang 
289f3c603d4SCarl Huang 	/* A read clear register. clear the register to prevent
290f3c603d4SCarl Huang 	 * Q6 from entering wrong code path.
291f3c603d4SCarl Huang 	 */
292f3c603d4SCarl Huang 	val = ath11k_pci_read32(ab, WLAON_SOC_RESET_CAUSE_REG);
293f3c603d4SCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "soc reset cause:%d\n", val);
294f3c603d4SCarl Huang }
295f3c603d4SCarl Huang 
29606999407SCarl Huang static int ath11k_pci_set_link_reg(struct ath11k_base *ab,
29706999407SCarl Huang 				   u32 offset, u32 value, u32 mask)
29806999407SCarl Huang {
29906999407SCarl Huang 	u32 v;
30006999407SCarl Huang 	int i;
30106999407SCarl Huang 
30206999407SCarl Huang 	v = ath11k_pci_read32(ab, offset);
30306999407SCarl Huang 	if ((v & mask) == value)
30406999407SCarl Huang 		return 0;
30506999407SCarl Huang 
30606999407SCarl Huang 	for (i = 0; i < 10; i++) {
30706999407SCarl Huang 		ath11k_pci_write32(ab, offset, (v & ~mask) | value);
30806999407SCarl Huang 
30906999407SCarl Huang 		v = ath11k_pci_read32(ab, offset);
31006999407SCarl Huang 		if ((v & mask) == value)
31106999407SCarl Huang 			return 0;
31206999407SCarl Huang 
31306999407SCarl Huang 		mdelay(2);
31406999407SCarl Huang 	}
31506999407SCarl Huang 
31606999407SCarl Huang 	ath11k_warn(ab, "failed to set pcie link register 0x%08x: 0x%08x != 0x%08x\n",
31706999407SCarl Huang 		    offset, v & mask, value);
31806999407SCarl Huang 
31906999407SCarl Huang 	return -ETIMEDOUT;
32006999407SCarl Huang }
32106999407SCarl Huang 
32206999407SCarl Huang static int ath11k_pci_fix_l1ss(struct ath11k_base *ab)
32306999407SCarl Huang {
32406999407SCarl Huang 	int ret;
32506999407SCarl Huang 
32606999407SCarl Huang 	ret = ath11k_pci_set_link_reg(ab,
327*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(ab),
32806999407SCarl Huang 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL,
32906999407SCarl Huang 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK);
33030d08503SDan Carpenter 	if (ret) {
33106999407SCarl Huang 		ath11k_warn(ab, "failed to set sysclk: %d\n", ret);
33206999407SCarl Huang 		return ret;
33306999407SCarl Huang 	}
33406999407SCarl Huang 
33506999407SCarl Huang 	ret = ath11k_pci_set_link_reg(ab,
336*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG1_REG(ab),
337*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG1_VAL,
338*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
33930d08503SDan Carpenter 	if (ret) {
34006999407SCarl Huang 		ath11k_warn(ab, "failed to set dtct config1 error: %d\n", ret);
34106999407SCarl Huang 		return ret;
34206999407SCarl Huang 	}
34306999407SCarl Huang 
34406999407SCarl Huang 	ret = ath11k_pci_set_link_reg(ab,
345*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG2_REG(ab),
346*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG2_VAL,
347*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
34830d08503SDan Carpenter 	if (ret) {
34906999407SCarl Huang 		ath11k_warn(ab, "failed to set dtct config2: %d\n", ret);
35006999407SCarl Huang 		return ret;
35106999407SCarl Huang 	}
35206999407SCarl Huang 
35306999407SCarl Huang 	ret = ath11k_pci_set_link_reg(ab,
354*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG4_REG(ab),
355*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG4_VAL,
356*6fe6f68fSKarthikeyan Periyasamy 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
35730d08503SDan Carpenter 	if (ret) {
35806999407SCarl Huang 		ath11k_warn(ab, "failed to set dtct config4: %d\n", ret);
35906999407SCarl Huang 		return ret;
36006999407SCarl Huang 	}
36106999407SCarl Huang 
36206999407SCarl Huang 	return 0;
36306999407SCarl Huang }
36406999407SCarl Huang 
365babb0cedSCarl Huang static void ath11k_pci_enable_ltssm(struct ath11k_base *ab)
366babb0cedSCarl Huang {
367babb0cedSCarl Huang 	u32 val;
368babb0cedSCarl Huang 	int i;
369babb0cedSCarl Huang 
370babb0cedSCarl Huang 	val = ath11k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
371babb0cedSCarl Huang 
372babb0cedSCarl Huang 	/* PCIE link seems very unstable after the Hot Reset*/
373babb0cedSCarl Huang 	for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
374babb0cedSCarl Huang 		if (val == 0xffffffff)
375babb0cedSCarl Huang 			mdelay(5);
376babb0cedSCarl Huang 
377babb0cedSCarl Huang 		ath11k_pci_write32(ab, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
378babb0cedSCarl Huang 		val = ath11k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
379babb0cedSCarl Huang 	}
380babb0cedSCarl Huang 
381babb0cedSCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci ltssm 0x%x\n", val);
382babb0cedSCarl Huang 
383babb0cedSCarl Huang 	val = ath11k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST);
384562934adSKalle Valo 	val |= GCC_GCC_PCIE_HOT_RST_VAL;
385babb0cedSCarl Huang 	ath11k_pci_write32(ab, GCC_GCC_PCIE_HOT_RST, val);
386babb0cedSCarl Huang 	val = ath11k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST);
387babb0cedSCarl Huang 
388babb0cedSCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci pcie_hot_rst 0x%x\n", val);
389babb0cedSCarl Huang 
390babb0cedSCarl Huang 	mdelay(5);
391babb0cedSCarl Huang }
392babb0cedSCarl Huang 
393babb0cedSCarl Huang static void ath11k_pci_clear_all_intrs(struct ath11k_base *ab)
394babb0cedSCarl Huang {
395babb0cedSCarl Huang 	/* This is a WAR for PCIE Hotreset.
396babb0cedSCarl Huang 	 * When target receive Hotreset, but will set the interrupt.
397babb0cedSCarl Huang 	 * So when download SBL again, SBL will open Interrupt and
398babb0cedSCarl Huang 	 * receive it, and crash immediately.
399babb0cedSCarl Huang 	 */
400babb0cedSCarl Huang 	ath11k_pci_write32(ab, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
401babb0cedSCarl Huang }
402babb0cedSCarl Huang 
4030ccdf439SCarl Huang static void ath11k_pci_set_wlaon_pwr_ctrl(struct ath11k_base *ab)
4040ccdf439SCarl Huang {
4050ccdf439SCarl Huang 	u32 val;
4060ccdf439SCarl Huang 
4070ccdf439SCarl Huang 	val = ath11k_pci_read32(ab, WLAON_QFPROM_PWR_CTRL_REG);
4080ccdf439SCarl Huang 	val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
4090ccdf439SCarl Huang 	ath11k_pci_write32(ab, WLAON_QFPROM_PWR_CTRL_REG, val);
4100ccdf439SCarl Huang }
4110ccdf439SCarl Huang 
412f3c603d4SCarl Huang static void ath11k_pci_force_wake(struct ath11k_base *ab)
413f3c603d4SCarl Huang {
414f3c603d4SCarl Huang 	ath11k_pci_write32(ab, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
415f3c603d4SCarl Huang 	mdelay(5);
416f3c603d4SCarl Huang }
417f3c603d4SCarl Huang 
418babb0cedSCarl Huang static void ath11k_pci_sw_reset(struct ath11k_base *ab, bool power_on)
419f3c603d4SCarl Huang {
420babb0cedSCarl Huang 	if (power_on) {
421babb0cedSCarl Huang 		ath11k_pci_enable_ltssm(ab);
422babb0cedSCarl Huang 		ath11k_pci_clear_all_intrs(ab);
4230ccdf439SCarl Huang 		ath11k_pci_set_wlaon_pwr_ctrl(ab);
42406999407SCarl Huang 		ath11k_pci_fix_l1ss(ab);
425babb0cedSCarl Huang 	}
426babb0cedSCarl Huang 
427f3c603d4SCarl Huang 	ath11k_mhi_clear_vector(ab);
428f3c603d4SCarl Huang 	ath11k_pci_soc_global_reset(ab);
429f3c603d4SCarl Huang 	ath11k_mhi_set_mhictrl_reset(ab);
430f3c603d4SCarl Huang 	ath11k_pci_clear_dbg_registers(ab);
431f3c603d4SCarl Huang }
432f3c603d4SCarl Huang 
4331399fb87SGovind Singh int ath11k_pci_get_msi_irq(struct device *dev, unsigned int vector)
4341399fb87SGovind Singh {
4351399fb87SGovind Singh 	struct pci_dev *pci_dev = to_pci_dev(dev);
4361399fb87SGovind Singh 
4371399fb87SGovind Singh 	return pci_irq_vector(pci_dev, vector);
4381399fb87SGovind Singh }
4391399fb87SGovind Singh 
440c4eacabeSGovind Singh static void ath11k_pci_get_msi_address(struct ath11k_base *ab, u32 *msi_addr_lo,
441c4eacabeSGovind Singh 				       u32 *msi_addr_hi)
442c4eacabeSGovind Singh {
443e8e55d89SAnilkumar Kolli 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
444c4eacabeSGovind Singh 	struct pci_dev *pci_dev = to_pci_dev(ab->dev);
445c4eacabeSGovind Singh 
446c4eacabeSGovind Singh 	pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
447c4eacabeSGovind Singh 			      msi_addr_lo);
448c4eacabeSGovind Singh 
449e8e55d89SAnilkumar Kolli 	if (test_bit(ATH11K_PCI_FLAG_IS_MSI_64, &ab_pci->flags)) {
450c4eacabeSGovind Singh 		pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
451c4eacabeSGovind Singh 				      msi_addr_hi);
452e8e55d89SAnilkumar Kolli 	} else {
453e8e55d89SAnilkumar Kolli 		*msi_addr_hi = 0;
454e8e55d89SAnilkumar Kolli 	}
455c4eacabeSGovind Singh }
456c4eacabeSGovind Singh 
4571399fb87SGovind Singh int ath11k_pci_get_user_msi_assignment(struct ath11k_pci *ab_pci, char *user_name,
4581399fb87SGovind Singh 				       int *num_vectors, u32 *user_base_data,
4591399fb87SGovind Singh 				       u32 *base_vector)
4601399fb87SGovind Singh {
4611399fb87SGovind Singh 	struct ath11k_base *ab = ab_pci->ab;
4627a3aed0cSAnilkumar Kolli 	const struct ath11k_msi_config *msi_config = ab_pci->msi_config;
4631399fb87SGovind Singh 	int idx;
4641399fb87SGovind Singh 
4657a3aed0cSAnilkumar Kolli 	for (idx = 0; idx < msi_config->total_users; idx++) {
4667a3aed0cSAnilkumar Kolli 		if (strcmp(user_name, msi_config->users[idx].name) == 0) {
4677a3aed0cSAnilkumar Kolli 			*num_vectors = msi_config->users[idx].num_vectors;
4687a3aed0cSAnilkumar Kolli 			*user_base_data = msi_config->users[idx].base_vector
4691399fb87SGovind Singh 				+ ab_pci->msi_ep_base_data;
4707a3aed0cSAnilkumar Kolli 			*base_vector = msi_config->users[idx].base_vector;
4711399fb87SGovind Singh 
4721399fb87SGovind Singh 			ath11k_dbg(ab, ATH11K_DBG_PCI, "Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
4731399fb87SGovind Singh 				   user_name, *num_vectors, *user_base_data,
4741399fb87SGovind Singh 				   *base_vector);
4751399fb87SGovind Singh 
4761399fb87SGovind Singh 			return 0;
4771399fb87SGovind Singh 		}
4781399fb87SGovind Singh 	}
4791399fb87SGovind Singh 
4801399fb87SGovind Singh 	ath11k_err(ab, "Failed to find MSI assignment for %s!\n", user_name);
4811399fb87SGovind Singh 
4821399fb87SGovind Singh 	return -EINVAL;
4831399fb87SGovind Singh }
4841399fb87SGovind Singh 
485c4eacabeSGovind Singh static int ath11k_get_user_msi_assignment(struct ath11k_base *ab, char *user_name,
486c4eacabeSGovind Singh 					  int *num_vectors, u32 *user_base_data,
487c4eacabeSGovind Singh 					  u32 *base_vector)
488c4eacabeSGovind Singh {
489c4eacabeSGovind Singh 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
490c4eacabeSGovind Singh 
491c4eacabeSGovind Singh 	return ath11k_pci_get_user_msi_assignment(ab_pci, user_name,
492c4eacabeSGovind Singh 						  num_vectors, user_base_data,
493c4eacabeSGovind Singh 						  base_vector);
494c4eacabeSGovind Singh }
495c4eacabeSGovind Singh 
496d4ecb90bSCarl Huang static void ath11k_pci_free_ext_irq(struct ath11k_base *ab)
497d4ecb90bSCarl Huang {
498d4ecb90bSCarl Huang 	int i, j;
499d4ecb90bSCarl Huang 
500d4ecb90bSCarl Huang 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
501d4ecb90bSCarl Huang 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
502d4ecb90bSCarl Huang 
503d4ecb90bSCarl Huang 		for (j = 0; j < irq_grp->num_irq; j++)
504d4ecb90bSCarl Huang 			free_irq(ab->irq_num[irq_grp->irqs[j]], irq_grp);
505d4ecb90bSCarl Huang 
506d4ecb90bSCarl Huang 		netif_napi_del(&irq_grp->napi);
507d4ecb90bSCarl Huang 	}
508d4ecb90bSCarl Huang }
509d4ecb90bSCarl Huang 
5107f4beda2SGovind Singh static void ath11k_pci_free_irq(struct ath11k_base *ab)
5117f4beda2SGovind Singh {
5127f4beda2SGovind Singh 	int i, irq_idx;
5137f4beda2SGovind Singh 
514d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
515e3396b8bSCarl Huang 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
5167f4beda2SGovind Singh 			continue;
5177f4beda2SGovind Singh 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
5187f4beda2SGovind Singh 		free_irq(ab->irq_num[irq_idx], &ab->ce.ce_pipe[i]);
5197f4beda2SGovind Singh 	}
520d4ecb90bSCarl Huang 
521d4ecb90bSCarl Huang 	ath11k_pci_free_ext_irq(ab);
5227f4beda2SGovind Singh }
5237f4beda2SGovind Singh 
5242c3960c2SGovind Singh static void ath11k_pci_ce_irq_enable(struct ath11k_base *ab, u16 ce_id)
5252c3960c2SGovind Singh {
5262c3960c2SGovind Singh 	u32 irq_idx;
5272c3960c2SGovind Singh 
5282c3960c2SGovind Singh 	irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_id;
5292c3960c2SGovind Singh 	enable_irq(ab->irq_num[irq_idx]);
5302c3960c2SGovind Singh }
5312c3960c2SGovind Singh 
5327f4beda2SGovind Singh static void ath11k_pci_ce_irq_disable(struct ath11k_base *ab, u16 ce_id)
5337f4beda2SGovind Singh {
5347f4beda2SGovind Singh 	u32 irq_idx;
5357f4beda2SGovind Singh 
5367f4beda2SGovind Singh 	irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_id;
5377f4beda2SGovind Singh 	disable_irq_nosync(ab->irq_num[irq_idx]);
5387f4beda2SGovind Singh }
5397f4beda2SGovind Singh 
5402c3960c2SGovind Singh static void ath11k_pci_ce_irqs_disable(struct ath11k_base *ab)
5412c3960c2SGovind Singh {
5422c3960c2SGovind Singh 	int i;
5432c3960c2SGovind Singh 
544d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
545e3396b8bSCarl Huang 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
5462c3960c2SGovind Singh 			continue;
5472c3960c2SGovind Singh 		ath11k_pci_ce_irq_disable(ab, i);
5482c3960c2SGovind Singh 	}
5492c3960c2SGovind Singh }
5502c3960c2SGovind Singh 
5512c3960c2SGovind Singh static void ath11k_pci_sync_ce_irqs(struct ath11k_base *ab)
5522c3960c2SGovind Singh {
5532c3960c2SGovind Singh 	int i;
5542c3960c2SGovind Singh 	int irq_idx;
5552c3960c2SGovind Singh 
556d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
557e3396b8bSCarl Huang 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
5582c3960c2SGovind Singh 			continue;
5592c3960c2SGovind Singh 
5602c3960c2SGovind Singh 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
5612c3960c2SGovind Singh 		synchronize_irq(ab->irq_num[irq_idx]);
5622c3960c2SGovind Singh 	}
5632c3960c2SGovind Singh }
5642c3960c2SGovind Singh 
5650f01dcb8SAllen Pais static void ath11k_pci_ce_tasklet(struct tasklet_struct *t)
5662c3960c2SGovind Singh {
5670f01dcb8SAllen Pais 	struct ath11k_ce_pipe *ce_pipe = from_tasklet(ce_pipe, t, intr_tq);
5682c3960c2SGovind Singh 
5692c3960c2SGovind Singh 	ath11k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);
5702c3960c2SGovind Singh 
5712c3960c2SGovind Singh 	ath11k_pci_ce_irq_enable(ce_pipe->ab, ce_pipe->pipe_num);
5722c3960c2SGovind Singh }
5732c3960c2SGovind Singh 
5747f4beda2SGovind Singh static irqreturn_t ath11k_pci_ce_interrupt_handler(int irq, void *arg)
5757f4beda2SGovind Singh {
5767f4beda2SGovind Singh 	struct ath11k_ce_pipe *ce_pipe = arg;
5777f4beda2SGovind Singh 
5787f4beda2SGovind Singh 	ath11k_pci_ce_irq_disable(ce_pipe->ab, ce_pipe->pipe_num);
5792c3960c2SGovind Singh 	tasklet_schedule(&ce_pipe->intr_tq);
5807f4beda2SGovind Singh 
5817f4beda2SGovind Singh 	return IRQ_HANDLED;
5827f4beda2SGovind Singh }
5837f4beda2SGovind Singh 
584d4ecb90bSCarl Huang static void ath11k_pci_ext_grp_disable(struct ath11k_ext_irq_grp *irq_grp)
585d4ecb90bSCarl Huang {
586d4ecb90bSCarl Huang 	int i;
587d4ecb90bSCarl Huang 
588d4ecb90bSCarl Huang 	for (i = 0; i < irq_grp->num_irq; i++)
589d4ecb90bSCarl Huang 		disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
590d4ecb90bSCarl Huang }
591d4ecb90bSCarl Huang 
592d4ecb90bSCarl Huang static void __ath11k_pci_ext_irq_disable(struct ath11k_base *sc)
593d4ecb90bSCarl Huang {
594d4ecb90bSCarl Huang 	int i;
595d4ecb90bSCarl Huang 
596d4ecb90bSCarl Huang 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
597d4ecb90bSCarl Huang 		struct ath11k_ext_irq_grp *irq_grp = &sc->ext_irq_grp[i];
598d4ecb90bSCarl Huang 
599d4ecb90bSCarl Huang 		ath11k_pci_ext_grp_disable(irq_grp);
600d4ecb90bSCarl Huang 
601d4ecb90bSCarl Huang 		napi_synchronize(&irq_grp->napi);
602d4ecb90bSCarl Huang 		napi_disable(&irq_grp->napi);
603d4ecb90bSCarl Huang 	}
604d4ecb90bSCarl Huang }
605d4ecb90bSCarl Huang 
606d4ecb90bSCarl Huang static void ath11k_pci_ext_grp_enable(struct ath11k_ext_irq_grp *irq_grp)
607d4ecb90bSCarl Huang {
608d4ecb90bSCarl Huang 	int i;
609d4ecb90bSCarl Huang 
610d4ecb90bSCarl Huang 	for (i = 0; i < irq_grp->num_irq; i++)
611d4ecb90bSCarl Huang 		enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
612d4ecb90bSCarl Huang }
613d4ecb90bSCarl Huang 
614d4ecb90bSCarl Huang static void ath11k_pci_ext_irq_enable(struct ath11k_base *ab)
615d4ecb90bSCarl Huang {
616d4ecb90bSCarl Huang 	int i;
617d4ecb90bSCarl Huang 
618d4ecb90bSCarl Huang 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
619d4ecb90bSCarl Huang 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
620d4ecb90bSCarl Huang 
621d4ecb90bSCarl Huang 		napi_enable(&irq_grp->napi);
622d4ecb90bSCarl Huang 		ath11k_pci_ext_grp_enable(irq_grp);
623d4ecb90bSCarl Huang 	}
624d4ecb90bSCarl Huang }
625d4ecb90bSCarl Huang 
626d4ecb90bSCarl Huang static void ath11k_pci_sync_ext_irqs(struct ath11k_base *ab)
627d4ecb90bSCarl Huang {
628d4ecb90bSCarl Huang 	int i, j, irq_idx;
629d4ecb90bSCarl Huang 
630d4ecb90bSCarl Huang 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
631d4ecb90bSCarl Huang 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
632d4ecb90bSCarl Huang 
633d4ecb90bSCarl Huang 		for (j = 0; j < irq_grp->num_irq; j++) {
634d4ecb90bSCarl Huang 			irq_idx = irq_grp->irqs[j];
635d4ecb90bSCarl Huang 			synchronize_irq(ab->irq_num[irq_idx]);
636d4ecb90bSCarl Huang 		}
637d4ecb90bSCarl Huang 	}
638d4ecb90bSCarl Huang }
639d4ecb90bSCarl Huang 
640d4ecb90bSCarl Huang static void ath11k_pci_ext_irq_disable(struct ath11k_base *ab)
641d4ecb90bSCarl Huang {
642d4ecb90bSCarl Huang 	__ath11k_pci_ext_irq_disable(ab);
643d4ecb90bSCarl Huang 	ath11k_pci_sync_ext_irqs(ab);
644d4ecb90bSCarl Huang }
645d4ecb90bSCarl Huang 
646d4ecb90bSCarl Huang static int ath11k_pci_ext_grp_napi_poll(struct napi_struct *napi, int budget)
647d4ecb90bSCarl Huang {
648d4ecb90bSCarl Huang 	struct ath11k_ext_irq_grp *irq_grp = container_of(napi,
649d4ecb90bSCarl Huang 						struct ath11k_ext_irq_grp,
650d4ecb90bSCarl Huang 						napi);
651d4ecb90bSCarl Huang 	struct ath11k_base *ab = irq_grp->ab;
652d4ecb90bSCarl Huang 	int work_done;
653d4ecb90bSCarl Huang 
654d4ecb90bSCarl Huang 	work_done = ath11k_dp_service_srng(ab, irq_grp, budget);
655d4ecb90bSCarl Huang 	if (work_done < budget) {
656d4ecb90bSCarl Huang 		napi_complete_done(napi, work_done);
657d4ecb90bSCarl Huang 		ath11k_pci_ext_grp_enable(irq_grp);
658d4ecb90bSCarl Huang 	}
659d4ecb90bSCarl Huang 
660d4ecb90bSCarl Huang 	if (work_done > budget)
661d4ecb90bSCarl Huang 		work_done = budget;
662d4ecb90bSCarl Huang 
663d4ecb90bSCarl Huang 	return work_done;
664d4ecb90bSCarl Huang }
665d4ecb90bSCarl Huang 
666d4ecb90bSCarl Huang static irqreturn_t ath11k_pci_ext_interrupt_handler(int irq, void *arg)
667d4ecb90bSCarl Huang {
668d4ecb90bSCarl Huang 	struct ath11k_ext_irq_grp *irq_grp = arg;
669d4ecb90bSCarl Huang 
670d4ecb90bSCarl Huang 	ath11k_dbg(irq_grp->ab, ATH11K_DBG_PCI, "ext irq:%d\n", irq);
671d4ecb90bSCarl Huang 
672d4ecb90bSCarl Huang 	ath11k_pci_ext_grp_disable(irq_grp);
673d4ecb90bSCarl Huang 
674d4ecb90bSCarl Huang 	napi_schedule(&irq_grp->napi);
675d4ecb90bSCarl Huang 
676d4ecb90bSCarl Huang 	return IRQ_HANDLED;
677d4ecb90bSCarl Huang }
678d4ecb90bSCarl Huang 
679d4ecb90bSCarl Huang static int ath11k_pci_ext_irq_config(struct ath11k_base *ab)
680d4ecb90bSCarl Huang {
681d4ecb90bSCarl Huang 	int i, j, ret, num_vectors = 0;
682d4ecb90bSCarl Huang 	u32 user_base_data = 0, base_vector = 0;
683d4ecb90bSCarl Huang 
684b2c09458SColin Ian King 	ret = ath11k_pci_get_user_msi_assignment(ath11k_pci_priv(ab), "DP",
685b2c09458SColin Ian King 						 &num_vectors,
686b2c09458SColin Ian King 						 &user_base_data,
687d4ecb90bSCarl Huang 						 &base_vector);
688b2c09458SColin Ian King 	if (ret < 0)
689b2c09458SColin Ian King 		return ret;
690d4ecb90bSCarl Huang 
691d4ecb90bSCarl Huang 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
692d4ecb90bSCarl Huang 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
693d4ecb90bSCarl Huang 		u32 num_irq = 0;
694d4ecb90bSCarl Huang 
695d4ecb90bSCarl Huang 		irq_grp->ab = ab;
696d4ecb90bSCarl Huang 		irq_grp->grp_id = i;
697d4ecb90bSCarl Huang 		init_dummy_netdev(&irq_grp->napi_ndev);
698d4ecb90bSCarl Huang 		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
699d4ecb90bSCarl Huang 			       ath11k_pci_ext_grp_napi_poll, NAPI_POLL_WEIGHT);
700d4ecb90bSCarl Huang 
701d4ecb90bSCarl Huang 		if (ab->hw_params.ring_mask->tx[i] ||
702d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->rx[i] ||
703d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->rx_err[i] ||
704d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->rx_wbm_rel[i] ||
705d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->reo_status[i] ||
706d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->rxdma2host[i] ||
707d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->host2rxdma[i] ||
708d4ecb90bSCarl Huang 		    ab->hw_params.ring_mask->rx_mon_status[i]) {
709d4ecb90bSCarl Huang 			num_irq = 1;
710d4ecb90bSCarl Huang 		}
711d4ecb90bSCarl Huang 
712d4ecb90bSCarl Huang 		irq_grp->num_irq = num_irq;
713d4ecb90bSCarl Huang 		irq_grp->irqs[0] = base_vector + i;
714d4ecb90bSCarl Huang 
715d4ecb90bSCarl Huang 		for (j = 0; j < irq_grp->num_irq; j++) {
716d4ecb90bSCarl Huang 			int irq_idx = irq_grp->irqs[j];
717d4ecb90bSCarl Huang 			int vector = (i % num_vectors) + base_vector;
718d4ecb90bSCarl Huang 			int irq = ath11k_pci_get_msi_irq(ab->dev, vector);
719d4ecb90bSCarl Huang 
720d4ecb90bSCarl Huang 			ab->irq_num[irq_idx] = irq;
721d4ecb90bSCarl Huang 
722d4ecb90bSCarl Huang 			ath11k_dbg(ab, ATH11K_DBG_PCI,
723d4ecb90bSCarl Huang 				   "irq:%d group:%d\n", irq, i);
724d4ecb90bSCarl Huang 			ret = request_irq(irq, ath11k_pci_ext_interrupt_handler,
725d4ecb90bSCarl Huang 					  IRQF_SHARED,
726d4ecb90bSCarl Huang 					  "DP_EXT_IRQ", irq_grp);
727d4ecb90bSCarl Huang 			if (ret) {
728d4ecb90bSCarl Huang 				ath11k_err(ab, "failed request irq %d: %d\n",
729d4ecb90bSCarl Huang 					   vector, ret);
730d4ecb90bSCarl Huang 				return ret;
731d4ecb90bSCarl Huang 			}
732d4ecb90bSCarl Huang 
733d4ecb90bSCarl Huang 			disable_irq_nosync(ab->irq_num[irq_idx]);
734d4ecb90bSCarl Huang 		}
735d4ecb90bSCarl Huang 	}
736d4ecb90bSCarl Huang 
737d4ecb90bSCarl Huang 	return 0;
738d4ecb90bSCarl Huang }
739d4ecb90bSCarl Huang 
7407f4beda2SGovind Singh static int ath11k_pci_config_irq(struct ath11k_base *ab)
7417f4beda2SGovind Singh {
7427f4beda2SGovind Singh 	struct ath11k_ce_pipe *ce_pipe;
7437f4beda2SGovind Singh 	u32 msi_data_start;
7447f4beda2SGovind Singh 	u32 msi_data_count;
7457f4beda2SGovind Singh 	u32 msi_irq_start;
7467f4beda2SGovind Singh 	unsigned int msi_data;
7477f4beda2SGovind Singh 	int irq, i, ret, irq_idx;
7487f4beda2SGovind Singh 
7497f4beda2SGovind Singh 	ret = ath11k_pci_get_user_msi_assignment(ath11k_pci_priv(ab),
7507f4beda2SGovind Singh 						 "CE", &msi_data_count,
7517f4beda2SGovind Singh 						 &msi_data_start, &msi_irq_start);
7527f4beda2SGovind Singh 	if (ret)
7537f4beda2SGovind Singh 		return ret;
7547f4beda2SGovind Singh 
7557f4beda2SGovind Singh 	/* Configure CE irqs */
756d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
7577f4beda2SGovind Singh 		msi_data = (i % msi_data_count) + msi_irq_start;
7587f4beda2SGovind Singh 		irq = ath11k_pci_get_msi_irq(ab->dev, msi_data);
7597f4beda2SGovind Singh 		ce_pipe = &ab->ce.ce_pipe[i];
7607f4beda2SGovind Singh 
761e3396b8bSCarl Huang 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
7627f4beda2SGovind Singh 			continue;
7637f4beda2SGovind Singh 
7647f4beda2SGovind Singh 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
7657f4beda2SGovind Singh 
7660f01dcb8SAllen Pais 		tasklet_setup(&ce_pipe->intr_tq, ath11k_pci_ce_tasklet);
7672c3960c2SGovind Singh 
7687f4beda2SGovind Singh 		ret = request_irq(irq, ath11k_pci_ce_interrupt_handler,
7697f4beda2SGovind Singh 				  IRQF_SHARED, irq_name[irq_idx],
7707f4beda2SGovind Singh 				  ce_pipe);
7717f4beda2SGovind Singh 		if (ret) {
7727f4beda2SGovind Singh 			ath11k_err(ab, "failed to request irq %d: %d\n",
7737f4beda2SGovind Singh 				   irq_idx, ret);
7747f4beda2SGovind Singh 			return ret;
7757f4beda2SGovind Singh 		}
7767f4beda2SGovind Singh 
7777f4beda2SGovind Singh 		ab->irq_num[irq_idx] = irq;
778e5c860e1SCarl Huang 		ath11k_pci_ce_irq_disable(ab, i);
7797f4beda2SGovind Singh 	}
7807f4beda2SGovind Singh 
781d4ecb90bSCarl Huang 	ret = ath11k_pci_ext_irq_config(ab);
782d4ecb90bSCarl Huang 	if (ret)
783d4ecb90bSCarl Huang 		return ret;
784d4ecb90bSCarl Huang 
7857f4beda2SGovind Singh 	return 0;
7867f4beda2SGovind Singh }
7877f4beda2SGovind Singh 
7887f4beda2SGovind Singh static void ath11k_pci_init_qmi_ce_config(struct ath11k_base *ab)
7897f4beda2SGovind Singh {
7907f4beda2SGovind Singh 	struct ath11k_qmi_ce_cfg *cfg = &ab->qmi.ce_cfg;
7917f4beda2SGovind Singh 
792967c1d11SAnilkumar Kolli 	cfg->tgt_ce = ab->hw_params.target_ce_config;
793967c1d11SAnilkumar Kolli 	cfg->tgt_ce_len = ab->hw_params.target_ce_count;
7947f4beda2SGovind Singh 
795967c1d11SAnilkumar Kolli 	cfg->svc_to_ce_map = ab->hw_params.svc_to_ce_map;
796967c1d11SAnilkumar Kolli 	cfg->svc_to_ce_map_len = ab->hw_params.svc_to_ce_map_len;
79716001e4bSAnilkumar Kolli 	ab->qmi.service_ins_id = ab->hw_params.qmi_service_ins_id;
798e838c14aSCarl Huang 
799e838c14aSCarl Huang 	ath11k_ce_get_shadow_config(ab, &cfg->shadow_reg_v2,
800e838c14aSCarl Huang 				    &cfg->shadow_reg_v2_len);
8017f4beda2SGovind Singh }
8027f4beda2SGovind Singh 
8037f4beda2SGovind Singh static void ath11k_pci_ce_irqs_enable(struct ath11k_base *ab)
8047f4beda2SGovind Singh {
8057f4beda2SGovind Singh 	int i;
8067f4beda2SGovind Singh 
807d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
808e3396b8bSCarl Huang 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
8097f4beda2SGovind Singh 			continue;
8107f4beda2SGovind Singh 		ath11k_pci_ce_irq_enable(ab, i);
8117f4beda2SGovind Singh 	}
8127f4beda2SGovind Singh }
8137f4beda2SGovind Singh 
8145697a564SGovind Singh static int ath11k_pci_enable_msi(struct ath11k_pci *ab_pci)
8155697a564SGovind Singh {
8165697a564SGovind Singh 	struct ath11k_base *ab = ab_pci->ab;
8177a3aed0cSAnilkumar Kolli 	const struct ath11k_msi_config *msi_config = ab_pci->msi_config;
8185697a564SGovind Singh 	struct msi_desc *msi_desc;
8195697a564SGovind Singh 	int num_vectors;
8205697a564SGovind Singh 	int ret;
8215697a564SGovind Singh 
8225697a564SGovind Singh 	num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
8237a3aed0cSAnilkumar Kolli 					    msi_config->total_vectors,
8247a3aed0cSAnilkumar Kolli 					    msi_config->total_vectors,
8255697a564SGovind Singh 					    PCI_IRQ_MSI);
8267a3aed0cSAnilkumar Kolli 	if (num_vectors != msi_config->total_vectors) {
8275697a564SGovind Singh 		ath11k_err(ab, "failed to get %d MSI vectors, only %d available",
8287a3aed0cSAnilkumar Kolli 			   msi_config->total_vectors, num_vectors);
8295697a564SGovind Singh 
8305697a564SGovind Singh 		if (num_vectors >= 0)
8315697a564SGovind Singh 			return -EINVAL;
8325697a564SGovind Singh 		else
8335697a564SGovind Singh 			return num_vectors;
8345697a564SGovind Singh 	}
8355697a564SGovind Singh 
8365697a564SGovind Singh 	msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
8375697a564SGovind Singh 	if (!msi_desc) {
8385697a564SGovind Singh 		ath11k_err(ab, "msi_desc is NULL!\n");
8395697a564SGovind Singh 		ret = -EINVAL;
8405697a564SGovind Singh 		goto free_msi_vector;
8415697a564SGovind Singh 	}
8425697a564SGovind Singh 
8435697a564SGovind Singh 	ab_pci->msi_ep_base_data = msi_desc->msg.data;
844e8e55d89SAnilkumar Kolli 	if (msi_desc->msi_attrib.is_64)
845e8e55d89SAnilkumar Kolli 		set_bit(ATH11K_PCI_FLAG_IS_MSI_64, &ab_pci->flags);
8465697a564SGovind Singh 
8475697a564SGovind Singh 	ath11k_dbg(ab, ATH11K_DBG_PCI, "msi base data is %d\n", ab_pci->msi_ep_base_data);
8485697a564SGovind Singh 
8495697a564SGovind Singh 	return 0;
8505697a564SGovind Singh 
8515697a564SGovind Singh free_msi_vector:
8525697a564SGovind Singh 	pci_free_irq_vectors(ab_pci->pdev);
8535697a564SGovind Singh 
8545697a564SGovind Singh 	return ret;
8555697a564SGovind Singh }
8565697a564SGovind Singh 
8575697a564SGovind Singh static void ath11k_pci_disable_msi(struct ath11k_pci *ab_pci)
8585697a564SGovind Singh {
8595697a564SGovind Singh 	pci_free_irq_vectors(ab_pci->pdev);
8605697a564SGovind Singh }
8615697a564SGovind Singh 
8625762613eSGovind Singh static int ath11k_pci_claim(struct ath11k_pci *ab_pci, struct pci_dev *pdev)
8635762613eSGovind Singh {
8645762613eSGovind Singh 	struct ath11k_base *ab = ab_pci->ab;
8655762613eSGovind Singh 	u16 device_id;
8665762613eSGovind Singh 	int ret = 0;
8675762613eSGovind Singh 
8685762613eSGovind Singh 	pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
8695762613eSGovind Singh 	if (device_id != ab_pci->dev_id)  {
8705762613eSGovind Singh 		ath11k_err(ab, "pci device id mismatch: 0x%x 0x%x\n",
8715762613eSGovind Singh 			   device_id, ab_pci->dev_id);
8725762613eSGovind Singh 		ret = -EIO;
8735762613eSGovind Singh 		goto out;
8745762613eSGovind Singh 	}
8755762613eSGovind Singh 
8765762613eSGovind Singh 	ret = pci_assign_resource(pdev, ATH11K_PCI_BAR_NUM);
8775762613eSGovind Singh 	if (ret) {
8785762613eSGovind Singh 		ath11k_err(ab, "failed to assign pci resource: %d\n", ret);
8795762613eSGovind Singh 		goto out;
8805762613eSGovind Singh 	}
8815762613eSGovind Singh 
8825762613eSGovind Singh 	ret = pci_enable_device(pdev);
8835762613eSGovind Singh 	if (ret) {
8845762613eSGovind Singh 		ath11k_err(ab, "failed to enable pci device: %d\n", ret);
8855762613eSGovind Singh 		goto out;
8865762613eSGovind Singh 	}
8875762613eSGovind Singh 
8885762613eSGovind Singh 	ret = pci_request_region(pdev, ATH11K_PCI_BAR_NUM, "ath11k_pci");
8895762613eSGovind Singh 	if (ret) {
8905762613eSGovind Singh 		ath11k_err(ab, "failed to request pci region: %d\n", ret);
8915762613eSGovind Singh 		goto disable_device;
8925762613eSGovind Singh 	}
8935762613eSGovind Singh 
8945762613eSGovind Singh 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(ATH11K_PCI_DMA_MASK));
8955762613eSGovind Singh 	if (ret) {
8965762613eSGovind Singh 		ath11k_err(ab, "failed to set pci dma mask to %d: %d\n",
8975762613eSGovind Singh 			   ATH11K_PCI_DMA_MASK, ret);
8985762613eSGovind Singh 		goto release_region;
8995762613eSGovind Singh 	}
9005762613eSGovind Singh 
9015762613eSGovind Singh 	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(ATH11K_PCI_DMA_MASK));
9025762613eSGovind Singh 	if (ret) {
9035762613eSGovind Singh 		ath11k_err(ab, "failed to set pci consistent dma mask to %d: %d\n",
9045762613eSGovind Singh 			   ATH11K_PCI_DMA_MASK, ret);
9055762613eSGovind Singh 		goto release_region;
9065762613eSGovind Singh 	}
9075762613eSGovind Singh 
9085762613eSGovind Singh 	pci_set_master(pdev);
9095762613eSGovind Singh 
9105762613eSGovind Singh 	ab->mem_len = pci_resource_len(pdev, ATH11K_PCI_BAR_NUM);
9115762613eSGovind Singh 	ab->mem = pci_iomap(pdev, ATH11K_PCI_BAR_NUM, 0);
9125762613eSGovind Singh 	if (!ab->mem) {
9135762613eSGovind Singh 		ath11k_err(ab, "failed to map pci bar %d\n", ATH11K_PCI_BAR_NUM);
9145762613eSGovind Singh 		ret = -EIO;
9155762613eSGovind Singh 		goto clear_master;
9165762613eSGovind Singh 	}
9175762613eSGovind Singh 
9185762613eSGovind Singh 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot pci_mem 0x%pK\n", ab->mem);
9195762613eSGovind Singh 	return 0;
9205762613eSGovind Singh 
9215762613eSGovind Singh clear_master:
9225762613eSGovind Singh 	pci_clear_master(pdev);
9235762613eSGovind Singh release_region:
9245762613eSGovind Singh 	pci_release_region(pdev, ATH11K_PCI_BAR_NUM);
9255762613eSGovind Singh disable_device:
9265762613eSGovind Singh 	pci_disable_device(pdev);
9275762613eSGovind Singh out:
9285762613eSGovind Singh 	return ret;
9295762613eSGovind Singh }
9305762613eSGovind Singh 
9315762613eSGovind Singh static void ath11k_pci_free_region(struct ath11k_pci *ab_pci)
9325762613eSGovind Singh {
9335762613eSGovind Singh 	struct ath11k_base *ab = ab_pci->ab;
9345762613eSGovind Singh 	struct pci_dev *pci_dev = ab_pci->pdev;
9355762613eSGovind Singh 
9365762613eSGovind Singh 	pci_iounmap(pci_dev, ab->mem);
9375762613eSGovind Singh 	ab->mem = NULL;
9385762613eSGovind Singh 	pci_clear_master(pci_dev);
9395762613eSGovind Singh 	pci_release_region(pci_dev, ATH11K_PCI_BAR_NUM);
9405762613eSGovind Singh 	if (pci_is_enabled(pci_dev))
9415762613eSGovind Singh 		pci_disable_device(pci_dev);
9425762613eSGovind Singh }
9435762613eSGovind Singh 
944e9603f4bSCarl Huang static void ath11k_pci_aspm_disable(struct ath11k_pci *ab_pci)
945e9603f4bSCarl Huang {
946e9603f4bSCarl Huang 	struct ath11k_base *ab = ab_pci->ab;
947e9603f4bSCarl Huang 
948e9603f4bSCarl Huang 	pcie_capability_read_word(ab_pci->pdev, PCI_EXP_LNKCTL,
949e9603f4bSCarl Huang 				  &ab_pci->link_ctl);
950e9603f4bSCarl Huang 
951e9603f4bSCarl Huang 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci link_ctl 0x%04x L0s %d L1 %d\n",
952e9603f4bSCarl Huang 		   ab_pci->link_ctl,
953e9603f4bSCarl Huang 		   u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L0S),
954e9603f4bSCarl Huang 		   u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L1));
955e9603f4bSCarl Huang 
956e9603f4bSCarl Huang 	/* disable L0s and L1 */
957e9603f4bSCarl Huang 	pcie_capability_write_word(ab_pci->pdev, PCI_EXP_LNKCTL,
958e9603f4bSCarl Huang 				   ab_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
959e9603f4bSCarl Huang 
960e9603f4bSCarl Huang 	set_bit(ATH11K_PCI_ASPM_RESTORE, &ab_pci->flags);
961e9603f4bSCarl Huang }
962e9603f4bSCarl Huang 
963e9603f4bSCarl Huang static void ath11k_pci_aspm_restore(struct ath11k_pci *ab_pci)
964e9603f4bSCarl Huang {
965e9603f4bSCarl Huang 	if (test_and_clear_bit(ATH11K_PCI_ASPM_RESTORE, &ab_pci->flags))
966e9603f4bSCarl Huang 		pcie_capability_write_word(ab_pci->pdev, PCI_EXP_LNKCTL,
967e9603f4bSCarl Huang 					   ab_pci->link_ctl);
968e9603f4bSCarl Huang }
969e9603f4bSCarl Huang 
9701399fb87SGovind Singh static int ath11k_pci_power_up(struct ath11k_base *ab)
9711399fb87SGovind Singh {
9721399fb87SGovind Singh 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
9731399fb87SGovind Singh 	int ret;
9741399fb87SGovind Singh 
975a05bd851SCarl Huang 	ab_pci->register_window = 0;
976a05bd851SCarl Huang 	clear_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
977babb0cedSCarl Huang 	ath11k_pci_sw_reset(ab_pci->ab, true);
978f3c603d4SCarl Huang 
979e9603f4bSCarl Huang 	/* Disable ASPM during firmware download due to problems switching
980e9603f4bSCarl Huang 	 * to AMSS state.
981e9603f4bSCarl Huang 	 */
982e9603f4bSCarl Huang 	ath11k_pci_aspm_disable(ab_pci);
983e9603f4bSCarl Huang 
9841399fb87SGovind Singh 	ret = ath11k_mhi_start(ab_pci);
9851399fb87SGovind Singh 	if (ret) {
9861399fb87SGovind Singh 		ath11k_err(ab, "failed to start mhi: %d\n", ret);
9871399fb87SGovind Singh 		return ret;
9881399fb87SGovind Singh 	}
9891399fb87SGovind Singh 
990480a7361SKarthikeyan Periyasamy 	if (ab->bus_params.static_window_map)
991480a7361SKarthikeyan Periyasamy 		ath11k_pci_select_static_window(ab_pci);
992480a7361SKarthikeyan Periyasamy 
9931399fb87SGovind Singh 	return 0;
9941399fb87SGovind Singh }
9951399fb87SGovind Singh 
9961399fb87SGovind Singh static void ath11k_pci_power_down(struct ath11k_base *ab)
9971399fb87SGovind Singh {
9981399fb87SGovind Singh 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
9991399fb87SGovind Singh 
1000e9603f4bSCarl Huang 	/* restore aspm in case firmware bootup fails */
1001e9603f4bSCarl Huang 	ath11k_pci_aspm_restore(ab_pci);
1002e9603f4bSCarl Huang 
1003babb0cedSCarl Huang 	ath11k_pci_force_wake(ab_pci->ab);
10041399fb87SGovind Singh 	ath11k_mhi_stop(ab_pci);
1005a05bd851SCarl Huang 	clear_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1006babb0cedSCarl Huang 	ath11k_pci_sw_reset(ab_pci->ab, false);
10071399fb87SGovind Singh }
10081399fb87SGovind Singh 
1009fa5917e4SCarl Huang static int ath11k_pci_hif_suspend(struct ath11k_base *ab)
1010fa5917e4SCarl Huang {
1011fa5917e4SCarl Huang 	struct ath11k_pci *ar_pci = ath11k_pci_priv(ab);
1012fa5917e4SCarl Huang 
1013fa5917e4SCarl Huang 	ath11k_mhi_suspend(ar_pci);
1014fa5917e4SCarl Huang 
1015fa5917e4SCarl Huang 	return 0;
1016fa5917e4SCarl Huang }
1017fa5917e4SCarl Huang 
1018fa5917e4SCarl Huang static int ath11k_pci_hif_resume(struct ath11k_base *ab)
1019fa5917e4SCarl Huang {
1020fa5917e4SCarl Huang 	struct ath11k_pci *ar_pci = ath11k_pci_priv(ab);
1021fa5917e4SCarl Huang 
1022fa5917e4SCarl Huang 	ath11k_mhi_resume(ar_pci);
1023fa5917e4SCarl Huang 
1024fa5917e4SCarl Huang 	return 0;
1025fa5917e4SCarl Huang }
1026fa5917e4SCarl Huang 
10272c3960c2SGovind Singh static void ath11k_pci_kill_tasklets(struct ath11k_base *ab)
10282c3960c2SGovind Singh {
10292c3960c2SGovind Singh 	int i;
10302c3960c2SGovind Singh 
1031d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
10322c3960c2SGovind Singh 		struct ath11k_ce_pipe *ce_pipe = &ab->ce.ce_pipe[i];
10332c3960c2SGovind Singh 
1034e3396b8bSCarl Huang 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
10352c3960c2SGovind Singh 			continue;
10362c3960c2SGovind Singh 
10372c3960c2SGovind Singh 		tasklet_kill(&ce_pipe->intr_tq);
10382c3960c2SGovind Singh 	}
10392c3960c2SGovind Singh }
10402c3960c2SGovind Singh 
1041d578ec2aSCarl Huang static void ath11k_pci_ce_irq_disable_sync(struct ath11k_base *ab)
10427f4beda2SGovind Singh {
10432c3960c2SGovind Singh 	ath11k_pci_ce_irqs_disable(ab);
10442c3960c2SGovind Singh 	ath11k_pci_sync_ce_irqs(ab);
10452c3960c2SGovind Singh 	ath11k_pci_kill_tasklets(ab);
1046d578ec2aSCarl Huang }
1047d578ec2aSCarl Huang 
1048d578ec2aSCarl Huang static void ath11k_pci_stop(struct ath11k_base *ab)
1049d578ec2aSCarl Huang {
1050d578ec2aSCarl Huang 	ath11k_pci_ce_irq_disable_sync(ab);
10517f4beda2SGovind Singh 	ath11k_ce_cleanup_pipes(ab);
10527f4beda2SGovind Singh }
10537f4beda2SGovind Singh 
10547f4beda2SGovind Singh static int ath11k_pci_start(struct ath11k_base *ab)
10557f4beda2SGovind Singh {
1056a05bd851SCarl Huang 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
1057a05bd851SCarl Huang 
1058a05bd851SCarl Huang 	set_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1059a05bd851SCarl Huang 
1060e9603f4bSCarl Huang 	ath11k_pci_aspm_restore(ab_pci);
1061e9603f4bSCarl Huang 
10627f4beda2SGovind Singh 	ath11k_pci_ce_irqs_enable(ab);
10632c3960c2SGovind Singh 	ath11k_ce_rx_post_buf(ab);
10642c3960c2SGovind Singh 
10652c3960c2SGovind Singh 	return 0;
10662c3960c2SGovind Singh }
10672c3960c2SGovind Singh 
1068d578ec2aSCarl Huang static void ath11k_pci_hif_ce_irq_enable(struct ath11k_base *ab)
1069d578ec2aSCarl Huang {
1070d578ec2aSCarl Huang 	ath11k_pci_ce_irqs_enable(ab);
1071d578ec2aSCarl Huang }
1072d578ec2aSCarl Huang 
1073d578ec2aSCarl Huang static void ath11k_pci_hif_ce_irq_disable(struct ath11k_base *ab)
1074d578ec2aSCarl Huang {
1075d578ec2aSCarl Huang 	ath11k_pci_ce_irq_disable_sync(ab);
1076d578ec2aSCarl Huang }
1077d578ec2aSCarl Huang 
10782c3960c2SGovind Singh static int ath11k_pci_map_service_to_pipe(struct ath11k_base *ab, u16 service_id,
10792c3960c2SGovind Singh 					  u8 *ul_pipe, u8 *dl_pipe)
10802c3960c2SGovind Singh {
10812c3960c2SGovind Singh 	const struct service_to_pipe *entry;
10822c3960c2SGovind Singh 	bool ul_set = false, dl_set = false;
10832c3960c2SGovind Singh 	int i;
10842c3960c2SGovind Singh 
1085967c1d11SAnilkumar Kolli 	for (i = 0; i < ab->hw_params.svc_to_ce_map_len; i++) {
1086967c1d11SAnilkumar Kolli 		entry = &ab->hw_params.svc_to_ce_map[i];
10872c3960c2SGovind Singh 
10882c3960c2SGovind Singh 		if (__le32_to_cpu(entry->service_id) != service_id)
10892c3960c2SGovind Singh 			continue;
10902c3960c2SGovind Singh 
10912c3960c2SGovind Singh 		switch (__le32_to_cpu(entry->pipedir)) {
10922c3960c2SGovind Singh 		case PIPEDIR_NONE:
10932c3960c2SGovind Singh 			break;
10942c3960c2SGovind Singh 		case PIPEDIR_IN:
10952c3960c2SGovind Singh 			WARN_ON(dl_set);
10962c3960c2SGovind Singh 			*dl_pipe = __le32_to_cpu(entry->pipenum);
10972c3960c2SGovind Singh 			dl_set = true;
10982c3960c2SGovind Singh 			break;
10992c3960c2SGovind Singh 		case PIPEDIR_OUT:
11002c3960c2SGovind Singh 			WARN_ON(ul_set);
11012c3960c2SGovind Singh 			*ul_pipe = __le32_to_cpu(entry->pipenum);
11022c3960c2SGovind Singh 			ul_set = true;
11032c3960c2SGovind Singh 			break;
11042c3960c2SGovind Singh 		case PIPEDIR_INOUT:
11052c3960c2SGovind Singh 			WARN_ON(dl_set);
11062c3960c2SGovind Singh 			WARN_ON(ul_set);
11072c3960c2SGovind Singh 			*dl_pipe = __le32_to_cpu(entry->pipenum);
11082c3960c2SGovind Singh 			*ul_pipe = __le32_to_cpu(entry->pipenum);
11092c3960c2SGovind Singh 			dl_set = true;
11102c3960c2SGovind Singh 			ul_set = true;
11112c3960c2SGovind Singh 			break;
11122c3960c2SGovind Singh 		}
11132c3960c2SGovind Singh 	}
11142c3960c2SGovind Singh 
11152c3960c2SGovind Singh 	if (WARN_ON(!ul_set || !dl_set))
11162c3960c2SGovind Singh 		return -ENOENT;
11177f4beda2SGovind Singh 
11187f4beda2SGovind Singh 	return 0;
11197f4beda2SGovind Singh }
11207f4beda2SGovind Singh 
11217f4beda2SGovind Singh static const struct ath11k_hif_ops ath11k_pci_hif_ops = {
11227f4beda2SGovind Singh 	.start = ath11k_pci_start,
11237f4beda2SGovind Singh 	.stop = ath11k_pci_stop,
1124654e959aSGovind Singh 	.read32 = ath11k_pci_read32,
1125654e959aSGovind Singh 	.write32 = ath11k_pci_write32,
11261399fb87SGovind Singh 	.power_down = ath11k_pci_power_down,
11271399fb87SGovind Singh 	.power_up = ath11k_pci_power_up,
1128fa5917e4SCarl Huang 	.suspend = ath11k_pci_hif_suspend,
1129fa5917e4SCarl Huang 	.resume = ath11k_pci_hif_resume,
1130d4ecb90bSCarl Huang 	.irq_enable = ath11k_pci_ext_irq_enable,
1131d4ecb90bSCarl Huang 	.irq_disable = ath11k_pci_ext_irq_disable,
1132c4eacabeSGovind Singh 	.get_msi_address =  ath11k_pci_get_msi_address,
1133c4eacabeSGovind Singh 	.get_user_msi_vector = ath11k_get_user_msi_assignment,
11342c3960c2SGovind Singh 	.map_service_to_pipe = ath11k_pci_map_service_to_pipe,
1135d578ec2aSCarl Huang 	.ce_irq_enable = ath11k_pci_hif_ce_irq_enable,
1136d578ec2aSCarl Huang 	.ce_irq_disable = ath11k_pci_hif_ce_irq_disable,
11371399fb87SGovind Singh };
11381399fb87SGovind Singh 
11396e0355afSGovind Singh static int ath11k_pci_probe(struct pci_dev *pdev,
11406e0355afSGovind Singh 			    const struct pci_device_id *pci_dev)
11416e0355afSGovind Singh {
11426e0355afSGovind Singh 	struct ath11k_base *ab;
11435762613eSGovind Singh 	struct ath11k_pci *ab_pci;
114418ac1665SKalle Valo 	u32 soc_hw_version, soc_hw_version_major, soc_hw_version_minor;
11455762613eSGovind Singh 	int ret;
11466e0355afSGovind Singh 
11471ff8ed78SGovind Singh 	ab = ath11k_core_alloc(&pdev->dev, sizeof(*ab_pci), ATH11K_BUS_PCI,
11481ff8ed78SGovind Singh 			       &ath11k_pci_bus_params);
11496e0355afSGovind Singh 	if (!ab) {
11506e0355afSGovind Singh 		dev_err(&pdev->dev, "failed to allocate ath11k base\n");
11516e0355afSGovind Singh 		return -ENOMEM;
11526e0355afSGovind Singh 	}
11536e0355afSGovind Singh 
11546e0355afSGovind Singh 	ab->dev = &pdev->dev;
11556e0355afSGovind Singh 	pci_set_drvdata(pdev, ab);
11565762613eSGovind Singh 	ab_pci = ath11k_pci_priv(ab);
11575762613eSGovind Singh 	ab_pci->dev_id = pci_dev->device;
11585762613eSGovind Singh 	ab_pci->ab = ab;
11595697a564SGovind Singh 	ab_pci->pdev = pdev;
11607f4beda2SGovind Singh 	ab->hif.ops = &ath11k_pci_hif_ops;
11615762613eSGovind Singh 	pci_set_drvdata(pdev, ab);
1162654e959aSGovind Singh 	spin_lock_init(&ab_pci->window_lock);
11635762613eSGovind Singh 
11645762613eSGovind Singh 	ret = ath11k_pci_claim(ab_pci, pdev);
11655762613eSGovind Singh 	if (ret) {
11665762613eSGovind Singh 		ath11k_err(ab, "failed to claim device: %d\n", ret);
11675762613eSGovind Singh 		goto err_free_core;
11685762613eSGovind Singh 	}
11696e0355afSGovind Singh 
117018ac1665SKalle Valo 	switch (pci_dev->device) {
117118ac1665SKalle Valo 	case QCA6390_DEVICE_ID:
117218ac1665SKalle Valo 		soc_hw_version = ath11k_pci_read32(ab, TCSR_SOC_HW_VERSION);
117318ac1665SKalle Valo 		soc_hw_version_major = FIELD_GET(TCSR_SOC_HW_VERSION_MAJOR_MASK,
117418ac1665SKalle Valo 						 soc_hw_version);
117518ac1665SKalle Valo 		soc_hw_version_minor = FIELD_GET(TCSR_SOC_HW_VERSION_MINOR_MASK,
117618ac1665SKalle Valo 						 soc_hw_version);
117718ac1665SKalle Valo 
117818ac1665SKalle Valo 		ath11k_dbg(ab, ATH11K_DBG_PCI, "pci tcsr_soc_hw_version major %d minor %d\n",
117918ac1665SKalle Valo 			   soc_hw_version_major, soc_hw_version_minor);
118018ac1665SKalle Valo 
118118ac1665SKalle Valo 		switch (soc_hw_version_major) {
118218ac1665SKalle Valo 		case 2:
118318ac1665SKalle Valo 			ab->hw_rev = ATH11K_HW_QCA6390_HW20;
118418ac1665SKalle Valo 			break;
118518ac1665SKalle Valo 		default:
118618ac1665SKalle Valo 			dev_err(&pdev->dev, "Unsupported QCA6390 SOC hardware version: %d %d\n",
118718ac1665SKalle Valo 				soc_hw_version_major, soc_hw_version_minor);
118818ac1665SKalle Valo 			ret = -EOPNOTSUPP;
118918ac1665SKalle Valo 			goto err_pci_free_region;
119018ac1665SKalle Valo 		}
119118ac1665SKalle Valo 		break;
119218ac1665SKalle Valo 	default:
119318ac1665SKalle Valo 		dev_err(&pdev->dev, "Unknown PCI device found: 0x%x\n",
119418ac1665SKalle Valo 			pci_dev->device);
119518ac1665SKalle Valo 		ret = -EOPNOTSUPP;
119618ac1665SKalle Valo 		goto err_pci_free_region;
119718ac1665SKalle Valo 	}
119818ac1665SKalle Valo 
11997a3aed0cSAnilkumar Kolli 	ab_pci->msi_config = &ath11k_msi_config[0];
12005697a564SGovind Singh 	ret = ath11k_pci_enable_msi(ab_pci);
12015697a564SGovind Singh 	if (ret) {
12025697a564SGovind Singh 		ath11k_err(ab, "failed to enable msi: %d\n", ret);
12035697a564SGovind Singh 		goto err_pci_free_region;
12045697a564SGovind Singh 	}
12055697a564SGovind Singh 
1206b8246f88SKalle Valo 	ret = ath11k_core_pre_init(ab);
1207b8246f88SKalle Valo 	if (ret)
1208b8246f88SKalle Valo 		goto err_pci_disable_msi;
1209b8246f88SKalle Valo 
12101399fb87SGovind Singh 	ret = ath11k_mhi_register(ab_pci);
12111399fb87SGovind Singh 	if (ret) {
12121399fb87SGovind Singh 		ath11k_err(ab, "failed to register mhi: %d\n", ret);
12131399fb87SGovind Singh 		goto err_pci_disable_msi;
12141399fb87SGovind Singh 	}
12151399fb87SGovind Singh 
12167f4beda2SGovind Singh 	ret = ath11k_hal_srng_init(ab);
12177f4beda2SGovind Singh 	if (ret)
12187f4beda2SGovind Singh 		goto err_mhi_unregister;
12197f4beda2SGovind Singh 
12207f4beda2SGovind Singh 	ret = ath11k_ce_alloc_pipes(ab);
12217f4beda2SGovind Singh 	if (ret) {
12227f4beda2SGovind Singh 		ath11k_err(ab, "failed to allocate ce pipes: %d\n", ret);
12237f4beda2SGovind Singh 		goto err_hal_srng_deinit;
12247f4beda2SGovind Singh 	}
12257f4beda2SGovind Singh 
12267f4beda2SGovind Singh 	ath11k_pci_init_qmi_ce_config(ab);
12277f4beda2SGovind Singh 
12287f4beda2SGovind Singh 	ret = ath11k_pci_config_irq(ab);
12297f4beda2SGovind Singh 	if (ret) {
12307f4beda2SGovind Singh 		ath11k_err(ab, "failed to config irq: %d\n", ret);
12317f4beda2SGovind Singh 		goto err_ce_free;
12327f4beda2SGovind Singh 	}
12337f4beda2SGovind Singh 
12347f4beda2SGovind Singh 	ret = ath11k_core_init(ab);
12357f4beda2SGovind Singh 	if (ret) {
12367f4beda2SGovind Singh 		ath11k_err(ab, "failed to init core: %d\n", ret);
12377f4beda2SGovind Singh 		goto err_free_irq;
12387f4beda2SGovind Singh 	}
12396e0355afSGovind Singh 	return 0;
12405762613eSGovind Singh 
12417f4beda2SGovind Singh err_free_irq:
12427f4beda2SGovind Singh 	ath11k_pci_free_irq(ab);
12437f4beda2SGovind Singh 
12447f4beda2SGovind Singh err_ce_free:
12457f4beda2SGovind Singh 	ath11k_ce_free_pipes(ab);
12467f4beda2SGovind Singh 
12477f4beda2SGovind Singh err_hal_srng_deinit:
12487f4beda2SGovind Singh 	ath11k_hal_srng_deinit(ab);
12497f4beda2SGovind Singh 
12507f4beda2SGovind Singh err_mhi_unregister:
12517f4beda2SGovind Singh 	ath11k_mhi_unregister(ab_pci);
12527f4beda2SGovind Singh 
1253b8246f88SKalle Valo err_pci_disable_msi:
1254b8246f88SKalle Valo 	ath11k_pci_disable_msi(ab_pci);
1255b8246f88SKalle Valo 
12565697a564SGovind Singh err_pci_free_region:
12575697a564SGovind Singh 	ath11k_pci_free_region(ab_pci);
12585697a564SGovind Singh 
12595762613eSGovind Singh err_free_core:
12605762613eSGovind Singh 	ath11k_core_free(ab);
12615697a564SGovind Singh 
12625762613eSGovind Singh 	return ret;
12636e0355afSGovind Singh }
12646e0355afSGovind Singh 
12656e0355afSGovind Singh static void ath11k_pci_remove(struct pci_dev *pdev)
12666e0355afSGovind Singh {
12676e0355afSGovind Singh 	struct ath11k_base *ab = pci_get_drvdata(pdev);
12685762613eSGovind Singh 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
12696e0355afSGovind Singh 
127061a57e51SAnilkumar Kolli 	if (test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags)) {
127161a57e51SAnilkumar Kolli 		ath11k_pci_power_down(ab);
127261a57e51SAnilkumar Kolli 		ath11k_debugfs_soc_destroy(ab);
127361a57e51SAnilkumar Kolli 		ath11k_qmi_deinit_service(ab);
127461a57e51SAnilkumar Kolli 		goto qmi_fail;
127561a57e51SAnilkumar Kolli 	}
127661a57e51SAnilkumar Kolli 
12776e0355afSGovind Singh 	set_bit(ATH11K_FLAG_UNREGISTERING, &ab->dev_flags);
12786fbd8898SCarl Huang 
12796fbd8898SCarl Huang 	ath11k_core_deinit(ab);
12806fbd8898SCarl Huang 
128161a57e51SAnilkumar Kolli qmi_fail:
12821399fb87SGovind Singh 	ath11k_mhi_unregister(ab_pci);
12836fbd8898SCarl Huang 
12846fbd8898SCarl Huang 	ath11k_pci_free_irq(ab);
12855697a564SGovind Singh 	ath11k_pci_disable_msi(ab_pci);
12865762613eSGovind Singh 	ath11k_pci_free_region(ab_pci);
12876fbd8898SCarl Huang 
12886fbd8898SCarl Huang 	ath11k_hal_srng_deinit(ab);
12896fbd8898SCarl Huang 	ath11k_ce_free_pipes(ab);
12906e0355afSGovind Singh 	ath11k_core_free(ab);
12916e0355afSGovind Singh }
12926e0355afSGovind Singh 
12931399fb87SGovind Singh static void ath11k_pci_shutdown(struct pci_dev *pdev)
12941399fb87SGovind Singh {
12951399fb87SGovind Singh 	struct ath11k_base *ab = pci_get_drvdata(pdev);
12961399fb87SGovind Singh 
12971399fb87SGovind Singh 	ath11k_pci_power_down(ab);
12981399fb87SGovind Singh }
12991399fb87SGovind Singh 
1300d1b0c338SCarl Huang static __maybe_unused int ath11k_pci_pm_suspend(struct device *dev)
1301d1b0c338SCarl Huang {
1302d1b0c338SCarl Huang 	struct ath11k_base *ab = dev_get_drvdata(dev);
1303d1b0c338SCarl Huang 	int ret;
1304d1b0c338SCarl Huang 
1305d1b0c338SCarl Huang 	ret = ath11k_core_suspend(ab);
1306d1b0c338SCarl Huang 	if (ret)
1307d1b0c338SCarl Huang 		ath11k_warn(ab, "failed to suspend core: %d\n", ret);
1308d1b0c338SCarl Huang 
1309d1b0c338SCarl Huang 	return ret;
1310d1b0c338SCarl Huang }
1311d1b0c338SCarl Huang 
1312d1b0c338SCarl Huang static __maybe_unused int ath11k_pci_pm_resume(struct device *dev)
1313d1b0c338SCarl Huang {
1314d1b0c338SCarl Huang 	struct ath11k_base *ab = dev_get_drvdata(dev);
1315d1b0c338SCarl Huang 	int ret;
1316d1b0c338SCarl Huang 
1317d1b0c338SCarl Huang 	ret = ath11k_core_resume(ab);
1318d1b0c338SCarl Huang 	if (ret)
1319d1b0c338SCarl Huang 		ath11k_warn(ab, "failed to resume core: %d\n", ret);
1320d1b0c338SCarl Huang 
1321d1b0c338SCarl Huang 	return ret;
1322d1b0c338SCarl Huang }
1323d1b0c338SCarl Huang 
1324d1b0c338SCarl Huang static SIMPLE_DEV_PM_OPS(ath11k_pci_pm_ops,
1325d1b0c338SCarl Huang 			 ath11k_pci_pm_suspend,
1326d1b0c338SCarl Huang 			 ath11k_pci_pm_resume);
1327d1b0c338SCarl Huang 
13286e0355afSGovind Singh static struct pci_driver ath11k_pci_driver = {
13296e0355afSGovind Singh 	.name = "ath11k_pci",
13306e0355afSGovind Singh 	.id_table = ath11k_pci_id_table,
13316e0355afSGovind Singh 	.probe = ath11k_pci_probe,
13326e0355afSGovind Singh 	.remove = ath11k_pci_remove,
13331399fb87SGovind Singh 	.shutdown = ath11k_pci_shutdown,
1334d1b0c338SCarl Huang #ifdef CONFIG_PM
1335d1b0c338SCarl Huang 	.driver.pm = &ath11k_pci_pm_ops,
1336d1b0c338SCarl Huang #endif
13376e0355afSGovind Singh };
13386e0355afSGovind Singh 
13396e0355afSGovind Singh static int ath11k_pci_init(void)
13406e0355afSGovind Singh {
13416e0355afSGovind Singh 	int ret;
13426e0355afSGovind Singh 
13436e0355afSGovind Singh 	ret = pci_register_driver(&ath11k_pci_driver);
13446e0355afSGovind Singh 	if (ret)
13456e0355afSGovind Singh 		pr_err("failed to register ath11k pci driver: %d\n",
13466e0355afSGovind Singh 		       ret);
13476e0355afSGovind Singh 
13486e0355afSGovind Singh 	return ret;
13496e0355afSGovind Singh }
13506e0355afSGovind Singh module_init(ath11k_pci_init);
13516e0355afSGovind Singh 
13526e0355afSGovind Singh static void ath11k_pci_exit(void)
13536e0355afSGovind Singh {
13546e0355afSGovind Singh 	pci_unregister_driver(&ath11k_pci_driver);
13556e0355afSGovind Singh }
13566e0355afSGovind Singh 
13576e0355afSGovind Singh module_exit(ath11k_pci_exit);
13586e0355afSGovind Singh 
13596e0355afSGovind Singh MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax WLAN PCIe devices");
13606e0355afSGovind Singh MODULE_LICENSE("Dual BSD/GPL");
13613dbd7fe7SDevin Bayer 
13623dbd7fe7SDevin Bayer /* QCA639x 2.0 firmware files */
13633dbd7fe7SDevin Bayer MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_BOARD_API2_FILE);
13643dbd7fe7SDevin Bayer MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_AMSS_FILE);
13653dbd7fe7SDevin Bayer MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_M3_FILE);
1366