xref: /freebsd/sys/contrib/dev/athk/ath11k/pci.c (revision 1d386b48)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/msi.h>
8 #include <linux/pci.h>
9 #if defined(CONFIG_OF)
10 #include <linux/of.h>
11 #endif
12 #if defined(__FreeBSD__)
13 #include <linux/delay.h>
14 #endif
15 
16 #include "pci.h"
17 #include "core.h"
18 #include "hif.h"
19 #include "mhi.h"
20 #include "debug.h"
21 
22 #define ATH11K_PCI_BAR_NUM		0
23 #define ATH11K_PCI_DMA_MASK		32
24 
25 #define ATH11K_PCI_IRQ_CE0_OFFSET	3
26 #define ATH11K_PCI_IRQ_DP_OFFSET	14
27 
28 #define WINDOW_ENABLE_BIT		0x40000000
29 #define WINDOW_REG_ADDRESS		0x310c
30 #define WINDOW_VALUE_MASK		GENMASK(24, 19)
31 #define WINDOW_START			0x80000
32 #define WINDOW_RANGE_MASK		GENMASK(18, 0)
33 
34 #define TCSR_SOC_HW_VERSION		0x0224
35 #define TCSR_SOC_HW_VERSION_MAJOR_MASK	GENMASK(11, 8)
36 #define TCSR_SOC_HW_VERSION_MINOR_MASK	GENMASK(7, 0)
37 
38 /* BAR0 + 4k is always accessible, and no
39  * need to force wakeup.
40  * 4K - 32 = 0xFE0
41  */
42 #define ACCESS_ALWAYS_OFF 0xFE0
43 
44 #define QCA6390_DEVICE_ID		0x1101
45 #define QCN9074_DEVICE_ID		0x1104
46 #define WCN6855_DEVICE_ID		0x1103
47 
48 static const struct pci_device_id ath11k_pci_id_table[] = {
49 	{ PCI_VDEVICE(QCOM, QCA6390_DEVICE_ID) },
50 	{ PCI_VDEVICE(QCOM, WCN6855_DEVICE_ID) },
51 	{ PCI_VDEVICE(QCOM, QCN9074_DEVICE_ID) },
52 	{0}
53 };
54 
55 MODULE_DEVICE_TABLE(pci, ath11k_pci_id_table);
56 
57 static const struct ath11k_bus_params ath11k_pci_bus_params = {
58 	.mhi_support = true,
59 	.m3_fw_support = true,
60 	.fixed_bdf_addr = false,
61 	.fixed_mem_region = false,
62 };
63 
64 static const struct ath11k_msi_config ath11k_msi_config[] = {
65 	{
66 		.total_vectors = 32,
67 		.total_users = 4,
68 		.users = (struct ath11k_msi_user[]) {
69 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
70 			{ .name = "CE", .num_vectors = 10, .base_vector = 3 },
71 			{ .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
72 			{ .name = "DP", .num_vectors = 18, .base_vector = 14 },
73 		},
74 	},
75 	{
76 		.total_vectors = 16,
77 		.total_users = 3,
78 		.users = (struct ath11k_msi_user[]) {
79 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
80 			{ .name = "CE", .num_vectors = 5, .base_vector = 3 },
81 			{ .name = "DP", .num_vectors = 8, .base_vector = 8 },
82 		},
83 	},
84 };
85 
86 static const struct ath11k_msi_config msi_config_one_msi = {
87 	.total_vectors = 1,
88 	.total_users = 4,
89 	.users = (struct ath11k_msi_user[]) {
90 		{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
91 		{ .name = "CE", .num_vectors = 1, .base_vector = 0 },
92 		{ .name = "WAKE", .num_vectors = 1, .base_vector = 0 },
93 		{ .name = "DP", .num_vectors = 1, .base_vector = 0 },
94 	},
95 };
96 
97 static const char *irq_name[ATH11K_IRQ_NUM_MAX] = {
98 	"bhi",
99 	"mhi-er0",
100 	"mhi-er1",
101 	"ce0",
102 	"ce1",
103 	"ce2",
104 	"ce3",
105 	"ce4",
106 	"ce5",
107 	"ce6",
108 	"ce7",
109 	"ce8",
110 	"ce9",
111 	"ce10",
112 	"ce11",
113 	"host2wbm-desc-feed",
114 	"host2reo-re-injection",
115 	"host2reo-command",
116 	"host2rxdma-monitor-ring3",
117 	"host2rxdma-monitor-ring2",
118 	"host2rxdma-monitor-ring1",
119 	"reo2ost-exception",
120 	"wbm2host-rx-release",
121 	"reo2host-status",
122 	"reo2host-destination-ring4",
123 	"reo2host-destination-ring3",
124 	"reo2host-destination-ring2",
125 	"reo2host-destination-ring1",
126 	"rxdma2host-monitor-destination-mac3",
127 	"rxdma2host-monitor-destination-mac2",
128 	"rxdma2host-monitor-destination-mac1",
129 	"ppdu-end-interrupts-mac3",
130 	"ppdu-end-interrupts-mac2",
131 	"ppdu-end-interrupts-mac1",
132 	"rxdma2host-monitor-status-ring-mac3",
133 	"rxdma2host-monitor-status-ring-mac2",
134 	"rxdma2host-monitor-status-ring-mac1",
135 	"host2rxdma-host-buf-ring-mac3",
136 	"host2rxdma-host-buf-ring-mac2",
137 	"host2rxdma-host-buf-ring-mac1",
138 	"rxdma2host-destination-ring-mac3",
139 	"rxdma2host-destination-ring-mac2",
140 	"rxdma2host-destination-ring-mac1",
141 	"host2tcl-input-ring4",
142 	"host2tcl-input-ring3",
143 	"host2tcl-input-ring2",
144 	"host2tcl-input-ring1",
145 	"wbm2host-tx-completions-ring3",
146 	"wbm2host-tx-completions-ring2",
147 	"wbm2host-tx-completions-ring1",
148 	"tcl2host-status-ring",
149 };
150 
151 static inline void ath11k_pci_select_window(struct ath11k_pci *ab_pci, u32 offset)
152 {
153 	struct ath11k_base *ab = ab_pci->ab;
154 
155 	u32 window = FIELD_GET(WINDOW_VALUE_MASK, offset);
156 
157 	lockdep_assert_held(&ab_pci->window_lock);
158 
159 	if (window != ab_pci->register_window) {
160 #if defined(__linux__)
161 		iowrite32(WINDOW_ENABLE_BIT | window,
162 			  ab->mem + WINDOW_REG_ADDRESS);
163 		ioread32(ab->mem + WINDOW_REG_ADDRESS);
164 #elif defined(__FreeBSD__)
165 		iowrite32(WINDOW_ENABLE_BIT | window,
166 			  (char *)ab->mem + WINDOW_REG_ADDRESS);
167 		ioread32((char *)ab->mem + WINDOW_REG_ADDRESS);
168 #endif
169 		ab_pci->register_window = window;
170 	}
171 }
172 
173 static inline void ath11k_pci_select_static_window(struct ath11k_pci *ab_pci)
174 {
175 	u32 umac_window = FIELD_GET(WINDOW_VALUE_MASK, HAL_SEQ_WCSS_UMAC_OFFSET);
176 	u32 ce_window = FIELD_GET(WINDOW_VALUE_MASK, HAL_CE_WFSS_CE_REG_BASE);
177 	u32 window;
178 
179 	window = (umac_window << 12) | (ce_window << 6);
180 
181 #if defined(__linux__)
182 	iowrite32(WINDOW_ENABLE_BIT | window, ab_pci->ab->mem + WINDOW_REG_ADDRESS);
183 #elif defined(__FreeBSD__)
184 	iowrite32(WINDOW_ENABLE_BIT | window, (char *)ab_pci->ab->mem + WINDOW_REG_ADDRESS);
185 #endif
186 }
187 
188 static inline u32 ath11k_pci_get_window_start(struct ath11k_base *ab,
189 					      u32 offset)
190 {
191 	u32 window_start;
192 
193 	/* If offset lies within DP register range, use 3rd window */
194 	if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < WINDOW_RANGE_MASK)
195 		window_start = 3 * WINDOW_START;
196 	/* If offset lies within CE register range, use 2nd window */
197 	else if ((offset ^ HAL_CE_WFSS_CE_REG_BASE) < WINDOW_RANGE_MASK)
198 		window_start = 2 * WINDOW_START;
199 	else
200 		window_start = WINDOW_START;
201 
202 	return window_start;
203 }
204 
205 void ath11k_pci_write32(struct ath11k_base *ab, u32 offset, u32 value)
206 {
207 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
208 	u32 window_start;
209 
210 	/* for offset beyond BAR + 4K - 32, may
211 	 * need to wakeup MHI to access.
212 	 */
213 	if (ab->hw_params.wakeup_mhi &&
214 	    test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
215 	    offset >= ACCESS_ALWAYS_OFF)
216 		mhi_device_get_sync(ab_pci->mhi_ctrl->mhi_dev);
217 
218 	if (offset < WINDOW_START) {
219 #if defined(__linux__)
220 		iowrite32(value, ab->mem  + offset);
221 #elif defined(__FreeBSD__)
222 		iowrite32(value, (char *)ab->mem  + offset);
223 #endif
224 	} else {
225 		if (ab->bus_params.static_window_map)
226 			window_start = ath11k_pci_get_window_start(ab, offset);
227 		else
228 			window_start = WINDOW_START;
229 
230 		if (window_start == WINDOW_START) {
231 			spin_lock_bh(&ab_pci->window_lock);
232 			ath11k_pci_select_window(ab_pci, offset);
233 #if defined(__linux__)
234 			iowrite32(value, ab->mem + window_start +
235 				  (offset & WINDOW_RANGE_MASK));
236 #elif defined(__FreeBSD__)
237 			iowrite32(value, (char *)ab->mem + window_start +
238 				  (offset & WINDOW_RANGE_MASK));
239 #endif
240 			spin_unlock_bh(&ab_pci->window_lock);
241 		} else {
242 #if defined(__linux__)
243 			iowrite32(value, ab->mem + window_start +
244 				  (offset & WINDOW_RANGE_MASK));
245 #elif defined(__FreeBSD__)
246 			iowrite32(value, (char *)ab->mem + window_start +
247 				  (offset & WINDOW_RANGE_MASK));
248 #endif
249 		}
250 	}
251 
252 	if (ab->hw_params.wakeup_mhi &&
253 	    test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
254 	    offset >= ACCESS_ALWAYS_OFF)
255 		mhi_device_put(ab_pci->mhi_ctrl->mhi_dev);
256 }
257 
258 u32 ath11k_pci_read32(struct ath11k_base *ab, u32 offset)
259 {
260 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
261 	u32 val, window_start;
262 
263 	/* for offset beyond BAR + 4K - 32, may
264 	 * need to wakeup MHI to access.
265 	 */
266 	if (ab->hw_params.wakeup_mhi &&
267 	    test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
268 	    offset >= ACCESS_ALWAYS_OFF)
269 		mhi_device_get_sync(ab_pci->mhi_ctrl->mhi_dev);
270 
271 	if (offset < WINDOW_START) {
272 #if defined(__linux__)
273 		val = ioread32(ab->mem + offset);
274 #elif defined(__FreeBSD__)
275 		val = ioread32((char *)ab->mem + offset);
276 #endif
277 	} else {
278 		if (ab->bus_params.static_window_map)
279 			window_start = ath11k_pci_get_window_start(ab, offset);
280 		else
281 			window_start = WINDOW_START;
282 
283 		if (window_start == WINDOW_START) {
284 			spin_lock_bh(&ab_pci->window_lock);
285 			ath11k_pci_select_window(ab_pci, offset);
286 #if defined(__linux__)
287 			val = ioread32(ab->mem + window_start +
288 				       (offset & WINDOW_RANGE_MASK));
289 #elif defined(__FreeBSD__)
290 			val = ioread32((char *)ab->mem + window_start +
291 				       (offset & WINDOW_RANGE_MASK));
292 #endif
293 			spin_unlock_bh(&ab_pci->window_lock);
294 		} else {
295 #if defined(__linux__)
296 			val = ioread32(ab->mem + window_start +
297 				       (offset & WINDOW_RANGE_MASK));
298 #elif defined(__FreeBSD__)
299 			val = ioread32((char *)ab->mem + window_start +
300 				       (offset & WINDOW_RANGE_MASK));
301 #endif
302 		}
303 	}
304 
305 	if (ab->hw_params.wakeup_mhi &&
306 	    test_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
307 	    offset >= ACCESS_ALWAYS_OFF)
308 		mhi_device_put(ab_pci->mhi_ctrl->mhi_dev);
309 
310 	return val;
311 }
312 
313 static void ath11k_pci_soc_global_reset(struct ath11k_base *ab)
314 {
315 	u32 val, delay;
316 
317 	val = ath11k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
318 
319 	val |= PCIE_SOC_GLOBAL_RESET_V;
320 
321 	ath11k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
322 
323 	/* TODO: exact time to sleep is uncertain */
324 	delay = 10;
325 	mdelay(delay);
326 
327 	/* Need to toggle V bit back otherwise stuck in reset status */
328 	val &= ~PCIE_SOC_GLOBAL_RESET_V;
329 
330 	ath11k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
331 
332 	mdelay(delay);
333 
334 	val = ath11k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
335 	if (val == 0xffffffff)
336 		ath11k_warn(ab, "link down error during global reset\n");
337 }
338 
339 static void ath11k_pci_clear_dbg_registers(struct ath11k_base *ab)
340 {
341 	u32 val;
342 
343 	/* read cookie */
344 	val = ath11k_pci_read32(ab, PCIE_Q6_COOKIE_ADDR);
345 	ath11k_dbg(ab, ATH11K_DBG_PCI, "cookie:0x%x\n", val);
346 
347 	val = ath11k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
348 	ath11k_dbg(ab, ATH11K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
349 
350 	/* TODO: exact time to sleep is uncertain */
351 	mdelay(10);
352 
353 	/* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
354 	 * continuing warm path and entering dead loop.
355 	 */
356 	ath11k_pci_write32(ab, WLAON_WARM_SW_ENTRY, 0);
357 	mdelay(10);
358 
359 	val = ath11k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
360 	ath11k_dbg(ab, ATH11K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
361 
362 	/* A read clear register. clear the register to prevent
363 	 * Q6 from entering wrong code path.
364 	 */
365 	val = ath11k_pci_read32(ab, WLAON_SOC_RESET_CAUSE_REG);
366 	ath11k_dbg(ab, ATH11K_DBG_PCI, "soc reset cause:%d\n", val);
367 }
368 
369 static int ath11k_pci_set_link_reg(struct ath11k_base *ab,
370 				   u32 offset, u32 value, u32 mask)
371 {
372 	u32 v;
373 	int i;
374 
375 	v = ath11k_pci_read32(ab, offset);
376 	if ((v & mask) == value)
377 		return 0;
378 
379 	for (i = 0; i < 10; i++) {
380 		ath11k_pci_write32(ab, offset, (v & ~mask) | value);
381 
382 		v = ath11k_pci_read32(ab, offset);
383 		if ((v & mask) == value)
384 			return 0;
385 
386 		mdelay(2);
387 	}
388 
389 	ath11k_warn(ab, "failed to set pcie link register 0x%08x: 0x%08x != 0x%08x\n",
390 		    offset, v & mask, value);
391 
392 	return -ETIMEDOUT;
393 }
394 
395 static int ath11k_pci_fix_l1ss(struct ath11k_base *ab)
396 {
397 	int ret;
398 
399 	ret = ath11k_pci_set_link_reg(ab,
400 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(ab),
401 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL,
402 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK);
403 	if (ret) {
404 		ath11k_warn(ab, "failed to set sysclk: %d\n", ret);
405 		return ret;
406 	}
407 
408 	ret = ath11k_pci_set_link_reg(ab,
409 				      PCIE_PCS_OSC_DTCT_CONFIG1_REG(ab),
410 				      PCIE_PCS_OSC_DTCT_CONFIG1_VAL,
411 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
412 	if (ret) {
413 		ath11k_warn(ab, "failed to set dtct config1 error: %d\n", ret);
414 		return ret;
415 	}
416 
417 	ret = ath11k_pci_set_link_reg(ab,
418 				      PCIE_PCS_OSC_DTCT_CONFIG2_REG(ab),
419 				      PCIE_PCS_OSC_DTCT_CONFIG2_VAL,
420 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
421 	if (ret) {
422 		ath11k_warn(ab, "failed to set dtct config2: %d\n", ret);
423 		return ret;
424 	}
425 
426 	ret = ath11k_pci_set_link_reg(ab,
427 				      PCIE_PCS_OSC_DTCT_CONFIG4_REG(ab),
428 				      PCIE_PCS_OSC_DTCT_CONFIG4_VAL,
429 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
430 	if (ret) {
431 		ath11k_warn(ab, "failed to set dtct config4: %d\n", ret);
432 		return ret;
433 	}
434 
435 	return 0;
436 }
437 
438 static void ath11k_pci_enable_ltssm(struct ath11k_base *ab)
439 {
440 	u32 val;
441 	int i;
442 
443 	val = ath11k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
444 
445 	/* PCIE link seems very unstable after the Hot Reset*/
446 	for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
447 		if (val == 0xffffffff)
448 			mdelay(5);
449 
450 		ath11k_pci_write32(ab, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
451 		val = ath11k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
452 	}
453 
454 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci ltssm 0x%x\n", val);
455 
456 	val = ath11k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST);
457 	val |= GCC_GCC_PCIE_HOT_RST_VAL;
458 	ath11k_pci_write32(ab, GCC_GCC_PCIE_HOT_RST, val);
459 	val = ath11k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST);
460 
461 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci pcie_hot_rst 0x%x\n", val);
462 
463 	mdelay(5);
464 }
465 
466 static void ath11k_pci_clear_all_intrs(struct ath11k_base *ab)
467 {
468 	/* This is a WAR for PCIE Hotreset.
469 	 * When target receive Hotreset, but will set the interrupt.
470 	 * So when download SBL again, SBL will open Interrupt and
471 	 * receive it, and crash immediately.
472 	 */
473 	ath11k_pci_write32(ab, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
474 }
475 
476 static void ath11k_pci_set_wlaon_pwr_ctrl(struct ath11k_base *ab)
477 {
478 	u32 val;
479 
480 	val = ath11k_pci_read32(ab, WLAON_QFPROM_PWR_CTRL_REG);
481 	val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
482 	ath11k_pci_write32(ab, WLAON_QFPROM_PWR_CTRL_REG, val);
483 }
484 
485 static void ath11k_pci_force_wake(struct ath11k_base *ab)
486 {
487 	ath11k_pci_write32(ab, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
488 	mdelay(5);
489 }
490 
491 static void ath11k_pci_sw_reset(struct ath11k_base *ab, bool power_on)
492 {
493 	mdelay(100);
494 
495 	if (power_on) {
496 		ath11k_pci_enable_ltssm(ab);
497 		ath11k_pci_clear_all_intrs(ab);
498 		ath11k_pci_set_wlaon_pwr_ctrl(ab);
499 		if (ab->hw_params.fix_l1ss)
500 			ath11k_pci_fix_l1ss(ab);
501 	}
502 
503 	ath11k_mhi_clear_vector(ab);
504 	ath11k_pci_clear_dbg_registers(ab);
505 	ath11k_pci_soc_global_reset(ab);
506 	ath11k_mhi_set_mhictrl_reset(ab);
507 }
508 
509 int ath11k_pci_get_msi_irq(struct device *dev, unsigned int vector)
510 {
511 	struct pci_dev *pci_dev = to_pci_dev(dev);
512 
513 	return pci_irq_vector(pci_dev, vector);
514 }
515 
516 static void ath11k_pci_get_msi_address(struct ath11k_base *ab, u32 *msi_addr_lo,
517 				       u32 *msi_addr_hi)
518 {
519 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
520 	struct pci_dev *pci_dev = to_pci_dev(ab->dev);
521 
522 	pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
523 			      msi_addr_lo);
524 
525 	if (test_bit(ATH11K_PCI_FLAG_IS_MSI_64, &ab_pci->flags)) {
526 		pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
527 				      msi_addr_hi);
528 	} else {
529 		*msi_addr_hi = 0;
530 	}
531 }
532 
533 int ath11k_pci_get_user_msi_assignment(struct ath11k_pci *ab_pci, char *user_name,
534 				       int *num_vectors, u32 *user_base_data,
535 				       u32 *base_vector)
536 {
537 	struct ath11k_base *ab = ab_pci->ab;
538 	const struct ath11k_msi_config *msi_config = ab_pci->msi_config;
539 	int idx;
540 
541 	for (idx = 0; idx < msi_config->total_users; idx++) {
542 		if (strcmp(user_name, msi_config->users[idx].name) == 0) {
543 			*num_vectors = msi_config->users[idx].num_vectors;
544 			*base_vector =  msi_config->users[idx].base_vector;
545 			*user_base_data = *base_vector + ab_pci->msi_ep_base_data;
546 
547 			ath11k_dbg(ab, ATH11K_DBG_PCI,
548 				   "Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
549 				   user_name, *num_vectors, *user_base_data,
550 				   *base_vector);
551 
552 			return 0;
553 		}
554 	}
555 
556 	ath11k_err(ab, "Failed to find MSI assignment for %s!\n", user_name);
557 
558 	return -EINVAL;
559 }
560 
561 static void ath11k_pci_get_ce_msi_idx(struct ath11k_base *ab, u32 ce_id,
562 				      u32 *msi_idx)
563 {
564 	u32 i, msi_data_idx;
565 
566 	for (i = 0, msi_data_idx = 0; i < ab->hw_params.ce_count; i++) {
567 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
568 			continue;
569 
570 		if (ce_id == i)
571 			break;
572 
573 		msi_data_idx++;
574 	}
575 	*msi_idx = msi_data_idx;
576 }
577 
578 static int ath11k_get_user_msi_assignment(struct ath11k_base *ab, char *user_name,
579 					  int *num_vectors, u32 *user_base_data,
580 					  u32 *base_vector)
581 {
582 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
583 
584 	return ath11k_pci_get_user_msi_assignment(ab_pci, user_name,
585 						  num_vectors, user_base_data,
586 						  base_vector);
587 }
588 
589 static void ath11k_pci_free_ext_irq(struct ath11k_base *ab)
590 {
591 	int i, j;
592 
593 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
594 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
595 
596 		for (j = 0; j < irq_grp->num_irq; j++)
597 			free_irq(ab->irq_num[irq_grp->irqs[j]], irq_grp);
598 
599 		netif_napi_del(&irq_grp->napi);
600 	}
601 }
602 
603 static void ath11k_pci_free_irq(struct ath11k_base *ab)
604 {
605 	int i, irq_idx;
606 
607 	for (i = 0; i < ab->hw_params.ce_count; i++) {
608 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
609 			continue;
610 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
611 		free_irq(ab->irq_num[irq_idx], &ab->ce.ce_pipe[i]);
612 	}
613 
614 	ath11k_pci_free_ext_irq(ab);
615 }
616 
617 static void ath11k_pci_ce_irq_enable(struct ath11k_base *ab, u16 ce_id)
618 {
619 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
620 	u32 irq_idx;
621 
622 	/* In case of one MSI vector, we handle irq enable/disable in a
623 	 * uniform way since we only have one irq
624 	 */
625 	if (!test_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
626 		return;
627 
628 	irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_id;
629 	enable_irq(ab->irq_num[irq_idx]);
630 }
631 
632 static void ath11k_pci_ce_irq_disable(struct ath11k_base *ab, u16 ce_id)
633 {
634 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
635 	u32 irq_idx;
636 
637 	/* In case of one MSI vector, we handle irq enable/disable in a
638 	 * uniform way since we only have one irq
639 	 */
640 	if (!test_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
641 		return;
642 
643 	irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_id;
644 	disable_irq_nosync(ab->irq_num[irq_idx]);
645 }
646 
647 static void ath11k_pci_ce_irqs_disable(struct ath11k_base *ab)
648 {
649 	int i;
650 
651 	clear_bit(ATH11K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags);
652 
653 	for (i = 0; i < ab->hw_params.ce_count; i++) {
654 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
655 			continue;
656 		ath11k_pci_ce_irq_disable(ab, i);
657 	}
658 }
659 
660 static void ath11k_pci_sync_ce_irqs(struct ath11k_base *ab)
661 {
662 	int i;
663 	int irq_idx;
664 
665 	for (i = 0; i < ab->hw_params.ce_count; i++) {
666 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
667 			continue;
668 
669 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
670 		synchronize_irq(ab->irq_num[irq_idx]);
671 	}
672 }
673 
674 static void ath11k_pci_ce_tasklet(struct tasklet_struct *t)
675 {
676 	struct ath11k_ce_pipe *ce_pipe = from_tasklet(ce_pipe, t, intr_tq);
677 	int irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
678 
679 	ath11k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);
680 
681 	enable_irq(ce_pipe->ab->irq_num[irq_idx]);
682 }
683 
684 static irqreturn_t ath11k_pci_ce_interrupt_handler(int irq, void *arg)
685 {
686 	struct ath11k_ce_pipe *ce_pipe = arg;
687 	struct ath11k_base *ab = ce_pipe->ab;
688 	int irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
689 
690 	if (!test_bit(ATH11K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags))
691 		return IRQ_HANDLED;
692 
693 	/* last interrupt received for this CE */
694 	ce_pipe->timestamp = jiffies;
695 
696 	disable_irq_nosync(ab->irq_num[irq_idx]);
697 
698 	tasklet_schedule(&ce_pipe->intr_tq);
699 
700 	return IRQ_HANDLED;
701 }
702 
703 static void ath11k_pci_ext_grp_disable(struct ath11k_ext_irq_grp *irq_grp)
704 {
705 	struct ath11k_pci *ab_pci = ath11k_pci_priv(irq_grp->ab);
706 	int i;
707 
708 	/* In case of one MSI vector, we handle irq enable/disable
709 	 * in a uniform way since we only have one irq
710 	 */
711 	if (!test_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
712 		return;
713 
714 	for (i = 0; i < irq_grp->num_irq; i++)
715 		disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
716 }
717 
718 static void __ath11k_pci_ext_irq_disable(struct ath11k_base *sc)
719 {
720 	int i;
721 
722 	clear_bit(ATH11K_FLAG_EXT_IRQ_ENABLED, &sc->dev_flags);
723 
724 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
725 		struct ath11k_ext_irq_grp *irq_grp = &sc->ext_irq_grp[i];
726 
727 		ath11k_pci_ext_grp_disable(irq_grp);
728 
729 		if (irq_grp->napi_enabled) {
730 			napi_synchronize(&irq_grp->napi);
731 			napi_disable(&irq_grp->napi);
732 			irq_grp->napi_enabled = false;
733 		}
734 	}
735 }
736 
737 static void ath11k_pci_ext_grp_enable(struct ath11k_ext_irq_grp *irq_grp)
738 {
739 	struct ath11k_pci *ab_pci = ath11k_pci_priv(irq_grp->ab);
740 	int i;
741 
742 	/* In case of one MSI vector, we handle irq enable/disable in a
743 	 * uniform way since we only have one irq
744 	 */
745 	if (!test_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
746 		return;
747 
748 	for (i = 0; i < irq_grp->num_irq; i++)
749 		enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
750 }
751 
752 static void ath11k_pci_ext_irq_enable(struct ath11k_base *ab)
753 {
754 	int i;
755 
756 	set_bit(ATH11K_FLAG_EXT_IRQ_ENABLED, &ab->dev_flags);
757 
758 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
759 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
760 
761 		if (!irq_grp->napi_enabled) {
762 			napi_enable(&irq_grp->napi);
763 			irq_grp->napi_enabled = true;
764 		}
765 		ath11k_pci_ext_grp_enable(irq_grp);
766 	}
767 }
768 
769 static void ath11k_pci_sync_ext_irqs(struct ath11k_base *ab)
770 {
771 	int i, j, irq_idx;
772 
773 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
774 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
775 
776 		for (j = 0; j < irq_grp->num_irq; j++) {
777 			irq_idx = irq_grp->irqs[j];
778 			synchronize_irq(ab->irq_num[irq_idx]);
779 		}
780 	}
781 }
782 
783 static void ath11k_pci_ext_irq_disable(struct ath11k_base *ab)
784 {
785 	__ath11k_pci_ext_irq_disable(ab);
786 	ath11k_pci_sync_ext_irqs(ab);
787 }
788 
789 static int ath11k_pci_ext_grp_napi_poll(struct napi_struct *napi, int budget)
790 {
791 	struct ath11k_ext_irq_grp *irq_grp = container_of(napi,
792 						struct ath11k_ext_irq_grp,
793 						napi);
794 	struct ath11k_base *ab = irq_grp->ab;
795 	int work_done;
796 	int i;
797 
798 	work_done = ath11k_dp_service_srng(ab, irq_grp, budget);
799 	if (work_done < budget) {
800 		napi_complete_done(napi, work_done);
801 		for (i = 0; i < irq_grp->num_irq; i++)
802 			enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
803 	}
804 
805 	if (work_done > budget)
806 		work_done = budget;
807 
808 	return work_done;
809 }
810 
811 static irqreturn_t ath11k_pci_ext_interrupt_handler(int irq, void *arg)
812 {
813 	struct ath11k_ext_irq_grp *irq_grp = arg;
814 	struct ath11k_base *ab = irq_grp->ab;
815 	int i;
816 
817 	if (!test_bit(ATH11K_FLAG_EXT_IRQ_ENABLED, &ab->dev_flags))
818 		return IRQ_HANDLED;
819 
820 	ath11k_dbg(irq_grp->ab, ATH11K_DBG_PCI, "ext irq:%d\n", irq);
821 
822 	/* last interrupt received for this group */
823 	irq_grp->timestamp = jiffies;
824 
825 	for (i = 0; i < irq_grp->num_irq; i++)
826 		disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
827 
828 	napi_schedule(&irq_grp->napi);
829 
830 	return IRQ_HANDLED;
831 }
832 
833 static int ath11k_pci_ext_irq_config(struct ath11k_base *ab)
834 {
835 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
836 	int i, j, ret, num_vectors = 0;
837 	u32 user_base_data = 0, base_vector = 0;
838 
839 	ret = ath11k_pci_get_user_msi_assignment(ath11k_pci_priv(ab), "DP",
840 						 &num_vectors,
841 						 &user_base_data,
842 						 &base_vector);
843 	if (ret < 0)
844 		return ret;
845 
846 	for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {
847 		struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
848 		u32 num_irq = 0;
849 
850 		irq_grp->ab = ab;
851 		irq_grp->grp_id = i;
852 		init_dummy_netdev(&irq_grp->napi_ndev);
853 		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
854 			       ath11k_pci_ext_grp_napi_poll);
855 
856 		if (ab->hw_params.ring_mask->tx[i] ||
857 		    ab->hw_params.ring_mask->rx[i] ||
858 		    ab->hw_params.ring_mask->rx_err[i] ||
859 		    ab->hw_params.ring_mask->rx_wbm_rel[i] ||
860 		    ab->hw_params.ring_mask->reo_status[i] ||
861 		    ab->hw_params.ring_mask->rxdma2host[i] ||
862 		    ab->hw_params.ring_mask->host2rxdma[i] ||
863 		    ab->hw_params.ring_mask->rx_mon_status[i]) {
864 			num_irq = 1;
865 		}
866 
867 		irq_grp->num_irq = num_irq;
868 		irq_grp->irqs[0] = ATH11K_PCI_IRQ_DP_OFFSET + i;
869 
870 		for (j = 0; j < irq_grp->num_irq; j++) {
871 			int irq_idx = irq_grp->irqs[j];
872 			int vector = (i % num_vectors) + base_vector;
873 			int irq = ath11k_pci_get_msi_irq(ab->dev, vector);
874 
875 			ab->irq_num[irq_idx] = irq;
876 
877 			ath11k_dbg(ab, ATH11K_DBG_PCI,
878 				   "irq:%d group:%d\n", irq, i);
879 
880 			irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
881 			ret = request_irq(irq, ath11k_pci_ext_interrupt_handler,
882 					  ab_pci->irq_flags,
883 					  "DP_EXT_IRQ", irq_grp);
884 			if (ret) {
885 				ath11k_err(ab, "failed request irq %d: %d\n",
886 					   vector, ret);
887 				return ret;
888 			}
889 		}
890 		ath11k_pci_ext_grp_disable(irq_grp);
891 	}
892 
893 	return 0;
894 }
895 
896 static int ath11k_pci_set_irq_affinity_hint(struct ath11k_pci *ab_pci,
897 #if defined(__linux__)
898 					    const struct cpumask *m)
899 #elif defined(__FreeBSD__)
900 					    const cpumask_t *m)
901 #endif
902 {
903 	if (test_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
904 		return 0;
905 
906 	return irq_set_affinity_hint(ab_pci->pdev->irq, m);
907 }
908 
909 static int ath11k_pci_config_irq(struct ath11k_base *ab)
910 {
911 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
912 	struct ath11k_ce_pipe *ce_pipe;
913 	u32 msi_data_start;
914 	u32 msi_data_count, msi_data_idx;
915 	u32 msi_irq_start;
916 	unsigned int msi_data;
917 	int irq, i, ret, irq_idx;
918 
919 	ret = ath11k_pci_get_user_msi_assignment(ath11k_pci_priv(ab),
920 						 "CE", &msi_data_count,
921 						 &msi_data_start, &msi_irq_start);
922 	if (ret)
923 		return ret;
924 
925 #if defined(__linux__)
926 	ret = ath11k_pci_set_irq_affinity_hint(ab_pci, cpumask_of(0));
927 	if (ret) {
928 		ath11k_err(ab, "failed to set irq affinity %d\n", ret);
929 		return ret;
930 	}
931 #elif defined(__FreeBSD__)
932 	/* We cannot find the interrupt until request_irq() was called. */
933 #endif
934 
935 	/* Configure CE irqs */
936 	for (i = 0, msi_data_idx = 0; i < ab->hw_params.ce_count; i++) {
937 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
938 			continue;
939 
940 		msi_data = (msi_data_idx % msi_data_count) + msi_irq_start;
941 		irq = ath11k_pci_get_msi_irq(ab->dev, msi_data);
942 		ce_pipe = &ab->ce.ce_pipe[i];
943 
944 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
945 
946 		tasklet_setup(&ce_pipe->intr_tq, ath11k_pci_ce_tasklet);
947 
948 		ret = request_irq(irq, ath11k_pci_ce_interrupt_handler,
949 				  ab_pci->irq_flags, irq_name[irq_idx],
950 				  ce_pipe);
951 		if (ret) {
952 #if defined(__linux__)
953 			ath11k_err(ab, "failed to request irq %d: %d\n",
954 				   irq_idx, ret);
955 #elif defined(__FreeBSD__)
956 			ath11k_err(ab, "failed to request irq %d irq_idx %d (%s): %d\n",
957 				   irq, irq_idx, irq_name[irq_idx], ret);
958 #endif
959 			goto err_irq_affinity_cleanup;
960 		}
961 
962 		ab->irq_num[irq_idx] = irq;
963 		msi_data_idx++;
964 
965 		ath11k_pci_ce_irq_disable(ab, i);
966 	}
967 
968 	ret = ath11k_pci_ext_irq_config(ab);
969 	if (ret)
970 		goto err_irq_affinity_cleanup;
971 
972 	return 0;
973 
974 err_irq_affinity_cleanup:
975 	ath11k_pci_set_irq_affinity_hint(ab_pci, NULL);
976 	return ret;
977 }
978 
979 static void ath11k_pci_init_qmi_ce_config(struct ath11k_base *ab)
980 {
981 	struct ath11k_qmi_ce_cfg *cfg = &ab->qmi.ce_cfg;
982 
983 	cfg->tgt_ce = ab->hw_params.target_ce_config;
984 	cfg->tgt_ce_len = ab->hw_params.target_ce_count;
985 
986 	cfg->svc_to_ce_map = ab->hw_params.svc_to_ce_map;
987 	cfg->svc_to_ce_map_len = ab->hw_params.svc_to_ce_map_len;
988 	ab->qmi.service_ins_id = ab->hw_params.qmi_service_ins_id;
989 
990 	ath11k_ce_get_shadow_config(ab, &cfg->shadow_reg_v2,
991 				    &cfg->shadow_reg_v2_len);
992 }
993 
994 static void ath11k_pci_ce_irqs_enable(struct ath11k_base *ab)
995 {
996 	int i;
997 
998 	set_bit(ATH11K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags);
999 
1000 	for (i = 0; i < ab->hw_params.ce_count; i++) {
1001 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
1002 			continue;
1003 		ath11k_pci_ce_irq_enable(ab, i);
1004 	}
1005 }
1006 
1007 static void ath11k_pci_msi_config(struct ath11k_pci *ab_pci, bool enable)
1008 {
1009 	struct pci_dev *dev = ab_pci->pdev;
1010 	u16 control;
1011 
1012 	pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
1013 
1014 	if (enable)
1015 		control |= PCI_MSI_FLAGS_ENABLE;
1016 	else
1017 		control &= ~PCI_MSI_FLAGS_ENABLE;
1018 
1019 	pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
1020 }
1021 
1022 static void ath11k_pci_msi_enable(struct ath11k_pci *ab_pci)
1023 {
1024 	ath11k_pci_msi_config(ab_pci, true);
1025 }
1026 
1027 static void ath11k_pci_msi_disable(struct ath11k_pci *ab_pci)
1028 {
1029 	ath11k_pci_msi_config(ab_pci, false);
1030 }
1031 
1032 static int ath11k_pci_alloc_msi(struct ath11k_pci *ab_pci)
1033 {
1034 	struct ath11k_base *ab = ab_pci->ab;
1035 	const struct ath11k_msi_config *msi_config = ab_pci->msi_config;
1036 	struct msi_desc *msi_desc;
1037 	int num_vectors;
1038 	int ret;
1039 
1040 	num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
1041 					    msi_config->total_vectors,
1042 					    msi_config->total_vectors,
1043 					    PCI_IRQ_MSI);
1044 	if (num_vectors == msi_config->total_vectors) {
1045 		set_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags);
1046 		ab_pci->irq_flags = IRQF_SHARED;
1047 	} else {
1048 		num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
1049 						    1,
1050 						    1,
1051 						    PCI_IRQ_MSI);
1052 		if (num_vectors < 0) {
1053 			ret = -EINVAL;
1054 			goto reset_msi_config;
1055 		}
1056 		clear_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags);
1057 		ab_pci->msi_config = &msi_config_one_msi;
1058 		ab_pci->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
1059 		ath11k_dbg(ab, ATH11K_DBG_PCI, "request MSI one vector\n");
1060 	}
1061 	ath11k_info(ab, "MSI vectors: %d\n", num_vectors);
1062 
1063 	ath11k_pci_msi_disable(ab_pci);
1064 
1065 	msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
1066 	if (!msi_desc) {
1067 		ath11k_err(ab, "msi_desc is NULL!\n");
1068 		ret = -EINVAL;
1069 		goto free_msi_vector;
1070 	}
1071 
1072 	ab_pci->msi_ep_base_data = msi_desc->msg.data;
1073 	if (msi_desc->msi_attrib.is_64)
1074 		set_bit(ATH11K_PCI_FLAG_IS_MSI_64, &ab_pci->flags);
1075 
1076 	ath11k_dbg(ab, ATH11K_DBG_PCI, "msi base data is %d\n", ab_pci->msi_ep_base_data);
1077 
1078 	return 0;
1079 
1080 free_msi_vector:
1081 	pci_free_irq_vectors(ab_pci->pdev);
1082 
1083 reset_msi_config:
1084 	return ret;
1085 }
1086 
1087 static void ath11k_pci_free_msi(struct ath11k_pci *ab_pci)
1088 {
1089 	pci_free_irq_vectors(ab_pci->pdev);
1090 }
1091 
1092 static int ath11k_pci_config_msi_data(struct ath11k_pci *ab_pci)
1093 {
1094 	struct msi_desc *msi_desc;
1095 
1096 	msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
1097 	if (!msi_desc) {
1098 		ath11k_err(ab_pci->ab, "msi_desc is NULL!\n");
1099 		pci_free_irq_vectors(ab_pci->pdev);
1100 		return -EINVAL;
1101 	}
1102 
1103 	ab_pci->msi_ep_base_data = msi_desc->msg.data;
1104 
1105 	ath11k_dbg(ab_pci->ab, ATH11K_DBG_PCI, "pci after request_irq msi_ep_base_data %d\n",
1106 		   ab_pci->msi_ep_base_data);
1107 
1108 	return 0;
1109 }
1110 
1111 static int ath11k_pci_claim(struct ath11k_pci *ab_pci, struct pci_dev *pdev)
1112 {
1113 	struct ath11k_base *ab = ab_pci->ab;
1114 	u16 device_id;
1115 	int ret = 0;
1116 
1117 	pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
1118 	if (device_id != ab_pci->dev_id)  {
1119 		ath11k_err(ab, "pci device id mismatch: 0x%x 0x%x\n",
1120 			   device_id, ab_pci->dev_id);
1121 		ret = -EIO;
1122 		goto out;
1123 	}
1124 
1125 	ret = pci_assign_resource(pdev, ATH11K_PCI_BAR_NUM);
1126 	if (ret) {
1127 		ath11k_err(ab, "failed to assign pci resource: %d\n", ret);
1128 		goto out;
1129 	}
1130 
1131 	ret = pci_enable_device(pdev);
1132 	if (ret) {
1133 		ath11k_err(ab, "failed to enable pci device: %d\n", ret);
1134 		goto out;
1135 	}
1136 
1137 	ret = pci_request_region(pdev, ATH11K_PCI_BAR_NUM, "ath11k_pci");
1138 	if (ret) {
1139 		ath11k_err(ab, "failed to request pci region: %d\n", ret);
1140 		goto disable_device;
1141 	}
1142 
1143 	ret = dma_set_mask_and_coherent(&pdev->dev,
1144 					DMA_BIT_MASK(ATH11K_PCI_DMA_MASK));
1145 	if (ret) {
1146 		ath11k_err(ab, "failed to set pci dma mask to %d: %d\n",
1147 			   ATH11K_PCI_DMA_MASK, ret);
1148 		goto release_region;
1149 	}
1150 
1151 	pci_set_master(pdev);
1152 
1153 	ab->mem_len = pci_resource_len(pdev, ATH11K_PCI_BAR_NUM);
1154 	ab->mem = pci_iomap(pdev, ATH11K_PCI_BAR_NUM, 0);
1155 	if (!ab->mem) {
1156 		ath11k_err(ab, "failed to map pci bar %d\n", ATH11K_PCI_BAR_NUM);
1157 		ret = -EIO;
1158 		goto clear_master;
1159 	}
1160 
1161 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot pci_mem 0x%pK\n", ab->mem);
1162 	return 0;
1163 
1164 clear_master:
1165 	pci_clear_master(pdev);
1166 release_region:
1167 	pci_release_region(pdev, ATH11K_PCI_BAR_NUM);
1168 disable_device:
1169 	pci_disable_device(pdev);
1170 out:
1171 	return ret;
1172 }
1173 
1174 static void ath11k_pci_free_region(struct ath11k_pci *ab_pci)
1175 {
1176 	struct ath11k_base *ab = ab_pci->ab;
1177 	struct pci_dev *pci_dev = ab_pci->pdev;
1178 
1179 	pci_iounmap(pci_dev, ab->mem);
1180 	ab->mem = NULL;
1181 	pci_clear_master(pci_dev);
1182 	pci_release_region(pci_dev, ATH11K_PCI_BAR_NUM);
1183 	if (pci_is_enabled(pci_dev))
1184 		pci_disable_device(pci_dev);
1185 }
1186 
1187 static void ath11k_pci_aspm_disable(struct ath11k_pci *ab_pci)
1188 {
1189 	struct ath11k_base *ab = ab_pci->ab;
1190 
1191 	pcie_capability_read_word(ab_pci->pdev, PCI_EXP_LNKCTL,
1192 				  &ab_pci->link_ctl);
1193 
1194 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci link_ctl 0x%04x L0s %d L1 %d\n",
1195 		   ab_pci->link_ctl,
1196 		   u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L0S),
1197 		   u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L1));
1198 
1199 	/* disable L0s and L1 */
1200 	pcie_capability_write_word(ab_pci->pdev, PCI_EXP_LNKCTL,
1201 				   ab_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
1202 
1203 	set_bit(ATH11K_PCI_ASPM_RESTORE, &ab_pci->flags);
1204 }
1205 
1206 static void ath11k_pci_aspm_restore(struct ath11k_pci *ab_pci)
1207 {
1208 	if (test_and_clear_bit(ATH11K_PCI_ASPM_RESTORE, &ab_pci->flags))
1209 		pcie_capability_write_word(ab_pci->pdev, PCI_EXP_LNKCTL,
1210 					   ab_pci->link_ctl);
1211 }
1212 
1213 static int ath11k_pci_power_up(struct ath11k_base *ab)
1214 {
1215 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
1216 	int ret;
1217 
1218 	ab_pci->register_window = 0;
1219 	clear_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1220 	ath11k_pci_sw_reset(ab_pci->ab, true);
1221 
1222 	/* Disable ASPM during firmware download due to problems switching
1223 	 * to AMSS state.
1224 	 */
1225 	ath11k_pci_aspm_disable(ab_pci);
1226 
1227 	ath11k_pci_msi_enable(ab_pci);
1228 
1229 	ret = ath11k_mhi_start(ab_pci);
1230 	if (ret) {
1231 		ath11k_err(ab, "failed to start mhi: %d\n", ret);
1232 		return ret;
1233 	}
1234 
1235 	if (ab->bus_params.static_window_map)
1236 		ath11k_pci_select_static_window(ab_pci);
1237 
1238 	return 0;
1239 }
1240 
1241 static void ath11k_pci_power_down(struct ath11k_base *ab)
1242 {
1243 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
1244 
1245 	/* restore aspm in case firmware bootup fails */
1246 	ath11k_pci_aspm_restore(ab_pci);
1247 
1248 	ath11k_pci_force_wake(ab_pci->ab);
1249 
1250 	ath11k_pci_msi_disable(ab_pci);
1251 
1252 	ath11k_mhi_stop(ab_pci);
1253 	clear_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1254 	ath11k_pci_sw_reset(ab_pci->ab, false);
1255 }
1256 
1257 static int ath11k_pci_hif_suspend(struct ath11k_base *ab)
1258 {
1259 	struct ath11k_pci *ar_pci = ath11k_pci_priv(ab);
1260 
1261 	ath11k_mhi_suspend(ar_pci);
1262 
1263 	return 0;
1264 }
1265 
1266 static int ath11k_pci_hif_resume(struct ath11k_base *ab)
1267 {
1268 	struct ath11k_pci *ar_pci = ath11k_pci_priv(ab);
1269 
1270 	ath11k_mhi_resume(ar_pci);
1271 
1272 	return 0;
1273 }
1274 
1275 static void ath11k_pci_kill_tasklets(struct ath11k_base *ab)
1276 {
1277 	int i;
1278 
1279 	for (i = 0; i < ab->hw_params.ce_count; i++) {
1280 		struct ath11k_ce_pipe *ce_pipe = &ab->ce.ce_pipe[i];
1281 
1282 		if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
1283 			continue;
1284 
1285 		tasklet_kill(&ce_pipe->intr_tq);
1286 	}
1287 }
1288 
1289 static void ath11k_pci_ce_irq_disable_sync(struct ath11k_base *ab)
1290 {
1291 	ath11k_pci_ce_irqs_disable(ab);
1292 	ath11k_pci_sync_ce_irqs(ab);
1293 	ath11k_pci_kill_tasklets(ab);
1294 }
1295 
1296 static void ath11k_pci_stop(struct ath11k_base *ab)
1297 {
1298 	ath11k_pci_ce_irq_disable_sync(ab);
1299 	ath11k_ce_cleanup_pipes(ab);
1300 }
1301 
1302 static int ath11k_pci_start(struct ath11k_base *ab)
1303 {
1304 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
1305 
1306 	set_bit(ATH11K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1307 
1308 	/* TODO: for now don't restore ASPM in case of single MSI
1309 	 * vector as MHI register reading in M2 causes system hang.
1310 	 */
1311 	if (test_bit(ATH11K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
1312 		ath11k_pci_aspm_restore(ab_pci);
1313 	else
1314 		ath11k_info(ab, "leaving PCI ASPM disabled to avoid MHI M2 problems\n");
1315 
1316 	ath11k_pci_ce_irqs_enable(ab);
1317 	ath11k_ce_rx_post_buf(ab);
1318 
1319 	return 0;
1320 }
1321 
1322 static void ath11k_pci_hif_ce_irq_enable(struct ath11k_base *ab)
1323 {
1324 	ath11k_pci_ce_irqs_enable(ab);
1325 }
1326 
1327 static void ath11k_pci_hif_ce_irq_disable(struct ath11k_base *ab)
1328 {
1329 	ath11k_pci_ce_irq_disable_sync(ab);
1330 }
1331 
1332 static int ath11k_pci_map_service_to_pipe(struct ath11k_base *ab, u16 service_id,
1333 					  u8 *ul_pipe, u8 *dl_pipe)
1334 {
1335 	const struct service_to_pipe *entry;
1336 	bool ul_set = false, dl_set = false;
1337 	int i;
1338 
1339 	for (i = 0; i < ab->hw_params.svc_to_ce_map_len; i++) {
1340 		entry = &ab->hw_params.svc_to_ce_map[i];
1341 
1342 		if (__le32_to_cpu(entry->service_id) != service_id)
1343 			continue;
1344 
1345 		switch (__le32_to_cpu(entry->pipedir)) {
1346 		case PIPEDIR_NONE:
1347 			break;
1348 		case PIPEDIR_IN:
1349 			WARN_ON(dl_set);
1350 			*dl_pipe = __le32_to_cpu(entry->pipenum);
1351 			dl_set = true;
1352 			break;
1353 		case PIPEDIR_OUT:
1354 			WARN_ON(ul_set);
1355 			*ul_pipe = __le32_to_cpu(entry->pipenum);
1356 			ul_set = true;
1357 			break;
1358 		case PIPEDIR_INOUT:
1359 			WARN_ON(dl_set);
1360 			WARN_ON(ul_set);
1361 			*dl_pipe = __le32_to_cpu(entry->pipenum);
1362 			*ul_pipe = __le32_to_cpu(entry->pipenum);
1363 			dl_set = true;
1364 			ul_set = true;
1365 			break;
1366 		}
1367 	}
1368 
1369 	if (WARN_ON(!ul_set || !dl_set))
1370 		return -ENOENT;
1371 
1372 	return 0;
1373 }
1374 
1375 static const struct ath11k_hif_ops ath11k_pci_hif_ops = {
1376 	.start = ath11k_pci_start,
1377 	.stop = ath11k_pci_stop,
1378 	.read32 = ath11k_pci_read32,
1379 	.write32 = ath11k_pci_write32,
1380 	.power_down = ath11k_pci_power_down,
1381 	.power_up = ath11k_pci_power_up,
1382 	.suspend = ath11k_pci_hif_suspend,
1383 	.resume = ath11k_pci_hif_resume,
1384 	.irq_enable = ath11k_pci_ext_irq_enable,
1385 	.irq_disable = ath11k_pci_ext_irq_disable,
1386 	.get_msi_address =  ath11k_pci_get_msi_address,
1387 	.get_user_msi_vector = ath11k_get_user_msi_assignment,
1388 	.map_service_to_pipe = ath11k_pci_map_service_to_pipe,
1389 	.ce_irq_enable = ath11k_pci_hif_ce_irq_enable,
1390 	.ce_irq_disable = ath11k_pci_hif_ce_irq_disable,
1391 	.get_ce_msi_idx = ath11k_pci_get_ce_msi_idx,
1392 };
1393 
1394 static void ath11k_pci_read_hw_version(struct ath11k_base *ab, u32 *major, u32 *minor)
1395 {
1396 	u32 soc_hw_version;
1397 
1398 	soc_hw_version = ath11k_pci_read32(ab, TCSR_SOC_HW_VERSION);
1399 	*major = FIELD_GET(TCSR_SOC_HW_VERSION_MAJOR_MASK,
1400 			   soc_hw_version);
1401 	*minor = FIELD_GET(TCSR_SOC_HW_VERSION_MINOR_MASK,
1402 			   soc_hw_version);
1403 
1404 	ath11k_dbg(ab, ATH11K_DBG_PCI, "pci tcsr_soc_hw_version major %d minor %d\n",
1405 		   *major, *minor);
1406 }
1407 
1408 static int ath11k_pci_probe(struct pci_dev *pdev,
1409 			    const struct pci_device_id *pci_dev)
1410 {
1411 	struct ath11k_base *ab;
1412 	struct ath11k_pci *ab_pci;
1413 #if defined(__linux__)
1414 	u32 soc_hw_version_major, soc_hw_version_minor, addr;
1415 #elif defined(__FreeBSD__)
1416 	u32 soc_hw_version_major, soc_hw_version_minor;
1417 #endif
1418 	int ret;
1419 
1420 	ab = ath11k_core_alloc(&pdev->dev, sizeof(*ab_pci), ATH11K_BUS_PCI,
1421 			       &ath11k_pci_bus_params);
1422 	if (!ab) {
1423 		dev_err(&pdev->dev, "failed to allocate ath11k base\n");
1424 		return -ENOMEM;
1425 	}
1426 
1427 	ab->dev = &pdev->dev;
1428 	pci_set_drvdata(pdev, ab);
1429 	ab_pci = ath11k_pci_priv(ab);
1430 	ab_pci->dev_id = pci_dev->device;
1431 	ab_pci->ab = ab;
1432 	ab_pci->pdev = pdev;
1433 	ab->hif.ops = &ath11k_pci_hif_ops;
1434 	pci_set_drvdata(pdev, ab);
1435 	spin_lock_init(&ab_pci->window_lock);
1436 
1437 	/* Set fixed_mem_region to true for platforms support reserved memory
1438 	 * from DT. If memory is reserved from DT for FW, ath11k driver need not
1439 	 * allocate memory.
1440 	 */
1441 #if defined(__linux__)
1442 	ret = of_property_read_u32(ab->dev->of_node, "memory-region", &addr);
1443 	if (!ret)
1444 		set_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags);
1445 #endif
1446 
1447 	ret = ath11k_pci_claim(ab_pci, pdev);
1448 	if (ret) {
1449 		ath11k_err(ab, "failed to claim device: %d\n", ret);
1450 		goto err_free_core;
1451 	}
1452 
1453 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
1454 		   pdev->vendor, pdev->device,
1455 		   pdev->subsystem_vendor, pdev->subsystem_device);
1456 
1457 	ab->id.vendor = pdev->vendor;
1458 	ab->id.device = pdev->device;
1459 	ab->id.subsystem_vendor = pdev->subsystem_vendor;
1460 	ab->id.subsystem_device = pdev->subsystem_device;
1461 
1462 	switch (pci_dev->device) {
1463 	case QCA6390_DEVICE_ID:
1464 		ath11k_pci_read_hw_version(ab, &soc_hw_version_major,
1465 					   &soc_hw_version_minor);
1466 		switch (soc_hw_version_major) {
1467 		case 2:
1468 			ab->hw_rev = ATH11K_HW_QCA6390_HW20;
1469 			break;
1470 		default:
1471 			dev_err(&pdev->dev, "Unsupported QCA6390 SOC hardware version: %d %d\n",
1472 				soc_hw_version_major, soc_hw_version_minor);
1473 			ret = -EOPNOTSUPP;
1474 			goto err_pci_free_region;
1475 		}
1476 		ab_pci->msi_config = &ath11k_msi_config[0];
1477 		break;
1478 	case QCN9074_DEVICE_ID:
1479 		ab_pci->msi_config = &ath11k_msi_config[1];
1480 		ab->bus_params.static_window_map = true;
1481 		ab->hw_rev = ATH11K_HW_QCN9074_HW10;
1482 		break;
1483 	case WCN6855_DEVICE_ID:
1484 		ab->id.bdf_search = ATH11K_BDF_SEARCH_BUS_AND_BOARD;
1485 		ath11k_pci_read_hw_version(ab, &soc_hw_version_major,
1486 					   &soc_hw_version_minor);
1487 		switch (soc_hw_version_major) {
1488 		case 2:
1489 			switch (soc_hw_version_minor) {
1490 			case 0x00:
1491 			case 0x01:
1492 				ab->hw_rev = ATH11K_HW_WCN6855_HW20;
1493 				break;
1494 			case 0x10:
1495 			case 0x11:
1496 				ab->hw_rev = ATH11K_HW_WCN6855_HW21;
1497 				break;
1498 			default:
1499 				goto unsupported_wcn6855_soc;
1500 			}
1501 			break;
1502 		default:
1503 unsupported_wcn6855_soc:
1504 			dev_err(&pdev->dev, "Unsupported WCN6855 SOC hardware version: %d %d\n",
1505 				soc_hw_version_major, soc_hw_version_minor);
1506 			ret = -EOPNOTSUPP;
1507 			goto err_pci_free_region;
1508 		}
1509 		ab_pci->msi_config = &ath11k_msi_config[0];
1510 		break;
1511 	default:
1512 		dev_err(&pdev->dev, "Unknown PCI device found: 0x%x\n",
1513 			pci_dev->device);
1514 		ret = -EOPNOTSUPP;
1515 		goto err_pci_free_region;
1516 	}
1517 
1518 	ret = ath11k_pci_alloc_msi(ab_pci);
1519 	if (ret) {
1520 		ath11k_err(ab, "failed to enable msi: %d\n", ret);
1521 		goto err_pci_free_region;
1522 	}
1523 
1524 	ret = ath11k_core_pre_init(ab);
1525 	if (ret)
1526 		goto err_pci_disable_msi;
1527 
1528 	ret = ath11k_mhi_register(ab_pci);
1529 	if (ret) {
1530 		ath11k_err(ab, "failed to register mhi: %d\n", ret);
1531 		goto err_pci_disable_msi;
1532 	}
1533 
1534 	ret = ath11k_hal_srng_init(ab);
1535 	if (ret)
1536 		goto err_mhi_unregister;
1537 
1538 	ret = ath11k_ce_alloc_pipes(ab);
1539 	if (ret) {
1540 		ath11k_err(ab, "failed to allocate ce pipes: %d\n", ret);
1541 		goto err_hal_srng_deinit;
1542 	}
1543 
1544 	ath11k_pci_init_qmi_ce_config(ab);
1545 
1546 	ret = ath11k_pci_config_irq(ab);
1547 	if (ret) {
1548 		ath11k_err(ab, "failed to config irq: %d\n", ret);
1549 		goto err_ce_free;
1550 	}
1551 
1552 	/* kernel may allocate a dummy vector before request_irq and
1553 	 * then allocate a real vector when request_irq is called.
1554 	 * So get msi_data here again to avoid spurious interrupt
1555 	 * as msi_data will configured to srngs.
1556 	 */
1557 	ret = ath11k_pci_config_msi_data(ab_pci);
1558 	if (ret) {
1559 		ath11k_err(ab, "failed to config msi_data: %d\n", ret);
1560 		goto err_free_irq;
1561 	}
1562 
1563 	ret = ath11k_core_init(ab);
1564 	if (ret) {
1565 		ath11k_err(ab, "failed to init core: %d\n", ret);
1566 		goto err_free_irq;
1567 	}
1568 	return 0;
1569 
1570 err_free_irq:
1571 	ath11k_pci_free_irq(ab);
1572 
1573 err_ce_free:
1574 	ath11k_ce_free_pipes(ab);
1575 
1576 err_hal_srng_deinit:
1577 	ath11k_hal_srng_deinit(ab);
1578 
1579 err_mhi_unregister:
1580 	ath11k_mhi_unregister(ab_pci);
1581 
1582 err_pci_disable_msi:
1583 	ath11k_pci_free_msi(ab_pci);
1584 
1585 err_pci_free_region:
1586 	ath11k_pci_free_region(ab_pci);
1587 
1588 err_free_core:
1589 	ath11k_core_free(ab);
1590 
1591 	return ret;
1592 }
1593 
1594 static void ath11k_pci_remove(struct pci_dev *pdev)
1595 {
1596 	struct ath11k_base *ab = pci_get_drvdata(pdev);
1597 	struct ath11k_pci *ab_pci = ath11k_pci_priv(ab);
1598 
1599 	ath11k_pci_set_irq_affinity_hint(ab_pci, NULL);
1600 
1601 	if (test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags)) {
1602 		ath11k_pci_power_down(ab);
1603 		ath11k_debugfs_soc_destroy(ab);
1604 		ath11k_qmi_deinit_service(ab);
1605 		goto qmi_fail;
1606 	}
1607 
1608 	set_bit(ATH11K_FLAG_UNREGISTERING, &ab->dev_flags);
1609 
1610 	ath11k_core_deinit(ab);
1611 
1612 qmi_fail:
1613 	ath11k_mhi_unregister(ab_pci);
1614 
1615 	ath11k_pci_free_irq(ab);
1616 	ath11k_pci_free_msi(ab_pci);
1617 	ath11k_pci_free_region(ab_pci);
1618 
1619 	ath11k_hal_srng_deinit(ab);
1620 	ath11k_ce_free_pipes(ab);
1621 	ath11k_core_free(ab);
1622 }
1623 
1624 static void ath11k_pci_shutdown(struct pci_dev *pdev)
1625 {
1626 	struct ath11k_base *ab = pci_get_drvdata(pdev);
1627 
1628 	ath11k_pci_power_down(ab);
1629 }
1630 
1631 #ifdef CONFIG_PM
1632 static __maybe_unused int ath11k_pci_pm_suspend(struct device *dev)
1633 {
1634 	struct ath11k_base *ab = dev_get_drvdata(dev);
1635 	int ret;
1636 
1637 	ret = ath11k_core_suspend(ab);
1638 	if (ret)
1639 		ath11k_warn(ab, "failed to suspend core: %d\n", ret);
1640 
1641 	return ret;
1642 }
1643 
1644 static __maybe_unused int ath11k_pci_pm_resume(struct device *dev)
1645 {
1646 	struct ath11k_base *ab = dev_get_drvdata(dev);
1647 	int ret;
1648 
1649 	ret = ath11k_core_resume(ab);
1650 	if (ret)
1651 		ath11k_warn(ab, "failed to resume core: %d\n", ret);
1652 
1653 	return ret;
1654 }
1655 
1656 static SIMPLE_DEV_PM_OPS(ath11k_pci_pm_ops,
1657 			 ath11k_pci_pm_suspend,
1658 			 ath11k_pci_pm_resume);
1659 #endif
1660 
1661 static struct pci_driver ath11k_pci_driver = {
1662 	.name = "ath11k_pci",
1663 	.id_table = ath11k_pci_id_table,
1664 	.probe = ath11k_pci_probe,
1665 	.remove = ath11k_pci_remove,
1666 	.shutdown = ath11k_pci_shutdown,
1667 #ifdef CONFIG_PM
1668 	.driver.pm = &ath11k_pci_pm_ops,
1669 #endif
1670 };
1671 
1672 static int ath11k_pci_init(void)
1673 {
1674 	int ret;
1675 
1676 	ret = pci_register_driver(&ath11k_pci_driver);
1677 	if (ret)
1678 		pr_err("failed to register ath11k pci driver: %d\n",
1679 		       ret);
1680 
1681 	return ret;
1682 }
1683 module_init(ath11k_pci_init);
1684 
1685 static void ath11k_pci_exit(void)
1686 {
1687 	pci_unregister_driver(&ath11k_pci_driver);
1688 }
1689 
1690 module_exit(ath11k_pci_exit);
1691 
1692 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax WLAN PCIe devices");
1693 MODULE_LICENSE("Dual BSD/GPL");
1694 #if defined(__FreeBSD__)
1695 MODULE_VERSION(ath11k_pci, 1);
1696 MODULE_DEPEND(ath11k_pci, linuxkpi, 1, 1, 1);
1697 MODULE_DEPEND(ath11k_pci, linuxkpi_wlan, 1, 1, 1);
1698 MODULE_DEPEND(ath11k_pci, athk_common, 1, 1, 1);
1699 #ifdef CONFIG_ATH11K_DEBUGFS
1700 MODULE_DEPEND(ath11k_pci, debugfs, 1, 1, 1);
1701 #endif
1702 #endif
1703 
1704 /* QCA639x 2.0 firmware files */
1705 MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_BOARD_API2_FILE);
1706 MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_AMSS_FILE);
1707 MODULE_FIRMWARE(ATH11K_FW_DIR "/QCA6390/hw2.0/" ATH11K_M3_FILE);
1708