xref: /openbsd-src/sys/dev/pci/if_qwx_pci.c (revision d58514d7a08deb45d17eba08df36e2cbbb273a24)
1*d58514d7Skevlo /*	$OpenBSD: if_qwx_pci.c,v 1.23 2024/10/04 07:46:33 kevlo Exp $	*/
2c2498839Sstsp 
3c2498839Sstsp /*
4c2498839Sstsp  * Copyright 2023 Stefan Sperling <stsp@openbsd.org>
5c2498839Sstsp  *
6c2498839Sstsp  * Permission to use, copy, modify, and distribute this software for any
7c2498839Sstsp  * purpose with or without fee is hereby granted, provided that the above
8c2498839Sstsp  * copyright notice and this permission notice appear in all copies.
9c2498839Sstsp  *
10c2498839Sstsp  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11c2498839Sstsp  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12c2498839Sstsp  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13c2498839Sstsp  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14c2498839Sstsp  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15c2498839Sstsp  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16c2498839Sstsp  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17c2498839Sstsp  */
18c2498839Sstsp 
19c2498839Sstsp /*
20c2498839Sstsp  * Copyright (c) 2021-2022, Qualcomm Innovation Center, Inc.
21c2498839Sstsp  * Copyright (c) 2018-2021 The Linux Foundation.
22c2498839Sstsp  * All rights reserved.
23c2498839Sstsp  *
24c2498839Sstsp  * Redistribution and use in source and binary forms, with or without
25c2498839Sstsp  * modification, are permitted (subject to the limitations in the disclaimer
26c2498839Sstsp  * below) provided that the following conditions are met:
27c2498839Sstsp  *
28c2498839Sstsp  *  * Redistributions of source code must retain the above copyright notice,
29c2498839Sstsp  *    this list of conditions and the following disclaimer.
30c2498839Sstsp  *
31c2498839Sstsp  *  * Redistributions in binary form must reproduce the above copyright
32c2498839Sstsp  *    notice, this list of conditions and the following disclaimer in the
33c2498839Sstsp  *    documentation and/or other materials provided with the distribution.
34c2498839Sstsp  *
35c2498839Sstsp  *  * Neither the name of [Owner Organization] nor the names of its
36c2498839Sstsp  *    contributors may be used to endorse or promote products derived from
37c2498839Sstsp  *    this software without specific prior written permission.
38c2498839Sstsp  *
39c2498839Sstsp  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
40c2498839Sstsp  * THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
41c2498839Sstsp  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
42c2498839Sstsp  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43c2498839Sstsp  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
44c2498839Sstsp  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45c2498839Sstsp  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46c2498839Sstsp  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
47c2498839Sstsp  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48c2498839Sstsp  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
49c2498839Sstsp  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
50c2498839Sstsp  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51c2498839Sstsp  */
52c2498839Sstsp 
536b8600e7Sstsp #include "bpfilter.h"
546b8600e7Sstsp 
55c2498839Sstsp #include <sys/param.h>
56c2498839Sstsp #include <sys/mbuf.h>
57d8a36b37Sbluhm #include <sys/lock.h>
58c2498839Sstsp #include <sys/socket.h>
59c2498839Sstsp #include <sys/systm.h>
60c2498839Sstsp #include <sys/malloc.h>
61c2498839Sstsp #include <sys/device.h>
62c2498839Sstsp #include <sys/endian.h>
63c2498839Sstsp 
64c2498839Sstsp #include <machine/bus.h>
65c2498839Sstsp #include <machine/intr.h>
66c2498839Sstsp 
67c2498839Sstsp #include <net/if.h>
68c2498839Sstsp #include <net/if_media.h>
69c2498839Sstsp 
70c2498839Sstsp #include <netinet/in.h>
71c2498839Sstsp #include <netinet/if_ether.h>
72c2498839Sstsp 
73c2498839Sstsp #include <net80211/ieee80211_var.h>
74c2498839Sstsp #include <net80211/ieee80211_radiotap.h>
75c2498839Sstsp 
76c2498839Sstsp #include <dev/pci/pcireg.h>
77c2498839Sstsp #include <dev/pci/pcivar.h>
78c2498839Sstsp #include <dev/pci/pcidevs.h>
79c2498839Sstsp 
80c2498839Sstsp /* XXX linux porting goo */
81c2498839Sstsp #ifdef __LP64__
82c2498839Sstsp #define BITS_PER_LONG		64
83c2498839Sstsp #else
84c2498839Sstsp #define BITS_PER_LONG		32
85c2498839Sstsp #endif
86c2498839Sstsp #define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
87c2498839Sstsp #define __bf_shf(x) (__builtin_ffsll(x) - 1)
88c2498839Sstsp #define FIELD_GET(_m, _v) ((typeof(_m))(((_v) & (_m)) >> __bf_shf(_m)))
89c2498839Sstsp #define BIT(x)               (1UL << (x))
90c2498839Sstsp #define test_bit(i, a)  ((a) & (1 << (i)))
91c2498839Sstsp #define clear_bit(i, a) ((a)) &= ~(1 << (i))
92c2498839Sstsp #define set_bit(i, a)   ((a)) |= (1 << (i))
93c2498839Sstsp 
94c2498839Sstsp /* #define QWX_DEBUG */
95c2498839Sstsp 
96c2498839Sstsp #include <dev/ic/qwxreg.h>
97c2498839Sstsp #include <dev/ic/qwxvar.h>
98c2498839Sstsp 
9981cacbbeSstsp #ifdef QWX_DEBUG
100c2498839Sstsp /* Headers needed for RDDM dump */
101c2498839Sstsp #include <sys/namei.h>
102c2498839Sstsp #include <sys/pledge.h>
103c2498839Sstsp #include <sys/vnode.h>
104c2498839Sstsp #include <sys/fcntl.h>
105c2498839Sstsp #include <sys/stat.h>
106c2498839Sstsp #include <sys/proc.h>
10781cacbbeSstsp #endif
108c2498839Sstsp 
109c2498839Sstsp #define ATH11K_PCI_IRQ_CE0_OFFSET	3
110c2498839Sstsp #define ATH11K_PCI_IRQ_DP_OFFSET	14
111c2498839Sstsp 
112c2498839Sstsp #define ATH11K_PCI_CE_WAKE_IRQ		2
113c2498839Sstsp 
114c2498839Sstsp #define ATH11K_PCI_WINDOW_ENABLE_BIT	0x40000000
115c2498839Sstsp #define ATH11K_PCI_WINDOW_REG_ADDRESS	0x310c
116c2498839Sstsp #define ATH11K_PCI_WINDOW_VALUE_MASK	GENMASK(24, 19)
117c2498839Sstsp #define ATH11K_PCI_WINDOW_START		0x80000
118c2498839Sstsp #define ATH11K_PCI_WINDOW_RANGE_MASK	GENMASK(18, 0)
119c2498839Sstsp 
120c2498839Sstsp /* BAR0 + 4k is always accessible, and no need to force wakeup. */
121c2498839Sstsp #define ATH11K_PCI_ACCESS_ALWAYS_OFF	0xFE0	/* 4K - 32 = 0xFE0 */
122c2498839Sstsp 
123c2498839Sstsp #define TCSR_SOC_HW_VERSION		0x0224
124c2498839Sstsp #define TCSR_SOC_HW_VERSION_MAJOR_MASK	GENMASK(11, 8)
125c2498839Sstsp #define TCSR_SOC_HW_VERSION_MINOR_MASK	GENMASK(7, 0)
126c2498839Sstsp 
127c2498839Sstsp /*
128c2498839Sstsp  * pci.h
129c2498839Sstsp  */
130c2498839Sstsp #define PCIE_SOC_GLOBAL_RESET			0x3008
131c2498839Sstsp #define PCIE_SOC_GLOBAL_RESET_V			1
132c2498839Sstsp 
133c2498839Sstsp #define WLAON_WARM_SW_ENTRY			0x1f80504
134c2498839Sstsp #define WLAON_SOC_RESET_CAUSE_REG		0x01f8060c
135c2498839Sstsp 
136c2498839Sstsp #define PCIE_Q6_COOKIE_ADDR			0x01f80500
137c2498839Sstsp #define PCIE_Q6_COOKIE_DATA			0xc0000000
138c2498839Sstsp 
139c2498839Sstsp /* register to wake the UMAC from power collapse */
140c2498839Sstsp #define PCIE_SCRATCH_0_SOC_PCIE_REG		0x4040
141c2498839Sstsp 
142c2498839Sstsp /* register used for handshake mechanism to validate UMAC is awake */
143c2498839Sstsp #define PCIE_SOC_WAKE_PCIE_LOCAL_REG		0x3004
144c2498839Sstsp 
145c2498839Sstsp #define PCIE_PCIE_PARF_LTSSM			0x1e081b0
146c2498839Sstsp #define PARM_LTSSM_VALUE			0x111
147c2498839Sstsp 
148c2498839Sstsp #define GCC_GCC_PCIE_HOT_RST			0x1e402bc
149c2498839Sstsp #define GCC_GCC_PCIE_HOT_RST_VAL		0x10
150c2498839Sstsp 
151c2498839Sstsp #define PCIE_PCIE_INT_ALL_CLEAR			0x1e08228
152c2498839Sstsp #define PCIE_SMLH_REQ_RST_LINK_DOWN		0x2
153c2498839Sstsp #define PCIE_INT_CLEAR_ALL			0xffffffff
154c2498839Sstsp 
155c2498839Sstsp #define PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(sc) \
156c2498839Sstsp 		(sc->hw_params.regs->pcie_qserdes_sysclk_en_sel)
157c2498839Sstsp #define PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL	0x10
158c2498839Sstsp #define PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK	0xffffffff
159c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG1_REG(sc) \
160c2498839Sstsp 		(sc->hw_params.regs->pcie_pcs_osc_dtct_config_base)
161c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG1_VAL		0x02
162c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG2_REG(sc) \
163c2498839Sstsp 		(sc->hw_params.regs->pcie_pcs_osc_dtct_config_base + 0x4)
164c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG2_VAL		0x52
165c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG4_REG(sc) \
166c2498839Sstsp 		(sc->hw_params.regs->pcie_pcs_osc_dtct_config_base + 0xc)
167c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG4_VAL		0xff
168c2498839Sstsp #define PCIE_PCS_OSC_DTCT_CONFIG_MSK		0x000000ff
169c2498839Sstsp 
170c2498839Sstsp #define WLAON_QFPROM_PWR_CTRL_REG		0x01f8031c
171c2498839Sstsp #define QFPROM_PWR_CTRL_VDD4BLOW_MASK		0x4
172c2498839Sstsp 
173c2498839Sstsp /*
174c2498839Sstsp  * mhi.h
175c2498839Sstsp  */
176c2498839Sstsp #define PCIE_TXVECDB				0x360
177c2498839Sstsp #define PCIE_TXVECSTATUS			0x368
178c2498839Sstsp #define PCIE_RXVECDB				0x394
179c2498839Sstsp #define PCIE_RXVECSTATUS			0x39C
180c2498839Sstsp 
181c2498839Sstsp #define MHI_CHAN_CTX_CHSTATE_MASK		GENMASK(7, 0)
182c2498839Sstsp #define   MHI_CHAN_CTX_CHSTATE_DISABLED		0
183c2498839Sstsp #define   MHI_CHAN_CTX_CHSTATE_ENABLED		1
184c2498839Sstsp #define   MHI_CHAN_CTX_CHSTATE_RUNNING		2
185c2498839Sstsp #define   MHI_CHAN_CTX_CHSTATE_SUSPENDED	3
186c2498839Sstsp #define   MHI_CHAN_CTX_CHSTATE_STOP		4
187c2498839Sstsp #define   MHI_CHAN_CTX_CHSTATE_ERROR		5
188c2498839Sstsp #define MHI_CHAN_CTX_BRSTMODE_MASK		GENMASK(9, 8)
189c2498839Sstsp #define MHI_CHAN_CTX_BRSTMODE_SHFT		8
190c2498839Sstsp #define   MHI_CHAN_CTX_BRSTMODE_DISABLE		2
191c2498839Sstsp #define   MHI_CHAN_CTX_BRSTMODE_ENABLE		3
192c2498839Sstsp #define MHI_CHAN_CTX_POLLCFG_MASK		GENMASK(15, 10)
193c2498839Sstsp #define MHI_CHAN_CTX_RESERVED_MASK		GENMASK(31, 16)
194c2498839Sstsp 
195c2498839Sstsp #define QWX_MHI_CONFIG_QCA6390_MAX_CHANNELS	128
196c2498839Sstsp #define QWX_MHI_CONFIG_QCA6390_TIMEOUT_MS	2000
197c2498839Sstsp #define QWX_MHI_CONFIG_QCA9074_MAX_CHANNELS	30
198c2498839Sstsp 
199c2498839Sstsp #define MHI_CHAN_TYPE_INVALID		0
200c2498839Sstsp #define MHI_CHAN_TYPE_OUTBOUND		1 /* to device */
201c2498839Sstsp #define MHI_CHAN_TYPE_INBOUND		2 /* from device */
202c2498839Sstsp #define MHI_CHAN_TYPE_INBOUND_COALESCED	3
203c2498839Sstsp 
204c2498839Sstsp #define MHI_EV_CTX_RESERVED_MASK	GENMASK(7, 0)
205c2498839Sstsp #define MHI_EV_CTX_INTMODC_MASK		GENMASK(15, 8)
206c2498839Sstsp #define MHI_EV_CTX_INTMODT_MASK		GENMASK(31, 16)
207c2498839Sstsp #define MHI_EV_CTX_INTMODT_SHFT		16
208c2498839Sstsp 
209c2498839Sstsp #define MHI_ER_TYPE_INVALID	0
210c2498839Sstsp #define MHI_ER_TYPE_VALID	1
211c2498839Sstsp 
212c2498839Sstsp #define MHI_ER_DATA	0
213c2498839Sstsp #define MHI_ER_CTRL	1
214c2498839Sstsp 
215c2498839Sstsp #define MHI_CH_STATE_DISABLED	0
216c2498839Sstsp #define MHI_CH_STATE_ENABLED	1
217c2498839Sstsp #define MHI_CH_STATE_RUNNING	2
218c2498839Sstsp #define MHI_CH_STATE_SUSPENDED	3
219c2498839Sstsp #define MHI_CH_STATE_STOP	4
220c2498839Sstsp #define MHI_CH_STATE_ERROR	5
221c2498839Sstsp 
222c2498839Sstsp #define QWX_NUM_EVENT_CTX	2
223c2498839Sstsp 
224c2498839Sstsp /* Event context. Shared with device. */
225c2498839Sstsp struct qwx_mhi_event_ctxt {
226c2498839Sstsp 	uint32_t intmod;
227c2498839Sstsp 	uint32_t ertype;
228c2498839Sstsp 	uint32_t msivec;
229c2498839Sstsp 
230c2498839Sstsp 	uint64_t rbase;
231c2498839Sstsp 	uint64_t rlen;
232c2498839Sstsp 	uint64_t rp;
233c2498839Sstsp 	uint64_t wp;
234c2498839Sstsp } __packed;
235c2498839Sstsp 
236c2498839Sstsp /* Channel context. Shared with device. */
237c2498839Sstsp struct qwx_mhi_chan_ctxt {
238c2498839Sstsp 	uint32_t chcfg;
239c2498839Sstsp 	uint32_t chtype;
240c2498839Sstsp 	uint32_t erindex;
241c2498839Sstsp 
242c2498839Sstsp 	uint64_t rbase;
243c2498839Sstsp 	uint64_t rlen;
244c2498839Sstsp 	uint64_t rp;
245c2498839Sstsp 	uint64_t wp;
246c2498839Sstsp } __packed;
247c2498839Sstsp 
248c2498839Sstsp /* Command context. Shared with device. */
249c2498839Sstsp struct qwx_mhi_cmd_ctxt {
250c2498839Sstsp 	uint32_t reserved0;
251c2498839Sstsp 	uint32_t reserved1;
252c2498839Sstsp 	uint32_t reserved2;
253c2498839Sstsp 
254c2498839Sstsp 	uint64_t rbase;
255c2498839Sstsp 	uint64_t rlen;
256c2498839Sstsp 	uint64_t rp;
257c2498839Sstsp 	uint64_t wp;
258c2498839Sstsp } __packed;
259c2498839Sstsp 
260c2498839Sstsp struct qwx_mhi_ring_element {
261c2498839Sstsp 	uint64_t ptr;
262c2498839Sstsp 	uint32_t dword[2];
263c2498839Sstsp };
264c2498839Sstsp 
265c2498839Sstsp struct qwx_xfer_data {
266c2498839Sstsp 	bus_dmamap_t	map;
267c2498839Sstsp 	struct mbuf	*m;
268c2498839Sstsp };
269c2498839Sstsp 
270c2498839Sstsp #define QWX_PCI_XFER_MAX_DATA_SIZE	0xffff
271c2498839Sstsp #define QWX_PCI_XFER_RING_MAX_ELEMENTS	64
272c2498839Sstsp 
273c2498839Sstsp struct qwx_pci_xfer_ring {
274c2498839Sstsp 	struct qwx_dmamem	*dmamem;
275c2498839Sstsp 	bus_size_t		size;
276c2498839Sstsp 	uint32_t		mhi_chan_id;
277c2498839Sstsp 	uint32_t		mhi_chan_state;
278c2498839Sstsp 	uint32_t		mhi_chan_direction;
279c2498839Sstsp 	uint32_t		mhi_chan_event_ring_index;
280c2498839Sstsp 	uint32_t		db_addr;
281c2498839Sstsp 	uint32_t		cmd_status;
282c2498839Sstsp 	int			num_elements;
283c2498839Sstsp 	int			queued;
284c2498839Sstsp 	struct qwx_xfer_data	data[QWX_PCI_XFER_RING_MAX_ELEMENTS];
285c2498839Sstsp 	uint64_t		rp;
286c2498839Sstsp 	uint64_t		wp;
287c2498839Sstsp 	struct qwx_mhi_chan_ctxt *chan_ctxt;
288c2498839Sstsp };
289c2498839Sstsp 
290c2498839Sstsp 
291c2498839Sstsp #define QWX_PCI_EVENT_RING_MAX_ELEMENTS	256
292c2498839Sstsp 
293c2498839Sstsp struct qwx_pci_event_ring {
294c2498839Sstsp 	struct qwx_dmamem	*dmamem;
295c2498839Sstsp 	bus_size_t		size;
296c2498839Sstsp 	uint32_t		mhi_er_type;
297c2498839Sstsp 	uint32_t		mhi_er_irq;
298c2498839Sstsp 	uint32_t		mhi_er_irq_moderation_ms;
299c2498839Sstsp 	uint32_t		db_addr;
300c2498839Sstsp 	int			num_elements;
301c2498839Sstsp 	uint64_t		rp;
302c2498839Sstsp 	uint64_t		wp;
303c2498839Sstsp 	struct qwx_mhi_event_ctxt *event_ctxt;
304c2498839Sstsp };
305c2498839Sstsp 
306c2498839Sstsp struct qwx_cmd_data {
307c2498839Sstsp 	bus_dmamap_t	map;
308c2498839Sstsp 	struct mbuf	*m;
309c2498839Sstsp };
310c2498839Sstsp 
311c2498839Sstsp #define QWX_PCI_CMD_RING_MAX_ELEMENTS	128
312c2498839Sstsp 
313c2498839Sstsp struct qwx_pci_cmd_ring {
314c2498839Sstsp 	struct qwx_dmamem	*dmamem;
315c2498839Sstsp 	bus_size_t		size;
316c2498839Sstsp 	uint64_t		rp;
317c2498839Sstsp 	uint64_t		wp;
318c2498839Sstsp 	int			num_elements;
319c2498839Sstsp 	int			queued;
320c2498839Sstsp };
321c2498839Sstsp 
322c2498839Sstsp struct qwx_pci_ops;
323c2498839Sstsp struct qwx_msi_config;
324c2498839Sstsp 
3255b068355Sstsp #define QWX_NUM_MSI_VEC	32
3265b068355Sstsp 
327c2498839Sstsp struct qwx_pci_softc {
328c2498839Sstsp 	struct qwx_softc	sc_sc;
329c2498839Sstsp 	pci_chipset_tag_t	sc_pc;
330c2498839Sstsp 	pcitag_t		sc_tag;
331c2498839Sstsp 	int			sc_cap_off;
332c2498839Sstsp 	int			sc_msi_off;
333c2498839Sstsp 	pcireg_t		sc_msi_cap;
3345b068355Sstsp 	void			*sc_ih[QWX_NUM_MSI_VEC];
3355b068355Sstsp 	char			sc_ivname[QWX_NUM_MSI_VEC][16];
3365b068355Sstsp 	struct qwx_ext_irq_grp	ext_irq_grp[ATH11K_EXT_IRQ_GRP_NUM_MAX];
3375b068355Sstsp 	int			mhi_irq[2];
338c2498839Sstsp 	bus_space_tag_t		sc_st;
339c2498839Sstsp 	bus_space_handle_t	sc_sh;
340c2498839Sstsp 	bus_addr_t		sc_map;
341c2498839Sstsp 	bus_size_t		sc_mapsize;
342c2498839Sstsp 
343c2498839Sstsp 	pcireg_t		sc_lcsr;
344c2498839Sstsp 	uint32_t		sc_flags;
345c2498839Sstsp #define ATH11K_PCI_ASPM_RESTORE	1
346c2498839Sstsp 
347c2498839Sstsp 	uint32_t		register_window;
348c2498839Sstsp 	const struct qwx_pci_ops *sc_pci_ops;
349c2498839Sstsp 
350c2498839Sstsp 	uint32_t		 bhi_off;
351c2498839Sstsp 	uint32_t		 bhi_ee;
352c2498839Sstsp 	uint32_t		 bhie_off;
353c2498839Sstsp 	uint32_t		 mhi_state;
354c2498839Sstsp 	uint32_t		 max_chan;
355c2498839Sstsp 
356c2498839Sstsp 	uint64_t		 wake_db;
357c2498839Sstsp 
358c2498839Sstsp 	/*
3599132454eSstsp 	 * DMA memory for AMSS.bin firmware image.
360c2498839Sstsp 	 * This memory must remain available to the device until
361c2498839Sstsp 	 * the device is powered down.
362c2498839Sstsp 	 */
363c2498839Sstsp 	struct qwx_dmamem	*amss_data;
364c2498839Sstsp 	struct qwx_dmamem	*amss_vec;
365c2498839Sstsp 
366c2498839Sstsp 	struct qwx_dmamem	 *rddm_vec;
367c2498839Sstsp 	struct qwx_dmamem	 *rddm_data;
368c2498839Sstsp 	int			 rddm_triggered;
369c2498839Sstsp 	struct task		 rddm_task;
370c2498839Sstsp #define	QWX_RDDM_DUMP_SIZE	0x420000
371c2498839Sstsp 
372c2498839Sstsp 	struct qwx_dmamem	*chan_ctxt;
373c2498839Sstsp 	struct qwx_dmamem	*event_ctxt;
374c2498839Sstsp 	struct qwx_dmamem	*cmd_ctxt;
375c2498839Sstsp 
376c2498839Sstsp 
377*d58514d7Skevlo 	struct qwx_pci_xfer_ring xfer_rings[2];
378*d58514d7Skevlo #define QWX_PCI_XFER_RING_IPCR_OUTBOUND		0
379*d58514d7Skevlo #define QWX_PCI_XFER_RING_IPCR_INBOUND		1
380c2498839Sstsp 	struct qwx_pci_event_ring event_rings[QWX_NUM_EVENT_CTX];
381c2498839Sstsp 	struct qwx_pci_cmd_ring cmd_ring;
382c2498839Sstsp };
383c2498839Sstsp 
384c2498839Sstsp int	qwx_pci_match(struct device *, void *, void *);
385c2498839Sstsp void	qwx_pci_attach(struct device *, struct device *, void *);
386c2498839Sstsp int	qwx_pci_detach(struct device *, int);
387c2498839Sstsp void	qwx_pci_attach_hook(struct device *);
388c2498839Sstsp void	qwx_pci_free_xfer_rings(struct qwx_pci_softc *);
389c2498839Sstsp int	qwx_pci_alloc_xfer_ring(struct qwx_softc *, struct qwx_pci_xfer_ring *,
390c2498839Sstsp 	    uint32_t, uint32_t, uint32_t, size_t);
391c2498839Sstsp int	qwx_pci_alloc_xfer_rings_qca6390(struct qwx_pci_softc *);
392c2498839Sstsp int	qwx_pci_alloc_xfer_rings_qcn9074(struct qwx_pci_softc *);
393c2498839Sstsp void	qwx_pci_free_event_rings(struct qwx_pci_softc *);
394c2498839Sstsp int	qwx_pci_alloc_event_ring(struct qwx_softc *,
395c2498839Sstsp 	    struct qwx_pci_event_ring *, uint32_t, uint32_t, uint32_t, size_t);
396c2498839Sstsp int	qwx_pci_alloc_event_rings(struct qwx_pci_softc *);
397c2498839Sstsp void	qwx_pci_free_cmd_ring(struct qwx_pci_softc *);
398c2498839Sstsp int	qwx_pci_init_cmd_ring(struct qwx_softc *, struct qwx_pci_cmd_ring *);
399c2498839Sstsp uint32_t qwx_pci_read(struct qwx_softc *, uint32_t);
400c2498839Sstsp void	qwx_pci_write(struct qwx_softc *, uint32_t, uint32_t);
401c2498839Sstsp 
402c2498839Sstsp void	qwx_pci_read_hw_version(struct qwx_softc *, uint32_t *, uint32_t *);
403c2498839Sstsp uint32_t qwx_pcic_read32(struct qwx_softc *, uint32_t);
404c2498839Sstsp void	 qwx_pcic_write32(struct qwx_softc *, uint32_t, uint32_t);
405c2498839Sstsp 
406c2498839Sstsp void	qwx_pcic_ext_irq_enable(struct qwx_softc *);
407c2498839Sstsp void	qwx_pcic_ext_irq_disable(struct qwx_softc *);
4085b068355Sstsp int	qwx_pcic_config_irq(struct qwx_softc *, struct pci_attach_args *);
409c2498839Sstsp 
410c2498839Sstsp int	qwx_pci_start(struct qwx_softc *);
411c2498839Sstsp void	qwx_pci_stop(struct qwx_softc *);
412c2498839Sstsp void	qwx_pci_aspm_disable(struct qwx_softc *);
413c2498839Sstsp void	qwx_pci_aspm_restore(struct qwx_softc *);
414c2498839Sstsp int	qwx_pci_power_up(struct qwx_softc *);
415c2498839Sstsp void	qwx_pci_power_down(struct qwx_softc *);
416c2498839Sstsp 
417c2498839Sstsp int	qwx_pci_bus_wake_up(struct qwx_softc *);
418c2498839Sstsp void	qwx_pci_bus_release(struct qwx_softc *);
419c2498839Sstsp void	qwx_pci_window_write32(struct qwx_softc *, uint32_t, uint32_t);
420c2498839Sstsp uint32_t qwx_pci_window_read32(struct qwx_softc *, uint32_t);
421c2498839Sstsp 
422c2498839Sstsp int	qwx_mhi_register(struct qwx_softc *);
423c2498839Sstsp void	qwx_mhi_unregister(struct qwx_softc *);
424c2498839Sstsp void	qwx_mhi_ring_doorbell(struct qwx_softc *sc, uint64_t, uint64_t);
425c2498839Sstsp void	qwx_mhi_device_wake(struct qwx_softc *);
426c2498839Sstsp void	qwx_mhi_device_zzz(struct qwx_softc *);
427c2498839Sstsp int	qwx_mhi_wake_db_clear_valid(struct qwx_softc *);
428c2498839Sstsp void	qwx_mhi_init_xfer_rings(struct qwx_pci_softc *);
429c2498839Sstsp void	qwx_mhi_init_event_rings(struct qwx_pci_softc *);
430c2498839Sstsp void	qwx_mhi_init_cmd_ring(struct qwx_pci_softc *);
431c2498839Sstsp void	qwx_mhi_init_dev_ctxt(struct qwx_pci_softc *);
432c2498839Sstsp int	qwx_mhi_send_cmd(struct qwx_pci_softc *psc, uint32_t, uint32_t);
433c2498839Sstsp void *	qwx_pci_xfer_ring_get_elem(struct qwx_pci_xfer_ring *, uint64_t);
434c2498839Sstsp struct qwx_xfer_data *qwx_pci_xfer_ring_get_data(struct qwx_pci_xfer_ring *,
435c2498839Sstsp 	    uint64_t);
436c2498839Sstsp int	qwx_mhi_submit_xfer(struct qwx_softc *sc, struct mbuf *m);
437c2498839Sstsp int	qwx_mhi_start_channel(struct qwx_pci_softc *,
438c2498839Sstsp 	    struct qwx_pci_xfer_ring *);
439c2498839Sstsp int	qwx_mhi_start_channels(struct qwx_pci_softc *);
440c2498839Sstsp int	qwx_mhi_start(struct qwx_pci_softc *);
441c2498839Sstsp void	qwx_mhi_stop(struct qwx_softc *);
442c2498839Sstsp int	qwx_mhi_reset_device(struct qwx_softc *, int);
443c2498839Sstsp void	qwx_mhi_clear_vector(struct qwx_softc *);
444c2498839Sstsp int	qwx_mhi_fw_load_handler(struct qwx_pci_softc *);
445c2498839Sstsp int	qwx_mhi_await_device_reset(struct qwx_softc *);
446c2498839Sstsp int	qwx_mhi_await_device_ready(struct qwx_softc *);
447c2498839Sstsp void	qwx_mhi_ready_state_transition(struct qwx_pci_softc *);
448c2498839Sstsp void	qwx_mhi_mission_mode_state_transition(struct qwx_pci_softc *);
449a9f6aa33Sstsp void	qwx_mhi_low_power_mode_state_transition(struct qwx_pci_softc *);
450c2498839Sstsp void	qwx_mhi_set_state(struct qwx_softc *, uint32_t);
451c2498839Sstsp void	qwx_mhi_init_mmio(struct qwx_pci_softc *);
452c2498839Sstsp int	qwx_mhi_fw_load_bhi(struct qwx_pci_softc *, uint8_t *, size_t);
453c2498839Sstsp int	qwx_mhi_fw_load_bhie(struct qwx_pci_softc *, uint8_t *, size_t);
454c2498839Sstsp void	qwx_rddm_prepare(struct qwx_pci_softc *);
45581cacbbeSstsp #ifdef QWX_DEBUG
456c2498839Sstsp void	qwx_rddm_task(void *);
45781cacbbeSstsp #endif
458c2498839Sstsp void *	qwx_pci_event_ring_get_elem(struct qwx_pci_event_ring *, uint64_t);
459c2498839Sstsp void	qwx_pci_intr_ctrl_event_mhi(struct qwx_pci_softc *, uint32_t);
460c2498839Sstsp void	qwx_pci_intr_ctrl_event_ee(struct qwx_pci_softc *, uint32_t);
461c2498839Sstsp void	qwx_pci_intr_ctrl_event_cmd_complete(struct qwx_pci_softc *,
462c2498839Sstsp 	    uint64_t, uint32_t);
463c2498839Sstsp int	qwx_pci_intr_ctrl_event(struct qwx_pci_softc *,
464c2498839Sstsp 	    struct qwx_pci_event_ring *);
465c2498839Sstsp void	qwx_pci_intr_data_event_tx(struct qwx_pci_softc *,
466c2498839Sstsp 	    struct qwx_mhi_ring_element *);
467c2498839Sstsp int	qwx_pci_intr_data_event(struct qwx_pci_softc *,
468c2498839Sstsp 	    struct qwx_pci_event_ring *);
4695b068355Sstsp int	qwx_pci_intr_mhi_ctrl(void *);
4705b068355Sstsp int	qwx_pci_intr_mhi_data(void *);
471c2498839Sstsp int	qwx_pci_intr(void *);
472c2498839Sstsp 
473c2498839Sstsp struct qwx_pci_ops {
474c2498839Sstsp 	int	 (*wakeup)(struct qwx_softc *);
475c2498839Sstsp 	void	 (*release)(struct qwx_softc *);
476c2498839Sstsp 	int	 (*get_msi_irq)(struct qwx_softc *, unsigned int);
477c2498839Sstsp 	void	 (*window_write32)(struct qwx_softc *, uint32_t, uint32_t);
478c2498839Sstsp 	uint32_t (*window_read32)(struct qwx_softc *, uint32_t);
479c2498839Sstsp 	int	 (*alloc_xfer_rings)(struct qwx_pci_softc *);
480c2498839Sstsp };
481c2498839Sstsp 
482c2498839Sstsp 
483c2498839Sstsp static const struct qwx_pci_ops qwx_pci_ops_qca6390 = {
484c2498839Sstsp 	.wakeup = qwx_pci_bus_wake_up,
485c2498839Sstsp 	.release = qwx_pci_bus_release,
486c2498839Sstsp #if notyet
487c2498839Sstsp 	.get_msi_irq = qwx_pci_get_msi_irq,
488c2498839Sstsp #endif
489c2498839Sstsp 	.window_write32 = qwx_pci_window_write32,
490c2498839Sstsp 	.window_read32 = qwx_pci_window_read32,
491c2498839Sstsp 	.alloc_xfer_rings = qwx_pci_alloc_xfer_rings_qca6390,
492c2498839Sstsp };
493c2498839Sstsp 
494c2498839Sstsp static const struct qwx_pci_ops qwx_pci_ops_qcn9074 = {
495c2498839Sstsp 	.wakeup = NULL,
496c2498839Sstsp 	.release = NULL,
497c2498839Sstsp #if notyet
498c2498839Sstsp 	.get_msi_irq = qwx_pci_get_msi_irq,
499c2498839Sstsp #endif
500c2498839Sstsp 	.window_write32 = qwx_pci_window_write32,
501c2498839Sstsp 	.window_read32 = qwx_pci_window_read32,
502c2498839Sstsp 	.alloc_xfer_rings = qwx_pci_alloc_xfer_rings_qcn9074,
503c2498839Sstsp };
504c2498839Sstsp 
505c2498839Sstsp const struct cfattach qwx_pci_ca = {
506c2498839Sstsp 	sizeof(struct qwx_pci_softc),
507c2498839Sstsp 	qwx_pci_match,
508c2498839Sstsp 	qwx_pci_attach,
509c2498839Sstsp 	qwx_pci_detach,
510de21b57dSstsp 	qwx_activate
511c2498839Sstsp };
512c2498839Sstsp 
513c2498839Sstsp /* XXX pcidev */
514c2498839Sstsp #define PCI_PRODUCT_QUALCOMM_QCA6390	0x1101
515c2498839Sstsp #define PCI_PRODUCT_QUALCOMM_QCN9074	0x1104
516c2498839Sstsp 
517c2498839Sstsp static const struct pci_matchid qwx_pci_devices[] = {
518c2498839Sstsp #if notyet
519c2498839Sstsp 	{ PCI_VENDOR_QUALCOMM, PCI_PRODUCT_QUALCOMM_QCA6390 },
520c2498839Sstsp 	{ PCI_VENDOR_QUALCOMM, PCI_PRODUCT_QUALCOMM_QCN9074 },
521c2498839Sstsp #endif
522c2498839Sstsp 	{ PCI_VENDOR_QUALCOMM, PCI_PRODUCT_QUALCOMM_QCNFA765 }
523c2498839Sstsp };
524c2498839Sstsp 
525c2498839Sstsp int
526c2498839Sstsp qwx_pci_match(struct device *parent, void *match, void *aux)
527c2498839Sstsp {
528c2498839Sstsp 	return pci_matchbyid(aux, qwx_pci_devices, nitems(qwx_pci_devices));
529c2498839Sstsp }
530c2498839Sstsp 
531c2498839Sstsp void
532c2498839Sstsp qwx_pci_init_qmi_ce_config(struct qwx_softc *sc)
533c2498839Sstsp {
534c2498839Sstsp 	struct qwx_qmi_ce_cfg *cfg = &sc->qmi_ce_cfg;
535c2498839Sstsp 
536c2498839Sstsp 	qwx_ce_get_shadow_config(sc, &cfg->shadow_reg_v2,
537c2498839Sstsp 	    &cfg->shadow_reg_v2_len);
538c2498839Sstsp }
539c2498839Sstsp 
540c2498839Sstsp const struct qwx_msi_config qwx_msi_config_one_msi = {
541c2498839Sstsp 	.total_vectors = 1,
542c2498839Sstsp 	.total_users = 4,
543c2498839Sstsp 	.users = (struct qwx_msi_user[]) {
544c2498839Sstsp 		{ .name = "MHI", .num_vectors = 1, .base_vector = 0 },
545c2498839Sstsp 		{ .name = "CE", .num_vectors = 1, .base_vector = 0 },
546c2498839Sstsp 		{ .name = "WAKE", .num_vectors = 1, .base_vector = 0 },
547c2498839Sstsp 		{ .name = "DP", .num_vectors = 1, .base_vector = 0 },
548c2498839Sstsp 	},
549c2498839Sstsp };
550c2498839Sstsp 
5515b068355Sstsp const struct qwx_msi_config qwx_msi_config[] = {
5525b068355Sstsp 	{
5535b068355Sstsp 		.total_vectors = 32,
5545b068355Sstsp 		.total_users = 4,
5555b068355Sstsp 		.users = (struct qwx_msi_user[]) {
5565b068355Sstsp 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
5575b068355Sstsp 			{ .name = "CE", .num_vectors = 10, .base_vector = 3 },
5585b068355Sstsp 			{ .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
5595b068355Sstsp 			{ .name = "DP", .num_vectors = 18, .base_vector = 14 },
5605b068355Sstsp 		},
5615b068355Sstsp 		.hw_rev = ATH11K_HW_QCA6390_HW20,
5625b068355Sstsp 	},
5635b068355Sstsp 	{
5645b068355Sstsp 		.total_vectors = 16,
5655b068355Sstsp 		.total_users = 3,
5665b068355Sstsp 		.users = (struct qwx_msi_user[]) {
5675b068355Sstsp 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
5685b068355Sstsp 			{ .name = "CE", .num_vectors = 5, .base_vector = 3 },
5695b068355Sstsp 			{ .name = "DP", .num_vectors = 8, .base_vector = 8 },
5705b068355Sstsp 		},
5715b068355Sstsp 		.hw_rev = ATH11K_HW_QCN9074_HW10,
5725b068355Sstsp 	},
5735b068355Sstsp 	{
5745b068355Sstsp 		.total_vectors = 32,
5755b068355Sstsp 		.total_users = 4,
5765b068355Sstsp 		.users = (struct qwx_msi_user[]) {
5775b068355Sstsp 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
5785b068355Sstsp 			{ .name = "CE", .num_vectors = 10, .base_vector = 3 },
5795b068355Sstsp 			{ .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
5805b068355Sstsp 			{ .name = "DP", .num_vectors = 18, .base_vector = 14 },
5815b068355Sstsp 		},
5825b068355Sstsp 		.hw_rev = ATH11K_HW_WCN6855_HW20,
5835b068355Sstsp 	},
5845b068355Sstsp 	{
5855b068355Sstsp 		.total_vectors = 32,
5865b068355Sstsp 		.total_users = 4,
5875b068355Sstsp 		.users = (struct qwx_msi_user[]) {
5885b068355Sstsp 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
5895b068355Sstsp 			{ .name = "CE", .num_vectors = 10, .base_vector = 3 },
5905b068355Sstsp 			{ .name = "WAKE", .num_vectors = 1, .base_vector = 13 },
5915b068355Sstsp 			{ .name = "DP", .num_vectors = 18, .base_vector = 14 },
5925b068355Sstsp 		},
5935b068355Sstsp 		.hw_rev = ATH11K_HW_WCN6855_HW21,
5945b068355Sstsp 	},
5955b068355Sstsp 	{
5965b068355Sstsp 		.total_vectors = 28,
5975b068355Sstsp 		.total_users = 2,
5985b068355Sstsp 		.users = (struct qwx_msi_user[]) {
5995b068355Sstsp 			{ .name = "CE", .num_vectors = 10, .base_vector = 0 },
6005b068355Sstsp 			{ .name = "DP", .num_vectors = 18, .base_vector = 10 },
6015b068355Sstsp 		},
6025b068355Sstsp 		.hw_rev = ATH11K_HW_WCN6750_HW10,
6035b068355Sstsp 	},
6045b068355Sstsp };
6055b068355Sstsp 
6065b068355Sstsp int
6075b068355Sstsp qwx_pcic_init_msi_config(struct qwx_softc *sc)
6085b068355Sstsp {
6095b068355Sstsp 	const struct qwx_msi_config *msi_config;
6105b068355Sstsp 	int i;
6115b068355Sstsp 
6125b068355Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags)) {
6135b068355Sstsp 		sc->msi_cfg = &qwx_msi_config_one_msi;
6145b068355Sstsp 		return 0;
6155b068355Sstsp 	}
6165b068355Sstsp 	for (i = 0; i < nitems(qwx_msi_config); i++) {
6175b068355Sstsp 		msi_config = &qwx_msi_config[i];
6185b068355Sstsp 
6195b068355Sstsp 		if (msi_config->hw_rev == sc->sc_hw_rev)
6205b068355Sstsp 			break;
6215b068355Sstsp 	}
6225b068355Sstsp 
6235b068355Sstsp 	if (i == nitems(qwx_msi_config)) {
6245b068355Sstsp 		printf("%s: failed to fetch msi config, "
6255b068355Sstsp 		    "unsupported hw version: 0x%x\n",
6265b068355Sstsp 		    sc->sc_dev.dv_xname, sc->sc_hw_rev);
6275b068355Sstsp 		return EINVAL;
6285b068355Sstsp 	}
6295b068355Sstsp 
6305b068355Sstsp 	sc->msi_cfg = msi_config;
6315b068355Sstsp 	return 0;
6325b068355Sstsp }
6335b068355Sstsp 
634c2498839Sstsp int
635c2498839Sstsp qwx_pci_alloc_msi(struct qwx_softc *sc)
636c2498839Sstsp {
637c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
638c2498839Sstsp 	uint64_t addr;
639c2498839Sstsp 	pcireg_t data;
640c2498839Sstsp 
641c2498839Sstsp 	if (psc->sc_msi_cap & PCI_MSI_MC_C64) {
642c2498839Sstsp 		uint64_t addr_hi;
643c2498839Sstsp 		pcireg_t addr_lo;
644c2498839Sstsp 
645c2498839Sstsp 		addr_lo = pci_conf_read(psc->sc_pc, psc->sc_tag,
646c2498839Sstsp 		    psc->sc_msi_off + PCI_MSI_MA);
647c2498839Sstsp 		addr_hi = pci_conf_read(psc->sc_pc, psc->sc_tag,
648c2498839Sstsp 		    psc->sc_msi_off + PCI_MSI_MAU32);
649c2498839Sstsp 		addr = addr_hi << 32 | addr_lo;
650c2498839Sstsp 		data = pci_conf_read(psc->sc_pc, psc->sc_tag,
651c2498839Sstsp 		    psc->sc_msi_off + PCI_MSI_MD64);
652c2498839Sstsp 	} else {
653c2498839Sstsp 		addr = pci_conf_read(psc->sc_pc, psc->sc_tag,
654c2498839Sstsp 		    psc->sc_msi_off + PCI_MSI_MA);
655c2498839Sstsp 		data = pci_conf_read(psc->sc_pc, psc->sc_tag,
656c2498839Sstsp 		    psc->sc_msi_off + PCI_MSI_MD32);
657c2498839Sstsp 	}
658c2498839Sstsp 
659c2498839Sstsp 	sc->msi_addr_lo = addr & 0xffffffff;
660c2498839Sstsp 	sc->msi_addr_hi = ((uint64_t)addr) >> 32;
661c2498839Sstsp 	sc->msi_data_start = data;
662c2498839Sstsp 
663c2498839Sstsp 	DPRINTF("%s: MSI addr: 0x%llx MSI data: 0x%x\n", sc->sc_dev.dv_xname,
664c2498839Sstsp 	    addr, data);
665c2498839Sstsp 
666c2498839Sstsp 	return 0;
667c2498839Sstsp }
668c2498839Sstsp 
669c2498839Sstsp int
670c2498839Sstsp qwx_pcic_map_service_to_pipe(struct qwx_softc *sc, uint16_t service_id,
671c2498839Sstsp     uint8_t *ul_pipe, uint8_t *dl_pipe)
672c2498839Sstsp {
673c2498839Sstsp 	const struct service_to_pipe *entry;
674c2498839Sstsp 	int ul_set = 0, dl_set = 0;
675c2498839Sstsp 	int i;
676c2498839Sstsp 
677c2498839Sstsp 	for (i = 0; i < sc->hw_params.svc_to_ce_map_len; i++) {
678c2498839Sstsp 		entry = &sc->hw_params.svc_to_ce_map[i];
679c2498839Sstsp 
680c2498839Sstsp 		if (le32toh(entry->service_id) != service_id)
681c2498839Sstsp 			continue;
682c2498839Sstsp 
683c2498839Sstsp 		switch (le32toh(entry->pipedir)) {
684c2498839Sstsp 		case PIPEDIR_NONE:
685c2498839Sstsp 			break;
686c2498839Sstsp 		case PIPEDIR_IN:
687c2498839Sstsp 			*dl_pipe = le32toh(entry->pipenum);
688c2498839Sstsp 			dl_set = 1;
689c2498839Sstsp 			break;
690c2498839Sstsp 		case PIPEDIR_OUT:
691c2498839Sstsp 			*ul_pipe = le32toh(entry->pipenum);
692c2498839Sstsp 			ul_set = 1;
693c2498839Sstsp 			break;
694c2498839Sstsp 		case PIPEDIR_INOUT:
695c2498839Sstsp 			*dl_pipe = le32toh(entry->pipenum);
696c2498839Sstsp 			*ul_pipe = le32toh(entry->pipenum);
697c2498839Sstsp 			dl_set = 1;
698c2498839Sstsp 			ul_set = 1;
699c2498839Sstsp 			break;
700c2498839Sstsp 		}
701c2498839Sstsp 	}
702c2498839Sstsp 
703c2498839Sstsp 	if (!ul_set || !dl_set) {
704c2498839Sstsp 		DPRINTF("%s: found no uplink and no downlink\n", __func__);
705c2498839Sstsp 		return ENOENT;
706c2498839Sstsp 	}
707c2498839Sstsp 
708c2498839Sstsp 	return 0;
709c2498839Sstsp }
710c2498839Sstsp 
711c2498839Sstsp int
712c2498839Sstsp qwx_pcic_get_user_msi_vector(struct qwx_softc *sc, char *user_name,
713c2498839Sstsp     int *num_vectors, uint32_t *user_base_data, uint32_t *base_vector)
714c2498839Sstsp {
715c2498839Sstsp 	const struct qwx_msi_config *msi_config = sc->msi_cfg;
716c2498839Sstsp 	int idx;
717c2498839Sstsp 
718c2498839Sstsp 	for (idx = 0; idx < msi_config->total_users; idx++) {
719c2498839Sstsp 		if (strcmp(user_name, msi_config->users[idx].name) == 0) {
720c2498839Sstsp 			*num_vectors = msi_config->users[idx].num_vectors;
721c2498839Sstsp 			*base_vector =  msi_config->users[idx].base_vector;
722c2498839Sstsp 			*user_base_data = *base_vector + sc->msi_data_start;
723c2498839Sstsp 
724c2498839Sstsp 			DPRINTF("%s: MSI assignment %s num_vectors %d "
725c2498839Sstsp 			    "user_base_data %u base_vector %u\n", __func__,
726c2498839Sstsp 			    user_name, *num_vectors, *user_base_data,
727c2498839Sstsp 			    *base_vector);
728c2498839Sstsp 			return 0;
729c2498839Sstsp 		}
730c2498839Sstsp 	}
731c2498839Sstsp 
732c2498839Sstsp 	DPRINTF("%s: Failed to find MSI assignment for %s\n",
733c2498839Sstsp 	    sc->sc_dev.dv_xname, user_name);
7345b068355Sstsp 
735c2498839Sstsp 	return EINVAL;
736c2498839Sstsp }
737c2498839Sstsp 
738c2498839Sstsp void
739c2498839Sstsp qwx_pci_attach(struct device *parent, struct device *self, void *aux)
740c2498839Sstsp {
741c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)self;
742c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
743c2498839Sstsp 	struct ieee80211com *ic = &sc->sc_ic;
744c2498839Sstsp 	struct ifnet *ifp = &ic->ic_if;
745c2498839Sstsp 	uint32_t soc_hw_version_major, soc_hw_version_minor;
746c2498839Sstsp 	struct pci_attach_args *pa = aux;
747c2498839Sstsp 	pci_intr_handle_t ih;
748c2498839Sstsp 	pcireg_t memtype, reg;
749c2498839Sstsp 	const char *intrstr;
750c2498839Sstsp 	int error;
751c2498839Sstsp 	pcireg_t sreg;
752c2498839Sstsp 
753c2498839Sstsp 	sc->sc_dmat = pa->pa_dmat;
754c2498839Sstsp 	psc->sc_pc = pa->pa_pc;
755c2498839Sstsp 	psc->sc_tag = pa->pa_tag;
756c2498839Sstsp 
75751dce3f8Skettenis #ifdef __HAVE_FDT
75851dce3f8Skettenis 	sc->sc_node = PCITAG_NODE(pa->pa_tag);
75951dce3f8Skettenis #endif
76051dce3f8Skettenis 
761c2498839Sstsp 	rw_init(&sc->ioctl_rwl, "qwxioctl");
762c2498839Sstsp 
763c2498839Sstsp 	sreg = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_SUBSYS_ID_REG);
764c2498839Sstsp 	sc->id.bdf_search = ATH11K_BDF_SEARCH_DEFAULT;
765c2498839Sstsp 	sc->id.vendor = PCI_VENDOR(pa->pa_id);
766c2498839Sstsp 	sc->id.device = PCI_PRODUCT(pa->pa_id);
767c2498839Sstsp 	sc->id.subsystem_vendor = PCI_VENDOR(sreg);
768c2498839Sstsp 	sc->id.subsystem_device = PCI_PRODUCT(sreg);
769c2498839Sstsp 
770c2498839Sstsp 	strlcpy(sc->sc_bus_str, "pci", sizeof(sc->sc_bus_str));
771c2498839Sstsp 
772c2498839Sstsp 	sc->ops.read32 = qwx_pcic_read32;
773c2498839Sstsp 	sc->ops.write32 = qwx_pcic_write32;
774c2498839Sstsp 	sc->ops.start = qwx_pci_start;
775c2498839Sstsp 	sc->ops.stop = qwx_pci_stop;
776c2498839Sstsp 	sc->ops.power_up = qwx_pci_power_up;
777c2498839Sstsp 	sc->ops.power_down = qwx_pci_power_down;
778c2498839Sstsp 	sc->ops.submit_xfer = qwx_mhi_submit_xfer;
779c2498839Sstsp 	sc->ops.irq_enable = qwx_pcic_ext_irq_enable;
780c2498839Sstsp 	sc->ops.irq_disable = qwx_pcic_ext_irq_disable;
781c2498839Sstsp 	sc->ops.map_service_to_pipe = qwx_pcic_map_service_to_pipe;
782c2498839Sstsp 	sc->ops.get_user_msi_vector = qwx_pcic_get_user_msi_vector;
783c2498839Sstsp 
784c2498839Sstsp 	if (pci_get_capability(psc->sc_pc, psc->sc_tag, PCI_CAP_PCIEXPRESS,
785c2498839Sstsp 	    &psc->sc_cap_off, NULL) == 0) {
786c2498839Sstsp 		printf(": can't find PCIe capability structure\n");
787c2498839Sstsp 		return;
788c2498839Sstsp 	}
789c2498839Sstsp 
790c2498839Sstsp 	if (pci_get_capability(psc->sc_pc, psc->sc_tag, PCI_CAP_MSI,
791c2498839Sstsp 	    &psc->sc_msi_off, &psc->sc_msi_cap) == 0) {
792c2498839Sstsp 		printf(": can't find MSI capability structure\n");
793c2498839Sstsp 		return;
794c2498839Sstsp 	}
795c2498839Sstsp 
796c2498839Sstsp 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
797c2498839Sstsp 	reg |= PCI_COMMAND_MASTER_ENABLE;
798c2498839Sstsp 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
799c2498839Sstsp 
800c2498839Sstsp 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
801c2498839Sstsp 	if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0,
802c2498839Sstsp 	    &psc->sc_st, &psc->sc_sh, &psc->sc_map, &psc->sc_mapsize, 0)) {
803c2498839Sstsp 		printf(": can't map mem space\n");
804c2498839Sstsp 		return;
805c2498839Sstsp 	}
806c2498839Sstsp 
807c2498839Sstsp 	sc->mem = psc->sc_map;
808c2498839Sstsp 
8095b068355Sstsp 	sc->num_msivec = 32;
8105b068355Sstsp 	if (pci_intr_enable_msivec(pa, sc->num_msivec) != 0) {
8115b068355Sstsp 		sc->num_msivec = 1;
8125b068355Sstsp 		if (pci_intr_map_msi(pa, &ih) != 0) {
813c2498839Sstsp 			printf(": can't map interrupt\n");
814c2498839Sstsp 			return;
815c2498839Sstsp 		}
8165b068355Sstsp 		clear_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags);
8175b068355Sstsp 	} else {
8185b068355Sstsp 		if (pci_intr_map_msivec(pa, 0, &ih) != 0 &&
8195b068355Sstsp 		    pci_intr_map_msi(pa, &ih) != 0) {
8205b068355Sstsp 			printf(": can't map interrupt\n");
8215b068355Sstsp 			return;
8225b068355Sstsp 		}
8235b068355Sstsp 		set_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags);
8245b068355Sstsp 		psc->mhi_irq[MHI_ER_CTRL] = 1;
8255b068355Sstsp 		psc->mhi_irq[MHI_ER_DATA] = 2;
8265b068355Sstsp 	}
827c2498839Sstsp 
828c2498839Sstsp 	intrstr = pci_intr_string(psc->sc_pc, ih);
8295b068355Sstsp 	snprintf(psc->sc_ivname[0], sizeof(psc->sc_ivname[0]), "%s:bhi",
8305b068355Sstsp 	    sc->sc_dev.dv_xname);
8315b068355Sstsp 	psc->sc_ih[0] = pci_intr_establish(psc->sc_pc, ih, IPL_NET,
8325b068355Sstsp 	    qwx_pci_intr, psc, psc->sc_ivname[0]);
8335b068355Sstsp 	if (psc->sc_ih[0] == NULL) {
834c2498839Sstsp 		printf(": can't establish interrupt");
835c2498839Sstsp 		if (intrstr != NULL)
836c2498839Sstsp 			printf(" at %s", intrstr);
837c2498839Sstsp 		printf("\n");
838c2498839Sstsp 		return;
839c2498839Sstsp 	}
840c2498839Sstsp 	printf(": %s\n", intrstr);
841c2498839Sstsp 
8425b068355Sstsp 	if (test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags)) {
8435b068355Sstsp 		int msivec;
8445b068355Sstsp 
8455b068355Sstsp 		msivec = psc->mhi_irq[MHI_ER_CTRL];
8465b068355Sstsp 		if (pci_intr_map_msivec(pa, msivec, &ih) != 0 &&
8475b068355Sstsp 		    pci_intr_map_msi(pa, &ih) != 0) {
8485b068355Sstsp 			printf(": can't map interrupt\n");
8495b068355Sstsp 			return;
8505b068355Sstsp 		}
8515b068355Sstsp 		snprintf(psc->sc_ivname[msivec],
8525b068355Sstsp 		    sizeof(psc->sc_ivname[msivec]),
8535b068355Sstsp 		    "%s:mhic", sc->sc_dev.dv_xname);
8545b068355Sstsp 		psc->sc_ih[msivec] = pci_intr_establish(psc->sc_pc, ih,
8555b068355Sstsp 		    IPL_NET, qwx_pci_intr_mhi_ctrl, psc,
8565b068355Sstsp 		    psc->sc_ivname[msivec]);
8575b068355Sstsp 		if (psc->sc_ih[msivec] == NULL) {
8585b068355Sstsp 			printf("%s: can't establish interrupt\n",
8595b068355Sstsp 			    sc->sc_dev.dv_xname);
8605b068355Sstsp 			return;
8615b068355Sstsp 		}
8625b068355Sstsp 
8635b068355Sstsp 		msivec = psc->mhi_irq[MHI_ER_DATA];
8645b068355Sstsp 		if (pci_intr_map_msivec(pa, msivec, &ih) != 0 &&
8655b068355Sstsp 		    pci_intr_map_msi(pa, &ih) != 0) {
8665b068355Sstsp 			printf(": can't map interrupt\n");
8675b068355Sstsp 			return;
8685b068355Sstsp 		}
8695b068355Sstsp 		snprintf(psc->sc_ivname[msivec],
8705b068355Sstsp 		    sizeof(psc->sc_ivname[msivec]),
8715b068355Sstsp 		    "%s:mhid", sc->sc_dev.dv_xname);
8725b068355Sstsp 		psc->sc_ih[msivec] = pci_intr_establish(psc->sc_pc, ih,
8735b068355Sstsp 		    IPL_NET, qwx_pci_intr_mhi_data, psc,
8745b068355Sstsp 		    psc->sc_ivname[msivec]);
8755b068355Sstsp 		if (psc->sc_ih[msivec] == NULL) {
8765b068355Sstsp 			printf("%s: can't establish interrupt\n",
8775b068355Sstsp 			    sc->sc_dev.dv_xname);
8785b068355Sstsp 			return;
8795b068355Sstsp 		}
8805b068355Sstsp 	}
8815b068355Sstsp 
882c2498839Sstsp 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
883c2498839Sstsp 
884*d58514d7Skevlo 	/* register PCI ops */
885*d58514d7Skevlo 	psc->sc_pci_ops = &qwx_pci_ops_qca6390;
886*d58514d7Skevlo 
887c2498839Sstsp 	switch (PCI_PRODUCT(pa->pa_id)) {
888c2498839Sstsp 	case PCI_PRODUCT_QUALCOMM_QCA6390:
889c2498839Sstsp 		qwx_pci_read_hw_version(sc, &soc_hw_version_major,
890c2498839Sstsp 		    &soc_hw_version_minor);
891c2498839Sstsp 		switch (soc_hw_version_major) {
892c2498839Sstsp 		case 2:
893c2498839Sstsp 			sc->sc_hw_rev = ATH11K_HW_QCA6390_HW20;
894c2498839Sstsp 			break;
895c2498839Sstsp 		default:
896c2498839Sstsp 			printf(": unsupported QCA6390 SOC version: %d %d\n",
897c2498839Sstsp 				soc_hw_version_major, soc_hw_version_minor);
898c2498839Sstsp 			return;
899c2498839Sstsp 		}
900c2498839Sstsp 
901c2498839Sstsp 		psc->max_chan = QWX_MHI_CONFIG_QCA6390_MAX_CHANNELS;
902c2498839Sstsp 		break;
903c2498839Sstsp 	case PCI_PRODUCT_QUALCOMM_QCN9074:
904*d58514d7Skevlo 		psc->sc_pci_ops = &qwx_pci_ops_qcn9074;
905c2498839Sstsp 		sc->sc_hw_rev = ATH11K_HW_QCN9074_HW10;
906c2498839Sstsp 		psc->max_chan = QWX_MHI_CONFIG_QCA9074_MAX_CHANNELS;
907c2498839Sstsp 		break;
908c2498839Sstsp 	case PCI_PRODUCT_QUALCOMM_QCNFA765:
909c2498839Sstsp 		sc->id.bdf_search = ATH11K_BDF_SEARCH_BUS_AND_BOARD;
910c2498839Sstsp 		qwx_pci_read_hw_version(sc, &soc_hw_version_major,
911c2498839Sstsp 		    &soc_hw_version_minor);
912c2498839Sstsp 		switch (soc_hw_version_major) {
913c2498839Sstsp 		case 2:
914c2498839Sstsp 			switch (soc_hw_version_minor) {
915c2498839Sstsp 			case 0x00:
916c2498839Sstsp 			case 0x01:
917c2498839Sstsp 				sc->sc_hw_rev = ATH11K_HW_WCN6855_HW20;
918c2498839Sstsp 				break;
919c2498839Sstsp 			case 0x10:
920c2498839Sstsp 			case 0x11:
921c2498839Sstsp 				sc->sc_hw_rev = ATH11K_HW_WCN6855_HW21;
922c2498839Sstsp 				break;
923c2498839Sstsp 			default:
924c2498839Sstsp 				goto unsupported_wcn6855_soc;
925c2498839Sstsp 			}
926c2498839Sstsp 			break;
927c2498839Sstsp 		default:
928c2498839Sstsp unsupported_wcn6855_soc:
929c2498839Sstsp 			printf(": unsupported WCN6855 SOC version: %d %d\n",
930c2498839Sstsp 				soc_hw_version_major, soc_hw_version_minor);
931c2498839Sstsp 			return;
932c2498839Sstsp 		}
933c2498839Sstsp 
934c2498839Sstsp 		psc->max_chan = QWX_MHI_CONFIG_QCA6390_MAX_CHANNELS;
935c2498839Sstsp 		break;
936c2498839Sstsp 	default:
937c2498839Sstsp 		printf(": unsupported chip\n");
938c2498839Sstsp 		return;
939c2498839Sstsp 	}
940c2498839Sstsp 
9415b068355Sstsp 	error = qwx_pcic_init_msi_config(sc);
9425b068355Sstsp 	if (error)
943c2498839Sstsp 		goto err_pci_free_region;
9445b068355Sstsp 
945c2498839Sstsp 	error = qwx_pci_alloc_msi(sc);
946c2498839Sstsp 	if (error) {
947c2498839Sstsp 		printf("%s: failed to enable msi: %d\n", sc->sc_dev.dv_xname,
948c2498839Sstsp 		    error);
949c2498839Sstsp 		goto err_pci_free_region;
950c2498839Sstsp 	}
951c2498839Sstsp 
952c2498839Sstsp 	error = qwx_init_hw_params(sc);
953c2498839Sstsp 	if (error)
954c2498839Sstsp 		goto err_pci_disable_msi;
955c2498839Sstsp 
956c2498839Sstsp 	psc->chan_ctxt = qwx_dmamem_alloc(sc->sc_dmat,
957c2498839Sstsp 	    sizeof(struct qwx_mhi_chan_ctxt) * psc->max_chan, 0);
958c2498839Sstsp 	if (psc->chan_ctxt == NULL) {
959c2498839Sstsp 		printf("%s: could not allocate channel context array\n",
960c2498839Sstsp 		    sc->sc_dev.dv_xname);
961c2498839Sstsp 		goto err_pci_disable_msi;
962c2498839Sstsp 	}
963c2498839Sstsp 
964c2498839Sstsp 	if (psc->sc_pci_ops->alloc_xfer_rings(psc)) {
965c2498839Sstsp 		printf("%s: could not allocate transfer rings\n",
966c2498839Sstsp 		    sc->sc_dev.dv_xname);
967c2498839Sstsp 		goto err_pci_free_chan_ctxt;
968c2498839Sstsp 	}
969c2498839Sstsp 
970c2498839Sstsp 	psc->event_ctxt = qwx_dmamem_alloc(sc->sc_dmat,
971c2498839Sstsp 	    sizeof(struct qwx_mhi_event_ctxt) * QWX_NUM_EVENT_CTX, 0);
972c2498839Sstsp 	if (psc->event_ctxt == NULL) {
973c2498839Sstsp 		printf("%s: could not allocate event context array\n",
974c2498839Sstsp 		    sc->sc_dev.dv_xname);
975c2498839Sstsp 		goto err_pci_free_xfer_rings;
976c2498839Sstsp 	}
977c2498839Sstsp 
978c2498839Sstsp 	if (qwx_pci_alloc_event_rings(psc)) {
979c2498839Sstsp 		printf("%s: could not allocate event rings\n",
980c2498839Sstsp 		    sc->sc_dev.dv_xname);
981c2498839Sstsp 		goto err_pci_free_event_ctxt;
982c2498839Sstsp 	}
983c2498839Sstsp 
984c2498839Sstsp 	psc->cmd_ctxt = qwx_dmamem_alloc(sc->sc_dmat,
985c2498839Sstsp 	    sizeof(struct qwx_mhi_cmd_ctxt), 0);
986c2498839Sstsp 	if (psc->cmd_ctxt == NULL) {
987c2498839Sstsp 		printf("%s: could not allocate command context array\n",
988c2498839Sstsp 		    sc->sc_dev.dv_xname);
989c2498839Sstsp 		goto err_pci_free_event_rings;
990c2498839Sstsp 	}
991c2498839Sstsp 
992c2498839Sstsp 	if (qwx_pci_init_cmd_ring(sc, &psc->cmd_ring))  {
993c2498839Sstsp 		printf("%s: could not allocate command ring\n",
994c2498839Sstsp 		    sc->sc_dev.dv_xname);
995c2498839Sstsp 		goto err_pci_free_cmd_ctxt;
996c2498839Sstsp 	}
997c2498839Sstsp 
998c2498839Sstsp 	error = qwx_mhi_register(sc);
999c2498839Sstsp 	if (error) {
1000c2498839Sstsp 		printf(": failed to register mhi: %d\n", error);
1001c2498839Sstsp 		goto err_pci_free_cmd_ring;
1002c2498839Sstsp 	}
1003c2498839Sstsp 
1004c2498839Sstsp 	error = qwx_hal_srng_init(sc);
1005c2498839Sstsp 	if (error)
1006c2498839Sstsp 		goto err_mhi_unregister;
1007c2498839Sstsp 
1008c2498839Sstsp 	error = qwx_ce_alloc_pipes(sc);
1009c2498839Sstsp 	if (error) {
1010c2498839Sstsp 		printf(": failed to allocate ce pipes: %d\n", error);
1011c2498839Sstsp 		goto err_hal_srng_deinit;
1012c2498839Sstsp 	}
1013c2498839Sstsp 
1014c2498839Sstsp 	sc->sc_nswq = taskq_create("qwxns", 1, IPL_NET, 0);
1015c2498839Sstsp 	if (sc->sc_nswq == NULL)
10165b068355Sstsp 		goto err_ce_free;
1017c2498839Sstsp 
1018c2498839Sstsp 	qwx_pci_init_qmi_ce_config(sc);
1019c2498839Sstsp 
10205b068355Sstsp 	error = qwx_pcic_config_irq(sc, pa);
10215b068355Sstsp 	if (error) {
10225b068355Sstsp 		printf("%s: failed to config irq: %d\n",
10235b068355Sstsp 		    sc->sc_dev.dv_xname, error);
1024c2498839Sstsp 		goto err_ce_free;
1025c2498839Sstsp 	}
10265b068355Sstsp #if notyet
1027c2498839Sstsp 	ret = ath11k_pci_set_irq_affinity_hint(ab_pci, cpumask_of(0));
1028c2498839Sstsp 	if (ret) {
1029c2498839Sstsp 		ath11k_err(ab, "failed to set irq affinity %d\n", ret);
1030c2498839Sstsp 		goto err_free_irq;
1031c2498839Sstsp 	}
1032c2498839Sstsp 
1033c2498839Sstsp 	/* kernel may allocate a dummy vector before request_irq and
1034c2498839Sstsp 	 * then allocate a real vector when request_irq is called.
1035c2498839Sstsp 	 * So get msi_data here again to avoid spurious interrupt
1036c2498839Sstsp 	 * as msi_data will configured to srngs.
1037c2498839Sstsp 	 */
1038c2498839Sstsp 	ret = ath11k_pci_config_msi_data(ab_pci);
1039c2498839Sstsp 	if (ret) {
1040c2498839Sstsp 		ath11k_err(ab, "failed to config msi_data: %d\n", ret);
1041c2498839Sstsp 		goto err_irq_affinity_cleanup;
1042c2498839Sstsp 	}
1043c2498839Sstsp #endif
104481cacbbeSstsp #ifdef QWX_DEBUG
1045c2498839Sstsp 	task_set(&psc->rddm_task, qwx_rddm_task, psc);
104681cacbbeSstsp #endif
1047c2498839Sstsp 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
1048c2498839Sstsp 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
1049c2498839Sstsp 	ic->ic_state = IEEE80211_S_INIT;
1050c2498839Sstsp 
1051c2498839Sstsp 	/* Set device capabilities. */
1052c2498839Sstsp 	ic->ic_caps =
1053c2498839Sstsp #if 0
1054c2498839Sstsp 	    IEEE80211_C_QOS | IEEE80211_C_TX_AMPDU | /* A-MPDU */
1055c2498839Sstsp #endif
1056c2498839Sstsp 	    IEEE80211_C_ADDBA_OFFLOAD | /* device sends ADDBA/DELBA frames */
1057c2498839Sstsp 	    IEEE80211_C_WEP |		/* WEP */
1058c2498839Sstsp 	    IEEE80211_C_RSN |		/* WPA/RSN */
1059c2498839Sstsp 	    IEEE80211_C_SCANALL |	/* device scans all channels at once */
1060c2498839Sstsp 	    IEEE80211_C_SCANALLBAND |	/* device scans all bands at once */
1061c2498839Sstsp #if 0
1062c2498839Sstsp 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
1063c2498839Sstsp #endif
1064c2498839Sstsp 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
1065c2498839Sstsp 	    IEEE80211_C_SHPREAMBLE;	/* short preamble supported */
1066c2498839Sstsp 
1067c2498839Sstsp 	ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_std_rateset_11a;
1068c2498839Sstsp 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
1069c2498839Sstsp 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
1070c2498839Sstsp 
1071c2498839Sstsp 	/* IBSS channel undefined for now. */
1072c2498839Sstsp 	ic->ic_ibss_chan = &ic->ic_channels[1];
1073c2498839Sstsp 
1074c2498839Sstsp 	ifp->if_softc = sc;
1075c2498839Sstsp 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1076c2498839Sstsp 	ifp->if_ioctl = qwx_ioctl;
1077c2498839Sstsp 	ifp->if_start = qwx_start;
1078c2498839Sstsp 	ifp->if_watchdog = qwx_watchdog;
1079c2498839Sstsp 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
1080c2498839Sstsp 	if_attach(ifp);
1081c2498839Sstsp 	ieee80211_ifattach(ifp);
1082c2498839Sstsp 	ieee80211_media_init(ifp, qwx_media_change, ieee80211_media_status);
1083c2498839Sstsp 
1084c2498839Sstsp 	ic->ic_node_alloc = qwx_node_alloc;
1085c2498839Sstsp 
1086c2498839Sstsp 	/* Override 802.11 state transition machine. */
1087c2498839Sstsp 	sc->sc_newstate = ic->ic_newstate;
1088c2498839Sstsp 	ic->ic_newstate = qwx_newstate;
1089fc05d6c1Sstsp 	ic->ic_set_key = qwx_set_key;
1090fc05d6c1Sstsp 	ic->ic_delete_key = qwx_delete_key;
1091c2498839Sstsp #if 0
1092c2498839Sstsp 	ic->ic_updatechan = qwx_updatechan;
1093c2498839Sstsp 	ic->ic_updateprot = qwx_updateprot;
1094c2498839Sstsp 	ic->ic_updateslot = qwx_updateslot;
1095c2498839Sstsp 	ic->ic_updateedca = qwx_updateedca;
1096c2498839Sstsp 	ic->ic_updatedtim = qwx_updatedtim;
1097c2498839Sstsp #endif
1098c2498839Sstsp 	/*
1099c2498839Sstsp 	 * We cannot read the MAC address without loading the
1100c2498839Sstsp 	 * firmware from disk. Postpone until mountroot is done.
1101c2498839Sstsp 	 */
1102c2498839Sstsp 	config_mountroot(self, qwx_pci_attach_hook);
1103c2498839Sstsp 	return;
1104c2498839Sstsp 
11055b068355Sstsp err_ce_free:
11065b068355Sstsp 	qwx_ce_free_pipes(sc);
1107c2498839Sstsp err_hal_srng_deinit:
1108c2498839Sstsp err_mhi_unregister:
1109c2498839Sstsp err_pci_free_cmd_ring:
1110c2498839Sstsp 	qwx_pci_free_cmd_ring(psc);
1111c2498839Sstsp err_pci_free_cmd_ctxt:
1112c2498839Sstsp 	qwx_dmamem_free(sc->sc_dmat, psc->cmd_ctxt);
1113c2498839Sstsp 	psc->cmd_ctxt = NULL;
1114c2498839Sstsp err_pci_free_event_rings:
1115c2498839Sstsp 	qwx_pci_free_event_rings(psc);
1116c2498839Sstsp err_pci_free_event_ctxt:
1117c2498839Sstsp 	qwx_dmamem_free(sc->sc_dmat, psc->event_ctxt);
1118c2498839Sstsp 	psc->event_ctxt = NULL;
1119c2498839Sstsp err_pci_free_xfer_rings:
1120c2498839Sstsp 	qwx_pci_free_xfer_rings(psc);
1121c2498839Sstsp err_pci_free_chan_ctxt:
1122c2498839Sstsp 	qwx_dmamem_free(sc->sc_dmat, psc->chan_ctxt);
1123c2498839Sstsp 	psc->chan_ctxt = NULL;
1124c2498839Sstsp err_pci_disable_msi:
1125c2498839Sstsp err_pci_free_region:
11265b068355Sstsp 	pci_intr_disestablish(psc->sc_pc, psc->sc_ih[0]);
1127c2498839Sstsp 	return;
1128c2498839Sstsp }
1129c2498839Sstsp 
1130c2498839Sstsp int
1131c2498839Sstsp qwx_pci_detach(struct device *self, int flags)
1132c2498839Sstsp {
1133c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)self;
1134c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1135c2498839Sstsp 
11365b068355Sstsp 	if (psc->sc_ih[0]) {
11375b068355Sstsp 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih[0]);
11385b068355Sstsp 		psc->sc_ih[0] = NULL;
1139c2498839Sstsp 	}
1140c2498839Sstsp 
1141c2498839Sstsp 	qwx_detach(sc);
1142c2498839Sstsp 
1143c2498839Sstsp 	qwx_pci_free_event_rings(psc);
1144c2498839Sstsp 	qwx_pci_free_xfer_rings(psc);
1145c2498839Sstsp 	qwx_pci_free_cmd_ring(psc);
1146c2498839Sstsp 
1147c2498839Sstsp 	if (psc->event_ctxt) {
1148c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, psc->event_ctxt);
1149c2498839Sstsp 		psc->event_ctxt = NULL;
1150c2498839Sstsp 	}
1151c2498839Sstsp 	if (psc->chan_ctxt) {
1152c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, psc->chan_ctxt);
1153c2498839Sstsp 		psc->chan_ctxt = NULL;
1154c2498839Sstsp 	}
1155c2498839Sstsp 	if (psc->cmd_ctxt) {
1156c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, psc->cmd_ctxt);
1157c2498839Sstsp 		psc->cmd_ctxt = NULL;
1158c2498839Sstsp 	}
1159c2498839Sstsp 
1160c2498839Sstsp 	if (psc->amss_data) {
1161c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, psc->amss_data);
1162c2498839Sstsp 		psc->amss_data = NULL;
1163c2498839Sstsp 	}
1164c2498839Sstsp 	if (psc->amss_vec) {
1165c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, psc->amss_vec);
1166c2498839Sstsp 		psc->amss_vec = NULL;
1167c2498839Sstsp 	}
1168c2498839Sstsp 
1169c2498839Sstsp 	return 0;
1170c2498839Sstsp }
1171c2498839Sstsp 
1172c2498839Sstsp void
1173c2498839Sstsp qwx_pci_attach_hook(struct device *self)
1174c2498839Sstsp {
1175c2498839Sstsp 	struct qwx_softc *sc = (void *)self;
1176c2498839Sstsp 	int s = splnet();
1177c2498839Sstsp 
1178c2498839Sstsp 	qwx_attach(sc);
1179c2498839Sstsp 
1180c2498839Sstsp 	splx(s);
1181c2498839Sstsp }
1182c2498839Sstsp 
1183c2498839Sstsp void
1184c2498839Sstsp qwx_pci_free_xfer_rings(struct qwx_pci_softc *psc)
1185c2498839Sstsp {
1186c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1187c2498839Sstsp 	int i;
1188c2498839Sstsp 
1189c2498839Sstsp 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
1190c2498839Sstsp 		struct qwx_pci_xfer_ring *ring = &psc->xfer_rings[i];
1191c2498839Sstsp 		if (ring->dmamem) {
1192c2498839Sstsp 			qwx_dmamem_free(sc->sc_dmat, ring->dmamem);
1193c2498839Sstsp 			ring->dmamem = NULL;
1194c2498839Sstsp 		}
1195c2498839Sstsp 		memset(ring, 0, sizeof(*ring));
1196c2498839Sstsp 	}
1197c2498839Sstsp }
1198c2498839Sstsp 
1199c2498839Sstsp int
1200c2498839Sstsp qwx_pci_alloc_xfer_ring(struct qwx_softc *sc, struct qwx_pci_xfer_ring *ring,
1201c2498839Sstsp     uint32_t id, uint32_t direction, uint32_t event_ring_index,
1202c2498839Sstsp     size_t num_elements)
1203c2498839Sstsp {
1204c2498839Sstsp 	bus_size_t size;
1205c2498839Sstsp 	int i, err;
1206c2498839Sstsp 
1207c2498839Sstsp 	memset(ring, 0, sizeof(*ring));
1208c2498839Sstsp 
1209c2498839Sstsp 	size = sizeof(struct qwx_mhi_ring_element) * num_elements;
1210c2498839Sstsp 	/* Hardware requires that rings are aligned to ring size. */
1211c2498839Sstsp 	ring->dmamem = qwx_dmamem_alloc(sc->sc_dmat, size, size);
1212c2498839Sstsp 	if (ring->dmamem == NULL)
1213c2498839Sstsp 		return ENOMEM;
1214c2498839Sstsp 
1215c2498839Sstsp 	ring->size = size;
1216c2498839Sstsp 	ring->mhi_chan_id = id;
1217c2498839Sstsp 	ring->mhi_chan_state = MHI_CH_STATE_DISABLED;
1218c2498839Sstsp 	ring->mhi_chan_direction = direction;
1219c2498839Sstsp 	ring->mhi_chan_event_ring_index = event_ring_index;
1220c2498839Sstsp 	ring->num_elements = num_elements;
1221c2498839Sstsp 
1222c2498839Sstsp 	memset(ring->data, 0, sizeof(ring->data));
1223c2498839Sstsp 	for (i = 0; i < ring->num_elements; i++) {
1224c2498839Sstsp 		struct qwx_xfer_data *xfer = &ring->data[i];
1225c2498839Sstsp 
1226c2498839Sstsp 		err = bus_dmamap_create(sc->sc_dmat, QWX_PCI_XFER_MAX_DATA_SIZE,
1227c2498839Sstsp 		    1, QWX_PCI_XFER_MAX_DATA_SIZE, 0, BUS_DMA_NOWAIT,
1228c2498839Sstsp 		    &xfer->map);
1229c2498839Sstsp 		if (err) {
1230c2498839Sstsp 			printf("%s: could not create xfer DMA map\n",
1231c2498839Sstsp 			    sc->sc_dev.dv_xname);
1232c2498839Sstsp 			goto fail;
1233c2498839Sstsp 		}
1234c2498839Sstsp 
1235c2498839Sstsp 		if (direction == MHI_CHAN_TYPE_INBOUND) {
1236c2498839Sstsp 			struct mbuf *m;
1237c2498839Sstsp 
1238c2498839Sstsp 			m = m_gethdr(M_DONTWAIT, MT_DATA);
1239c2498839Sstsp 			if (m == NULL) {
1240c2498839Sstsp 				err = ENOBUFS;
1241c2498839Sstsp 				goto fail;
1242c2498839Sstsp 			}
1243c2498839Sstsp 
1244c2498839Sstsp 			MCLGETL(m, M_DONTWAIT, QWX_PCI_XFER_MAX_DATA_SIZE);
1245c2498839Sstsp 			if ((m->m_flags & M_EXT) == 0) {
1246c2498839Sstsp 				m_freem(m);
1247c2498839Sstsp 				err = ENOBUFS;
1248c2498839Sstsp 				goto fail;
1249c2498839Sstsp 			}
1250c2498839Sstsp 
1251c2498839Sstsp 			m->m_len = m->m_pkthdr.len = QWX_PCI_XFER_MAX_DATA_SIZE;
1252c2498839Sstsp 			err = bus_dmamap_load_mbuf(sc->sc_dmat, xfer->map,
1253c2498839Sstsp 			    m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1254c2498839Sstsp 			if (err) {
1255c2498839Sstsp 				printf("%s: can't map mbuf (error %d)\n",
1256c2498839Sstsp 				    sc->sc_dev.dv_xname, err);
1257c2498839Sstsp 				m_freem(m);
1258c2498839Sstsp 				goto fail;
1259c2498839Sstsp 			}
1260c2498839Sstsp 
1261c2498839Sstsp 			bus_dmamap_sync(sc->sc_dmat, xfer->map, 0,
1262c2498839Sstsp 			    QWX_PCI_XFER_MAX_DATA_SIZE, BUS_DMASYNC_PREREAD);
1263c2498839Sstsp 			xfer->m = m;
1264c2498839Sstsp 		}
1265c2498839Sstsp 	}
1266c2498839Sstsp 
1267c2498839Sstsp 	return 0;
1268c2498839Sstsp fail:
1269c2498839Sstsp 	for (i = 0; i < ring->num_elements; i++) {
1270c2498839Sstsp 		struct qwx_xfer_data *xfer = &ring->data[i];
1271c2498839Sstsp 
1272c2498839Sstsp 		if (xfer->map) {
1273c2498839Sstsp 			bus_dmamap_sync(sc->sc_dmat, xfer->map, 0,
1274c2498839Sstsp 			    xfer->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1275c2498839Sstsp 			bus_dmamap_unload(sc->sc_dmat, xfer->map);
1276c2498839Sstsp 			bus_dmamap_destroy(sc->sc_dmat, xfer->map);
1277c2498839Sstsp 			xfer->map = NULL;
1278c2498839Sstsp 		}
1279c2498839Sstsp 
1280c2498839Sstsp 		if (xfer->m) {
1281c2498839Sstsp 			m_freem(xfer->m);
1282c2498839Sstsp 			xfer->m = NULL;
1283c2498839Sstsp 		}
1284c2498839Sstsp 	}
1285c2498839Sstsp 	return 1;
1286c2498839Sstsp }
1287c2498839Sstsp 
1288c2498839Sstsp int
1289c2498839Sstsp qwx_pci_alloc_xfer_rings_qca6390(struct qwx_pci_softc *psc)
1290c2498839Sstsp {
1291c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1292c2498839Sstsp 	int ret;
1293c2498839Sstsp 
1294c2498839Sstsp 	ret = qwx_pci_alloc_xfer_ring(sc,
1295c2498839Sstsp 	    &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_OUTBOUND],
1296c2498839Sstsp 	    20, MHI_CHAN_TYPE_OUTBOUND, 1, 64);
1297c2498839Sstsp 	if (ret)
1298c2498839Sstsp 		goto fail;
1299c2498839Sstsp 
1300c2498839Sstsp 	ret = qwx_pci_alloc_xfer_ring(sc,
1301c2498839Sstsp 	    &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_INBOUND],
1302c2498839Sstsp 	    21, MHI_CHAN_TYPE_INBOUND, 1, 64);
1303c2498839Sstsp 	if (ret)
1304c2498839Sstsp 		goto fail;
1305c2498839Sstsp 
1306c2498839Sstsp 	return 0;
1307c2498839Sstsp fail:
1308c2498839Sstsp 	qwx_pci_free_xfer_rings(psc);
1309c2498839Sstsp 	return ret;
1310c2498839Sstsp }
1311c2498839Sstsp 
1312c2498839Sstsp int
1313c2498839Sstsp qwx_pci_alloc_xfer_rings_qcn9074(struct qwx_pci_softc *psc)
1314c2498839Sstsp {
1315c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1316c2498839Sstsp 	int ret;
1317c2498839Sstsp 
1318c2498839Sstsp 	ret = qwx_pci_alloc_xfer_ring(sc,
1319c2498839Sstsp 	    &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_OUTBOUND],
1320c2498839Sstsp 	    20, MHI_CHAN_TYPE_OUTBOUND, 1, 32);
1321c2498839Sstsp 	if (ret)
1322c2498839Sstsp 		goto fail;
1323c2498839Sstsp 
1324c2498839Sstsp 	ret = qwx_pci_alloc_xfer_ring(sc,
1325c2498839Sstsp 	    &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_INBOUND],
1326c2498839Sstsp 	    21, MHI_CHAN_TYPE_INBOUND, 1, 32);
1327c2498839Sstsp 	if (ret)
1328c2498839Sstsp 		goto fail;
1329c2498839Sstsp 
1330c2498839Sstsp 	return 0;
1331c2498839Sstsp fail:
1332c2498839Sstsp 	qwx_pci_free_xfer_rings(psc);
1333c2498839Sstsp 	return ret;
1334c2498839Sstsp }
1335c2498839Sstsp 
1336c2498839Sstsp void
1337c2498839Sstsp qwx_pci_free_event_rings(struct qwx_pci_softc *psc)
1338c2498839Sstsp {
1339c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1340c2498839Sstsp 	int i;
1341c2498839Sstsp 
1342c2498839Sstsp 	for (i = 0; i < nitems(psc->event_rings); i++) {
1343c2498839Sstsp 		struct qwx_pci_event_ring *ring = &psc->event_rings[i];
1344c2498839Sstsp 		if (ring->dmamem) {
1345c2498839Sstsp 			qwx_dmamem_free(sc->sc_dmat, ring->dmamem);
1346c2498839Sstsp 			ring->dmamem = NULL;
1347c2498839Sstsp 		}
1348c2498839Sstsp 		memset(ring, 0, sizeof(*ring));
1349c2498839Sstsp 	}
1350c2498839Sstsp }
1351c2498839Sstsp 
1352c2498839Sstsp int
1353c2498839Sstsp qwx_pci_alloc_event_ring(struct qwx_softc *sc, struct qwx_pci_event_ring *ring,
1354c2498839Sstsp     uint32_t type, uint32_t irq, uint32_t intmod, size_t num_elements)
1355c2498839Sstsp {
1356c2498839Sstsp 	bus_size_t size;
1357c2498839Sstsp 
1358c2498839Sstsp 	memset(ring, 0, sizeof(*ring));
1359c2498839Sstsp 
1360c2498839Sstsp 	size = sizeof(struct qwx_mhi_ring_element) * num_elements;
1361c2498839Sstsp 	/* Hardware requires that rings are aligned to ring size. */
1362c2498839Sstsp 	ring->dmamem = qwx_dmamem_alloc(sc->sc_dmat, size, size);
1363c2498839Sstsp 	if (ring->dmamem == NULL)
1364c2498839Sstsp 		return ENOMEM;
1365c2498839Sstsp 
1366c2498839Sstsp 	ring->size = size;
1367c2498839Sstsp 	ring->mhi_er_type = type;
1368c2498839Sstsp 	ring->mhi_er_irq = irq;
1369c2498839Sstsp 	ring->mhi_er_irq_moderation_ms = intmod;
1370c2498839Sstsp 	ring->num_elements = num_elements;
1371c2498839Sstsp 	return 0;
1372c2498839Sstsp }
1373c2498839Sstsp 
1374c2498839Sstsp int
1375c2498839Sstsp qwx_pci_alloc_event_rings(struct qwx_pci_softc *psc)
1376c2498839Sstsp {
1377c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1378c2498839Sstsp 	int ret;
1379c2498839Sstsp 
1380c2498839Sstsp 	ret = qwx_pci_alloc_event_ring(sc, &psc->event_rings[0],
13815b068355Sstsp 	    MHI_ER_CTRL, psc->mhi_irq[MHI_ER_CTRL], 0, 32);
1382c2498839Sstsp 	if (ret)
1383c2498839Sstsp 		goto fail;
1384c2498839Sstsp 
1385c2498839Sstsp 	ret = qwx_pci_alloc_event_ring(sc, &psc->event_rings[1],
13865b068355Sstsp 	    MHI_ER_DATA, psc->mhi_irq[MHI_ER_DATA], 1, 256);
1387c2498839Sstsp 	if (ret)
1388c2498839Sstsp 		goto fail;
1389c2498839Sstsp 
1390c2498839Sstsp 	return 0;
1391c2498839Sstsp fail:
1392c2498839Sstsp 	qwx_pci_free_event_rings(psc);
1393c2498839Sstsp 	return ret;
1394c2498839Sstsp }
1395c2498839Sstsp 
1396c2498839Sstsp void
1397c2498839Sstsp qwx_pci_free_cmd_ring(struct qwx_pci_softc *psc)
1398c2498839Sstsp {
1399c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
1400c2498839Sstsp 	struct qwx_pci_cmd_ring *ring = &psc->cmd_ring;
1401c2498839Sstsp 
1402c2498839Sstsp 	if (ring->dmamem)
1403c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, ring->dmamem);
1404c2498839Sstsp 
1405c2498839Sstsp 	memset(ring, 0, sizeof(*ring));
1406c2498839Sstsp }
1407c2498839Sstsp 
1408c2498839Sstsp int
1409c2498839Sstsp qwx_pci_init_cmd_ring(struct qwx_softc *sc, struct qwx_pci_cmd_ring *ring)
1410c2498839Sstsp {
1411c2498839Sstsp 	memset(ring, 0, sizeof(*ring));
1412c2498839Sstsp 
1413c2498839Sstsp 	ring->num_elements = QWX_PCI_CMD_RING_MAX_ELEMENTS;
1414c2498839Sstsp 	ring->size = sizeof(struct qwx_mhi_ring_element) * ring->num_elements;
1415c2498839Sstsp 
1416c2498839Sstsp 	/* Hardware requires that rings are aligned to ring size. */
1417c2498839Sstsp 	ring->dmamem = qwx_dmamem_alloc(sc->sc_dmat, ring->size, ring->size);
1418c2498839Sstsp 	if (ring->dmamem == NULL)
1419c2498839Sstsp 		return ENOMEM;
1420c2498839Sstsp 
1421c2498839Sstsp 	return 0;
1422c2498839Sstsp }
1423c2498839Sstsp 
1424c2498839Sstsp uint32_t
1425c2498839Sstsp qwx_pci_read(struct qwx_softc *sc, uint32_t addr)
1426c2498839Sstsp {
1427c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
1428c2498839Sstsp 
1429c2498839Sstsp 	return (bus_space_read_4(psc->sc_st, psc->sc_sh, addr));
1430c2498839Sstsp }
1431c2498839Sstsp 
1432c2498839Sstsp void
1433c2498839Sstsp qwx_pci_write(struct qwx_softc *sc, uint32_t addr, uint32_t val)
1434c2498839Sstsp {
1435c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
1436c2498839Sstsp 
1437c2498839Sstsp 	bus_space_write_4(psc->sc_st, psc->sc_sh, addr, val);
1438c2498839Sstsp }
1439c2498839Sstsp 
1440c2498839Sstsp void
1441c2498839Sstsp qwx_pci_read_hw_version(struct qwx_softc *sc, uint32_t *major,
1442c2498839Sstsp     uint32_t *minor)
1443c2498839Sstsp {
1444c2498839Sstsp 	uint32_t soc_hw_version;
1445c2498839Sstsp 
1446c2498839Sstsp 	soc_hw_version = qwx_pcic_read32(sc, TCSR_SOC_HW_VERSION);
1447c2498839Sstsp 	*major = FIELD_GET(TCSR_SOC_HW_VERSION_MAJOR_MASK, soc_hw_version);
1448c2498839Sstsp 	*minor = FIELD_GET(TCSR_SOC_HW_VERSION_MINOR_MASK, soc_hw_version);
1449c2498839Sstsp 	DPRINTF("%s: pci tcsr_soc_hw_version major %d minor %d\n",
1450c2498839Sstsp 	    sc->sc_dev.dv_xname, *major, *minor);
1451c2498839Sstsp }
1452c2498839Sstsp 
1453c2498839Sstsp uint32_t
1454c2498839Sstsp qwx_pcic_read32(struct qwx_softc *sc, uint32_t offset)
1455c2498839Sstsp {
1456c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
1457c2498839Sstsp 	int ret = 0;
1458c2498839Sstsp 	uint32_t val;
1459c2498839Sstsp 	bool wakeup_required;
1460c2498839Sstsp 
1461c2498839Sstsp 	/* for offset beyond BAR + 4K - 32, may
1462c2498839Sstsp 	 * need to wakeup the device to access.
1463c2498839Sstsp 	 */
1464c2498839Sstsp 	wakeup_required = test_bit(ATH11K_FLAG_DEVICE_INIT_DONE, sc->sc_flags)
1465c2498839Sstsp 	    && offset >= ATH11K_PCI_ACCESS_ALWAYS_OFF;
1466c2498839Sstsp 	if (wakeup_required && psc->sc_pci_ops->wakeup)
1467c2498839Sstsp 		ret = psc->sc_pci_ops->wakeup(sc);
1468c2498839Sstsp 
1469c2498839Sstsp 	if (offset < ATH11K_PCI_WINDOW_START)
1470c2498839Sstsp 		val = qwx_pci_read(sc, offset);
1471c2498839Sstsp 	else
1472c2498839Sstsp 		val = psc->sc_pci_ops->window_read32(sc, offset);
1473c2498839Sstsp 
1474c2498839Sstsp 	if (wakeup_required && !ret && psc->sc_pci_ops->release)
1475c2498839Sstsp 		psc->sc_pci_ops->release(sc);
1476c2498839Sstsp 
1477c2498839Sstsp 	return val;
1478c2498839Sstsp }
1479c2498839Sstsp 
1480c2498839Sstsp void
1481c2498839Sstsp qwx_pcic_write32(struct qwx_softc *sc, uint32_t offset, uint32_t value)
1482c2498839Sstsp {
1483c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
1484c2498839Sstsp 	int ret = 0;
1485c2498839Sstsp 	bool wakeup_required;
1486c2498839Sstsp 
1487c2498839Sstsp 	/* for offset beyond BAR + 4K - 32, may
1488c2498839Sstsp 	 * need to wakeup the device to access.
1489c2498839Sstsp 	 */
1490c2498839Sstsp 	wakeup_required = test_bit(ATH11K_FLAG_DEVICE_INIT_DONE, sc->sc_flags)
1491c2498839Sstsp 	    && offset >= ATH11K_PCI_ACCESS_ALWAYS_OFF;
1492c2498839Sstsp 	if (wakeup_required && psc->sc_pci_ops->wakeup)
1493c2498839Sstsp 		ret = psc->sc_pci_ops->wakeup(sc);
1494c2498839Sstsp 
1495c2498839Sstsp 	if (offset < ATH11K_PCI_WINDOW_START)
1496c2498839Sstsp 		qwx_pci_write(sc, offset, value);
1497c2498839Sstsp 	else
1498c2498839Sstsp 		psc->sc_pci_ops->window_write32(sc, offset, value);
1499c2498839Sstsp 
1500c2498839Sstsp 	if (wakeup_required && !ret && psc->sc_pci_ops->release)
1501c2498839Sstsp 		psc->sc_pci_ops->release(sc);
1502c2498839Sstsp }
1503c2498839Sstsp 
1504c2498839Sstsp void
1505c2498839Sstsp qwx_pcic_ext_irq_disable(struct qwx_softc *sc)
1506c2498839Sstsp {
1507c2498839Sstsp 	clear_bit(ATH11K_FLAG_EXT_IRQ_ENABLED, sc->sc_flags);
1508c2498839Sstsp 
1509c2498839Sstsp 	/* In case of one MSI vector, we handle irq enable/disable in a
1510c2498839Sstsp 	 * uniform way since we only have one irq
1511c2498839Sstsp 	 */
1512c2498839Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
1513c2498839Sstsp 		return;
1514c2498839Sstsp 
1515e45dcf58Sphessler 	DPRINTF("%s not implemented\n", __func__);
1516c2498839Sstsp }
1517c2498839Sstsp 
1518c2498839Sstsp void
1519c2498839Sstsp qwx_pcic_ext_irq_enable(struct qwx_softc *sc)
1520c2498839Sstsp {
1521c2498839Sstsp 	set_bit(ATH11K_FLAG_EXT_IRQ_ENABLED, sc->sc_flags);
1522c2498839Sstsp 
1523c2498839Sstsp 	/* In case of one MSI vector, we handle irq enable/disable in a
1524c2498839Sstsp 	 * uniform way since we only have one irq
1525c2498839Sstsp 	 */
1526c2498839Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
1527c2498839Sstsp 		return;
1528c2498839Sstsp 
1529e45dcf58Sphessler 	DPRINTF("%s not implemented\n", __func__);
1530c2498839Sstsp }
1531c2498839Sstsp 
1532c2498839Sstsp void
1533c2498839Sstsp qwx_pcic_ce_irq_enable(struct qwx_softc *sc, uint16_t ce_id)
1534c2498839Sstsp {
1535c2498839Sstsp 	/* In case of one MSI vector, we handle irq enable/disable in a
1536c2498839Sstsp 	 * uniform way since we only have one irq
1537c2498839Sstsp 	 */
1538c2498839Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
1539c2498839Sstsp 		return;
1540c2498839Sstsp 
15415b068355Sstsp 	/* OpenBSD PCI stack does not yet implement MSI interrupt masking. */
15425b068355Sstsp 	sc->msi_ce_irqmask |= (1U << ce_id);
1543c2498839Sstsp }
1544c2498839Sstsp 
1545c2498839Sstsp void
1546c2498839Sstsp qwx_pcic_ce_irq_disable(struct qwx_softc *sc, uint16_t ce_id)
1547c2498839Sstsp {
1548c2498839Sstsp 	/* In case of one MSI vector, we handle irq enable/disable in a
1549c2498839Sstsp 	 * uniform way since we only have one irq
1550c2498839Sstsp 	 */
1551c2498839Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
1552c2498839Sstsp 		return;
1553c2498839Sstsp 
15545b068355Sstsp 	/* OpenBSD PCI stack does not yet implement MSI interrupt masking. */
15555b068355Sstsp 	sc->msi_ce_irqmask &= ~(1U << ce_id);
15565b068355Sstsp }
15575b068355Sstsp 
15585b068355Sstsp void
15595b068355Sstsp qwx_pcic_ext_grp_disable(struct qwx_ext_irq_grp *irq_grp)
15605b068355Sstsp {
15615b068355Sstsp 	struct qwx_softc *sc = irq_grp->sc;
15625b068355Sstsp 
15635b068355Sstsp 	/* In case of one MSI vector, we handle irq enable/disable
15645b068355Sstsp 	 * in a uniform way since we only have one irq
15655b068355Sstsp 	 */
15665b068355Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
15675b068355Sstsp 		return;
15685b068355Sstsp }
15695b068355Sstsp 
15705b068355Sstsp int
15715b068355Sstsp qwx_pcic_ext_irq_config(struct qwx_softc *sc, struct pci_attach_args *pa)
15725b068355Sstsp {
15735b068355Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
15745b068355Sstsp 	int i, ret, num_vectors = 0;
15755b068355Sstsp 	uint32_t msi_data_start = 0;
15765b068355Sstsp 	uint32_t base_vector = 0;
15775b068355Sstsp 
15785b068355Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
15795b068355Sstsp 		return 0;
15805b068355Sstsp 
15815b068355Sstsp 	ret = qwx_pcic_get_user_msi_vector(sc, "DP", &num_vectors,
15825b068355Sstsp 	    &msi_data_start, &base_vector);
15835b068355Sstsp 	if (ret < 0)
15845b068355Sstsp 		return ret;
15855b068355Sstsp 
15865b068355Sstsp 	for (i = 0; i < nitems(sc->ext_irq_grp); i++) {
15875b068355Sstsp 		struct qwx_ext_irq_grp *irq_grp = &sc->ext_irq_grp[i];
15885b068355Sstsp 		uint32_t num_irq = 0;
15895b068355Sstsp 
15905b068355Sstsp 		irq_grp->sc = sc;
15915b068355Sstsp 		irq_grp->grp_id = i;
15925b068355Sstsp #if 0
15935b068355Sstsp 		init_dummy_netdev(&irq_grp->napi_ndev);
15945b068355Sstsp 		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
15955b068355Sstsp 			       ath11k_pcic_ext_grp_napi_poll);
15965b068355Sstsp #endif
15975b068355Sstsp 		if (sc->hw_params.ring_mask->tx[i] ||
15985b068355Sstsp 		    sc->hw_params.ring_mask->rx[i] ||
15995b068355Sstsp 		    sc->hw_params.ring_mask->rx_err[i] ||
16005b068355Sstsp 		    sc->hw_params.ring_mask->rx_wbm_rel[i] ||
16015b068355Sstsp 		    sc->hw_params.ring_mask->reo_status[i] ||
16025b068355Sstsp 		    sc->hw_params.ring_mask->rxdma2host[i] ||
16035b068355Sstsp 		    sc->hw_params.ring_mask->host2rxdma[i] ||
16045b068355Sstsp 		    sc->hw_params.ring_mask->rx_mon_status[i]) {
16055b068355Sstsp 			num_irq = 1;
16065b068355Sstsp 		}
16075b068355Sstsp 
16085b068355Sstsp 		irq_grp->num_irq = num_irq;
16095b068355Sstsp 		irq_grp->irqs[0] = ATH11K_PCI_IRQ_DP_OFFSET + i;
16105b068355Sstsp 
16115b068355Sstsp 		if (num_irq) {
16125b068355Sstsp 			int irq_idx = irq_grp->irqs[0];
16135b068355Sstsp 			pci_intr_handle_t ih;
16145b068355Sstsp 
16155b068355Sstsp 			if (pci_intr_map_msivec(pa, irq_idx, &ih) != 0 &&
16165b068355Sstsp 			    pci_intr_map(pa, &ih) != 0) {
16175b068355Sstsp 				printf("%s: can't map interrupt\n",
16185b068355Sstsp 				    sc->sc_dev.dv_xname);
16195b068355Sstsp 				return EIO;
16205b068355Sstsp 			}
16215b068355Sstsp 
16225b068355Sstsp 			snprintf(psc->sc_ivname[irq_idx], sizeof(psc->sc_ivname[0]),
16235b068355Sstsp 			    "%s:ex%d", sc->sc_dev.dv_xname, i);
16245b068355Sstsp 			psc->sc_ih[irq_idx] = pci_intr_establish(psc->sc_pc, ih,
16255b068355Sstsp 			    IPL_NET, qwx_ext_intr, irq_grp, psc->sc_ivname[irq_idx]);
16265b068355Sstsp 			if (psc->sc_ih[irq_idx] == NULL) {
16275b068355Sstsp 				printf("%s: failed to request irq %d\n",
16285b068355Sstsp 				    sc->sc_dev.dv_xname, irq_idx);
16295b068355Sstsp 				return EIO;
16305b068355Sstsp 			}
16315b068355Sstsp 		}
16325b068355Sstsp 
16335b068355Sstsp 		qwx_pcic_ext_grp_disable(irq_grp);
16345b068355Sstsp 	}
16355b068355Sstsp 
16365b068355Sstsp 	return 0;
16375b068355Sstsp }
16385b068355Sstsp 
16395b068355Sstsp int
16405b068355Sstsp qwx_pcic_config_irq(struct qwx_softc *sc, struct pci_attach_args *pa)
16415b068355Sstsp {
16425b068355Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
16435b068355Sstsp 	struct qwx_ce_pipe *ce_pipe;
16445b068355Sstsp 	uint32_t msi_data_start;
16455b068355Sstsp 	uint32_t msi_data_count, msi_data_idx;
16465b068355Sstsp 	uint32_t msi_irq_start;
16475b068355Sstsp 	int i, ret, irq_idx;
16485b068355Sstsp 	pci_intr_handle_t ih;
16495b068355Sstsp 
16505b068355Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
16515b068355Sstsp 		return 0;
16525b068355Sstsp 
16535b068355Sstsp 	ret = qwx_pcic_get_user_msi_vector(sc, "CE", &msi_data_count,
16545b068355Sstsp 	    &msi_data_start, &msi_irq_start);
16555b068355Sstsp 	if (ret)
16565b068355Sstsp 		return ret;
16575b068355Sstsp 
16585b068355Sstsp 	/* Configure CE irqs */
16595b068355Sstsp 	for (i = 0, msi_data_idx = 0; i < sc->hw_params.ce_count; i++) {
16605b068355Sstsp 		if (qwx_ce_get_attr_flags(sc, i) & CE_ATTR_DIS_INTR)
16615b068355Sstsp 			continue;
16625b068355Sstsp 
16635b068355Sstsp 		ce_pipe = &sc->ce.ce_pipe[i];
16645b068355Sstsp 		irq_idx = ATH11K_PCI_IRQ_CE0_OFFSET + i;
16655b068355Sstsp 
16665b068355Sstsp 		if (pci_intr_map_msivec(pa, irq_idx, &ih) != 0 &&
16675b068355Sstsp 		    pci_intr_map(pa, &ih) != 0) {
16685b068355Sstsp 			printf("%s: can't map interrupt\n",
16695b068355Sstsp 			    sc->sc_dev.dv_xname);
16705b068355Sstsp 			return EIO;
16715b068355Sstsp 		}
16725b068355Sstsp 
16735b068355Sstsp 		snprintf(psc->sc_ivname[irq_idx], sizeof(psc->sc_ivname[0]),
16745b068355Sstsp 		    "%s:ce%d", sc->sc_dev.dv_xname, ce_pipe->pipe_num);
16755b068355Sstsp 		psc->sc_ih[irq_idx] = pci_intr_establish(psc->sc_pc, ih,
16765b068355Sstsp 		    IPL_NET, qwx_ce_intr, ce_pipe, psc->sc_ivname[irq_idx]);
16775b068355Sstsp 		if (psc->sc_ih[irq_idx] == NULL) {
16785b068355Sstsp 			printf("%s: failed to request irq %d\n",
16795b068355Sstsp 			    sc->sc_dev.dv_xname, irq_idx);
16805b068355Sstsp 			return EIO;
16815b068355Sstsp 		}
16825b068355Sstsp 
16835b068355Sstsp 		msi_data_idx++;
16845b068355Sstsp 
16855b068355Sstsp 		qwx_pcic_ce_irq_disable(sc, i);
16865b068355Sstsp 	}
16875b068355Sstsp 
16885b068355Sstsp 	ret = qwx_pcic_ext_irq_config(sc, pa);
16895b068355Sstsp 	if (ret)
16905b068355Sstsp 		return ret;
16915b068355Sstsp 
16925b068355Sstsp 	return 0;
1693c2498839Sstsp }
1694c2498839Sstsp 
1695c2498839Sstsp void
1696c2498839Sstsp qwx_pcic_ce_irqs_enable(struct qwx_softc *sc)
1697c2498839Sstsp {
1698c2498839Sstsp 	int i;
1699c2498839Sstsp 
1700c2498839Sstsp 	set_bit(ATH11K_FLAG_CE_IRQ_ENABLED, sc->sc_flags);
1701c2498839Sstsp 
1702c2498839Sstsp 	for (i = 0; i < sc->hw_params.ce_count; i++) {
1703c2498839Sstsp 		if (qwx_ce_get_attr_flags(sc, i) & CE_ATTR_DIS_INTR)
1704c2498839Sstsp 			continue;
1705c2498839Sstsp 		qwx_pcic_ce_irq_enable(sc, i);
1706c2498839Sstsp 	}
1707c2498839Sstsp }
1708c2498839Sstsp 
1709c2498839Sstsp void
1710c2498839Sstsp qwx_pcic_ce_irqs_disable(struct qwx_softc *sc)
1711c2498839Sstsp {
1712c2498839Sstsp 	int i;
1713c2498839Sstsp 
1714c2498839Sstsp 	clear_bit(ATH11K_FLAG_CE_IRQ_ENABLED, sc->sc_flags);
1715c2498839Sstsp 
1716c2498839Sstsp 	for (i = 0; i < sc->hw_params.ce_count; i++) {
1717c2498839Sstsp 		if (qwx_ce_get_attr_flags(sc, i) & CE_ATTR_DIS_INTR)
1718c2498839Sstsp 			continue;
1719c2498839Sstsp 		qwx_pcic_ce_irq_disable(sc, i);
1720c2498839Sstsp 	}
1721c2498839Sstsp }
1722c2498839Sstsp 
1723c2498839Sstsp int
1724c2498839Sstsp qwx_pci_start(struct qwx_softc *sc)
1725c2498839Sstsp {
1726c2498839Sstsp 	/* TODO: for now don't restore ASPM in case of single MSI
1727c2498839Sstsp 	 * vector as MHI register reading in M2 causes system hang.
1728c2498839Sstsp 	 */
1729c2498839Sstsp 	if (test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
1730c2498839Sstsp 		qwx_pci_aspm_restore(sc);
1731c2498839Sstsp 	else
1732c2498839Sstsp 		DPRINTF("%s: leaving PCI ASPM disabled to avoid MHI M2 problems"
1733c2498839Sstsp 		    "\n", sc->sc_dev.dv_xname);
1734c2498839Sstsp 
1735c2498839Sstsp 	set_bit(ATH11K_FLAG_DEVICE_INIT_DONE, sc->sc_flags);
1736c2498839Sstsp 
1737c2498839Sstsp 	qwx_ce_rx_post_buf(sc);
1738c2498839Sstsp 	qwx_pcic_ce_irqs_enable(sc);
1739c2498839Sstsp 
1740c2498839Sstsp 	return 0;
1741c2498839Sstsp }
1742c2498839Sstsp 
1743c2498839Sstsp void
1744c2498839Sstsp qwx_pcic_ce_irq_disable_sync(struct qwx_softc *sc)
1745c2498839Sstsp {
1746c2498839Sstsp 	qwx_pcic_ce_irqs_disable(sc);
1747c2498839Sstsp #if 0
1748c2498839Sstsp 	ath11k_pcic_sync_ce_irqs(ab);
1749c2498839Sstsp 	ath11k_pcic_kill_tasklets(ab);
1750c2498839Sstsp #endif
1751c2498839Sstsp }
1752c2498839Sstsp 
1753c2498839Sstsp void
1754c2498839Sstsp qwx_pci_stop(struct qwx_softc *sc)
1755c2498839Sstsp {
1756c2498839Sstsp 	qwx_pcic_ce_irq_disable_sync(sc);
1757c2498839Sstsp 	qwx_ce_cleanup_pipes(sc);
1758c2498839Sstsp }
1759c2498839Sstsp 
1760c2498839Sstsp int
1761c2498839Sstsp qwx_pci_bus_wake_up(struct qwx_softc *sc)
1762c2498839Sstsp {
1763c2498839Sstsp 	if (qwx_mhi_wake_db_clear_valid(sc))
1764c2498839Sstsp 		qwx_mhi_device_wake(sc);
1765c2498839Sstsp 
1766c2498839Sstsp 	return 0;
1767c2498839Sstsp }
1768c2498839Sstsp 
1769c2498839Sstsp void
1770c2498839Sstsp qwx_pci_bus_release(struct qwx_softc *sc)
1771c2498839Sstsp {
1772c2498839Sstsp 	if (qwx_mhi_wake_db_clear_valid(sc))
1773c2498839Sstsp 		qwx_mhi_device_zzz(sc);
1774c2498839Sstsp }
1775c2498839Sstsp 
1776c2498839Sstsp uint32_t
1777c2498839Sstsp qwx_pci_get_window_start(struct qwx_softc *sc, uint32_t offset)
1778c2498839Sstsp {
1779c2498839Sstsp 	if (!sc->hw_params.static_window_map)
1780c2498839Sstsp 		return ATH11K_PCI_WINDOW_START;
1781c2498839Sstsp 
1782c2498839Sstsp 	if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < ATH11K_PCI_WINDOW_RANGE_MASK)
1783c2498839Sstsp 		/* if offset lies within DP register range, use 3rd window */
1784c2498839Sstsp 		return 3 * ATH11K_PCI_WINDOW_START;
1785c2498839Sstsp 	else if ((offset ^ HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(sc)) <
1786c2498839Sstsp 		 ATH11K_PCI_WINDOW_RANGE_MASK)
1787c2498839Sstsp 		 /* if offset lies within CE register range, use 2nd window */
1788c2498839Sstsp 		return 2 * ATH11K_PCI_WINDOW_START;
1789c2498839Sstsp 	else
1790c2498839Sstsp 		return ATH11K_PCI_WINDOW_START;
1791c2498839Sstsp }
1792c2498839Sstsp 
1793c2498839Sstsp void
1794c2498839Sstsp qwx_pci_select_window(struct qwx_softc *sc, uint32_t offset)
1795c2498839Sstsp {
1796c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
1797c2498839Sstsp 	uint32_t window = FIELD_GET(ATH11K_PCI_WINDOW_VALUE_MASK, offset);
1798c2498839Sstsp 
1799c2498839Sstsp #if notyet
1800c2498839Sstsp 	lockdep_assert_held(&ab_pci->window_lock);
1801c2498839Sstsp #endif
1802c2498839Sstsp 
1803c2498839Sstsp 	if (window != psc->register_window) {
1804c2498839Sstsp 		qwx_pci_write(sc, ATH11K_PCI_WINDOW_REG_ADDRESS,
1805c2498839Sstsp 		    ATH11K_PCI_WINDOW_ENABLE_BIT | window);
1806c2498839Sstsp 		(void) qwx_pci_read(sc, ATH11K_PCI_WINDOW_REG_ADDRESS);
1807c2498839Sstsp 		psc->register_window = window;
1808c2498839Sstsp 	}
1809c2498839Sstsp }
1810c2498839Sstsp 
1811c2498839Sstsp void
1812c2498839Sstsp qwx_pci_window_write32(struct qwx_softc *sc, uint32_t offset, uint32_t value)
1813c2498839Sstsp {
1814c2498839Sstsp 	uint32_t window_start;
1815c2498839Sstsp 
1816c2498839Sstsp 	window_start = qwx_pci_get_window_start(sc, offset);
1817c2498839Sstsp 
1818c2498839Sstsp 	if (window_start == ATH11K_PCI_WINDOW_START) {
1819c2498839Sstsp #if notyet
1820c2498839Sstsp 		spin_lock_bh(&ab_pci->window_lock);
1821c2498839Sstsp #endif
1822c2498839Sstsp 		qwx_pci_select_window(sc, offset);
1823c2498839Sstsp 		qwx_pci_write(sc, window_start +
1824c2498839Sstsp 		    (offset & ATH11K_PCI_WINDOW_RANGE_MASK), value);
1825c2498839Sstsp #if notyet
1826c2498839Sstsp 		spin_unlock_bh(&ab_pci->window_lock);
1827c2498839Sstsp #endif
1828c2498839Sstsp 	} else {
1829c2498839Sstsp 		qwx_pci_write(sc, window_start +
1830c2498839Sstsp 		    (offset & ATH11K_PCI_WINDOW_RANGE_MASK), value);
1831c2498839Sstsp 	}
1832c2498839Sstsp }
1833c2498839Sstsp 
1834c2498839Sstsp uint32_t
1835c2498839Sstsp qwx_pci_window_read32(struct qwx_softc *sc, uint32_t offset)
1836c2498839Sstsp {
1837c2498839Sstsp 	uint32_t window_start, val;
1838c2498839Sstsp 
1839c2498839Sstsp 	window_start = qwx_pci_get_window_start(sc, offset);
1840c2498839Sstsp 
1841c2498839Sstsp 	if (window_start == ATH11K_PCI_WINDOW_START) {
1842c2498839Sstsp #if notyet
1843c2498839Sstsp 		spin_lock_bh(&ab_pci->window_lock);
1844c2498839Sstsp #endif
1845c2498839Sstsp 		qwx_pci_select_window(sc, offset);
1846c2498839Sstsp 		val = qwx_pci_read(sc, window_start +
1847c2498839Sstsp 		    (offset & ATH11K_PCI_WINDOW_RANGE_MASK));
1848c2498839Sstsp #if notyet
1849c2498839Sstsp 		spin_unlock_bh(&ab_pci->window_lock);
1850c2498839Sstsp #endif
1851c2498839Sstsp 	} else {
1852c2498839Sstsp 		val = qwx_pci_read(sc, window_start +
1853c2498839Sstsp 		    (offset & ATH11K_PCI_WINDOW_RANGE_MASK));
1854c2498839Sstsp 	}
1855c2498839Sstsp 
1856c2498839Sstsp 	return val;
1857c2498839Sstsp }
1858c2498839Sstsp 
1859c2498839Sstsp void
1860c2498839Sstsp qwx_pci_select_static_window(struct qwx_softc *sc)
1861c2498839Sstsp {
1862c2498839Sstsp 	uint32_t umac_window;
1863c2498839Sstsp 	uint32_t ce_window;
1864c2498839Sstsp 	uint32_t window;
1865c2498839Sstsp 
1866c2498839Sstsp 	umac_window = FIELD_GET(ATH11K_PCI_WINDOW_VALUE_MASK, HAL_SEQ_WCSS_UMAC_OFFSET);
1867c2498839Sstsp 	ce_window = FIELD_GET(ATH11K_PCI_WINDOW_VALUE_MASK, HAL_CE_WFSS_CE_REG_BASE);
1868c2498839Sstsp 	window = (umac_window << 12) | (ce_window << 6);
1869c2498839Sstsp 
1870c2498839Sstsp 	qwx_pci_write(sc, ATH11K_PCI_WINDOW_REG_ADDRESS,
1871c2498839Sstsp 	    ATH11K_PCI_WINDOW_ENABLE_BIT | window);
1872c2498839Sstsp }
1873c2498839Sstsp 
1874c2498839Sstsp void
1875c2498839Sstsp qwx_pci_soc_global_reset(struct qwx_softc *sc)
1876c2498839Sstsp {
1877c2498839Sstsp 	uint32_t val, msecs;
1878c2498839Sstsp 
1879c2498839Sstsp 	val = qwx_pcic_read32(sc, PCIE_SOC_GLOBAL_RESET);
1880c2498839Sstsp 
1881c2498839Sstsp 	val |= PCIE_SOC_GLOBAL_RESET_V;
1882c2498839Sstsp 
1883c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_SOC_GLOBAL_RESET, val);
1884c2498839Sstsp 
1885c2498839Sstsp 	/* TODO: exact time to sleep is uncertain */
1886c2498839Sstsp 	msecs = 10;
1887c2498839Sstsp 	DELAY(msecs * 1000);
1888c2498839Sstsp 
1889c2498839Sstsp 	/* Need to toggle V bit back otherwise stuck in reset status */
1890c2498839Sstsp 	val &= ~PCIE_SOC_GLOBAL_RESET_V;
1891c2498839Sstsp 
1892c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_SOC_GLOBAL_RESET, val);
1893c2498839Sstsp 
1894c2498839Sstsp 	DELAY(msecs * 1000);
1895c2498839Sstsp 
1896c2498839Sstsp 	val = qwx_pcic_read32(sc, PCIE_SOC_GLOBAL_RESET);
1897c2498839Sstsp 	if (val == 0xffffffff)
1898c2498839Sstsp 		printf("%s: link down error during global reset\n",
1899c2498839Sstsp 		    sc->sc_dev.dv_xname);
1900c2498839Sstsp }
1901c2498839Sstsp 
1902c2498839Sstsp void
1903c2498839Sstsp qwx_pci_clear_dbg_registers(struct qwx_softc *sc)
1904c2498839Sstsp {
1905c2498839Sstsp 	uint32_t val;
1906c2498839Sstsp 
1907c2498839Sstsp 	/* read cookie */
1908c2498839Sstsp 	val = qwx_pcic_read32(sc, PCIE_Q6_COOKIE_ADDR);
1909c2498839Sstsp 	DPRINTF("%s: cookie:0x%x\n", sc->sc_dev.dv_xname, val);
1910c2498839Sstsp 
1911c2498839Sstsp 	val = qwx_pcic_read32(sc, WLAON_WARM_SW_ENTRY);
1912c2498839Sstsp 	DPRINTF("%s: WLAON_WARM_SW_ENTRY 0x%x\n", sc->sc_dev.dv_xname, val);
1913c2498839Sstsp 
1914c2498839Sstsp 	/* TODO: exact time to sleep is uncertain */
1915c2498839Sstsp 	DELAY(10 * 1000);
1916c2498839Sstsp 
1917c2498839Sstsp 	/* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
1918c2498839Sstsp 	 * continuing warm path and entering dead loop.
1919c2498839Sstsp 	 */
1920c2498839Sstsp 	qwx_pcic_write32(sc, WLAON_WARM_SW_ENTRY, 0);
1921c2498839Sstsp 	DELAY(10 * 1000);
1922c2498839Sstsp 
1923c2498839Sstsp 	val = qwx_pcic_read32(sc, WLAON_WARM_SW_ENTRY);
1924c2498839Sstsp 	DPRINTF("%s: WLAON_WARM_SW_ENTRY 0x%x\n", sc->sc_dev.dv_xname, val);
1925c2498839Sstsp 
1926c2498839Sstsp 	/* A read clear register. clear the register to prevent
1927c2498839Sstsp 	 * Q6 from entering wrong code path.
1928c2498839Sstsp 	 */
1929c2498839Sstsp 	val = qwx_pcic_read32(sc, WLAON_SOC_RESET_CAUSE_REG);
1930c2498839Sstsp 	DPRINTF("%s: soc reset cause:%d\n", sc->sc_dev.dv_xname, val);
1931c2498839Sstsp }
1932c2498839Sstsp 
1933c2498839Sstsp int
1934c2498839Sstsp qwx_pci_set_link_reg(struct qwx_softc *sc, uint32_t offset, uint32_t value,
1935c2498839Sstsp     uint32_t mask)
1936c2498839Sstsp {
1937c2498839Sstsp 	uint32_t v;
1938c2498839Sstsp 	int i;
1939c2498839Sstsp 
1940c2498839Sstsp 	v = qwx_pcic_read32(sc, offset);
1941c2498839Sstsp 	if ((v & mask) == value)
1942c2498839Sstsp 		return 0;
1943c2498839Sstsp 
1944c2498839Sstsp 	for (i = 0; i < 10; i++) {
1945c2498839Sstsp 		qwx_pcic_write32(sc, offset, (v & ~mask) | value);
1946c2498839Sstsp 
1947c2498839Sstsp 		v = qwx_pcic_read32(sc, offset);
1948c2498839Sstsp 		if ((v & mask) == value)
1949c2498839Sstsp 			return 0;
1950c2498839Sstsp 
1951c2498839Sstsp 		delay((2 * 1000));
1952c2498839Sstsp 	}
1953c2498839Sstsp 
1954c2498839Sstsp 	DPRINTF("failed to set pcie link register 0x%08x: 0x%08x != 0x%08x\n",
1955c2498839Sstsp 	    offset, v & mask, value);
1956c2498839Sstsp 
1957c2498839Sstsp 	return ETIMEDOUT;
1958c2498839Sstsp }
1959c2498839Sstsp 
1960c2498839Sstsp int
1961c2498839Sstsp qwx_pci_fix_l1ss(struct qwx_softc *sc)
1962c2498839Sstsp {
1963c2498839Sstsp 	int ret;
1964c2498839Sstsp 
1965c2498839Sstsp 	ret = qwx_pci_set_link_reg(sc,
1966c2498839Sstsp 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(sc),
1967c2498839Sstsp 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL,
1968c2498839Sstsp 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK);
1969c2498839Sstsp 	if (ret) {
1970c2498839Sstsp 		DPRINTF("failed to set sysclk: %d\n", ret);
1971c2498839Sstsp 		return ret;
1972c2498839Sstsp 	}
1973c2498839Sstsp 
1974c2498839Sstsp 	ret = qwx_pci_set_link_reg(sc,
1975c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG1_REG(sc),
1976c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG1_VAL,
1977c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
1978c2498839Sstsp 	if (ret) {
1979c2498839Sstsp 		DPRINTF("failed to set dtct config1 error: %d\n", ret);
1980c2498839Sstsp 		return ret;
1981c2498839Sstsp 	}
1982c2498839Sstsp 
1983c2498839Sstsp 	ret = qwx_pci_set_link_reg(sc,
1984c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG2_REG(sc),
1985c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG2_VAL,
1986c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
1987c2498839Sstsp 	if (ret) {
1988c2498839Sstsp 		DPRINTF("failed to set dtct config2: %d\n", ret);
1989c2498839Sstsp 		return ret;
1990c2498839Sstsp 	}
1991c2498839Sstsp 
1992c2498839Sstsp 	ret = qwx_pci_set_link_reg(sc,
1993c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG4_REG(sc),
1994c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG4_VAL,
1995c2498839Sstsp 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
1996c2498839Sstsp 	if (ret) {
1997c2498839Sstsp 		DPRINTF("failed to set dtct config4: %d\n", ret);
1998c2498839Sstsp 		return ret;
1999c2498839Sstsp 	}
2000c2498839Sstsp 
2001c2498839Sstsp 	return 0;
2002c2498839Sstsp }
2003c2498839Sstsp 
2004c2498839Sstsp void
2005c2498839Sstsp qwx_pci_enable_ltssm(struct qwx_softc *sc)
2006c2498839Sstsp {
2007c2498839Sstsp 	uint32_t val;
2008c2498839Sstsp 	int i;
2009c2498839Sstsp 
2010c2498839Sstsp 	val = qwx_pcic_read32(sc, PCIE_PCIE_PARF_LTSSM);
2011c2498839Sstsp 
2012c2498839Sstsp 	/* PCIE link seems very unstable after the Hot Reset*/
2013c2498839Sstsp 	for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
2014c2498839Sstsp 		if (val == 0xffffffff)
2015c2498839Sstsp 			DELAY(5 * 1000);
2016c2498839Sstsp 
2017c2498839Sstsp 		qwx_pcic_write32(sc, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
2018c2498839Sstsp 		val = qwx_pcic_read32(sc, PCIE_PCIE_PARF_LTSSM);
2019c2498839Sstsp 	}
2020c2498839Sstsp 
2021c2498839Sstsp 	DPRINTF("%s: pci ltssm 0x%x\n", sc->sc_dev.dv_xname, val);
2022c2498839Sstsp 
2023c2498839Sstsp 	val = qwx_pcic_read32(sc, GCC_GCC_PCIE_HOT_RST);
2024c2498839Sstsp 	val |= GCC_GCC_PCIE_HOT_RST_VAL;
2025c2498839Sstsp 	qwx_pcic_write32(sc, GCC_GCC_PCIE_HOT_RST, val);
2026c2498839Sstsp 	val = qwx_pcic_read32(sc, GCC_GCC_PCIE_HOT_RST);
2027c2498839Sstsp 
2028c2498839Sstsp 	DPRINTF("%s: pci pcie_hot_rst 0x%x\n", sc->sc_dev.dv_xname, val);
2029c2498839Sstsp 
2030c2498839Sstsp 	DELAY(5 * 1000);
2031c2498839Sstsp }
2032c2498839Sstsp 
2033c2498839Sstsp void
2034c2498839Sstsp qwx_pci_clear_all_intrs(struct qwx_softc *sc)
2035c2498839Sstsp {
2036c2498839Sstsp 	/* This is a WAR for PCIE Hotreset.
2037c2498839Sstsp 	 * When target receive Hotreset, but will set the interrupt.
2038c2498839Sstsp 	 * So when download SBL again, SBL will open Interrupt and
2039c2498839Sstsp 	 * receive it, and crash immediately.
2040c2498839Sstsp 	 */
2041c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
2042c2498839Sstsp }
2043c2498839Sstsp 
2044c2498839Sstsp void
2045c2498839Sstsp qwx_pci_set_wlaon_pwr_ctrl(struct qwx_softc *sc)
2046c2498839Sstsp {
2047c2498839Sstsp 	uint32_t val;
2048c2498839Sstsp 
2049c2498839Sstsp 	val = qwx_pcic_read32(sc, WLAON_QFPROM_PWR_CTRL_REG);
2050c2498839Sstsp 	val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
2051c2498839Sstsp 	qwx_pcic_write32(sc, WLAON_QFPROM_PWR_CTRL_REG, val);
2052c2498839Sstsp }
2053c2498839Sstsp 
2054c2498839Sstsp void
2055c2498839Sstsp qwx_pci_force_wake(struct qwx_softc *sc)
2056c2498839Sstsp {
2057c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
2058c2498839Sstsp 	DELAY(5 * 1000);
2059c2498839Sstsp }
2060c2498839Sstsp 
2061c2498839Sstsp void
2062c2498839Sstsp qwx_pci_sw_reset(struct qwx_softc *sc, bool power_on)
2063c2498839Sstsp {
2064c2498839Sstsp 	DELAY(100 * 1000); /* msecs */
2065c2498839Sstsp 
2066c2498839Sstsp 	if (power_on) {
2067c2498839Sstsp 		qwx_pci_enable_ltssm(sc);
2068c2498839Sstsp 		qwx_pci_clear_all_intrs(sc);
2069c2498839Sstsp 		qwx_pci_set_wlaon_pwr_ctrl(sc);
2070c2498839Sstsp 		if (sc->hw_params.fix_l1ss)
2071c2498839Sstsp 			qwx_pci_fix_l1ss(sc);
2072c2498839Sstsp 	}
2073c2498839Sstsp 
2074c2498839Sstsp 	qwx_mhi_clear_vector(sc);
2075c2498839Sstsp 	qwx_pci_clear_dbg_registers(sc);
2076c2498839Sstsp 	qwx_pci_soc_global_reset(sc);
2077c2498839Sstsp 	qwx_mhi_reset_device(sc, 0);
2078c2498839Sstsp }
2079c2498839Sstsp 
2080c2498839Sstsp void
2081c2498839Sstsp qwx_pci_msi_config(struct qwx_softc *sc, bool enable)
2082c2498839Sstsp {
2083c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2084c2498839Sstsp 	uint32_t val;
2085c2498839Sstsp 
2086c2498839Sstsp 	val = pci_conf_read(psc->sc_pc, psc->sc_tag,
2087c2498839Sstsp 	    psc->sc_msi_off + PCI_MSI_MC);
2088c2498839Sstsp 
2089c2498839Sstsp 	if (enable)
2090c2498839Sstsp 		val |= PCI_MSI_MC_MSIE;
2091c2498839Sstsp 	else
2092c2498839Sstsp 		val &= ~PCI_MSI_MC_MSIE;
2093c2498839Sstsp 
2094c2498839Sstsp 	pci_conf_write(psc->sc_pc, psc->sc_tag,  psc->sc_msi_off + PCI_MSI_MC,
2095c2498839Sstsp 	    val);
2096c2498839Sstsp }
2097c2498839Sstsp 
2098c2498839Sstsp void
2099c2498839Sstsp qwx_pci_msi_enable(struct qwx_softc *sc)
2100c2498839Sstsp {
2101c2498839Sstsp 	qwx_pci_msi_config(sc, true);
2102c2498839Sstsp }
2103c2498839Sstsp 
2104c2498839Sstsp void
2105c2498839Sstsp qwx_pci_msi_disable(struct qwx_softc *sc)
2106c2498839Sstsp {
2107c2498839Sstsp 	qwx_pci_msi_config(sc, false);
2108c2498839Sstsp }
2109c2498839Sstsp 
2110c2498839Sstsp void
2111c2498839Sstsp qwx_pci_aspm_disable(struct qwx_softc *sc)
2112c2498839Sstsp {
2113c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2114c2498839Sstsp 
2115c2498839Sstsp 	psc->sc_lcsr = pci_conf_read(psc->sc_pc, psc->sc_tag,
2116c2498839Sstsp 	    psc->sc_cap_off + PCI_PCIE_LCSR);
2117c2498839Sstsp 
2118c2498839Sstsp 	DPRINTF("%s: pci link_ctl 0x%04x L0s %d L1 %d\n", sc->sc_dev.dv_xname,
2119c2498839Sstsp 	    (uint16_t)psc->sc_lcsr, (psc->sc_lcsr & PCI_PCIE_LCSR_ASPM_L0S),
2120c2498839Sstsp 	    (psc->sc_lcsr & PCI_PCIE_LCSR_ASPM_L1));
2121c2498839Sstsp 
2122c2498839Sstsp 	/* disable L0s and L1 */
2123c2498839Sstsp 	pci_conf_write(psc->sc_pc, psc->sc_tag, psc->sc_cap_off + PCI_PCIE_LCSR,
2124c2498839Sstsp 	    psc->sc_lcsr & ~(PCI_PCIE_LCSR_ASPM_L0S | PCI_PCIE_LCSR_ASPM_L1));
2125c2498839Sstsp 
2126c2498839Sstsp 	psc->sc_flags |= ATH11K_PCI_ASPM_RESTORE;
2127c2498839Sstsp }
2128c2498839Sstsp 
2129c2498839Sstsp void
2130c2498839Sstsp qwx_pci_aspm_restore(struct qwx_softc *sc)
2131c2498839Sstsp {
2132c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2133c2498839Sstsp 
2134c2498839Sstsp 	if (psc->sc_flags & ATH11K_PCI_ASPM_RESTORE) {
2135c2498839Sstsp 		pci_conf_write(psc->sc_pc, psc->sc_tag,
2136c2498839Sstsp 		    psc->sc_cap_off + PCI_PCIE_LCSR, psc->sc_lcsr);
2137c2498839Sstsp 		psc->sc_flags &= ~ATH11K_PCI_ASPM_RESTORE;
2138c2498839Sstsp 	}
2139c2498839Sstsp }
2140c2498839Sstsp 
2141c2498839Sstsp int
2142c2498839Sstsp qwx_pci_power_up(struct qwx_softc *sc)
2143c2498839Sstsp {
2144c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2145c2498839Sstsp 	int error;
2146c2498839Sstsp 
2147c2498839Sstsp 	psc->register_window = 0;
2148c2498839Sstsp 	clear_bit(ATH11K_FLAG_DEVICE_INIT_DONE, sc->sc_flags);
2149c2498839Sstsp 
2150c2498839Sstsp 	qwx_pci_sw_reset(sc, true);
2151c2498839Sstsp 
2152c2498839Sstsp 	/* Disable ASPM during firmware download due to problems switching
2153c2498839Sstsp 	 * to AMSS state.
2154c2498839Sstsp 	 */
2155c2498839Sstsp 	qwx_pci_aspm_disable(sc);
2156c2498839Sstsp 
2157c2498839Sstsp 	qwx_pci_msi_enable(sc);
2158c2498839Sstsp 
2159c2498839Sstsp 	error = qwx_mhi_start(psc);
2160c2498839Sstsp 	if (error)
2161c2498839Sstsp 		return error;
2162c2498839Sstsp 
2163c2498839Sstsp 	if (sc->hw_params.static_window_map)
2164c2498839Sstsp 		qwx_pci_select_static_window(sc);
2165c2498839Sstsp 
2166c2498839Sstsp 	return 0;
2167c2498839Sstsp }
2168c2498839Sstsp 
2169c2498839Sstsp void
2170c2498839Sstsp qwx_pci_power_down(struct qwx_softc *sc)
2171c2498839Sstsp {
2172c2498839Sstsp 	/* restore aspm in case firmware bootup fails */
2173c2498839Sstsp 	qwx_pci_aspm_restore(sc);
2174c2498839Sstsp 
2175c2498839Sstsp 	qwx_pci_force_wake(sc);
2176c2498839Sstsp 
2177c2498839Sstsp 	qwx_pci_msi_disable(sc);
2178c2498839Sstsp 
2179c2498839Sstsp 	qwx_mhi_stop(sc);
2180c2498839Sstsp 	clear_bit(ATH11K_FLAG_DEVICE_INIT_DONE, sc->sc_flags);
2181c2498839Sstsp 	qwx_pci_sw_reset(sc, false);
2182c2498839Sstsp }
2183c2498839Sstsp 
2184c2498839Sstsp /*
2185c2498839Sstsp  * MHI
2186c2498839Sstsp  */
2187c2498839Sstsp int
2188c2498839Sstsp qwx_mhi_register(struct qwx_softc *sc)
2189c2498839Sstsp {
2190c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: STUB %s()\n", sc->sc_dev.dv_xname, __func__);
2191c2498839Sstsp 	return 0;
2192c2498839Sstsp }
2193c2498839Sstsp 
2194c2498839Sstsp void
2195c2498839Sstsp qwx_mhi_unregister(struct qwx_softc *sc)
2196c2498839Sstsp {
2197c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: STUB %s()\n", sc->sc_dev.dv_xname, __func__);
2198c2498839Sstsp }
2199c2498839Sstsp 
2200c2498839Sstsp // XXX MHI is GPLd - we provide a compatible bare-bones implementation
2201c2498839Sstsp #define MHI_CFG				0x10
2202c2498839Sstsp #define   MHI_CFG_NHWER_MASK		GENMASK(31, 24)
2203c2498839Sstsp #define   MHI_CFG_NHWER_SHFT		24
2204c2498839Sstsp #define   MHI_CFG_NER_MASK		GENMASK(23, 16)
2205c2498839Sstsp #define   MHI_CFG_NER_SHFT		16
2206c2498839Sstsp #define   MHI_CFG_NHWCH_MASK		GENMASK(15, 8)
2207c2498839Sstsp #define   MHI_CFG_NHWCH_SHFT		8
2208c2498839Sstsp #define   MHI_CFG_NCH_MASK		GENMASK(7, 0)
2209c2498839Sstsp #define MHI_CHDBOFF			0x18
2210c2498839Sstsp #define MHI_DEV_WAKE_DB			127
2211c2498839Sstsp #define MHI_ERDBOFF			0x20
2212c2498839Sstsp #define MHI_BHI_OFFSET			0x28
2213c2498839Sstsp #define   MHI_BHI_IMGADDR_LOW			0x08
2214c2498839Sstsp #define   MHI_BHI_IMGADDR_HIGH			0x0c
2215c2498839Sstsp #define   MHI_BHI_IMGSIZE			0x10
2216c2498839Sstsp #define   MHI_BHI_IMGTXDB			0x18
2217c2498839Sstsp #define   MHI_BHI_INTVEC			0x20
2218c2498839Sstsp #define   MHI_BHI_EXECENV			0x28
2219c2498839Sstsp #define   MHI_BHI_STATUS			0x2c
2220c2498839Sstsp #define	  MHI_BHI_SERIALNU			0x40
2221c2498839Sstsp #define MHI_BHIE_OFFSET			0x2c
2222c2498839Sstsp #define   MHI_BHIE_TXVECADDR_LOW_OFFS		0x2c
2223c2498839Sstsp #define   MHI_BHIE_TXVECADDR_HIGH_OFFS		0x30
2224c2498839Sstsp #define   MHI_BHIE_TXVECSIZE_OFFS		0x34
2225c2498839Sstsp #define   MHI_BHIE_TXVECDB_OFFS			0x3c
2226c2498839Sstsp #define   MHI_BHIE_TXVECSTATUS_OFFS		0x44
2227c2498839Sstsp #define   MHI_BHIE_RXVECADDR_LOW_OFFS		0x60
2228c2498839Sstsp #define   MHI_BHIE_RXVECSTATUS_OFFS		0x78
2229c2498839Sstsp #define MHI_CTRL			0x38
2230c2498839Sstsp #define    MHI_CTRL_READY_MASK			0x1
2231c2498839Sstsp #define    MHI_CTRL_RESET_MASK			0x2
2232c2498839Sstsp #define    MHI_CTRL_MHISTATE_MASK		GENMASK(15, 8)
2233c2498839Sstsp #define    MHI_CTRL_MHISTATE_SHFT		8
2234c2498839Sstsp #define MHI_STATUS			0x48
2235c2498839Sstsp #define    MHI_STATUS_MHISTATE_MASK		GENMASK(15, 8)
2236c2498839Sstsp #define    MHI_STATUS_MHISTATE_SHFT		8
2237c2498839Sstsp #define        MHI_STATE_RESET			0x0
2238c2498839Sstsp #define        MHI_STATE_READY			0x1
2239c2498839Sstsp #define        MHI_STATE_M0			0x2
2240c2498839Sstsp #define        MHI_STATE_M1			0x3
2241c2498839Sstsp #define        MHI_STATE_M2			0x4
2242c2498839Sstsp #define        MHI_STATE_M3			0x5
2243c2498839Sstsp #define        MHI_STATE_M3_FAST		0x6
2244c2498839Sstsp #define        MHI_STATE_BHI			0x7
2245c2498839Sstsp #define        MHI_STATE_SYS_ERR		0xff
2246c2498839Sstsp #define    MHI_STATUS_READY_MASK		0x1
2247c2498839Sstsp #define    MHI_STATUS_SYSERR_MASK		0x4
2248c2498839Sstsp #define MHI_CCABAP_LOWER		0x58
2249c2498839Sstsp #define MHI_CCABAP_HIGHER		0x5c
2250c2498839Sstsp #define MHI_ECABAP_LOWER		0x60
2251c2498839Sstsp #define MHI_ECABAP_HIGHER		0x64
2252c2498839Sstsp #define MHI_CRCBAP_LOWER		0x68
2253c2498839Sstsp #define MHI_CRCBAP_HIGHER		0x6c
2254c2498839Sstsp #define MHI_CRDB_LOWER			0x70
2255c2498839Sstsp #define MHI_CRDB_HIGHER			0x74
2256c2498839Sstsp #define MHI_CTRLBASE_LOWER		0x80
2257c2498839Sstsp #define MHI_CTRLBASE_HIGHER		0x84
2258c2498839Sstsp #define MHI_CTRLLIMIT_LOWER		0x88
2259c2498839Sstsp #define MHI_CTRLLIMIT_HIGHER		0x8c
2260c2498839Sstsp #define MHI_DATABASE_LOWER		0x98
2261c2498839Sstsp #define MHI_DATABASE_HIGHER		0x9c
2262c2498839Sstsp #define MHI_DATALIMIT_LOWER		0xa0
2263c2498839Sstsp #define MHI_DATALIMIT_HIGHER		0xa4
2264c2498839Sstsp 
2265c2498839Sstsp #define MHI_EE_PBL	0x0	/* Primary Bootloader */
2266c2498839Sstsp #define MHI_EE_SBL	0x1	/* Secondary Bootloader */
2267c2498839Sstsp #define MHI_EE_AMSS	0x2	/* Modem, aka the primary runtime EE */
2268c2498839Sstsp #define MHI_EE_RDDM	0x3	/* Ram dump download mode */
2269c2498839Sstsp #define MHI_EE_WFW	0x4	/* WLAN firmware mode */
2270c2498839Sstsp #define MHI_EE_PTHRU	0x5	/* Passthrough */
2271c2498839Sstsp #define MHI_EE_EDL	0x6	/* Embedded downloader */
2272c2498839Sstsp #define MHI_EE_FP	0x7	/* Flash Programmer Environment */
2273c2498839Sstsp 
2274c2498839Sstsp #define MHI_IN_PBL(e) (e == MHI_EE_PBL || e == MHI_EE_PTHRU || e == MHI_EE_EDL)
2275c2498839Sstsp #define MHI_POWER_UP_CAPABLE(e) (MHI_IN_PBL(e) || e == MHI_EE_AMSS)
2276c2498839Sstsp #define MHI_IN_MISSION_MODE(e) \
2277c2498839Sstsp 	(e == MHI_EE_AMSS || e == MHI_EE_WFW || e == MHI_EE_FP)
2278c2498839Sstsp 
2279c2498839Sstsp /* BHI register bits */
2280c2498839Sstsp #define MHI_BHI_TXDB_SEQNUM_BMSK	GENMASK(29, 0)
2281c2498839Sstsp #define MHI_BHI_TXDB_SEQNUM_SHFT	0
2282c2498839Sstsp #define MHI_BHI_STATUS_MASK		GENMASK(31, 30)
2283c2498839Sstsp #define MHI_BHI_STATUS_SHFT		30
2284c2498839Sstsp #define MHI_BHI_STATUS_ERROR		0x03
2285c2498839Sstsp #define MHI_BHI_STATUS_SUCCESS		0x02
2286c2498839Sstsp #define MHI_BHI_STATUS_RESET		0x00
2287c2498839Sstsp 
2288c2498839Sstsp /* MHI BHIE registers */
2289c2498839Sstsp #define MHI_BHIE_MSMSOCID_OFFS		0x00
2290c2498839Sstsp #define MHI_BHIE_RXVECADDR_LOW_OFFS	0x60
2291c2498839Sstsp #define MHI_BHIE_RXVECADDR_HIGH_OFFS	0x64
2292c2498839Sstsp #define MHI_BHIE_RXVECSIZE_OFFS		0x68
2293c2498839Sstsp #define MHI_BHIE_RXVECDB_OFFS		0x70
2294c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_OFFS	0x78
2295c2498839Sstsp 
2296c2498839Sstsp /* BHIE register bits */
2297c2498839Sstsp #define MHI_BHIE_TXVECDB_SEQNUM_BMSK		GENMASK(29, 0)
2298c2498839Sstsp #define MHI_BHIE_TXVECDB_SEQNUM_SHFT		0
2299c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_SEQNUM_BMSK	GENMASK(29, 0)
2300c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_SEQNUM_SHFT	0
2301c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_STATUS_BMSK	GENMASK(31, 30)
2302c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_STATUS_SHFT	30
2303c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_STATUS_RESET	0x00
2304c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_STATUS_XFER_COMPL	0x02
2305c2498839Sstsp #define MHI_BHIE_TXVECSTATUS_STATUS_ERROR	0x03
2306c2498839Sstsp #define MHI_BHIE_RXVECDB_SEQNUM_BMSK		GENMASK(29, 0)
2307c2498839Sstsp #define MHI_BHIE_RXVECDB_SEQNUM_SHFT		0
2308c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_SEQNUM_BMSK	GENMASK(29, 0)
2309c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_SEQNUM_SHFT	0
2310c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_STATUS_BMSK	GENMASK(31, 30)
2311c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_STATUS_SHFT	30
2312c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_STATUS_RESET	0x00
2313c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_STATUS_XFER_COMPL	0x02
2314c2498839Sstsp #define MHI_BHIE_RXVECSTATUS_STATUS_ERROR	0x03
2315c2498839Sstsp 
2316c2498839Sstsp #define MHI_EV_CC_INVALID	0x0
2317c2498839Sstsp #define MHI_EV_CC_SUCCESS	0x1
2318c2498839Sstsp #define MHI_EV_CC_EOT		0x2
2319c2498839Sstsp #define MHI_EV_CC_OVERFLOW	0x3
2320c2498839Sstsp #define MHI_EV_CC_EOB		0x4
2321c2498839Sstsp #define MHI_EV_CC_OOB		0x5
2322c2498839Sstsp #define MHI_EV_CC_DB_MODE	0x6
2323c2498839Sstsp #define MHI_EV_CC_UNDEFINED_ERR	0x10
2324c2498839Sstsp #define MHI_EV_CC_BAD_TRE	0x11
2325c2498839Sstsp 
2326c2498839Sstsp #define MHI_CMD_NOP		01
2327c2498839Sstsp #define MHI_CMD_RESET_CHAN	16
2328c2498839Sstsp #define MHI_CMD_STOP_CHAN	17
2329c2498839Sstsp #define MHI_CMD_START_CHAN	18
2330c2498839Sstsp 
2331c2498839Sstsp #define MHI_TRE_CMD_CHID_MASK	GENMASK(31, 24)
2332c2498839Sstsp #define MHI_TRE_CMD_CHID_SHFT	24
2333c2498839Sstsp #define MHI_TRE_CMD_CMDID_MASK	GENMASK(23, 16)
2334c2498839Sstsp #define MHI_TRE_CMD_CMDID_SHFT	16
2335c2498839Sstsp 
2336c2498839Sstsp #define MHI_TRE0_EV_LEN_MASK	GENMASK(15, 0)
2337c2498839Sstsp #define MHI_TRE0_EV_LEN_SHFT	0
2338c2498839Sstsp #define MHI_TRE0_EV_CODE_MASK	GENMASK(31, 24)
2339c2498839Sstsp #define MHI_TRE0_EV_CODE_SHFT	24
2340c2498839Sstsp #define MHI_TRE1_EV_TYPE_MASK	GENMASK(23, 16)
2341c2498839Sstsp #define MHI_TRE1_EV_TYPE_SHFT	16
2342c2498839Sstsp #define MHI_TRE1_EV_CHID_MASK	GENMASK(31, 24)
2343c2498839Sstsp #define MHI_TRE1_EV_CHID_SHFT	24
2344c2498839Sstsp 
2345c2498839Sstsp #define MHI_TRE0_DATA_LEN_MASK	GENMASK(15, 0)
2346c2498839Sstsp #define MHI_TRE0_DATA_LEN_SHFT	0
2347c2498839Sstsp #define MHI_TRE1_DATA_CHAIN	(1 << 0)
2348c2498839Sstsp #define MHI_TRE1_DATA_IEOB	(1 << 8)
2349c2498839Sstsp #define MHI_TRE1_DATA_IEOT	(1 << 9)
2350c2498839Sstsp #define MHI_TRE1_DATA_BEI	(1 << 10)
2351c2498839Sstsp #define MHI_TRE1_DATA_TYPE_MASK		GENMASK(23, 16)
2352c2498839Sstsp #define MHI_TRE1_DATA_TYPE_SHIFT	16
2353c2498839Sstsp #define MHI_TRE1_DATA_TYPE_TRANSFER	0x2
2354c2498839Sstsp 
2355c2498839Sstsp #define MHI_PKT_TYPE_INVALID			0x00
2356c2498839Sstsp #define MHI_PKT_TYPE_NOOP_CMD			0x01
2357c2498839Sstsp #define MHI_PKT_TYPE_TRANSFER			0x02
2358c2498839Sstsp #define MHI_PKT_TYPE_COALESCING			0x08
2359c2498839Sstsp #define MHI_PKT_TYPE_RESET_CHAN_CMD		0x10
2360c2498839Sstsp #define MHI_PKT_TYPE_STOP_CHAN_CMD		0x11
2361c2498839Sstsp #define MHI_PKT_TYPE_START_CHAN_CMD		0x12
2362c2498839Sstsp #define MHI_PKT_TYPE_STATE_CHANGE_EVENT		0x20
2363c2498839Sstsp #define MHI_PKT_TYPE_CMD_COMPLETION_EVENT	0x21
2364c2498839Sstsp #define MHI_PKT_TYPE_TX_EVENT			0x22
2365c2498839Sstsp #define MHI_PKT_TYPE_RSC_TX_EVENT		0x28
2366c2498839Sstsp #define MHI_PKT_TYPE_EE_EVENT			0x40
2367c2498839Sstsp #define MHI_PKT_TYPE_TSYNC_EVENT		0x48
2368c2498839Sstsp #define MHI_PKT_TYPE_BW_REQ_EVENT		0x50
2369c2498839Sstsp 
2370c2498839Sstsp 
2371c2498839Sstsp #define MHI_DMA_VEC_CHUNK_SIZE			524288 /* 512 KB */
2372c2498839Sstsp struct qwx_dma_vec_entry {
2373c2498839Sstsp 	uint64_t paddr;
2374c2498839Sstsp 	uint64_t size;
2375c2498839Sstsp };
2376c2498839Sstsp 
2377c2498839Sstsp void
2378c2498839Sstsp qwx_mhi_ring_doorbell(struct qwx_softc *sc, uint64_t db_addr, uint64_t val)
2379c2498839Sstsp {
2380c2498839Sstsp 	qwx_pci_write(sc, db_addr + 4, val >> 32);
2381c2498839Sstsp 	qwx_pci_write(sc, db_addr, val & 0xffffffff);
2382c2498839Sstsp }
2383c2498839Sstsp 
2384c2498839Sstsp void
2385c2498839Sstsp qwx_mhi_device_wake(struct qwx_softc *sc)
2386c2498839Sstsp {
2387c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2388c2498839Sstsp 
2389c2498839Sstsp 	/*
2390c2498839Sstsp 	 * Device wake is async only for now because we do not
2391c2498839Sstsp 	 * keep track of PM state in software.
2392c2498839Sstsp 	 */
2393c2498839Sstsp 	qwx_mhi_ring_doorbell(sc, psc->wake_db, 1);
2394c2498839Sstsp }
2395c2498839Sstsp 
2396c2498839Sstsp void
2397c2498839Sstsp qwx_mhi_device_zzz(struct qwx_softc *sc)
2398c2498839Sstsp {
2399c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2400c2498839Sstsp 
2401c2498839Sstsp 	qwx_mhi_ring_doorbell(sc, psc->wake_db, 0);
2402c2498839Sstsp }
2403c2498839Sstsp 
2404c2498839Sstsp int
2405c2498839Sstsp qwx_mhi_wake_db_clear_valid(struct qwx_softc *sc)
2406c2498839Sstsp {
2407c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2408c2498839Sstsp 
2409c2498839Sstsp 	return (psc->mhi_state == MHI_STATE_M0); /* TODO other states? */
2410c2498839Sstsp }
2411c2498839Sstsp 
2412c2498839Sstsp void
2413c2498839Sstsp qwx_mhi_init_xfer_rings(struct qwx_pci_softc *psc)
2414c2498839Sstsp {
2415c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2416c2498839Sstsp 	int i;
2417c2498839Sstsp 	uint32_t chcfg;
2418c2498839Sstsp 	struct qwx_pci_xfer_ring *ring;
2419c2498839Sstsp 	struct qwx_mhi_chan_ctxt *cbase, *c;
2420c2498839Sstsp 
2421c2498839Sstsp 	cbase = (struct qwx_mhi_chan_ctxt *)QWX_DMA_KVA(psc->chan_ctxt);
2422c2498839Sstsp 	for (i = 0; i < psc->max_chan; i++) {
2423c2498839Sstsp 		c = &cbase[i];
2424c2498839Sstsp 		chcfg = le32toh(c->chcfg);
2425c2498839Sstsp 		chcfg &= ~(MHI_CHAN_CTX_CHSTATE_MASK |
2426c2498839Sstsp 		    MHI_CHAN_CTX_BRSTMODE_MASK |
2427c2498839Sstsp 		    MHI_CHAN_CTX_POLLCFG_MASK);
2428c2498839Sstsp 		chcfg |= (MHI_CHAN_CTX_CHSTATE_DISABLED |
2429c2498839Sstsp 		    (MHI_CHAN_CTX_BRSTMODE_DISABLE <<
2430c2498839Sstsp 		    MHI_CHAN_CTX_BRSTMODE_SHFT));
2431c2498839Sstsp 		c->chcfg = htole32(chcfg);
2432c2498839Sstsp 		c->chtype = htole32(MHI_CHAN_TYPE_INVALID);
2433c2498839Sstsp 		c->erindex = 0;
2434c2498839Sstsp 	}
2435c2498839Sstsp 
2436c2498839Sstsp 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
2437c2498839Sstsp 		ring = &psc->xfer_rings[i];
2438c2498839Sstsp 		KASSERT(ring->mhi_chan_id < psc->max_chan);
2439c2498839Sstsp 		c = &cbase[ring->mhi_chan_id];
2440c2498839Sstsp 		c->chtype = htole32(ring->mhi_chan_direction);
2441c2498839Sstsp 		c->erindex = htole32(ring->mhi_chan_event_ring_index);
2442c2498839Sstsp 		ring->chan_ctxt = c;
2443c2498839Sstsp 	}
2444c2498839Sstsp 
2445c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->chan_ctxt), 0,
2446c2498839Sstsp 	    QWX_DMA_LEN(psc->chan_ctxt), BUS_DMASYNC_PREWRITE);
2447c2498839Sstsp }
2448c2498839Sstsp 
2449c2498839Sstsp void
2450c2498839Sstsp qwx_mhi_init_event_rings(struct qwx_pci_softc *psc)
2451c2498839Sstsp {
2452c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2453c2498839Sstsp 	int i;
2454c2498839Sstsp 	uint32_t intmod;
2455c2498839Sstsp 	uint64_t paddr, len;
2456c2498839Sstsp 	struct qwx_pci_event_ring *ring;
2457c2498839Sstsp 	struct qwx_mhi_event_ctxt *c;
2458c2498839Sstsp 
2459c2498839Sstsp 	c = (struct qwx_mhi_event_ctxt *)QWX_DMA_KVA(psc->event_ctxt);
2460c2498839Sstsp 	for (i = 0; i < nitems(psc->event_rings); i++, c++) {
2461c2498839Sstsp 		ring = &psc->event_rings[i];
2462c2498839Sstsp 
2463c2498839Sstsp 		ring->event_ctxt = c;
2464c2498839Sstsp 
2465c2498839Sstsp 		intmod = le32toh(c->intmod);
2466c2498839Sstsp 		intmod &= ~(MHI_EV_CTX_INTMODC_MASK | MHI_EV_CTX_INTMODT_MASK);
2467c2498839Sstsp 		intmod |= (ring->mhi_er_irq_moderation_ms <<
2468c2498839Sstsp 		    MHI_EV_CTX_INTMODT_SHFT) & MHI_EV_CTX_INTMODT_MASK;
2469c2498839Sstsp 		c->intmod = htole32(intmod);
2470c2498839Sstsp 
2471c2498839Sstsp 		c->ertype = htole32(MHI_ER_TYPE_VALID);
2472c2498839Sstsp 		c->msivec = htole32(ring->mhi_er_irq);
2473c2498839Sstsp 
2474c2498839Sstsp 		paddr = QWX_DMA_DVA(ring->dmamem);
2475c2498839Sstsp 		ring->rp = paddr;
2476c2498839Sstsp 		ring->wp = paddr + ring->size -
2477c2498839Sstsp 		    sizeof(struct qwx_mhi_ring_element);
2478c2498839Sstsp 		c->rbase = htole64(paddr);
2479c2498839Sstsp 		c->rp = htole64(ring->rp);
2480c2498839Sstsp 		c->wp = htole64(ring->wp);
2481c2498839Sstsp 
2482c2498839Sstsp 		len = sizeof(struct qwx_mhi_ring_element) * ring->num_elements;
2483c2498839Sstsp 		c->rlen = htole64(len);
2484c2498839Sstsp 	}
2485c2498839Sstsp 
2486c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->event_ctxt), 0,
2487c2498839Sstsp 	    QWX_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_PREWRITE);
2488c2498839Sstsp }
2489c2498839Sstsp 
2490c2498839Sstsp void
2491c2498839Sstsp qwx_mhi_init_cmd_ring(struct qwx_pci_softc *psc)
2492c2498839Sstsp {
2493c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2494c2498839Sstsp 	struct qwx_pci_cmd_ring *ring = &psc->cmd_ring;
2495c2498839Sstsp 	struct qwx_mhi_cmd_ctxt *c;
2496c2498839Sstsp 	uint64_t paddr, len;
2497c2498839Sstsp 
2498c2498839Sstsp 	paddr = QWX_DMA_DVA(ring->dmamem);
2499c2498839Sstsp 	len = ring->size;
2500c2498839Sstsp 
2501c2498839Sstsp 	ring->rp = ring->wp = paddr;
2502c2498839Sstsp 
2503c2498839Sstsp 	c = (struct qwx_mhi_cmd_ctxt *)QWX_DMA_KVA(psc->cmd_ctxt);
2504c2498839Sstsp 	c->rbase = htole64(paddr);
2505c2498839Sstsp 	c->rp = htole64(paddr);
2506c2498839Sstsp 	c->wp = htole64(paddr);
2507c2498839Sstsp 	c->rlen = htole64(len);
2508c2498839Sstsp 
2509c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->cmd_ctxt), 0,
2510c2498839Sstsp 	    QWX_DMA_LEN(psc->cmd_ctxt), BUS_DMASYNC_PREWRITE);
2511c2498839Sstsp }
2512c2498839Sstsp 
2513c2498839Sstsp void
2514c2498839Sstsp qwx_mhi_init_dev_ctxt(struct qwx_pci_softc *psc)
2515c2498839Sstsp {
2516c2498839Sstsp 	qwx_mhi_init_xfer_rings(psc);
2517c2498839Sstsp 	qwx_mhi_init_event_rings(psc);
2518c2498839Sstsp 	qwx_mhi_init_cmd_ring(psc);
2519c2498839Sstsp }
2520c2498839Sstsp 
2521c2498839Sstsp void *
2522c2498839Sstsp qwx_pci_cmd_ring_get_elem(struct qwx_pci_cmd_ring *ring, uint64_t ptr)
2523c2498839Sstsp {
2524c2498839Sstsp 	uint64_t base = QWX_DMA_DVA(ring->dmamem), offset;
2525c2498839Sstsp 
2526c2498839Sstsp 	if (ptr < base || ptr >= base + ring->size)
2527c2498839Sstsp 		return NULL;
2528c2498839Sstsp 
2529c2498839Sstsp 	offset = ptr - base;
2530c2498839Sstsp 	if (offset >= ring->size)
2531c2498839Sstsp 		return NULL;
2532c2498839Sstsp 
2533c2498839Sstsp 	return QWX_DMA_KVA(ring->dmamem) + offset;
2534c2498839Sstsp }
2535c2498839Sstsp 
2536c2498839Sstsp int
2537c2498839Sstsp qwx_mhi_cmd_ring_submit(struct qwx_pci_softc *psc,
2538c2498839Sstsp     struct qwx_pci_cmd_ring *ring)
2539c2498839Sstsp {
2540c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2541c2498839Sstsp 	uint64_t base = QWX_DMA_DVA(ring->dmamem);
2542c2498839Sstsp 	struct qwx_mhi_cmd_ctxt *c;
2543c2498839Sstsp 
2544c2498839Sstsp 	if (ring->queued >= ring->num_elements)
2545c2498839Sstsp 		return 1;
2546c2498839Sstsp 
2547c2498839Sstsp 	if (ring->wp + sizeof(struct qwx_mhi_ring_element) >= base + ring->size)
2548c2498839Sstsp 		ring->wp = base;
2549c2498839Sstsp 	else
2550c2498839Sstsp 		ring->wp += sizeof(struct qwx_mhi_ring_element);
2551c2498839Sstsp 
2552c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->cmd_ctxt), 0,
2553c2498839Sstsp 	    QWX_DMA_LEN(psc->cmd_ctxt), BUS_DMASYNC_POSTREAD);
2554c2498839Sstsp 
2555c2498839Sstsp 	c = (struct qwx_mhi_cmd_ctxt *)QWX_DMA_KVA(psc->cmd_ctxt);
2556c2498839Sstsp 	c->wp = htole64(ring->wp);
2557c2498839Sstsp 
2558c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->cmd_ctxt), 0,
2559c2498839Sstsp 	    QWX_DMA_LEN(psc->cmd_ctxt), BUS_DMASYNC_PREWRITE);
2560c2498839Sstsp 
2561c2498839Sstsp 	ring->queued++;
2562c2498839Sstsp 	qwx_mhi_ring_doorbell(sc, MHI_CRDB_LOWER, ring->wp);
2563c2498839Sstsp 	return 0;
2564c2498839Sstsp }
2565c2498839Sstsp 
2566c2498839Sstsp int
2567c2498839Sstsp qwx_mhi_send_cmd(struct qwx_pci_softc *psc, uint32_t cmd, uint32_t chan)
2568c2498839Sstsp {
2569c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2570c2498839Sstsp 	struct qwx_pci_cmd_ring	*ring = &psc->cmd_ring;
2571c2498839Sstsp 	struct qwx_mhi_ring_element *e;
2572c2498839Sstsp 
2573c2498839Sstsp 	if (ring->queued >= ring->num_elements) {
2574c2498839Sstsp 		printf("%s: command ring overflow\n", sc->sc_dev.dv_xname);
2575c2498839Sstsp 		return 1;
2576c2498839Sstsp 	}
2577c2498839Sstsp 
2578c2498839Sstsp 	e = qwx_pci_cmd_ring_get_elem(ring, ring->wp);
2579c2498839Sstsp 	if (e == NULL)
2580c2498839Sstsp 		return 1;
2581c2498839Sstsp 
2582c2498839Sstsp 	e->ptr = 0ULL;
2583c2498839Sstsp 	e->dword[0] = 0;
2584c2498839Sstsp 	e->dword[1] = htole32(
2585c2498839Sstsp 	    ((chan << MHI_TRE_CMD_CHID_SHFT) & MHI_TRE_CMD_CHID_MASK) |
2586c2498839Sstsp 	    ((cmd << MHI_TRE_CMD_CMDID_SHFT) & MHI_TRE_CMD_CMDID_MASK));
2587c2498839Sstsp 
2588c2498839Sstsp 	return qwx_mhi_cmd_ring_submit(psc, ring);
2589c2498839Sstsp }
2590c2498839Sstsp 
2591c2498839Sstsp void *
2592c2498839Sstsp qwx_pci_xfer_ring_get_elem(struct qwx_pci_xfer_ring *ring, uint64_t wp)
2593c2498839Sstsp {
2594c2498839Sstsp 	uint64_t base = QWX_DMA_DVA(ring->dmamem), offset;
2595c2498839Sstsp 	void *addr = QWX_DMA_KVA(ring->dmamem);
2596c2498839Sstsp 
2597c2498839Sstsp 	if (wp < base)
2598c2498839Sstsp 		return NULL;
2599c2498839Sstsp 
2600c2498839Sstsp 	offset = wp - base;
2601c2498839Sstsp 	if (offset >= ring->size)
2602c2498839Sstsp 		return NULL;
2603c2498839Sstsp 
2604c2498839Sstsp 	return addr + offset;
2605c2498839Sstsp }
2606c2498839Sstsp 
2607c2498839Sstsp struct qwx_xfer_data *
2608c2498839Sstsp qwx_pci_xfer_ring_get_data(struct qwx_pci_xfer_ring *ring, uint64_t wp)
2609c2498839Sstsp {
2610c2498839Sstsp 	uint64_t base = QWX_DMA_DVA(ring->dmamem), offset;
2611c2498839Sstsp 
2612c2498839Sstsp 	if (wp < base)
2613c2498839Sstsp 		return NULL;
2614c2498839Sstsp 
2615c2498839Sstsp 	offset = wp - base;
2616c2498839Sstsp 	if (offset >= ring->size)
2617c2498839Sstsp 		return NULL;
2618c2498839Sstsp 
2619c2498839Sstsp 	return &ring->data[offset / sizeof(ring->data[0])];
2620c2498839Sstsp }
2621c2498839Sstsp 
2622c2498839Sstsp int
2623c2498839Sstsp qwx_mhi_submit_xfer(struct qwx_softc *sc, struct mbuf *m)
2624c2498839Sstsp {
2625c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2626c2498839Sstsp 	struct qwx_pci_xfer_ring *ring;
2627c2498839Sstsp 	struct qwx_mhi_ring_element *e;
2628c2498839Sstsp 	struct qwx_xfer_data *xfer;
2629c2498839Sstsp 	uint64_t paddr, base;
2630c2498839Sstsp 	int err;
2631c2498839Sstsp 
2632c2498839Sstsp 	ring = &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_OUTBOUND];
2633c2498839Sstsp 
2634c2498839Sstsp 	if (ring->queued >= ring->num_elements)
2635c2498839Sstsp 		return 1;
2636c2498839Sstsp 
2637c2498839Sstsp 	if (m->m_pkthdr.len > QWX_PCI_XFER_MAX_DATA_SIZE) {
2638c2498839Sstsp 		/* TODO: chunk xfers */
2639c2498839Sstsp 		printf("%s: xfer too large: %d bytes\n", __func__, m->m_pkthdr.len);
2640c2498839Sstsp 		return 1;
2641c2498839Sstsp 
2642c2498839Sstsp 	}
2643c2498839Sstsp 
2644c2498839Sstsp 	e = qwx_pci_xfer_ring_get_elem(ring, ring->wp);
2645c2498839Sstsp 	if (e == NULL)
2646c2498839Sstsp 		return 1;
2647c2498839Sstsp 
2648c2498839Sstsp 	xfer = qwx_pci_xfer_ring_get_data(ring, ring->wp);
2649c2498839Sstsp 	if (xfer == NULL || xfer->m != NULL)
2650c2498839Sstsp 		return 1;
2651c2498839Sstsp 
2652c2498839Sstsp 	err = bus_dmamap_load_mbuf(sc->sc_dmat, xfer->map, m,
2653c2498839Sstsp 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
2654c2498839Sstsp 	if (err && err != EFBIG) {
2655c2498839Sstsp 		printf("%s: can't map mbuf (error %d)\n",
2656c2498839Sstsp 		    sc->sc_dev.dv_xname, err);
2657c2498839Sstsp 		return err;
2658c2498839Sstsp 	}
2659c2498839Sstsp 	if (err) {
2660c2498839Sstsp 		/* Too many DMA segments, linearize mbuf. */
2661c2498839Sstsp 		if (m_defrag(m, M_DONTWAIT))
2662c2498839Sstsp 			return ENOBUFS;
2663c2498839Sstsp 		err = bus_dmamap_load_mbuf(sc->sc_dmat, xfer->map, m,
2664c2498839Sstsp 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
2665c2498839Sstsp 		if (err) {
2666c2498839Sstsp 			printf("%s: can't map mbuf (error %d)\n",
2667c2498839Sstsp 			    sc->sc_dev.dv_xname, err);
2668c2498839Sstsp 			return err;
2669c2498839Sstsp 		}
2670c2498839Sstsp 	}
2671c2498839Sstsp 
2672c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, xfer->map, 0, m->m_pkthdr.len,
2673c2498839Sstsp 	    BUS_DMASYNC_PREWRITE);
2674c2498839Sstsp 
2675c2498839Sstsp 	xfer->m = m;
2676c2498839Sstsp 	paddr = xfer->map->dm_segs[0].ds_addr;
2677c2498839Sstsp 
2678c2498839Sstsp 	e->ptr = htole64(paddr);
2679c2498839Sstsp 	e->dword[0] = htole32((m->m_pkthdr.len << MHI_TRE0_DATA_LEN_SHFT) &
2680c2498839Sstsp 	    MHI_TRE0_DATA_LEN_MASK);
2681c2498839Sstsp 	e->dword[1] = htole32(MHI_TRE1_DATA_IEOT |
2682c2498839Sstsp 	    MHI_TRE1_DATA_TYPE_TRANSFER << MHI_TRE1_DATA_TYPE_SHIFT);
2683c2498839Sstsp 
2684c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(ring->dmamem),
2685c2498839Sstsp 	    0, QWX_DMA_LEN(ring->dmamem), BUS_DMASYNC_PREWRITE);
2686c2498839Sstsp 
2687c2498839Sstsp 	base = QWX_DMA_DVA(ring->dmamem);
2688c2498839Sstsp 	if (ring->wp + sizeof(struct qwx_mhi_ring_element) >= base + ring->size)
2689c2498839Sstsp 		ring->wp = base;
2690c2498839Sstsp 	else
2691c2498839Sstsp 		ring->wp += sizeof(struct qwx_mhi_ring_element);
2692c2498839Sstsp 	ring->queued++;
2693c2498839Sstsp 
2694c2498839Sstsp 	ring->chan_ctxt->wp = htole64(ring->wp);
2695c2498839Sstsp 
2696c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->chan_ctxt), 0,
2697c2498839Sstsp 	    QWX_DMA_LEN(psc->chan_ctxt), BUS_DMASYNC_PREWRITE);
2698c2498839Sstsp 
2699c2498839Sstsp 	qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
2700c2498839Sstsp 	return 0;
2701c2498839Sstsp }
2702c2498839Sstsp 
2703c2498839Sstsp int
2704c2498839Sstsp qwx_mhi_start_channel(struct qwx_pci_softc *psc,
2705c2498839Sstsp 	struct qwx_pci_xfer_ring *ring)
2706c2498839Sstsp {
2707c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2708c2498839Sstsp 	struct qwx_mhi_chan_ctxt *c;
2709c2498839Sstsp 	int ret = 0;
2710c2498839Sstsp 	uint32_t chcfg;
2711c2498839Sstsp 	uint64_t paddr, len;
2712c2498839Sstsp 
2713c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: start MHI channel %d in state %d\n", __func__,
2714c2498839Sstsp 	    ring->mhi_chan_id, ring->mhi_chan_state);
2715c2498839Sstsp 
2716c2498839Sstsp 	c = ring->chan_ctxt;
2717c2498839Sstsp 
2718c2498839Sstsp 	chcfg = le32toh(c->chcfg);
2719c2498839Sstsp 	chcfg &= ~MHI_CHAN_CTX_CHSTATE_MASK;
2720c2498839Sstsp 	chcfg |= MHI_CHAN_CTX_CHSTATE_ENABLED;
2721c2498839Sstsp 	c->chcfg = htole32(chcfg);
2722c2498839Sstsp 
2723c2498839Sstsp 	paddr = QWX_DMA_DVA(ring->dmamem);
2724c2498839Sstsp 	ring->rp = ring->wp = paddr;
2725c2498839Sstsp 	c->rbase = htole64(paddr);
2726c2498839Sstsp 	c->rp = htole64(ring->rp);
2727c2498839Sstsp 	c->wp = htole64(ring->wp);
2728c2498839Sstsp 	len = sizeof(struct qwx_mhi_ring_element) * ring->num_elements;
2729c2498839Sstsp 	c->rlen = htole64(len);
2730c2498839Sstsp 
2731c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->chan_ctxt), 0,
2732c2498839Sstsp 	    QWX_DMA_LEN(psc->chan_ctxt), BUS_DMASYNC_PREWRITE);
2733c2498839Sstsp 
2734c2498839Sstsp 	ring->cmd_status = MHI_EV_CC_INVALID;
2735c2498839Sstsp 	if (qwx_mhi_send_cmd(psc, MHI_CMD_START_CHAN, ring->mhi_chan_id))
2736c2498839Sstsp 		return 1;
2737c2498839Sstsp 
2738c2498839Sstsp 	while (ring->cmd_status != MHI_EV_CC_SUCCESS) {
2739c2498839Sstsp 		ret = tsleep_nsec(&ring->cmd_status, 0, "qwxcmd",
2740c2498839Sstsp 		    SEC_TO_NSEC(5));
2741c2498839Sstsp 		if (ret)
2742c2498839Sstsp 			break;
2743c2498839Sstsp 	}
2744c2498839Sstsp 
2745c2498839Sstsp 	if (ret) {
2746c2498839Sstsp 		printf("%s: could not start MHI channel %d in state %d: status 0x%x\n",
2747c2498839Sstsp 		    sc->sc_dev.dv_xname, ring->mhi_chan_id,
2748c2498839Sstsp 		    ring->mhi_chan_state, ring->cmd_status);
2749c2498839Sstsp 		return 1;
2750c2498839Sstsp 	}
2751c2498839Sstsp 
2752c2498839Sstsp 	if (ring->mhi_chan_direction == MHI_CHAN_TYPE_INBOUND) {
2753c2498839Sstsp 		uint64_t wp = QWX_DMA_DVA(ring->dmamem);
2754c2498839Sstsp 		int i;
2755c2498839Sstsp 
2756c2498839Sstsp 		for (i = 0; i < ring->num_elements; i++) {
2757c2498839Sstsp 			struct qwx_mhi_ring_element *e;
2758c2498839Sstsp 			struct qwx_xfer_data *xfer;
2759c2498839Sstsp 			uint64_t paddr;
2760c2498839Sstsp 
2761c2498839Sstsp 			e = qwx_pci_xfer_ring_get_elem(ring, wp);
2762c2498839Sstsp 			xfer = qwx_pci_xfer_ring_get_data(ring, wp);
2763c2498839Sstsp 			paddr = xfer->map->dm_segs[0].ds_addr;
2764c2498839Sstsp 
2765c2498839Sstsp 			e->ptr = htole64(paddr);
2766c2498839Sstsp 			e->dword[0] = htole32((QWX_PCI_XFER_MAX_DATA_SIZE <<
2767c2498839Sstsp 			    MHI_TRE0_DATA_LEN_SHFT) &
2768c2498839Sstsp 			    MHI_TRE0_DATA_LEN_MASK);
2769c2498839Sstsp 			e->dword[1] = htole32(MHI_TRE1_DATA_IEOT |
2770c2498839Sstsp 			    MHI_TRE1_DATA_BEI |
2771c2498839Sstsp 			    MHI_TRE1_DATA_TYPE_TRANSFER <<
2772c2498839Sstsp 			    MHI_TRE1_DATA_TYPE_SHIFT);
2773c2498839Sstsp 
2774c2498839Sstsp 			ring->wp = wp;
2775c2498839Sstsp 			wp += sizeof(*e);
2776c2498839Sstsp 		}
2777c2498839Sstsp 
2778c2498839Sstsp 		bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(ring->dmamem), 0,
2779c2498839Sstsp 		    QWX_DMA_LEN(ring->dmamem), BUS_DMASYNC_PREWRITE);
2780c2498839Sstsp 
2781c2498839Sstsp 		qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
2782c2498839Sstsp 	}
2783c2498839Sstsp 
2784c2498839Sstsp 	return 0;
2785c2498839Sstsp }
2786c2498839Sstsp 
2787c2498839Sstsp int
2788c2498839Sstsp qwx_mhi_start_channels(struct qwx_pci_softc *psc)
2789c2498839Sstsp {
2790c2498839Sstsp 	struct qwx_pci_xfer_ring *ring;
2791c2498839Sstsp 	int ret = 0;
2792c2498839Sstsp 
2793c2498839Sstsp 	qwx_mhi_device_wake(&psc->sc_sc);
2794c2498839Sstsp 
2795c2498839Sstsp 	ring = &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_OUTBOUND];
2796c2498839Sstsp 	if (qwx_mhi_start_channel(psc, ring)) {
2797c2498839Sstsp 		ret = 1;
2798c2498839Sstsp 		goto done;
2799c2498839Sstsp 	}
2800c2498839Sstsp 
2801c2498839Sstsp 	ring = &psc->xfer_rings[QWX_PCI_XFER_RING_IPCR_INBOUND];
2802c2498839Sstsp 	if (qwx_mhi_start_channel(psc, ring))
2803c2498839Sstsp 		ret = 1;
2804c2498839Sstsp done:
2805c2498839Sstsp 	qwx_mhi_device_zzz(&psc->sc_sc);
2806c2498839Sstsp 	return ret;
2807c2498839Sstsp }
2808c2498839Sstsp 
2809c2498839Sstsp int
2810c2498839Sstsp qwx_mhi_start(struct qwx_pci_softc *psc)
2811c2498839Sstsp {
2812c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2813c2498839Sstsp 	uint32_t off;
2814c2498839Sstsp 	uint32_t ee, state;
2815c2498839Sstsp 	int ret;
2816c2498839Sstsp 
2817c2498839Sstsp 	qwx_mhi_init_dev_ctxt(psc);
2818c2498839Sstsp 
2819c2498839Sstsp 	psc->bhi_off = qwx_pci_read(sc, MHI_BHI_OFFSET);
2820c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: BHI offset 0x%x\n", __func__, psc->bhi_off);
2821c2498839Sstsp 
2822c2498839Sstsp 	psc->bhie_off = qwx_pci_read(sc, MHI_BHIE_OFFSET);
2823c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: BHIE offset 0x%x\n", __func__, psc->bhie_off);
2824c2498839Sstsp 
2825c2498839Sstsp 	/* Clean BHIE RX registers */
2826c2498839Sstsp 	for (off = MHI_BHIE_RXVECADDR_LOW_OFFS;
2827c2498839Sstsp 	     off < (MHI_BHIE_RXVECSTATUS_OFFS - 4);
2828c2498839Sstsp 	     off += 4)
2829c2498839Sstsp 	     	qwx_pci_write(sc, psc->bhie_off + off, 0x0);
2830c2498839Sstsp 
2831c2498839Sstsp 	qwx_rddm_prepare(psc);
2832c2498839Sstsp 
2833c2498839Sstsp 	/* Program BHI INTVEC */
2834c2498839Sstsp 	qwx_pci_write(sc, psc->bhi_off + MHI_BHI_INTVEC, 0x00);
2835c2498839Sstsp 
2836c2498839Sstsp 	/*
2837c2498839Sstsp 	 * Get BHI execution environment and confirm that it is valid
2838c2498839Sstsp 	 * for power on.
2839c2498839Sstsp 	 */
2840c2498839Sstsp 	ee = qwx_pci_read(sc, psc->bhi_off + MHI_BHI_EXECENV);
2841c2498839Sstsp 	if (!MHI_POWER_UP_CAPABLE(ee)) {
2842c2498839Sstsp 		printf("%s: invalid EE for power on: 0x%x\n",
2843c2498839Sstsp 		     sc->sc_dev.dv_xname, ee);
2844c2498839Sstsp 		return 1;
2845c2498839Sstsp 	}
2846c2498839Sstsp 
2847c2498839Sstsp 	/*
2848c2498839Sstsp 	 * Get MHI state of the device and reset it if it is in system
2849c2498839Sstsp 	 * error.
2850c2498839Sstsp 	 */
2851c2498839Sstsp 	state = qwx_pci_read(sc, MHI_STATUS);
2852c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: MHI power on with EE: 0x%x, status: 0x%x\n",
2853c2498839Sstsp 	     sc->sc_dev.dv_xname, ee, state);
2854c2498839Sstsp 	state = (state & MHI_STATUS_MHISTATE_MASK) >> MHI_STATUS_MHISTATE_SHFT;
2855c2498839Sstsp 	if (state == MHI_STATE_SYS_ERR) {
2856c2498839Sstsp 		if (qwx_mhi_reset_device(sc, 0))
2857c2498839Sstsp 			return 1;
2858c2498839Sstsp 		state = qwx_pci_read(sc, MHI_STATUS);
2859c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: MHI state after reset: 0x%x\n",
2860c2498839Sstsp 		    sc->sc_dev.dv_xname, state);
2861c2498839Sstsp 		state = (state & MHI_STATUS_MHISTATE_MASK) >>
2862c2498839Sstsp 		    MHI_STATUS_MHISTATE_SHFT;
2863c2498839Sstsp 		if (state == MHI_STATE_SYS_ERR) {
2864c2498839Sstsp 			printf("%s: MHI stuck in system error state\n",
2865c2498839Sstsp 			    sc->sc_dev.dv_xname);
2866c2498839Sstsp 			return 1;
2867c2498839Sstsp 		}
2868c2498839Sstsp 	}
2869c2498839Sstsp 
2870c2498839Sstsp 	psc->bhi_ee = ee;
2871c2498839Sstsp 	psc->mhi_state = state;
2872c2498839Sstsp 
2873c2498839Sstsp #if notyet
2874c2498839Sstsp 	/* Enable IRQs */
2875c2498839Sstsp 	//  XXX todo?
2876c2498839Sstsp #endif
2877c2498839Sstsp 
2878c2498839Sstsp 	/* Transition to primary runtime. */
2879c2498839Sstsp 	if (MHI_IN_PBL(ee)) {
2880c2498839Sstsp 		ret = qwx_mhi_fw_load_handler(psc);
2881c2498839Sstsp 		if (ret)
2882c2498839Sstsp 			return ret;
28834b7d609eSstsp 
28844b7d609eSstsp 		/* XXX without this delay starting the channels may fail */
28854b7d609eSstsp 		delay(1000);
28864b7d609eSstsp 		qwx_mhi_start_channels(psc);
2887c2498839Sstsp 	} else {
2888c2498839Sstsp 		/* XXX Handle partially initialized device...?!? */
2889c2498839Sstsp 		ee = qwx_pci_read(sc, psc->bhi_off + MHI_BHI_EXECENV);
2890c2498839Sstsp 		if (!MHI_IN_MISSION_MODE(ee)) {
2891c2498839Sstsp 			printf("%s: failed to power up MHI, ee=0x%x\n",
2892c2498839Sstsp 			    sc->sc_dev.dv_xname, ee);
2893c2498839Sstsp 			return EIO;
2894c2498839Sstsp 		}
2895c2498839Sstsp 	}
2896c2498839Sstsp 
2897c2498839Sstsp 	return 0;
2898c2498839Sstsp }
2899c2498839Sstsp 
2900c2498839Sstsp void
2901c2498839Sstsp qwx_mhi_stop(struct qwx_softc *sc)
2902c2498839Sstsp {
2903c2498839Sstsp 	qwx_mhi_reset_device(sc, 1);
2904c2498839Sstsp }
2905c2498839Sstsp 
2906c2498839Sstsp int
2907c2498839Sstsp qwx_mhi_reset_device(struct qwx_softc *sc, int force)
2908c2498839Sstsp {
2909c2498839Sstsp 	struct qwx_pci_softc *psc = (struct qwx_pci_softc *)sc;
2910c2498839Sstsp 	uint32_t reg;
2911c2498839Sstsp 	int ret = 0;
2912c2498839Sstsp 
2913c2498839Sstsp 	reg = qwx_pcic_read32(sc, MHI_STATUS);
2914c2498839Sstsp 
2915c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: MHISTATUS 0x%x\n", sc->sc_dev.dv_xname, reg);
2916c2498839Sstsp 	/*
2917c2498839Sstsp 	 * Observed on QCA6390 that after SOC_GLOBAL_RESET, MHISTATUS
2918c2498839Sstsp 	 * has SYSERR bit set and thus need to set MHICTRL_RESET
2919c2498839Sstsp 	 * to clear SYSERR.
2920c2498839Sstsp 	 */
2921c2498839Sstsp 	if (force || (reg & MHI_STATUS_SYSERR_MASK)) {
2922c2498839Sstsp 		/* Trigger MHI Reset in device. */
2923c2498839Sstsp 		qwx_pcic_write32(sc, MHI_CTRL, MHI_CTRL_RESET_MASK);
2924c2498839Sstsp 
2925c2498839Sstsp 		/* Wait for the reset bit to be cleared by the device. */
2926c2498839Sstsp 		ret = qwx_mhi_await_device_reset(sc);
2927c2498839Sstsp 		if (ret)
2928c2498839Sstsp 			return ret;
2929c2498839Sstsp 
2930c2498839Sstsp 		if (psc->bhi_off == 0)
2931c2498839Sstsp 			psc->bhi_off = qwx_pci_read(sc, MHI_BHI_OFFSET);
2932c2498839Sstsp 
2933c2498839Sstsp 		/* Device clear BHI INTVEC so re-program it. */
2934c2498839Sstsp 		qwx_pci_write(sc, psc->bhi_off + MHI_BHI_INTVEC, 0x00);
2935c2498839Sstsp 	}
2936c2498839Sstsp 
2937c2498839Sstsp 	return 0;
2938c2498839Sstsp }
2939c2498839Sstsp 
2940c2498839Sstsp static inline void
2941c2498839Sstsp qwx_mhi_reset_txvecdb(struct qwx_softc *sc)
2942c2498839Sstsp {
2943c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_TXVECDB, 0);
2944c2498839Sstsp }
2945c2498839Sstsp 
2946c2498839Sstsp static inline void
2947c2498839Sstsp qwx_mhi_reset_txvecstatus(struct qwx_softc *sc)
2948c2498839Sstsp {
2949c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_TXVECSTATUS, 0);
2950c2498839Sstsp }
2951c2498839Sstsp 
2952c2498839Sstsp static inline void
2953c2498839Sstsp qwx_mhi_reset_rxvecdb(struct qwx_softc *sc)
2954c2498839Sstsp {
2955c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_RXVECDB, 0);
2956c2498839Sstsp }
2957c2498839Sstsp 
2958c2498839Sstsp static inline void
2959c2498839Sstsp qwx_mhi_reset_rxvecstatus(struct qwx_softc *sc)
2960c2498839Sstsp {
2961c2498839Sstsp 	qwx_pcic_write32(sc, PCIE_RXVECSTATUS, 0);
2962c2498839Sstsp }
2963c2498839Sstsp 
2964c2498839Sstsp void
2965c2498839Sstsp qwx_mhi_clear_vector(struct qwx_softc *sc)
2966c2498839Sstsp {
2967c2498839Sstsp 	qwx_mhi_reset_txvecdb(sc);
2968c2498839Sstsp 	qwx_mhi_reset_txvecstatus(sc);
2969c2498839Sstsp 	qwx_mhi_reset_rxvecdb(sc);
2970c2498839Sstsp 	qwx_mhi_reset_rxvecstatus(sc);
2971c2498839Sstsp }
2972c2498839Sstsp 
2973c2498839Sstsp int
2974c2498839Sstsp qwx_mhi_fw_load_handler(struct qwx_pci_softc *psc)
2975c2498839Sstsp {
2976c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
2977c2498839Sstsp 	int ret;
2978c2498839Sstsp 	char amss_path[PATH_MAX];
2979c2498839Sstsp 	u_char *data;
2980c2498839Sstsp 	size_t len;
2981c2498839Sstsp 
29829132454eSstsp 	if (sc->fw_img[QWX_FW_AMSS].data) {
29839132454eSstsp 		data = sc->fw_img[QWX_FW_AMSS].data;
29849132454eSstsp 		len = sc->fw_img[QWX_FW_AMSS].size;
29859132454eSstsp 	} else {
2986a533396bSstsp 		ret = snprintf(amss_path, sizeof(amss_path), "%s-%s-%s",
2987c2498839Sstsp 		    ATH11K_FW_DIR, sc->hw_params.fw.dir, ATH11K_AMSS_FILE);
2988c2498839Sstsp 		if (ret < 0 || ret >= sizeof(amss_path))
2989c2498839Sstsp 			return ENOSPC;
2990c2498839Sstsp 
2991c2498839Sstsp 		ret = loadfirmware(amss_path, &data, &len);
2992c2498839Sstsp 		if (ret) {
2993c2498839Sstsp 			printf("%s: could not read %s (error %d)\n",
2994c2498839Sstsp 			    sc->sc_dev.dv_xname, amss_path, ret);
2995c2498839Sstsp 			return ret;
2996c2498839Sstsp 		}
2997c2498839Sstsp 
2998c2498839Sstsp 		if (len < MHI_DMA_VEC_CHUNK_SIZE) {
2999c2498839Sstsp 			printf("%s: %s is too short, have only %zu bytes\n",
3000c2498839Sstsp 			    sc->sc_dev.dv_xname, amss_path, len);
3001c2498839Sstsp 			free(data, M_DEVBUF, len);
3002c2498839Sstsp 			return EINVAL;
3003c2498839Sstsp 		}
3004c2498839Sstsp 
30059132454eSstsp 		sc->fw_img[QWX_FW_AMSS].data = data;
30069132454eSstsp 		sc->fw_img[QWX_FW_AMSS].size = len;
30079132454eSstsp 	}
30089132454eSstsp 
3009c2498839Sstsp 	/* Second-stage boot loader sits in the first 512 KB of image. */
3010c2498839Sstsp 	ret = qwx_mhi_fw_load_bhi(psc, data, MHI_DMA_VEC_CHUNK_SIZE);
3011c2498839Sstsp 	if (ret != 0) {
3012c2498839Sstsp 		printf("%s: could not load firmware %s\n",
3013c2498839Sstsp 		    sc->sc_dev.dv_xname, amss_path);
3014c2498839Sstsp 		return ret;
3015c2498839Sstsp 	}
3016c2498839Sstsp 
3017c2498839Sstsp 	/* Now load the full image. */
3018c2498839Sstsp 	ret = qwx_mhi_fw_load_bhie(psc, data, len);
3019c2498839Sstsp 	if (ret != 0) {
3020c2498839Sstsp 		printf("%s: could not load firmware %s\n",
3021c2498839Sstsp 		    sc->sc_dev.dv_xname, amss_path);
30229132454eSstsp 		return ret;
3023c2498839Sstsp 	}
3024c2498839Sstsp 
3025c2498839Sstsp 	while (psc->bhi_ee < MHI_EE_AMSS) {
3026c2498839Sstsp 		ret = tsleep_nsec(&psc->bhi_ee, 0, "qwxamss",
3027c2498839Sstsp 		    SEC_TO_NSEC(5));
3028c2498839Sstsp 		if (ret)
3029c2498839Sstsp 			break;
3030c2498839Sstsp 	}
3031c2498839Sstsp 	if (ret != 0) {
3032c2498839Sstsp 		printf("%s: device failed to enter AMSS EE\n",
3033c2498839Sstsp 		    sc->sc_dev.dv_xname);
3034c2498839Sstsp 	}
3035c2498839Sstsp 
3036c2498839Sstsp 	return ret;
3037c2498839Sstsp }
3038c2498839Sstsp 
3039c2498839Sstsp int
3040c2498839Sstsp qwx_mhi_await_device_reset(struct qwx_softc *sc)
3041c2498839Sstsp {
3042c2498839Sstsp 	const uint32_t msecs = 24, retries = 2;
3043c2498839Sstsp 	uint32_t reg;
3044c2498839Sstsp 	int timeout;
3045c2498839Sstsp 
3046c2498839Sstsp 	/* Poll for CTRL RESET to clear. */
3047c2498839Sstsp 	timeout = retries;
3048c2498839Sstsp 	while (timeout > 0) {
3049c2498839Sstsp 		reg = qwx_pci_read(sc, MHI_CTRL);
3050c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: MHI_CTRL is 0x%x\n", __func__, reg);
3051c2498839Sstsp 		if ((reg & MHI_CTRL_RESET_MASK) == 0)
3052c2498839Sstsp 			break;
3053c2498839Sstsp 		DELAY((msecs / retries) * 1000);
3054c2498839Sstsp 		timeout--;
3055c2498839Sstsp 	}
3056c2498839Sstsp 	if (timeout == 0) {
3057c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: MHI reset failed\n", __func__);
3058c2498839Sstsp 		return ETIMEDOUT;
3059c2498839Sstsp 	}
3060c2498839Sstsp 
3061c2498839Sstsp 	return 0;
3062c2498839Sstsp }
3063c2498839Sstsp 
3064c2498839Sstsp int
3065c2498839Sstsp qwx_mhi_await_device_ready(struct qwx_softc *sc)
3066c2498839Sstsp {
3067c2498839Sstsp 	uint32_t reg;
3068c2498839Sstsp 	int timeout;
3069c2498839Sstsp 	const uint32_t msecs = 2000, retries = 4;
3070c2498839Sstsp 
3071c2498839Sstsp 
3072c2498839Sstsp 	/* Poll for READY to be set. */
3073c2498839Sstsp 	timeout = retries;
3074c2498839Sstsp 	while (timeout > 0) {
3075c2498839Sstsp 		reg = qwx_pci_read(sc, MHI_STATUS);
3076c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: MHI_STATUS is 0x%x\n", __func__, reg);
3077c2498839Sstsp 		if (reg & MHI_STATUS_READY_MASK) {
3078c2498839Sstsp 			reg &= ~MHI_STATUS_READY_MASK;
3079c2498839Sstsp 			qwx_pci_write(sc, MHI_STATUS, reg);
3080c2498839Sstsp 			break;
3081c2498839Sstsp 		}
3082c2498839Sstsp 		DELAY((msecs / retries) * 1000);
3083c2498839Sstsp 		timeout--;
3084c2498839Sstsp 	}
3085c2498839Sstsp 	if (timeout == 0) {
3086c2498839Sstsp 		printf("%s: MHI not ready\n", sc->sc_dev.dv_xname);
3087c2498839Sstsp 		return ETIMEDOUT;
3088c2498839Sstsp 	}
3089c2498839Sstsp 
3090c2498839Sstsp 	return 0;
3091c2498839Sstsp }
3092c2498839Sstsp 
3093c2498839Sstsp void
3094c2498839Sstsp qwx_mhi_ready_state_transition(struct qwx_pci_softc *psc)
3095c2498839Sstsp {
3096c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3097c2498839Sstsp 	int ret, i;
3098c2498839Sstsp 
3099c2498839Sstsp 	ret = qwx_mhi_await_device_reset(sc);
3100c2498839Sstsp 	if (ret)
3101c2498839Sstsp 		return;
3102c2498839Sstsp 
3103c2498839Sstsp 	ret = qwx_mhi_await_device_ready(sc);
3104c2498839Sstsp 	if (ret)
3105c2498839Sstsp 		return;
3106c2498839Sstsp 
3107c2498839Sstsp 	/* Set up memory-mapped IO for channels, events, etc. */
3108c2498839Sstsp 	qwx_mhi_init_mmio(psc);
3109c2498839Sstsp 
3110c2498839Sstsp 	/* Notify event rings. */
3111c2498839Sstsp 	for (i = 0; i < nitems(psc->event_rings); i++) {
3112c2498839Sstsp 		struct qwx_pci_event_ring *ring = &psc->event_rings[i];
3113c2498839Sstsp 		qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
3114c2498839Sstsp 	}
3115c2498839Sstsp 
3116c2498839Sstsp 	/*
3117c2498839Sstsp 	 * Set the device into M0 state. The device will transition
3118c2498839Sstsp 	 * into M0 and the execution environment will switch to SBL.
3119c2498839Sstsp 	 */
3120c2498839Sstsp 	qwx_mhi_set_state(sc, MHI_STATE_M0);
3121c2498839Sstsp }
3122c2498839Sstsp 
3123c2498839Sstsp void
3124c2498839Sstsp qwx_mhi_mission_mode_state_transition(struct qwx_pci_softc *psc)
3125c2498839Sstsp {
3126c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3127c2498839Sstsp 	int i;
3128c2498839Sstsp 
3129c2498839Sstsp 	qwx_mhi_device_wake(sc);
3130c2498839Sstsp 
3131c2498839Sstsp 	/* Notify event rings. */
3132c2498839Sstsp 	for (i = 0; i < nitems(psc->event_rings); i++) {
3133c2498839Sstsp 		struct qwx_pci_event_ring *ring = &psc->event_rings[i];
3134c2498839Sstsp 		qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
3135c2498839Sstsp 	}
3136c2498839Sstsp 
3137c2498839Sstsp 	/* TODO: Notify transfer/command rings? */
3138c2498839Sstsp 
3139c2498839Sstsp 	qwx_mhi_device_zzz(sc);
3140c2498839Sstsp }
3141c2498839Sstsp 
3142c2498839Sstsp void
3143a9f6aa33Sstsp qwx_mhi_low_power_mode_state_transition(struct qwx_pci_softc *psc)
3144a9f6aa33Sstsp {
3145a9f6aa33Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3146a9f6aa33Sstsp 
3147a9f6aa33Sstsp 	qwx_mhi_set_state(sc, MHI_STATE_M2);
3148a9f6aa33Sstsp }
3149a9f6aa33Sstsp 
3150a9f6aa33Sstsp void
3151c2498839Sstsp qwx_mhi_set_state(struct qwx_softc *sc, uint32_t state)
3152c2498839Sstsp {
3153c2498839Sstsp 	uint32_t reg;
3154c2498839Sstsp 
3155c2498839Sstsp 	reg = qwx_pci_read(sc, MHI_CTRL);
3156c2498839Sstsp 
3157c2498839Sstsp 	if (state != MHI_STATE_RESET) {
3158c2498839Sstsp 		reg &= ~MHI_CTRL_MHISTATE_MASK;
3159c2498839Sstsp 		reg |= (state << MHI_CTRL_MHISTATE_SHFT) & MHI_CTRL_MHISTATE_MASK;
3160c2498839Sstsp 	} else
3161c2498839Sstsp 		reg |= MHI_CTRL_RESET_MASK;
3162c2498839Sstsp 
3163c2498839Sstsp 	qwx_pci_write(sc, MHI_CTRL, reg);
3164c2498839Sstsp }
3165c2498839Sstsp 
3166c2498839Sstsp void
3167c2498839Sstsp qwx_mhi_init_mmio(struct qwx_pci_softc *psc)
3168c2498839Sstsp {
3169c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3170c2498839Sstsp 	uint64_t paddr;
3171c2498839Sstsp 	uint32_t reg;
3172c2498839Sstsp 	int i;
3173c2498839Sstsp 
3174c2498839Sstsp 	reg = qwx_pci_read(sc, MHI_CHDBOFF);
3175c2498839Sstsp 
3176c2498839Sstsp 	/* Set device wake doorbell address. */
3177c2498839Sstsp 	psc->wake_db = reg + 8 * MHI_DEV_WAKE_DB;
3178c2498839Sstsp 
3179c2498839Sstsp 	/* Set doorbell address for each transfer ring. */
3180c2498839Sstsp 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
3181c2498839Sstsp 		struct qwx_pci_xfer_ring *ring = &psc->xfer_rings[i];
3182c2498839Sstsp 		ring->db_addr = reg + (8 * ring->mhi_chan_id);
3183c2498839Sstsp 	}
3184c2498839Sstsp 
3185c2498839Sstsp 	reg = qwx_pci_read(sc, MHI_ERDBOFF);
3186c2498839Sstsp 	/* Set doorbell address for each event ring. */
3187c2498839Sstsp 	for (i = 0; i < nitems(psc->event_rings); i++) {
3188c2498839Sstsp 		struct qwx_pci_event_ring *ring = &psc->event_rings[i];
3189c2498839Sstsp 		ring->db_addr = reg + (8 * i);
3190c2498839Sstsp 	}
3191c2498839Sstsp 
3192c2498839Sstsp 	paddr = QWX_DMA_DVA(psc->chan_ctxt);
3193c2498839Sstsp 	qwx_pci_write(sc, MHI_CCABAP_HIGHER, paddr >> 32);
3194c2498839Sstsp 	qwx_pci_write(sc, MHI_CCABAP_LOWER, paddr & 0xffffffff);
3195c2498839Sstsp 
3196c2498839Sstsp 	paddr = QWX_DMA_DVA(psc->event_ctxt);
3197c2498839Sstsp 	qwx_pci_write(sc, MHI_ECABAP_HIGHER, paddr >> 32);
3198c2498839Sstsp 	qwx_pci_write(sc, MHI_ECABAP_LOWER, paddr & 0xffffffff);
3199c2498839Sstsp 
3200c2498839Sstsp 	paddr = QWX_DMA_DVA(psc->cmd_ctxt);
3201c2498839Sstsp 	qwx_pci_write(sc, MHI_CRCBAP_HIGHER, paddr >> 32);
3202c2498839Sstsp 	qwx_pci_write(sc, MHI_CRCBAP_LOWER, paddr & 0xffffffff);
3203c2498839Sstsp 
3204c2498839Sstsp 	/* Not (yet?) using fixed memory space from a device-tree. */
3205c2498839Sstsp 	qwx_pci_write(sc, MHI_CTRLBASE_HIGHER, 0);
3206c2498839Sstsp 	qwx_pci_write(sc, MHI_CTRLBASE_LOWER, 0);
3207c2498839Sstsp 	qwx_pci_write(sc, MHI_DATABASE_HIGHER, 0);
3208c2498839Sstsp 	qwx_pci_write(sc, MHI_DATABASE_LOWER, 0);
3209c2498839Sstsp 	qwx_pci_write(sc, MHI_CTRLLIMIT_HIGHER, 0x0);
3210c2498839Sstsp 	qwx_pci_write(sc, MHI_CTRLLIMIT_LOWER, 0xffffffff);
3211c2498839Sstsp 	qwx_pci_write(sc, MHI_DATALIMIT_HIGHER, 0x0);
3212c2498839Sstsp 	qwx_pci_write(sc, MHI_DATALIMIT_LOWER, 0xffffffff);
3213c2498839Sstsp 
3214c2498839Sstsp 	reg = qwx_pci_read(sc, MHI_CFG);
3215c2498839Sstsp 	reg &= ~(MHI_CFG_NER_MASK | MHI_CFG_NHWER_MASK);
3216c2498839Sstsp 	reg |= QWX_NUM_EVENT_CTX << MHI_CFG_NER_SHFT;
3217c2498839Sstsp 	qwx_pci_write(sc, MHI_CFG, reg);
3218c2498839Sstsp }
3219c2498839Sstsp 
3220c2498839Sstsp int
3221c2498839Sstsp qwx_mhi_fw_load_bhi(struct qwx_pci_softc *psc, uint8_t *data, size_t len)
3222c2498839Sstsp {
3223c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3224c2498839Sstsp 	struct qwx_dmamem *data_adm;
3225c2498839Sstsp 	uint32_t seq, reg, status = MHI_BHI_STATUS_RESET;
3226c2498839Sstsp 	uint64_t paddr;
3227c2498839Sstsp 	int ret;
3228c2498839Sstsp 
3229c2498839Sstsp 	data_adm = qwx_dmamem_alloc(sc->sc_dmat, len, 0);
3230c2498839Sstsp 	if (data_adm == NULL) {
3231c2498839Sstsp 		printf("%s: could not allocate BHI DMA data buffer\n",
3232c2498839Sstsp 		    sc->sc_dev.dv_xname);
3233c2498839Sstsp 		return 1;
3234c2498839Sstsp 	}
3235c2498839Sstsp 
3236c2498839Sstsp 	/* Copy firmware image to DMA memory. */
3237c2498839Sstsp 	memcpy(QWX_DMA_KVA(data_adm), data, len);
3238c2498839Sstsp 
3239c2498839Sstsp 	qwx_pci_write(sc, psc->bhi_off + MHI_BHI_STATUS, 0);
3240c2498839Sstsp 
3241c2498839Sstsp 	/* Set data physical address and length. */
3242c2498839Sstsp 	paddr = QWX_DMA_DVA(data_adm);
3243c2498839Sstsp 	qwx_pci_write(sc, psc->bhi_off + MHI_BHI_IMGADDR_HIGH, paddr >> 32);
3244c2498839Sstsp 	qwx_pci_write(sc, psc->bhi_off + MHI_BHI_IMGADDR_LOW,
3245c2498839Sstsp 	    paddr & 0xffffffff);
3246c2498839Sstsp 	qwx_pci_write(sc, psc->bhi_off + MHI_BHI_IMGSIZE, len);
3247c2498839Sstsp 
3248c2498839Sstsp 	/* Set a random transaction sequence number. */
3249c2498839Sstsp 	do {
3250c2498839Sstsp 		seq = arc4random_uniform(MHI_BHI_TXDB_SEQNUM_BMSK);
3251c2498839Sstsp 	} while (seq == 0);
3252c2498839Sstsp 	qwx_pci_write(sc, psc->bhi_off + MHI_BHI_IMGTXDB, seq);
3253c2498839Sstsp 
3254c2498839Sstsp 	/* Wait for completion. */
3255c2498839Sstsp 	ret = 0;
32566ff32849Sstsp 	while (status != MHI_BHI_STATUS_SUCCESS && psc->bhi_ee < MHI_EE_SBL) {
32576ff32849Sstsp 		ret = tsleep_nsec(&psc->bhi_ee, 0, "qwxbhi", SEC_TO_NSEC(5));
3258c2498839Sstsp 		if (ret)
3259c2498839Sstsp 			break;
3260c2498839Sstsp 		reg = qwx_pci_read(sc, psc->bhi_off + MHI_BHI_STATUS);
3261c2498839Sstsp 		status = (reg & MHI_BHI_STATUS_MASK) >> MHI_BHI_STATUS_SHFT;
3262c2498839Sstsp 	}
3263c2498839Sstsp 
3264c2498839Sstsp 	if (ret) {
3265c2498839Sstsp 		printf("%s: BHI load timeout\n", sc->sc_dev.dv_xname);
32666ff32849Sstsp 		reg = qwx_pci_read(sc, psc->bhi_off + MHI_BHI_STATUS);
32676ff32849Sstsp 		status = (reg & MHI_BHI_STATUS_MASK) >> MHI_BHI_STATUS_SHFT;
32686ff32849Sstsp 		DNPRINTF(QWX_D_MHI, "%s: BHI status is 0x%x EE is 0x%x\n",
32696ff32849Sstsp 		    __func__, status, psc->bhi_ee);
3270c2498839Sstsp 	}
32716ff32849Sstsp 
32726ff32849Sstsp 	qwx_dmamem_free(sc->sc_dmat, data_adm);
32736ff32849Sstsp 	return ret;
3274c2498839Sstsp }
3275c2498839Sstsp 
3276c2498839Sstsp int
3277c2498839Sstsp qwx_mhi_fw_load_bhie(struct qwx_pci_softc *psc, uint8_t *data, size_t len)
3278c2498839Sstsp {
3279c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3280c2498839Sstsp 	struct qwx_dma_vec_entry *vec;
3281c2498839Sstsp 	uint32_t seq, reg, state = MHI_BHIE_TXVECSTATUS_STATUS_RESET;
3282c2498839Sstsp 	uint64_t paddr;
3283c2498839Sstsp 	const size_t chunk_size = MHI_DMA_VEC_CHUNK_SIZE;
3284c2498839Sstsp 	size_t nseg, remain, vec_size;
3285c2498839Sstsp 	int i, ret;
3286c2498839Sstsp 
3287c2498839Sstsp 	nseg = howmany(len, chunk_size);
3288c2498839Sstsp 	if (nseg == 0) {
3289c2498839Sstsp 		printf("%s: BHIE data too short, have only %zu bytes\n",
3290c2498839Sstsp 		    sc->sc_dev.dv_xname, len);
3291c2498839Sstsp 		return 1;
3292c2498839Sstsp 	}
3293c2498839Sstsp 
3294c2498839Sstsp 	if (psc->amss_data == NULL || QWX_DMA_LEN(psc->amss_data) < len) {
3295c2498839Sstsp 		if (psc->amss_data)
3296c2498839Sstsp 			qwx_dmamem_free(sc->sc_dmat, psc->amss_data);
3297c2498839Sstsp 		psc->amss_data = qwx_dmamem_alloc(sc->sc_dmat, len, 0);
3298c2498839Sstsp 		if (psc->amss_data == NULL) {
3299c2498839Sstsp 			printf("%s: could not allocate BHIE DMA data buffer\n",
3300c2498839Sstsp 			    sc->sc_dev.dv_xname);
3301c2498839Sstsp 			return 1;
3302c2498839Sstsp 		}
3303c2498839Sstsp 	}
3304c2498839Sstsp 
3305c2498839Sstsp 	vec_size = nseg * sizeof(*vec);
3306c2498839Sstsp 	if (psc->amss_vec == NULL || QWX_DMA_LEN(psc->amss_vec) < vec_size) {
3307c2498839Sstsp 		if (psc->amss_vec)
3308c2498839Sstsp 			qwx_dmamem_free(sc->sc_dmat, psc->amss_vec);
3309c2498839Sstsp 		psc->amss_vec = qwx_dmamem_alloc(sc->sc_dmat, vec_size, 0);
3310c2498839Sstsp 		if (psc->amss_vec == NULL) {
3311c2498839Sstsp 			printf("%s: could not allocate BHIE DMA vec buffer\n",
3312c2498839Sstsp 			    sc->sc_dev.dv_xname);
3313c2498839Sstsp 			qwx_dmamem_free(sc->sc_dmat, psc->amss_data);
3314c2498839Sstsp 			psc->amss_data = NULL;
3315c2498839Sstsp 			return 1;
3316c2498839Sstsp 		}
3317c2498839Sstsp 	}
3318c2498839Sstsp 
3319c2498839Sstsp 	/* Copy firmware image to DMA memory. */
3320c2498839Sstsp 	memcpy(QWX_DMA_KVA(psc->amss_data), data, len);
3321c2498839Sstsp 
3322c2498839Sstsp 	/* Create vector which controls chunk-wise DMA copy in hardware. */
3323c2498839Sstsp 	paddr = QWX_DMA_DVA(psc->amss_data);
3324c2498839Sstsp 	vec = QWX_DMA_KVA(psc->amss_vec);
3325c2498839Sstsp 	remain = len;
3326c2498839Sstsp 	for (i = 0; i < nseg; i++) {
3327c2498839Sstsp 		vec[i].paddr = paddr;
3328c2498839Sstsp 		if (remain >= chunk_size) {
3329c2498839Sstsp 			vec[i].size = chunk_size;
3330c2498839Sstsp 			remain -= chunk_size;
3331c2498839Sstsp 			paddr += chunk_size;
3332c2498839Sstsp 		} else
3333c2498839Sstsp 			vec[i].size = remain;
3334c2498839Sstsp 	}
3335c2498839Sstsp 
3336c2498839Sstsp 	/* Set vector physical address and length. */
3337c2498839Sstsp 	paddr = QWX_DMA_DVA(psc->amss_vec);
3338c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECADDR_HIGH_OFFS,
3339c2498839Sstsp 	    paddr >> 32);
3340c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECADDR_LOW_OFFS,
3341c2498839Sstsp 	    paddr & 0xffffffff);
3342c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECSIZE_OFFS, vec_size);
3343c2498839Sstsp 
3344c2498839Sstsp 	/* Set a random transaction sequence number. */
3345c2498839Sstsp 	do {
3346c2498839Sstsp 		seq = arc4random_uniform(MHI_BHIE_TXVECSTATUS_SEQNUM_BMSK);
3347c2498839Sstsp 	} while (seq == 0);
3348c2498839Sstsp 	reg = qwx_pci_read(sc, psc->bhie_off + MHI_BHIE_TXVECDB_OFFS);
3349c2498839Sstsp 	reg &= ~MHI_BHIE_TXVECDB_SEQNUM_BMSK;
3350c2498839Sstsp 	reg |= seq << MHI_BHIE_TXVECDB_SEQNUM_SHFT;
3351c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECDB_OFFS, reg);
3352c2498839Sstsp 
3353c2498839Sstsp 	/* Wait for completion. */
3354c2498839Sstsp 	ret = 0;
3355c2498839Sstsp 	while (state != MHI_BHIE_TXVECSTATUS_STATUS_XFER_COMPL) {
3356c2498839Sstsp 		ret = tsleep_nsec(&psc->bhie_off, 0, "qwxbhie",
3357c2498839Sstsp 		    SEC_TO_NSEC(5));
3358c2498839Sstsp 		if (ret)
3359c2498839Sstsp 			break;
3360c2498839Sstsp 		reg = qwx_pci_read(sc,
3361c2498839Sstsp 		    psc->bhie_off + MHI_BHIE_TXVECSTATUS_OFFS);
3362c2498839Sstsp 		state = (reg & MHI_BHIE_TXVECSTATUS_STATUS_BMSK) >>
3363c2498839Sstsp 		    MHI_BHIE_TXVECSTATUS_STATUS_SHFT;
3364c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: txvec state is 0x%x\n", __func__,
3365c2498839Sstsp 		    state);
3366c2498839Sstsp 	}
3367c2498839Sstsp 
3368c2498839Sstsp 	if (ret) {
3369c2498839Sstsp 		printf("%s: BHIE load timeout\n", sc->sc_dev.dv_xname);
3370c2498839Sstsp 		return ret;
3371c2498839Sstsp 	}
3372c2498839Sstsp 	return 0;
3373c2498839Sstsp }
3374c2498839Sstsp 
3375c2498839Sstsp void
3376c2498839Sstsp qwx_rddm_prepare(struct qwx_pci_softc *psc)
3377c2498839Sstsp {
3378c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3379c2498839Sstsp 	struct qwx_dma_vec_entry *vec;
3380c2498839Sstsp 	struct qwx_dmamem *data_adm, *vec_adm;
3381c2498839Sstsp 	uint32_t seq, reg;
3382c2498839Sstsp 	uint64_t paddr;
3383c2498839Sstsp 	const size_t len = QWX_RDDM_DUMP_SIZE;
3384c2498839Sstsp 	const size_t chunk_size = MHI_DMA_VEC_CHUNK_SIZE;
3385c2498839Sstsp 	size_t nseg, remain, vec_size;
3386c2498839Sstsp 	int i;
3387c2498839Sstsp 
3388c2498839Sstsp 	nseg = howmany(len, chunk_size);
3389c2498839Sstsp 	if (nseg == 0) {
3390c2498839Sstsp 		printf("%s: RDDM data too short, have only %zu bytes\n",
3391c2498839Sstsp 		    sc->sc_dev.dv_xname, len);
3392c2498839Sstsp 		return;
3393c2498839Sstsp 	}
3394c2498839Sstsp 
3395c2498839Sstsp 	data_adm = qwx_dmamem_alloc(sc->sc_dmat, len, 0);
3396c2498839Sstsp 	if (data_adm == NULL) {
3397c2498839Sstsp 		printf("%s: could not allocate BHIE DMA data buffer\n",
3398c2498839Sstsp 		    sc->sc_dev.dv_xname);
3399c2498839Sstsp 		return;
3400c2498839Sstsp 	}
3401c2498839Sstsp 
3402c2498839Sstsp 	vec_size = nseg * sizeof(*vec);
3403c2498839Sstsp 	vec_adm = qwx_dmamem_alloc(sc->sc_dmat, vec_size, 0);
3404c2498839Sstsp 	if (vec_adm == NULL) {
3405c2498839Sstsp 		printf("%s: could not allocate BHIE DMA vector buffer\n",
3406c2498839Sstsp 		    sc->sc_dev.dv_xname);
3407c2498839Sstsp 		qwx_dmamem_free(sc->sc_dmat, data_adm);
3408c2498839Sstsp 		return;
3409c2498839Sstsp 	}
3410c2498839Sstsp 
3411c2498839Sstsp 	/* Create vector which controls chunk-wise DMA copy from hardware. */
3412c2498839Sstsp 	paddr = QWX_DMA_DVA(data_adm);
3413c2498839Sstsp 	vec = QWX_DMA_KVA(vec_adm);
3414c2498839Sstsp 	remain = len;
3415c2498839Sstsp 	for (i = 0; i < nseg; i++) {
3416c2498839Sstsp 		vec[i].paddr = paddr;
3417c2498839Sstsp 		if (remain >= chunk_size) {
3418c2498839Sstsp 			vec[i].size = chunk_size;
3419c2498839Sstsp 			remain -= chunk_size;
3420c2498839Sstsp 			paddr += chunk_size;
3421c2498839Sstsp 		} else
3422c2498839Sstsp 			vec[i].size = remain;
3423c2498839Sstsp 	}
3424c2498839Sstsp 
3425c2498839Sstsp 	/* Set vector physical address and length. */
3426c2498839Sstsp 	paddr = QWX_DMA_DVA(vec_adm);
3427c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECADDR_HIGH_OFFS,
3428c2498839Sstsp 	    paddr >> 32);
3429c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECADDR_LOW_OFFS,
3430c2498839Sstsp 	    paddr & 0xffffffff);
3431c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECSIZE_OFFS, vec_size);
3432c2498839Sstsp 
3433c2498839Sstsp 	/* Set a random transaction sequence number. */
3434c2498839Sstsp 	do {
3435c2498839Sstsp 		seq = arc4random_uniform(MHI_BHIE_RXVECSTATUS_SEQNUM_BMSK);
3436c2498839Sstsp 	} while (seq == 0);
3437c2498839Sstsp 
3438c2498839Sstsp 	reg = qwx_pci_read(sc, psc->bhie_off + MHI_BHIE_RXVECDB_OFFS);
3439c2498839Sstsp 	reg &= ~MHI_BHIE_RXVECDB_SEQNUM_BMSK;
3440c2498839Sstsp 	reg |= seq << MHI_BHIE_RXVECDB_SEQNUM_SHFT;
3441c2498839Sstsp 	qwx_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECDB_OFFS, reg);
3442c2498839Sstsp 
3443c2498839Sstsp 	psc->rddm_data = data_adm;
3444c2498839Sstsp 	psc->rddm_vec = vec_adm;
3445c2498839Sstsp }
3446c2498839Sstsp 
344781cacbbeSstsp #ifdef QWX_DEBUG
3448c2498839Sstsp void
3449c2498839Sstsp qwx_rddm_task(void *arg)
3450c2498839Sstsp {
3451c2498839Sstsp 	struct qwx_pci_softc *psc = arg;
3452c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3453c2498839Sstsp 	uint32_t reg, state = MHI_BHIE_RXVECSTATUS_STATUS_RESET;
3454c2498839Sstsp 	const size_t len = QWX_RDDM_DUMP_SIZE;
3455c2498839Sstsp 	int i, timeout;
3456c2498839Sstsp 	const uint32_t msecs = 100, retries = 20;
3457c2498839Sstsp 	uint8_t *rddm;
3458c2498839Sstsp 	struct nameidata nd;
3459c2498839Sstsp 	struct vnode *vp = NULL;
3460c2498839Sstsp 	struct iovec iov[3];
3461c2498839Sstsp 	struct uio uio;
3462c2498839Sstsp 	char path[PATH_MAX];
3463c2498839Sstsp 	int error = 0;
3464c2498839Sstsp 
3465c2498839Sstsp 	if (psc->rddm_data == NULL) {
3466c2498839Sstsp 		DPRINTF("%s: RDDM not prepared\n", __func__);
3467c2498839Sstsp 		return;
3468c2498839Sstsp 	}
3469c2498839Sstsp 
3470c2498839Sstsp 	/* Poll for completion */
3471c2498839Sstsp 	timeout = retries;
3472c2498839Sstsp 	while (timeout > 0 && state != MHI_BHIE_RXVECSTATUS_STATUS_XFER_COMPL) {
3473c2498839Sstsp 		reg = qwx_pci_read(sc,
3474c2498839Sstsp 		    psc->bhie_off + MHI_BHIE_RXVECSTATUS_OFFS);
3475c2498839Sstsp 		state = (reg & MHI_BHIE_RXVECSTATUS_STATUS_BMSK) >>
3476c2498839Sstsp 		    MHI_BHIE_RXVECSTATUS_STATUS_SHFT;
3477c2498839Sstsp 		DPRINTF("%s: txvec state is 0x%x\n", __func__, state);
3478c2498839Sstsp 		DELAY((msecs / retries) * 1000);
3479c2498839Sstsp 		timeout--;
3480c2498839Sstsp 	}
3481c2498839Sstsp 
3482c2498839Sstsp 	if (timeout == 0) {
3483c2498839Sstsp 		DPRINTF("%s: RDDM dump failed\n", sc->sc_dev.dv_xname);
3484c2498839Sstsp 		return;
3485c2498839Sstsp 	}
3486c2498839Sstsp 
3487c2498839Sstsp 	rddm = QWX_DMA_KVA(psc->rddm_data);
3488c2498839Sstsp 	DPRINTF("%s: RDDM snippet:\n", __func__);
3489c2498839Sstsp 	for (i = 0; i < MIN(64, len); i++) {
3490c2498839Sstsp 		DPRINTF("%s %.2x", i % 16 == 0 ? "\n" : "", rddm[i]);
3491c2498839Sstsp 	}
3492c2498839Sstsp 	DPRINTF("\n");
3493c2498839Sstsp 
3494c2498839Sstsp 	DPRINTF("%s: sleeping for 30 seconds to allow userland to boot\n", __func__);
3495c2498839Sstsp 	tsleep_nsec(&psc->rddm_data, 0, "qwxrddm", SEC_TO_NSEC(30));
3496c2498839Sstsp 
3497c2498839Sstsp 	snprintf(path, sizeof(path), "/root/%s-rddm.bin", sc->sc_dev.dv_xname);
3498c2498839Sstsp 	DPRINTF("%s: saving RDDM to %s\n", __func__, path);
3499c2498839Sstsp 	NDINIT(&nd, 0, 0, UIO_SYSSPACE, path, curproc);
3500c2498839Sstsp 	nd.ni_pledge = PLEDGE_CPATH | PLEDGE_WPATH;
3501c2498839Sstsp 	nd.ni_unveil = UNVEIL_CREATE | UNVEIL_WRITE;
3502c2498839Sstsp 	error = vn_open(&nd, FWRITE | O_CREAT | O_NOFOLLOW | O_TRUNC,
3503c2498839Sstsp 	    S_IRUSR | S_IWUSR);
3504c2498839Sstsp 	if (error) {
3505c2498839Sstsp 		DPRINTF("%s: vn_open: error %d\n", __func__, error);
3506c2498839Sstsp 		goto done;
3507c2498839Sstsp 	}
3508c2498839Sstsp 	vp = nd.ni_vp;
3509c2498839Sstsp 	VOP_UNLOCK(vp);
3510c2498839Sstsp 
3511c2498839Sstsp 	iov[0].iov_base = (void *)rddm;
3512c2498839Sstsp 	iov[0].iov_len = len;
3513c2498839Sstsp 	iov[1].iov_len = 0;
3514c2498839Sstsp 	uio.uio_iov = &iov[0];
3515c2498839Sstsp 	uio.uio_offset = 0;
3516c2498839Sstsp 	uio.uio_segflg = UIO_SYSSPACE;
3517c2498839Sstsp 	uio.uio_rw = UIO_WRITE;
3518c2498839Sstsp 	uio.uio_resid = len;
3519c2498839Sstsp 	uio.uio_iovcnt = 1;
3520c2498839Sstsp 	uio.uio_procp = curproc;
3521c2498839Sstsp 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
3522c2498839Sstsp 	if (error) {
3523c2498839Sstsp 		DPRINTF("%s: vget: error %d\n", __func__, error);
3524c2498839Sstsp 		goto done;
3525c2498839Sstsp 	}
3526c2498839Sstsp 	error = VOP_WRITE(vp, &uio, IO_UNIT|IO_APPEND, curproc->p_ucred);
3527c2498839Sstsp 	vput(vp);
3528c2498839Sstsp 	if (error)
3529c2498839Sstsp 		DPRINTF("%s: VOP_WRITE: error %d\n", __func__, error);
3530c2498839Sstsp 	#if 0
3531c2498839Sstsp 	error = vn_close(vp, FWRITE, curproc->p_ucred, curproc);
3532c2498839Sstsp 	if (error)
3533c2498839Sstsp 		DPRINTF("%s: vn_close: error %d\n", __func__, error);
3534c2498839Sstsp 	#endif
3535c2498839Sstsp done:
3536c2498839Sstsp 	qwx_dmamem_free(sc->sc_dmat, psc->rddm_data);
3537c2498839Sstsp 	qwx_dmamem_free(sc->sc_dmat, psc->rddm_vec);
3538c2498839Sstsp 	psc->rddm_data = NULL;
3539c2498839Sstsp 	psc->rddm_vec = NULL;
3540c2498839Sstsp 	DPRINTF("%s: done, error %d\n", __func__, error);
3541c2498839Sstsp }
354281cacbbeSstsp #endif
3543c2498839Sstsp 
3544c2498839Sstsp void *
3545c2498839Sstsp qwx_pci_event_ring_get_elem(struct qwx_pci_event_ring *ring, uint64_t rp)
3546c2498839Sstsp {
3547c2498839Sstsp 	uint64_t base = QWX_DMA_DVA(ring->dmamem), offset;
3548c2498839Sstsp 	void *addr = QWX_DMA_KVA(ring->dmamem);
3549c2498839Sstsp 
3550c2498839Sstsp 	if (rp < base)
3551c2498839Sstsp 		return NULL;
3552c2498839Sstsp 
3553c2498839Sstsp 	offset = rp - base;
3554c2498839Sstsp 	if (offset >= ring->size)
3555c2498839Sstsp 		return NULL;
3556c2498839Sstsp 
3557c2498839Sstsp 	return addr + offset;
3558c2498839Sstsp }
3559c2498839Sstsp 
3560c2498839Sstsp void
3561a5176bb5Sstsp qwx_mhi_state_change(struct qwx_pci_softc *psc, int ee, int mhi_state)
3562c2498839Sstsp {
3563c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3564c2498839Sstsp 	uint32_t old_ee = psc->bhi_ee;
3565c2498839Sstsp 	uint32_t old_mhi_state = psc->mhi_state;
3566c2498839Sstsp 
3567c2498839Sstsp 	if (ee != -1 && psc->bhi_ee != ee) {
3568c2498839Sstsp 		switch (ee) {
3569c2498839Sstsp 		case MHI_EE_PBL:
3570c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new EE PBL\n",
3571c2498839Sstsp 			    sc->sc_dev.dv_xname);
3572c2498839Sstsp 			psc->bhi_ee = ee;
3573c2498839Sstsp 			break;
3574c2498839Sstsp 		case MHI_EE_SBL:
3575c2498839Sstsp 			psc->bhi_ee = ee;
3576c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new EE SBL\n",
3577c2498839Sstsp 			    sc->sc_dev.dv_xname);
3578c2498839Sstsp 			break;
3579c2498839Sstsp 		case MHI_EE_AMSS:
3580c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new EE AMSS\n",
3581c2498839Sstsp 			    sc->sc_dev.dv_xname);
3582c2498839Sstsp 			psc->bhi_ee = ee;
3583c2498839Sstsp 			/* Wake thread loading the full AMSS image. */
3584c2498839Sstsp 			wakeup(&psc->bhie_off);
3585c2498839Sstsp 			break;
3586c2498839Sstsp 		case MHI_EE_WFW:
3587c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new EE WFW\n",
3588c2498839Sstsp 			    sc->sc_dev.dv_xname);
3589c2498839Sstsp 			psc->bhi_ee = ee;
3590c2498839Sstsp 			break;
3591c2498839Sstsp 		default:
3592c2498839Sstsp 			printf("%s: unhandled EE change to %x\n",
3593c2498839Sstsp 			    sc->sc_dev.dv_xname, ee);
3594c2498839Sstsp 			break;
3595c2498839Sstsp 		}
3596c2498839Sstsp 	}
3597c2498839Sstsp 
3598c2498839Sstsp 	if (mhi_state != -1 && psc->mhi_state != mhi_state) {
3599c2498839Sstsp 		switch (mhi_state) {
3600c2498839Sstsp 		case -1:
3601c2498839Sstsp 			break;
3602c2498839Sstsp 		case MHI_STATE_RESET:
3603c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new MHI state RESET\n",
3604c2498839Sstsp 			    sc->sc_dev.dv_xname);
3605c2498839Sstsp 			psc->mhi_state = mhi_state;
3606c2498839Sstsp 			break;
3607c2498839Sstsp 		case MHI_STATE_READY:
3608c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new MHI state READY\n",
3609c2498839Sstsp 			    sc->sc_dev.dv_xname);
3610c2498839Sstsp 			psc->mhi_state = mhi_state;
3611c2498839Sstsp 			qwx_mhi_ready_state_transition(psc);
3612c2498839Sstsp 			break;
3613c2498839Sstsp 		case MHI_STATE_M0:
3614c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new MHI state M0\n",
3615c2498839Sstsp 			    sc->sc_dev.dv_xname);
3616c2498839Sstsp 			psc->mhi_state = mhi_state;
3617c2498839Sstsp 			qwx_mhi_mission_mode_state_transition(psc);
3618c2498839Sstsp 			break;
3619a9f6aa33Sstsp 		case MHI_STATE_M1:
3620a9f6aa33Sstsp 			DNPRINTF(QWX_D_MHI, "%s: new MHI state M1\n",
3621a9f6aa33Sstsp 			    sc->sc_dev.dv_xname);
3622a9f6aa33Sstsp 			psc->mhi_state = mhi_state;
3623a9f6aa33Sstsp 			qwx_mhi_low_power_mode_state_transition(psc);
3624a9f6aa33Sstsp 			break;
3625c2498839Sstsp 		case MHI_STATE_SYS_ERR:
3626c2498839Sstsp 			DNPRINTF(QWX_D_MHI,
3627c2498839Sstsp 			    "%s: new MHI state SYS ERR\n",
3628c2498839Sstsp 			    sc->sc_dev.dv_xname);
3629c2498839Sstsp 			psc->mhi_state = mhi_state;
3630c2498839Sstsp 			break;
3631c2498839Sstsp 		default:
3632c2498839Sstsp 			printf("%s: unhandled MHI state change to %x\n",
3633c2498839Sstsp 			    sc->sc_dev.dv_xname, mhi_state);
3634c2498839Sstsp 			break;
3635c2498839Sstsp 		}
3636c2498839Sstsp 	}
3637c2498839Sstsp 
3638c2498839Sstsp 	if (old_ee != psc->bhi_ee)
3639c2498839Sstsp 		wakeup(&psc->bhi_ee);
3640c2498839Sstsp 	if (old_mhi_state != psc->mhi_state)
3641c2498839Sstsp 		wakeup(&psc->mhi_state);
3642c2498839Sstsp }
3643c2498839Sstsp 
3644c2498839Sstsp void
3645c2498839Sstsp qwx_pci_intr_ctrl_event_mhi(struct qwx_pci_softc *psc, uint32_t mhi_state)
3646c2498839Sstsp {
3647c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: MHI state change 0x%x -> 0x%x\n", __func__,
3648c2498839Sstsp 	    psc->mhi_state, mhi_state);
3649c2498839Sstsp 
36506ff32849Sstsp 	if (psc->mhi_state != mhi_state)
3651a5176bb5Sstsp 		qwx_mhi_state_change(psc, -1, mhi_state);
3652c2498839Sstsp }
3653c2498839Sstsp 
3654c2498839Sstsp void
3655c2498839Sstsp qwx_pci_intr_ctrl_event_ee(struct qwx_pci_softc *psc, uint32_t ee)
3656c2498839Sstsp {
3657c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: EE change 0x%x to 0x%x\n", __func__,
3658c2498839Sstsp 	    psc->bhi_ee, ee);
3659c2498839Sstsp 
36606ff32849Sstsp 	if (psc->bhi_ee != ee)
3661a5176bb5Sstsp 		qwx_mhi_state_change(psc, ee, -1);
3662c2498839Sstsp }
3663c2498839Sstsp 
3664c2498839Sstsp void
3665c2498839Sstsp qwx_pci_intr_ctrl_event_cmd_complete(struct qwx_pci_softc *psc,
3666c2498839Sstsp     uint64_t ptr, uint32_t cmd_status)
3667c2498839Sstsp {
3668c2498839Sstsp 	struct qwx_pci_cmd_ring	*cmd_ring = &psc->cmd_ring;
3669c2498839Sstsp 	uint64_t base = QWX_DMA_DVA(cmd_ring->dmamem);
3670c2498839Sstsp 	struct qwx_pci_xfer_ring *xfer_ring = NULL;
3671c2498839Sstsp 	struct qwx_mhi_ring_element *e;
3672c2498839Sstsp 	uint32_t tre1, chid;
3673c2498839Sstsp 	size_t i;
3674c2498839Sstsp 
3675c2498839Sstsp 	e = qwx_pci_cmd_ring_get_elem(cmd_ring, ptr);
3676c2498839Sstsp 	if (e == NULL)
3677c2498839Sstsp 		return;
3678c2498839Sstsp 
3679c2498839Sstsp 	tre1 = le32toh(e->dword[1]);
3680c2498839Sstsp 	chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
3681c2498839Sstsp 
3682c2498839Sstsp 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
3683c2498839Sstsp 		if (psc->xfer_rings[i].mhi_chan_id == chid) {
3684c2498839Sstsp 			xfer_ring = &psc->xfer_rings[i];
3685c2498839Sstsp 			break;
3686c2498839Sstsp 		}
3687c2498839Sstsp 	}
3688c2498839Sstsp 	if (xfer_ring == NULL) {
3689c2498839Sstsp 		printf("%s: no transfer ring found for command completion "
3690c2498839Sstsp 		    "on channel %u\n", __func__, chid);
3691c2498839Sstsp 		return;
3692c2498839Sstsp 	}
3693c2498839Sstsp 
3694c2498839Sstsp 	xfer_ring->cmd_status = cmd_status;
3695c2498839Sstsp 	wakeup(&xfer_ring->cmd_status);
3696c2498839Sstsp 
3697c2498839Sstsp 	if (cmd_ring->rp + sizeof(*e) >= base + cmd_ring->size)
3698c2498839Sstsp 		cmd_ring->rp = base;
3699c2498839Sstsp 	else
3700c2498839Sstsp 		cmd_ring->rp += sizeof(*e);
3701c2498839Sstsp }
3702c2498839Sstsp 
3703c2498839Sstsp int
3704c2498839Sstsp qwx_pci_intr_ctrl_event(struct qwx_pci_softc *psc, struct qwx_pci_event_ring *ring)
3705c2498839Sstsp {
3706c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3707c2498839Sstsp 	struct qwx_mhi_event_ctxt *c;
3708c2498839Sstsp 	uint64_t rp, wp, base;
3709c2498839Sstsp 	struct qwx_mhi_ring_element *e;
3710c2498839Sstsp 	uint32_t tre0, tre1, type, code, chid, len;
3711c2498839Sstsp 
3712c2498839Sstsp 	c = ring->event_ctxt;
3713c2498839Sstsp 	if (c == NULL) {
3714c2498839Sstsp 		/*
3715c2498839Sstsp 		 * Interrupts can trigger before mhi_init_event_rings()
3716c2498839Sstsp 		 * if the device is still active after a warm reboot.
3717c2498839Sstsp 		 */
3718c2498839Sstsp 		return 0;
3719c2498839Sstsp 	}
3720c2498839Sstsp 
3721c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->event_ctxt), 0,
3722c2498839Sstsp 	    QWX_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_POSTREAD);
3723c2498839Sstsp 
3724c2498839Sstsp 	rp = le64toh(c->rp);
3725c2498839Sstsp 	wp = le64toh(c->wp);
3726c2498839Sstsp 
3727c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: kernel rp=0x%llx\n", __func__, ring->rp);
3728c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: device rp=0x%llx\n", __func__, rp);
3729c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: kernel wp=0x%llx\n", __func__, ring->wp);
3730c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: device wp=0x%llx\n", __func__, wp);
3731c2498839Sstsp 
3732c2498839Sstsp 	base = QWX_DMA_DVA(ring->dmamem);
3733c2498839Sstsp 	if (ring->rp == rp || rp < base || rp >= base + ring->size)
3734c2498839Sstsp 		return 0;
3735c2498839Sstsp 	if (wp < base || wp >= base + ring->size)
3736c2498839Sstsp 		return 0;
3737c2498839Sstsp 
3738c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(ring->dmamem),
3739c2498839Sstsp 	    0, QWX_DMA_LEN(ring->dmamem), BUS_DMASYNC_POSTREAD);
3740c2498839Sstsp 
3741c2498839Sstsp 	while (ring->rp != rp) {
3742c2498839Sstsp 		e = qwx_pci_event_ring_get_elem(ring, ring->rp);
3743c2498839Sstsp 		if (e == NULL)
3744c2498839Sstsp 			return 0;
3745c2498839Sstsp 
3746c2498839Sstsp 		tre0 = le32toh(e->dword[0]);
3747c2498839Sstsp 		tre1 = le32toh(e->dword[1]);
3748c2498839Sstsp 
3749c2498839Sstsp 		len = (tre0 & MHI_TRE0_EV_LEN_MASK) >> MHI_TRE0_EV_LEN_SHFT;
3750c2498839Sstsp 		code = (tre0 & MHI_TRE0_EV_CODE_MASK) >> MHI_TRE0_EV_CODE_SHFT;
3751c2498839Sstsp 		type = (tre1 & MHI_TRE1_EV_TYPE_MASK) >> MHI_TRE1_EV_TYPE_SHFT;
3752c2498839Sstsp 		chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
3753c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: len=%u code=0x%x type=0x%x chid=%d\n",
3754c2498839Sstsp 		    __func__, len, code, type, chid);
3755c2498839Sstsp 
3756c2498839Sstsp 		switch (type) {
3757c2498839Sstsp 		case MHI_PKT_TYPE_STATE_CHANGE_EVENT:
3758c2498839Sstsp 			qwx_pci_intr_ctrl_event_mhi(psc, code);
3759c2498839Sstsp 			break;
3760c2498839Sstsp 		case MHI_PKT_TYPE_EE_EVENT:
3761c2498839Sstsp 			qwx_pci_intr_ctrl_event_ee(psc, code);
3762c2498839Sstsp 			break;
3763c2498839Sstsp 		case MHI_PKT_TYPE_CMD_COMPLETION_EVENT:
3764c2498839Sstsp 			qwx_pci_intr_ctrl_event_cmd_complete(psc,
3765c2498839Sstsp 			    le64toh(e->ptr), code);
3766c2498839Sstsp 			break;
3767c2498839Sstsp 		default:
3768c2498839Sstsp 			printf("%s: unhandled event type 0x%x\n",
3769c2498839Sstsp 			    __func__, type);
3770c2498839Sstsp 			break;
3771c2498839Sstsp 		}
3772c2498839Sstsp 
3773c2498839Sstsp 		if (ring->rp + sizeof(*e) >= base + ring->size)
3774c2498839Sstsp 			ring->rp = base;
3775c2498839Sstsp 		else
3776c2498839Sstsp 			ring->rp += sizeof(*e);
3777c2498839Sstsp 
3778c2498839Sstsp 		if (ring->wp + sizeof(*e) >= base + ring->size)
3779c2498839Sstsp 			ring->wp = base;
3780c2498839Sstsp 		else
3781c2498839Sstsp 			ring->wp += sizeof(*e);
3782c2498839Sstsp 	}
3783c2498839Sstsp 
3784c2498839Sstsp 	c->wp = htole64(ring->wp);
3785c2498839Sstsp 
3786c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->event_ctxt), 0,
3787c2498839Sstsp 	    QWX_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_PREWRITE);
3788c2498839Sstsp 
3789c2498839Sstsp 	qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
3790c2498839Sstsp 	return 1;
3791c2498839Sstsp }
3792c2498839Sstsp 
3793c2498839Sstsp void
3794c2498839Sstsp qwx_pci_intr_data_event_tx(struct qwx_pci_softc *psc, struct qwx_mhi_ring_element *e)
3795c2498839Sstsp {
3796c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3797c2498839Sstsp 	struct qwx_pci_xfer_ring *ring;
3798c2498839Sstsp 	struct qwx_xfer_data *xfer;
3799c2498839Sstsp 	uint64_t rp, evrp, base, paddr;
3800c2498839Sstsp 	uint32_t tre0, tre1, code, chid, evlen, len;
3801c2498839Sstsp 	int i;
3802c2498839Sstsp 
3803c2498839Sstsp 	tre0 = le32toh(e->dword[0]);
3804c2498839Sstsp 	tre1 = le32toh(e->dword[1]);
3805c2498839Sstsp 
3806c2498839Sstsp 	evlen = (tre0 & MHI_TRE0_EV_LEN_MASK) >> MHI_TRE0_EV_LEN_SHFT;
3807c2498839Sstsp 	code = (tre0 & MHI_TRE0_EV_CODE_MASK) >> MHI_TRE0_EV_CODE_SHFT;
3808c2498839Sstsp 	chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
3809c2498839Sstsp 
3810c2498839Sstsp 	switch (code) {
3811c2498839Sstsp 	case MHI_EV_CC_EOT:
3812c2498839Sstsp 		for (i = 0; i < nitems(psc->xfer_rings); i++) {
3813c2498839Sstsp 			ring = &psc->xfer_rings[i];
3814c2498839Sstsp 			if (ring->mhi_chan_id == chid)
3815c2498839Sstsp 				break;
3816c2498839Sstsp 		}
3817c2498839Sstsp 		if (i == nitems(psc->xfer_rings)) {
3818c2498839Sstsp 			printf("%s: unhandled channel 0x%x\n",
3819c2498839Sstsp 			    __func__, chid);
3820c2498839Sstsp 			break;
3821c2498839Sstsp 		}
3822c2498839Sstsp 		base = QWX_DMA_DVA(ring->dmamem);
3823c2498839Sstsp 		/* PTR contains the entry that was last written */
3824c2498839Sstsp 		evrp = letoh64(e->ptr);
3825c2498839Sstsp 		rp = evrp;
3826c2498839Sstsp 		if (rp < base || rp >= base + ring->size) {
3827c2498839Sstsp 			printf("%s: invalid ptr 0x%llx\n",
3828c2498839Sstsp 			    __func__, rp);
3829c2498839Sstsp 			break;
3830c2498839Sstsp 		}
3831c2498839Sstsp 		/* Point rp to next empty slot */
3832c2498839Sstsp 		if (rp + sizeof(*e) >= base + ring->size)
3833c2498839Sstsp 			rp = base;
3834c2498839Sstsp 		else
3835c2498839Sstsp 			rp += sizeof(*e);
3836c2498839Sstsp 		/* Parse until next empty slot */
3837c2498839Sstsp 		while (ring->rp != rp) {
3838c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s:%d: ring->rp 0x%llx "
3839c2498839Sstsp 			    "ring->wp 0x%llx rp 0x%llx\n", __func__,
3840c2498839Sstsp 			    __LINE__, ring->rp, ring->wp, rp);
3841c2498839Sstsp 			e = qwx_pci_xfer_ring_get_elem(ring, ring->rp);
3842c2498839Sstsp 			xfer = qwx_pci_xfer_ring_get_data(ring, ring->rp);
3843c2498839Sstsp 
3844c2498839Sstsp 			if (ring->rp == evrp)
3845c2498839Sstsp 				len = evlen;
3846c2498839Sstsp 			else
3847c2498839Sstsp 				len = xfer->m->m_pkthdr.len;
3848c2498839Sstsp 
3849c2498839Sstsp 			bus_dmamap_sync(sc->sc_dmat, xfer->map, 0,
3850c2498839Sstsp 			    xfer->m->m_pkthdr.len, BUS_DMASYNC_POSTREAD);
3851c2498839Sstsp #ifdef QWX_DEBUG
3852c2498839Sstsp 			{
3853c2498839Sstsp 			int i;
3854c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "%s: chan %u data (len %u): ",
3855c2498839Sstsp 			    __func__,
3856c2498839Sstsp 			    ring->mhi_chan_id, len);
3857c2498839Sstsp 			for (i = 0; i < MIN(32, len); i++) {
3858c2498839Sstsp 				DNPRINTF(QWX_D_MHI, "%02x ",
3859c2498839Sstsp 				    (unsigned char)mtod(xfer->m, caddr_t)[i]);
3860c2498839Sstsp 			}
3861c2498839Sstsp 			if (i < len)
3862c2498839Sstsp 				DNPRINTF(QWX_D_MHI, "...");
3863c2498839Sstsp 			DNPRINTF(QWX_D_MHI, "\n");
3864c2498839Sstsp 			}
3865c2498839Sstsp #endif
3866c2498839Sstsp 			if (ring->mhi_chan_direction == MHI_CHAN_TYPE_INBOUND) {
3867c2498839Sstsp 				/* Save m_data as upper layers use m_adj(9) */
3868c2498839Sstsp 				void *o_data = xfer->m->m_data;
3869c2498839Sstsp 
3870c2498839Sstsp 				/* Pass mbuf to upper layers */
3871c2498839Sstsp 				qwx_qrtr_recv_msg(sc, xfer->m);
3872c2498839Sstsp 
3873c2498839Sstsp 				/* Reset RX mbuf instead of free/alloc */
3874c2498839Sstsp 				KASSERT(xfer->m->m_next == NULL);
3875c2498839Sstsp 				xfer->m->m_data = o_data;
3876c2498839Sstsp 				xfer->m->m_len = xfer->m->m_pkthdr.len =
3877c2498839Sstsp 				    QWX_PCI_XFER_MAX_DATA_SIZE;
3878c2498839Sstsp 
3879c2498839Sstsp 				paddr = xfer->map->dm_segs[0].ds_addr;
3880c2498839Sstsp 
3881c2498839Sstsp 				e->ptr = htole64(paddr);
3882c2498839Sstsp 				e->dword[0] = htole32((
3883c2498839Sstsp 				    QWX_PCI_XFER_MAX_DATA_SIZE <<
3884c2498839Sstsp 				    MHI_TRE0_DATA_LEN_SHFT) &
3885c2498839Sstsp 				    MHI_TRE0_DATA_LEN_MASK);
3886c2498839Sstsp 				e->dword[1] = htole32(MHI_TRE1_DATA_IEOT |
3887c2498839Sstsp 				    MHI_TRE1_DATA_BEI |
3888c2498839Sstsp 				    MHI_TRE1_DATA_TYPE_TRANSFER <<
3889c2498839Sstsp 				    MHI_TRE1_DATA_TYPE_SHIFT);
3890c2498839Sstsp 
3891c2498839Sstsp 				if (ring->wp + sizeof(*e) >= base + ring->size)
3892c2498839Sstsp 					ring->wp = base;
3893c2498839Sstsp 				else
3894c2498839Sstsp 					ring->wp += sizeof(*e);
3895c2498839Sstsp 			} else {
3896c2498839Sstsp 				/* Unload and free TX mbuf */
3897c2498839Sstsp 				bus_dmamap_unload(sc->sc_dmat, xfer->map);
3898c2498839Sstsp 				m_freem(xfer->m);
3899c2498839Sstsp 				xfer->m = NULL;
3900c2498839Sstsp 				ring->queued--;
3901c2498839Sstsp 			}
3902c2498839Sstsp 
3903c2498839Sstsp 			if (ring->rp + sizeof(*e) >= base + ring->size)
3904c2498839Sstsp 				ring->rp = base;
3905c2498839Sstsp 			else
3906c2498839Sstsp 				ring->rp += sizeof(*e);
3907c2498839Sstsp 		}
3908c2498839Sstsp 
3909c2498839Sstsp 		if (ring->mhi_chan_direction == MHI_CHAN_TYPE_INBOUND) {
3910c2498839Sstsp 			ring->chan_ctxt->wp = htole64(ring->wp);
3911c2498839Sstsp 
3912c2498839Sstsp 			bus_dmamap_sync(sc->sc_dmat,
3913c2498839Sstsp 			    QWX_DMA_MAP(psc->chan_ctxt), 0,
3914c2498839Sstsp 			    QWX_DMA_LEN(psc->chan_ctxt),
3915c2498839Sstsp 			    BUS_DMASYNC_PREWRITE);
3916c2498839Sstsp 
3917c2498839Sstsp 			qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
3918c2498839Sstsp 		}
3919c2498839Sstsp 		break;
3920c2498839Sstsp 	default:
3921c2498839Sstsp 		printf("%s: unhandled event code 0x%x\n",
3922c2498839Sstsp 		    __func__, code);
3923c2498839Sstsp 	}
3924c2498839Sstsp }
3925c2498839Sstsp 
3926c2498839Sstsp int
3927c2498839Sstsp qwx_pci_intr_data_event(struct qwx_pci_softc *psc, struct qwx_pci_event_ring *ring)
3928c2498839Sstsp {
3929c2498839Sstsp 	struct qwx_softc *sc = &psc->sc_sc;
3930c2498839Sstsp 	struct qwx_mhi_event_ctxt *c;
3931c2498839Sstsp 	uint64_t rp, wp, base;
3932c2498839Sstsp 	struct qwx_mhi_ring_element *e;
3933c2498839Sstsp 	uint32_t tre0, tre1, type, code, chid, len;
3934c2498839Sstsp 
3935c2498839Sstsp 	c = ring->event_ctxt;
3936c2498839Sstsp 	if (c == NULL) {
3937c2498839Sstsp 		/*
3938c2498839Sstsp 		 * Interrupts can trigger before mhi_init_event_rings()
3939c2498839Sstsp 		 * if the device is still active after a warm reboot.
3940c2498839Sstsp 		 */
3941c2498839Sstsp 		return 0;
3942c2498839Sstsp 	}
3943c2498839Sstsp 
3944c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->event_ctxt), 0,
3945c2498839Sstsp 	    QWX_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_POSTREAD);
3946c2498839Sstsp 
3947c2498839Sstsp 	rp = le64toh(c->rp);
3948c2498839Sstsp 	wp = le64toh(c->wp);
3949c2498839Sstsp 
3950c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: kernel rp=0x%llx\n", __func__, ring->rp);
3951c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: device rp=0x%llx\n", __func__, rp);
3952c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: kernel wp=0x%llx\n", __func__, ring->wp);
3953c2498839Sstsp 	DNPRINTF(QWX_D_MHI, "%s: device wp=0x%llx\n", __func__, wp);
3954c2498839Sstsp 
3955c2498839Sstsp 	base = QWX_DMA_DVA(ring->dmamem);
3956c2498839Sstsp 	if (ring->rp == rp || rp < base || rp >= base + ring->size)
3957c2498839Sstsp 		return 0;
3958c2498839Sstsp 
3959c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(ring->dmamem),
3960c2498839Sstsp 	    0, QWX_DMA_LEN(ring->dmamem), BUS_DMASYNC_POSTREAD);
3961c2498839Sstsp 
3962c2498839Sstsp 	while (ring->rp != rp) {
3963c2498839Sstsp 		e = qwx_pci_event_ring_get_elem(ring, ring->rp);
3964c2498839Sstsp 		if (e == NULL)
3965c2498839Sstsp 			return 0;
3966c2498839Sstsp 
3967c2498839Sstsp 		tre0 = le32toh(e->dword[0]);
3968c2498839Sstsp 		tre1 = le32toh(e->dword[1]);
3969c2498839Sstsp 
3970c2498839Sstsp 		len = (tre0 & MHI_TRE0_EV_LEN_MASK) >> MHI_TRE0_EV_LEN_SHFT;
3971c2498839Sstsp 		code = (tre0 & MHI_TRE0_EV_CODE_MASK) >> MHI_TRE0_EV_CODE_SHFT;
3972c2498839Sstsp 		type = (tre1 & MHI_TRE1_EV_TYPE_MASK) >> MHI_TRE1_EV_TYPE_SHFT;
3973c2498839Sstsp 		chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
3974c2498839Sstsp 		DNPRINTF(QWX_D_MHI, "%s: len=%u code=0x%x type=0x%x chid=%d\n",
3975c2498839Sstsp 		    __func__, len, code, type, chid);
3976c2498839Sstsp 
3977c2498839Sstsp 		switch (type) {
3978c2498839Sstsp 		case MHI_PKT_TYPE_TX_EVENT:
3979c2498839Sstsp 			qwx_pci_intr_data_event_tx(psc, e);
3980c2498839Sstsp 			break;
3981c2498839Sstsp 		default:
3982c2498839Sstsp 			printf("%s: unhandled event type 0x%x\n",
3983c2498839Sstsp 			    __func__, type);
3984c2498839Sstsp 			break;
3985c2498839Sstsp 		}
3986c2498839Sstsp 
3987c2498839Sstsp 		if (ring->rp + sizeof(*e) >= base + ring->size)
3988c2498839Sstsp 			ring->rp = base;
3989c2498839Sstsp 		else
3990c2498839Sstsp 			ring->rp += sizeof(*e);
3991c2498839Sstsp 
3992c2498839Sstsp 		if (ring->wp + sizeof(*e) >= base + ring->size)
3993c2498839Sstsp 			ring->wp = base;
3994c2498839Sstsp 		else
3995c2498839Sstsp 			ring->wp += sizeof(*e);
3996c2498839Sstsp 	}
3997c2498839Sstsp 
3998c2498839Sstsp 	c->wp = htole64(ring->wp);
3999c2498839Sstsp 
4000c2498839Sstsp 	bus_dmamap_sync(sc->sc_dmat, QWX_DMA_MAP(psc->event_ctxt), 0,
4001c2498839Sstsp 	    QWX_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_PREWRITE);
4002c2498839Sstsp 
4003c2498839Sstsp 	qwx_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
4004c2498839Sstsp 	return 1;
4005c2498839Sstsp }
4006c2498839Sstsp 
4007c2498839Sstsp int
40085b068355Sstsp qwx_pci_intr_mhi_ctrl(void *arg)
40095b068355Sstsp {
40105b068355Sstsp 	struct qwx_pci_softc *psc = arg;
40115b068355Sstsp 
40125b068355Sstsp 	if (qwx_pci_intr_ctrl_event(psc, &psc->event_rings[0]))
40135b068355Sstsp 		return 1;
40145b068355Sstsp 
40155b068355Sstsp 	return 0;
40165b068355Sstsp }
40175b068355Sstsp 
40185b068355Sstsp int
40195b068355Sstsp qwx_pci_intr_mhi_data(void *arg)
40205b068355Sstsp {
40215b068355Sstsp 	struct qwx_pci_softc *psc = arg;
40225b068355Sstsp 
40235b068355Sstsp 	if (qwx_pci_intr_data_event(psc, &psc->event_rings[1]))
40245b068355Sstsp 		return 1;
40255b068355Sstsp 
40265b068355Sstsp 	return 0;
40275b068355Sstsp }
40285b068355Sstsp 
40295b068355Sstsp int
4030c2498839Sstsp qwx_pci_intr(void *arg)
4031c2498839Sstsp {
4032c2498839Sstsp 	struct qwx_pci_softc *psc = arg;
4033c2498839Sstsp 	struct qwx_softc *sc = (void *)psc;
4034c2498839Sstsp 	uint32_t ee, state;
4035c2498839Sstsp 	int ret = 0;
4036c2498839Sstsp 
4037c2498839Sstsp 	/*
4038c2498839Sstsp 	 * Interrupts can trigger before mhi_start() during boot if the device
4039c2498839Sstsp 	 * is still active after a warm reboot.
4040c2498839Sstsp 	 */
4041c2498839Sstsp 	if (psc->bhi_off == 0)
4042c2498839Sstsp 		psc->bhi_off = qwx_pci_read(sc, MHI_BHI_OFFSET);
4043c2498839Sstsp 
4044c2498839Sstsp 	ee = qwx_pci_read(sc, psc->bhi_off + MHI_BHI_EXECENV);
4045c2498839Sstsp 	state = qwx_pci_read(sc, MHI_STATUS);
4046c2498839Sstsp 	state = (state & MHI_STATUS_MHISTATE_MASK) >>
4047c2498839Sstsp 	    MHI_STATUS_MHISTATE_SHFT;
4048c2498839Sstsp 
4049c2498839Sstsp 	DNPRINTF(QWX_D_MHI,
40505b068355Sstsp 	    "%s: BHI interrupt with EE: 0x%x -> 0x%x state: 0x%x -> 0x%x\n",
4051c2498839Sstsp 	     sc->sc_dev.dv_xname, psc->bhi_ee, ee, psc->mhi_state, state);
4052c2498839Sstsp 
4053c2498839Sstsp 	if (ee == MHI_EE_RDDM) {
405481cacbbeSstsp 		/* Firmware crash, e.g. due to invalid DMA memory access. */
4055c2498839Sstsp 		psc->bhi_ee = ee;
405681cacbbeSstsp #ifdef QWX_DEBUG
4057a891b672Sstsp 		if (!psc->rddm_triggered) {
405881cacbbeSstsp 			/* Write fw memory dump to root's home directory. */
4059c2498839Sstsp 			task_add(systq, &psc->rddm_task);
4060c2498839Sstsp 			psc->rddm_triggered = 1;
4061a891b672Sstsp 		}
4062a891b672Sstsp #else
406381cacbbeSstsp 		printf("%s: fatal firmware error\n",
406481cacbbeSstsp 		   sc->sc_dev.dv_xname);
40659032038bSstsp 		if (!test_bit(ATH11K_FLAG_CRASH_FLUSH, sc->sc_flags) &&
40669032038bSstsp 		    (sc->sc_ic.ic_if.if_flags & (IFF_UP | IFF_RUNNING)) ==
40679032038bSstsp 		    (IFF_UP | IFF_RUNNING)) {
406881cacbbeSstsp 			/* Try to reset the device. */
406981cacbbeSstsp 			set_bit(ATH11K_FLAG_CRASH_FLUSH, sc->sc_flags);
407081cacbbeSstsp 			task_add(systq, &sc->init_task);
407181cacbbeSstsp 		}
4072a891b672Sstsp #endif
4073c2498839Sstsp 		return 1;
4074c2498839Sstsp 	} else if (psc->bhi_ee == MHI_EE_PBL || psc->bhi_ee == MHI_EE_SBL) {
4075c2498839Sstsp 		int new_ee = -1, new_mhi_state = -1;
4076c2498839Sstsp 
4077c2498839Sstsp 		if (psc->bhi_ee != ee)
4078c2498839Sstsp 			new_ee = ee;
40796ff32849Sstsp 
40806ff32849Sstsp 		if (psc->mhi_state != state)
4081c2498839Sstsp 			new_mhi_state = state;
4082c2498839Sstsp 
4083c2498839Sstsp 		if (new_ee != -1 || new_mhi_state != -1)
4084a5176bb5Sstsp 			qwx_mhi_state_change(psc, new_ee, new_mhi_state);
4085c2498839Sstsp 
4086c2498839Sstsp 		ret = 1;
4087c2498839Sstsp 	}
4088c2498839Sstsp 
40895b068355Sstsp 	if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags)) {
40905b068355Sstsp 		int i;
40915b068355Sstsp 
4092c2498839Sstsp 		if (qwx_pci_intr_ctrl_event(psc, &psc->event_rings[0]))
4093c2498839Sstsp 			ret = 1;
4094c2498839Sstsp 		if (qwx_pci_intr_data_event(psc, &psc->event_rings[1]))
4095c2498839Sstsp 			ret = 1;
4096c2498839Sstsp 
40975b068355Sstsp 		for (i = 0; i < sc->hw_params.ce_count; i++) {
40985b068355Sstsp 			struct qwx_ce_pipe *ce_pipe = &sc->ce.ce_pipe[i];
40995b068355Sstsp 
41005b068355Sstsp 			if (qwx_ce_intr(ce_pipe))
4101c2498839Sstsp 				ret = 1;
41025b068355Sstsp 		}
41035b068355Sstsp 
4104b8360d45Spatrick 		if (test_bit(ATH11K_FLAG_EXT_IRQ_ENABLED, sc->sc_flags)) {
41055b068355Sstsp 			for (i = 0; i < nitems(sc->ext_irq_grp); i++) {
41065b068355Sstsp 				if (qwx_dp_service_srng(sc, i))
41075b068355Sstsp 					ret = 1;
41085b068355Sstsp 			}
41095b068355Sstsp 		}
411055f13601Spatrick 	}
4111c2498839Sstsp 
4112c2498839Sstsp 	return ret;
4113c2498839Sstsp }
4114