xref: /openbsd/sys/dev/pci/if_qwz_pci.c (revision eb49c7f0)
1*eb49c7f0Spatrick /*	$OpenBSD: if_qwz_pci.c,v 1.6 2024/12/09 09:35:33 patrick Exp $	*/
24bba8532Spatrick 
34bba8532Spatrick /*
44bba8532Spatrick  * Copyright 2023 Stefan Sperling <stsp@openbsd.org>
54bba8532Spatrick  *
64bba8532Spatrick  * Permission to use, copy, modify, and distribute this software for any
74bba8532Spatrick  * purpose with or without fee is hereby granted, provided that the above
84bba8532Spatrick  * copyright notice and this permission notice appear in all copies.
94bba8532Spatrick  *
104bba8532Spatrick  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
114bba8532Spatrick  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
124bba8532Spatrick  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
134bba8532Spatrick  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
144bba8532Spatrick  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
154bba8532Spatrick  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
164bba8532Spatrick  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
174bba8532Spatrick  */
184bba8532Spatrick 
194bba8532Spatrick /*
204bba8532Spatrick  * Copyright (c) 2021-2022, Qualcomm Innovation Center, Inc.
214bba8532Spatrick  * Copyright (c) 2018-2021 The Linux Foundation.
224bba8532Spatrick  * All rights reserved.
234bba8532Spatrick  *
244bba8532Spatrick  * Redistribution and use in source and binary forms, with or without
254bba8532Spatrick  * modification, are permitted (subject to the limitations in the disclaimer
264bba8532Spatrick  * below) provided that the following conditions are met:
274bba8532Spatrick  *
284bba8532Spatrick  *  * Redistributions of source code must retain the above copyright notice,
294bba8532Spatrick  *    this list of conditions and the following disclaimer.
304bba8532Spatrick  *
314bba8532Spatrick  *  * Redistributions in binary form must reproduce the above copyright
324bba8532Spatrick  *    notice, this list of conditions and the following disclaimer in the
334bba8532Spatrick  *    documentation and/or other materials provided with the distribution.
344bba8532Spatrick  *
354bba8532Spatrick  *  * Neither the name of [Owner Organization] nor the names of its
364bba8532Spatrick  *    contributors may be used to endorse or promote products derived from
374bba8532Spatrick  *    this software without specific prior written permission.
384bba8532Spatrick  *
394bba8532Spatrick  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
404bba8532Spatrick  * THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
414bba8532Spatrick  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
424bba8532Spatrick  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
434bba8532Spatrick  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
444bba8532Spatrick  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
454bba8532Spatrick  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
464bba8532Spatrick  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
474bba8532Spatrick  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
484bba8532Spatrick  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
494bba8532Spatrick  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
504bba8532Spatrick  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
514bba8532Spatrick  */
524bba8532Spatrick 
534bba8532Spatrick #include "bpfilter.h"
544bba8532Spatrick 
554bba8532Spatrick #include <sys/param.h>
564bba8532Spatrick #include <sys/mbuf.h>
574bba8532Spatrick #include <sys/lock.h>
584bba8532Spatrick #include <sys/socket.h>
594bba8532Spatrick #include <sys/systm.h>
604bba8532Spatrick #include <sys/malloc.h>
614bba8532Spatrick #include <sys/device.h>
624bba8532Spatrick #include <sys/endian.h>
634bba8532Spatrick 
644bba8532Spatrick #include <machine/bus.h>
654bba8532Spatrick #include <machine/intr.h>
664bba8532Spatrick 
674bba8532Spatrick #include <net/if.h>
684bba8532Spatrick #include <net/if_media.h>
694bba8532Spatrick 
704bba8532Spatrick #include <netinet/in.h>
714bba8532Spatrick #include <netinet/if_ether.h>
724bba8532Spatrick 
734bba8532Spatrick #include <net80211/ieee80211_var.h>
744bba8532Spatrick #include <net80211/ieee80211_radiotap.h>
754bba8532Spatrick 
764bba8532Spatrick #include <dev/pci/pcireg.h>
774bba8532Spatrick #include <dev/pci/pcivar.h>
784bba8532Spatrick #include <dev/pci/pcidevs.h>
794bba8532Spatrick 
804bba8532Spatrick /* XXX linux porting goo */
814bba8532Spatrick #ifdef __LP64__
824bba8532Spatrick #define BITS_PER_LONG		64
834bba8532Spatrick #else
844bba8532Spatrick #define BITS_PER_LONG		32
854bba8532Spatrick #endif
864bba8532Spatrick #define GENMASK(h, l) (((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
874bba8532Spatrick #define __bf_shf(x) (__builtin_ffsll(x) - 1)
884bba8532Spatrick #define FIELD_GET(_m, _v) ((typeof(_m))(((_v) & (_m)) >> __bf_shf(_m)))
894bba8532Spatrick #define BIT(x)               (1UL << (x))
904bba8532Spatrick #define test_bit(i, a)  ((a) & (1 << (i)))
914bba8532Spatrick #define clear_bit(i, a) ((a)) &= ~(1 << (i))
924bba8532Spatrick #define set_bit(i, a)   ((a)) |= (1 << (i))
934bba8532Spatrick 
944bba8532Spatrick /* #define QWZ_DEBUG */
954bba8532Spatrick 
964bba8532Spatrick #include <dev/ic/qwzreg.h>
974bba8532Spatrick #include <dev/ic/qwzvar.h>
984bba8532Spatrick 
994bba8532Spatrick #ifdef QWZ_DEBUG
1004bba8532Spatrick /* Headers needed for RDDM dump */
1014bba8532Spatrick #include <sys/namei.h>
1024bba8532Spatrick #include <sys/pledge.h>
1034bba8532Spatrick #include <sys/vnode.h>
1044bba8532Spatrick #include <sys/fcntl.h>
1054bba8532Spatrick #include <sys/stat.h>
1064bba8532Spatrick #include <sys/proc.h>
1074bba8532Spatrick #endif
1084bba8532Spatrick 
1094bba8532Spatrick #define ATH12K_PCI_IRQ_CE0_OFFSET	3
1104bba8532Spatrick #define ATH12K_PCI_IRQ_DP_OFFSET	14
1114bba8532Spatrick 
1124bba8532Spatrick #define ATH12K_PCI_CE_WAKE_IRQ		2
1134bba8532Spatrick 
1144bba8532Spatrick #define ATH12K_PCI_WINDOW_ENABLE_BIT	0x40000000
1154bba8532Spatrick #define ATH12K_PCI_WINDOW_REG_ADDRESS	0x310c
1164bba8532Spatrick #define ATH12K_PCI_WINDOW_VALUE_MASK	GENMASK(24, 19)
1174bba8532Spatrick #define ATH12K_PCI_WINDOW_START		0x80000
1184bba8532Spatrick #define ATH12K_PCI_WINDOW_RANGE_MASK	GENMASK(18, 0)
11927c3d914Spatrick #define ATH12K_PCI_WINDOW_STATIC_MASK	GENMASK(31, 6)
1204bba8532Spatrick 
1214bba8532Spatrick /* BAR0 + 4k is always accessible, and no need to force wakeup. */
1224bba8532Spatrick #define ATH12K_PCI_ACCESS_ALWAYS_OFF	0xFE0	/* 4K - 32 = 0xFE0 */
1234bba8532Spatrick 
12427c3d914Spatrick #define TCSR_SOC_HW_VERSION		0x1b00000
1254bba8532Spatrick #define TCSR_SOC_HW_VERSION_MAJOR_MASK	GENMASK(11, 8)
1264bba8532Spatrick #define TCSR_SOC_HW_VERSION_MINOR_MASK	GENMASK(7, 0)
1274bba8532Spatrick 
1284bba8532Spatrick /*
1294bba8532Spatrick  * pci.h
1304bba8532Spatrick  */
1314bba8532Spatrick #define PCIE_SOC_GLOBAL_RESET			0x3008
1324bba8532Spatrick #define PCIE_SOC_GLOBAL_RESET_V			1
1334bba8532Spatrick 
1344bba8532Spatrick #define WLAON_WARM_SW_ENTRY			0x1f80504
1354bba8532Spatrick #define WLAON_SOC_RESET_CAUSE_REG		0x01f8060c
1364bba8532Spatrick 
1374bba8532Spatrick #define PCIE_Q6_COOKIE_ADDR			0x01f80500
1384bba8532Spatrick #define PCIE_Q6_COOKIE_DATA			0xc0000000
1394bba8532Spatrick 
1404bba8532Spatrick /* register to wake the UMAC from power collapse */
1414bba8532Spatrick #define PCIE_SCRATCH_0_SOC_PCIE_REG		0x4040
1424bba8532Spatrick 
1434bba8532Spatrick /* register used for handshake mechanism to validate UMAC is awake */
1444bba8532Spatrick #define PCIE_SOC_WAKE_PCIE_LOCAL_REG		0x3004
1454bba8532Spatrick 
1464bba8532Spatrick #define PCIE_PCIE_PARF_LTSSM			0x1e081b0
1474bba8532Spatrick #define PARM_LTSSM_VALUE			0x111
1484bba8532Spatrick 
14927c3d914Spatrick #define GCC_GCC_PCIE_HOT_RST			0x1e38338
1504bba8532Spatrick #define GCC_GCC_PCIE_HOT_RST_VAL		0x10
1514bba8532Spatrick 
1524bba8532Spatrick #define PCIE_PCIE_INT_ALL_CLEAR			0x1e08228
1534bba8532Spatrick #define PCIE_SMLH_REQ_RST_LINK_DOWN		0x2
1544bba8532Spatrick #define PCIE_INT_CLEAR_ALL			0xffffffff
1554bba8532Spatrick 
1564bba8532Spatrick #define PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(sc) \
1574bba8532Spatrick 		(sc->hw_params.regs->pcie_qserdes_sysclk_en_sel)
1584bba8532Spatrick #define PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL	0x10
1594bba8532Spatrick #define PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK	0xffffffff
1604bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG1_REG(sc) \
1614bba8532Spatrick 		(sc->hw_params.regs->pcie_pcs_osc_dtct_config_base)
1624bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG1_VAL		0x02
1634bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG2_REG(sc) \
1644bba8532Spatrick 		(sc->hw_params.regs->pcie_pcs_osc_dtct_config_base + 0x4)
1654bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG2_VAL		0x52
1664bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG4_REG(sc) \
1674bba8532Spatrick 		(sc->hw_params.regs->pcie_pcs_osc_dtct_config_base + 0xc)
1684bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG4_VAL		0xff
1694bba8532Spatrick #define PCIE_PCS_OSC_DTCT_CONFIG_MSK		0x000000ff
1704bba8532Spatrick 
1714bba8532Spatrick #define WLAON_QFPROM_PWR_CTRL_REG		0x01f8031c
1724bba8532Spatrick #define QFPROM_PWR_CTRL_VDD4BLOW_MASK		0x4
1734bba8532Spatrick 
17427c3d914Spatrick #define PCI_MHIREGLEN_REG			0x1e0e100
17527c3d914Spatrick #define PCI_MHI_REGION_END			0x1e0effc
17627c3d914Spatrick 
1774bba8532Spatrick /*
1784bba8532Spatrick  * mhi.h
1794bba8532Spatrick  */
1804bba8532Spatrick #define PCIE_TXVECDB				0x360
1814bba8532Spatrick #define PCIE_TXVECSTATUS			0x368
1824bba8532Spatrick #define PCIE_RXVECDB				0x394
1834bba8532Spatrick #define PCIE_RXVECSTATUS			0x39C
1844bba8532Spatrick 
1854bba8532Spatrick #define MHI_CHAN_CTX_CHSTATE_MASK		GENMASK(7, 0)
1864bba8532Spatrick #define   MHI_CHAN_CTX_CHSTATE_DISABLED		0
1874bba8532Spatrick #define   MHI_CHAN_CTX_CHSTATE_ENABLED		1
1884bba8532Spatrick #define   MHI_CHAN_CTX_CHSTATE_RUNNING		2
1894bba8532Spatrick #define   MHI_CHAN_CTX_CHSTATE_SUSPENDED	3
1904bba8532Spatrick #define   MHI_CHAN_CTX_CHSTATE_STOP		4
1914bba8532Spatrick #define   MHI_CHAN_CTX_CHSTATE_ERROR		5
1924bba8532Spatrick #define MHI_CHAN_CTX_BRSTMODE_MASK		GENMASK(9, 8)
1934bba8532Spatrick #define MHI_CHAN_CTX_BRSTMODE_SHFT		8
1944bba8532Spatrick #define   MHI_CHAN_CTX_BRSTMODE_DISABLE		2
1954bba8532Spatrick #define   MHI_CHAN_CTX_BRSTMODE_ENABLE		3
1964bba8532Spatrick #define MHI_CHAN_CTX_POLLCFG_MASK		GENMASK(15, 10)
1974bba8532Spatrick #define MHI_CHAN_CTX_RESERVED_MASK		GENMASK(31, 16)
1984bba8532Spatrick 
19909a673e5Spatrick #define QWZ_MHI_CONFIG_WCN7850_MAX_CHANNELS	128
20009a673e5Spatrick #define QWZ_MHI_CONFIG_WCN7850_TIMEOUT_MS	2000
2014bba8532Spatrick 
2024bba8532Spatrick #define MHI_CHAN_TYPE_INVALID		0
2034bba8532Spatrick #define MHI_CHAN_TYPE_OUTBOUND		1 /* to device */
2044bba8532Spatrick #define MHI_CHAN_TYPE_INBOUND		2 /* from device */
2054bba8532Spatrick #define MHI_CHAN_TYPE_INBOUND_COALESCED	3
2064bba8532Spatrick 
2074bba8532Spatrick #define MHI_EV_CTX_RESERVED_MASK	GENMASK(7, 0)
2084bba8532Spatrick #define MHI_EV_CTX_INTMODC_MASK		GENMASK(15, 8)
2094bba8532Spatrick #define MHI_EV_CTX_INTMODT_MASK		GENMASK(31, 16)
2104bba8532Spatrick #define MHI_EV_CTX_INTMODT_SHFT		16
2114bba8532Spatrick 
2124bba8532Spatrick #define MHI_ER_TYPE_INVALID	0
2134bba8532Spatrick #define MHI_ER_TYPE_VALID	1
2144bba8532Spatrick 
2154bba8532Spatrick #define MHI_ER_DATA	0
2164bba8532Spatrick #define MHI_ER_CTRL	1
2174bba8532Spatrick 
2184bba8532Spatrick #define MHI_CH_STATE_DISABLED	0
2194bba8532Spatrick #define MHI_CH_STATE_ENABLED	1
2204bba8532Spatrick #define MHI_CH_STATE_RUNNING	2
2214bba8532Spatrick #define MHI_CH_STATE_SUSPENDED	3
2224bba8532Spatrick #define MHI_CH_STATE_STOP	4
2234bba8532Spatrick #define MHI_CH_STATE_ERROR	5
2244bba8532Spatrick 
2254bba8532Spatrick #define QWZ_NUM_EVENT_CTX	2
2264bba8532Spatrick 
2274bba8532Spatrick /* Event context. Shared with device. */
2284bba8532Spatrick struct qwz_mhi_event_ctxt {
2294bba8532Spatrick 	uint32_t intmod;
2304bba8532Spatrick 	uint32_t ertype;
2314bba8532Spatrick 	uint32_t msivec;
2324bba8532Spatrick 
2334bba8532Spatrick 	uint64_t rbase;
2344bba8532Spatrick 	uint64_t rlen;
2354bba8532Spatrick 	uint64_t rp;
2364bba8532Spatrick 	uint64_t wp;
2374bba8532Spatrick } __packed;
2384bba8532Spatrick 
2394bba8532Spatrick /* Channel context. Shared with device. */
2404bba8532Spatrick struct qwz_mhi_chan_ctxt {
2414bba8532Spatrick 	uint32_t chcfg;
2424bba8532Spatrick 	uint32_t chtype;
2434bba8532Spatrick 	uint32_t erindex;
2444bba8532Spatrick 
2454bba8532Spatrick 	uint64_t rbase;
2464bba8532Spatrick 	uint64_t rlen;
2474bba8532Spatrick 	uint64_t rp;
2484bba8532Spatrick 	uint64_t wp;
2494bba8532Spatrick } __packed;
2504bba8532Spatrick 
2514bba8532Spatrick /* Command context. Shared with device. */
2524bba8532Spatrick struct qwz_mhi_cmd_ctxt {
2534bba8532Spatrick 	uint32_t reserved0;
2544bba8532Spatrick 	uint32_t reserved1;
2554bba8532Spatrick 	uint32_t reserved2;
2564bba8532Spatrick 
2574bba8532Spatrick 	uint64_t rbase;
2584bba8532Spatrick 	uint64_t rlen;
2594bba8532Spatrick 	uint64_t rp;
2604bba8532Spatrick 	uint64_t wp;
2614bba8532Spatrick } __packed;
2624bba8532Spatrick 
2634bba8532Spatrick struct qwz_mhi_ring_element {
2644bba8532Spatrick 	uint64_t ptr;
2654bba8532Spatrick 	uint32_t dword[2];
2664bba8532Spatrick };
2674bba8532Spatrick 
2684bba8532Spatrick struct qwz_xfer_data {
2694bba8532Spatrick 	bus_dmamap_t	map;
2704bba8532Spatrick 	struct mbuf	*m;
2714bba8532Spatrick };
2724bba8532Spatrick 
2734bba8532Spatrick #define QWZ_PCI_XFER_MAX_DATA_SIZE	0xffff
2744bba8532Spatrick #define QWZ_PCI_XFER_RING_MAX_ELEMENTS	64
2754bba8532Spatrick 
2764bba8532Spatrick struct qwz_pci_xfer_ring {
2774bba8532Spatrick 	struct qwz_dmamem	*dmamem;
2784bba8532Spatrick 	bus_size_t		size;
2794bba8532Spatrick 	uint32_t		mhi_chan_id;
2804bba8532Spatrick 	uint32_t		mhi_chan_state;
2814bba8532Spatrick 	uint32_t		mhi_chan_direction;
2824bba8532Spatrick 	uint32_t		mhi_chan_event_ring_index;
2834bba8532Spatrick 	uint32_t		db_addr;
2844bba8532Spatrick 	uint32_t		cmd_status;
2854bba8532Spatrick 	int			num_elements;
2864bba8532Spatrick 	int			queued;
2874bba8532Spatrick 	struct qwz_xfer_data	data[QWZ_PCI_XFER_RING_MAX_ELEMENTS];
2884bba8532Spatrick 	uint64_t		rp;
2894bba8532Spatrick 	uint64_t		wp;
2904bba8532Spatrick 	struct qwz_mhi_chan_ctxt *chan_ctxt;
2914bba8532Spatrick };
2924bba8532Spatrick 
2934bba8532Spatrick 
2944bba8532Spatrick #define QWZ_PCI_EVENT_RING_MAX_ELEMENTS	256
2954bba8532Spatrick 
2964bba8532Spatrick struct qwz_pci_event_ring {
2974bba8532Spatrick 	struct qwz_dmamem	*dmamem;
2984bba8532Spatrick 	bus_size_t		size;
2994bba8532Spatrick 	uint32_t		mhi_er_type;
3004bba8532Spatrick 	uint32_t		mhi_er_irq;
3014bba8532Spatrick 	uint32_t		mhi_er_irq_moderation_ms;
3024bba8532Spatrick 	uint32_t		db_addr;
3034bba8532Spatrick 	int			num_elements;
3044bba8532Spatrick 	uint64_t		rp;
3054bba8532Spatrick 	uint64_t		wp;
3064bba8532Spatrick 	struct qwz_mhi_event_ctxt *event_ctxt;
3074bba8532Spatrick };
3084bba8532Spatrick 
3094bba8532Spatrick struct qwz_cmd_data {
3104bba8532Spatrick 	bus_dmamap_t	map;
3114bba8532Spatrick 	struct mbuf	*m;
3124bba8532Spatrick };
3134bba8532Spatrick 
3144bba8532Spatrick #define QWZ_PCI_CMD_RING_MAX_ELEMENTS	128
3154bba8532Spatrick 
3164bba8532Spatrick struct qwz_pci_cmd_ring {
3174bba8532Spatrick 	struct qwz_dmamem	*dmamem;
3184bba8532Spatrick 	bus_size_t		size;
3194bba8532Spatrick 	uint64_t		rp;
3204bba8532Spatrick 	uint64_t		wp;
3214bba8532Spatrick 	int			num_elements;
3224bba8532Spatrick 	int			queued;
3234bba8532Spatrick };
3244bba8532Spatrick 
3254bba8532Spatrick struct qwz_pci_ops;
3264bba8532Spatrick struct qwz_msi_config;
3274bba8532Spatrick 
3284bba8532Spatrick #define QWZ_NUM_MSI_VEC	32
3294bba8532Spatrick 
3304bba8532Spatrick struct qwz_pci_softc {
3314bba8532Spatrick 	struct qwz_softc	sc_sc;
3324bba8532Spatrick 	pci_chipset_tag_t	sc_pc;
3334bba8532Spatrick 	pcitag_t		sc_tag;
3344bba8532Spatrick 	int			sc_cap_off;
3354bba8532Spatrick 	int			sc_msi_off;
3364bba8532Spatrick 	pcireg_t		sc_msi_cap;
3374bba8532Spatrick 	void			*sc_ih[QWZ_NUM_MSI_VEC];
3384bba8532Spatrick 	char			sc_ivname[QWZ_NUM_MSI_VEC][16];
3394bba8532Spatrick 	struct qwz_ext_irq_grp	ext_irq_grp[ATH12K_EXT_IRQ_GRP_NUM_MAX];
3404bba8532Spatrick 	int			mhi_irq[2];
3414bba8532Spatrick 	bus_space_tag_t		sc_st;
3424bba8532Spatrick 	bus_space_handle_t	sc_sh;
3434bba8532Spatrick 	bus_addr_t		sc_map;
3444bba8532Spatrick 	bus_size_t		sc_mapsize;
3454bba8532Spatrick 
3464bba8532Spatrick 	pcireg_t		sc_lcsr;
3474bba8532Spatrick 	uint32_t		sc_flags;
3484bba8532Spatrick #define ATH12K_PCI_ASPM_RESTORE	1
3494bba8532Spatrick 
3504bba8532Spatrick 	uint32_t		register_window;
3514bba8532Spatrick 	const struct qwz_pci_ops *sc_pci_ops;
3524bba8532Spatrick 
3534bba8532Spatrick 	uint32_t		 bhi_off;
3544bba8532Spatrick 	uint32_t		 bhi_ee;
3554bba8532Spatrick 	uint32_t		 bhie_off;
3564bba8532Spatrick 	uint32_t		 mhi_state;
3574bba8532Spatrick 	uint32_t		 max_chan;
3584bba8532Spatrick 
3594bba8532Spatrick 	uint64_t		 wake_db;
3604bba8532Spatrick 
3614bba8532Spatrick 	/*
3624bba8532Spatrick 	 * DMA memory for AMSS.bin firmware image.
3634bba8532Spatrick 	 * This memory must remain available to the device until
3644bba8532Spatrick 	 * the device is powered down.
3654bba8532Spatrick 	 */
3664bba8532Spatrick 	struct qwz_dmamem	*amss_data;
3674bba8532Spatrick 	struct qwz_dmamem	*amss_vec;
3684bba8532Spatrick 
3694bba8532Spatrick 	struct qwz_dmamem	 *rddm_vec;
3704bba8532Spatrick 	struct qwz_dmamem	 *rddm_data;
3714bba8532Spatrick 	int			 rddm_triggered;
3724bba8532Spatrick 	struct task		 rddm_task;
3734bba8532Spatrick #define	QWZ_RDDM_DUMP_SIZE	0x420000
3744bba8532Spatrick 
3754bba8532Spatrick 	struct qwz_dmamem	*chan_ctxt;
3764bba8532Spatrick 	struct qwz_dmamem	*event_ctxt;
3774bba8532Spatrick 	struct qwz_dmamem	*cmd_ctxt;
3784bba8532Spatrick 
3794bba8532Spatrick 
38027c3d914Spatrick 	struct qwz_pci_xfer_ring xfer_rings[2];
38127c3d914Spatrick #define QWZ_PCI_XFER_RING_IPCR_OUTBOUND		0
38227c3d914Spatrick #define QWZ_PCI_XFER_RING_IPCR_INBOUND		1
3834bba8532Spatrick 	struct qwz_pci_event_ring event_rings[QWZ_NUM_EVENT_CTX];
3844bba8532Spatrick 	struct qwz_pci_cmd_ring cmd_ring;
3854bba8532Spatrick };
3864bba8532Spatrick 
3874bba8532Spatrick int	qwz_pci_match(struct device *, void *, void *);
3884bba8532Spatrick void	qwz_pci_attach(struct device *, struct device *, void *);
3894bba8532Spatrick int	qwz_pci_detach(struct device *, int);
3904bba8532Spatrick void	qwz_pci_attach_hook(struct device *);
3914bba8532Spatrick void	qwz_pci_free_xfer_rings(struct qwz_pci_softc *);
3924bba8532Spatrick int	qwz_pci_alloc_xfer_ring(struct qwz_softc *, struct qwz_pci_xfer_ring *,
3934bba8532Spatrick 	    uint32_t, uint32_t, uint32_t, size_t);
39409a673e5Spatrick int	qwz_pci_alloc_xfer_rings_wcn7850(struct qwz_pci_softc *);
3954bba8532Spatrick void	qwz_pci_free_event_rings(struct qwz_pci_softc *);
3964bba8532Spatrick int	qwz_pci_alloc_event_ring(struct qwz_softc *,
3974bba8532Spatrick 	    struct qwz_pci_event_ring *, uint32_t, uint32_t, uint32_t, size_t);
3984bba8532Spatrick int	qwz_pci_alloc_event_rings(struct qwz_pci_softc *);
3994bba8532Spatrick void	qwz_pci_free_cmd_ring(struct qwz_pci_softc *);
4004bba8532Spatrick int	qwz_pci_init_cmd_ring(struct qwz_softc *, struct qwz_pci_cmd_ring *);
4014bba8532Spatrick uint32_t qwz_pci_read(struct qwz_softc *, uint32_t);
4024bba8532Spatrick void	qwz_pci_write(struct qwz_softc *, uint32_t, uint32_t);
4034bba8532Spatrick 
4044bba8532Spatrick void	qwz_pci_read_hw_version(struct qwz_softc *, uint32_t *, uint32_t *);
4054bba8532Spatrick uint32_t qwz_pcic_read32(struct qwz_softc *, uint32_t);
4064bba8532Spatrick void	 qwz_pcic_write32(struct qwz_softc *, uint32_t, uint32_t);
4074bba8532Spatrick 
4084bba8532Spatrick void	qwz_pcic_ext_irq_enable(struct qwz_softc *);
4094bba8532Spatrick void	qwz_pcic_ext_irq_disable(struct qwz_softc *);
4104bba8532Spatrick int	qwz_pcic_config_irq(struct qwz_softc *, struct pci_attach_args *);
4114bba8532Spatrick 
4124bba8532Spatrick int	qwz_pci_start(struct qwz_softc *);
4134bba8532Spatrick void	qwz_pci_stop(struct qwz_softc *);
4144bba8532Spatrick void	qwz_pci_aspm_disable(struct qwz_softc *);
4154bba8532Spatrick void	qwz_pci_aspm_restore(struct qwz_softc *);
4164bba8532Spatrick int	qwz_pci_power_up(struct qwz_softc *);
4174bba8532Spatrick void	qwz_pci_power_down(struct qwz_softc *);
4184bba8532Spatrick 
4194bba8532Spatrick int	qwz_pci_bus_wake_up(struct qwz_softc *);
4204bba8532Spatrick void	qwz_pci_bus_release(struct qwz_softc *);
4214bba8532Spatrick void	qwz_pci_window_write32(struct qwz_softc *, uint32_t, uint32_t);
4224bba8532Spatrick uint32_t qwz_pci_window_read32(struct qwz_softc *, uint32_t);
4234bba8532Spatrick 
4244bba8532Spatrick int	qwz_mhi_register(struct qwz_softc *);
4254bba8532Spatrick void	qwz_mhi_unregister(struct qwz_softc *);
4264bba8532Spatrick void	qwz_mhi_ring_doorbell(struct qwz_softc *sc, uint64_t, uint64_t);
4274bba8532Spatrick void	qwz_mhi_device_wake(struct qwz_softc *);
4284bba8532Spatrick void	qwz_mhi_device_zzz(struct qwz_softc *);
4294bba8532Spatrick int	qwz_mhi_wake_db_clear_valid(struct qwz_softc *);
4304bba8532Spatrick void	qwz_mhi_init_xfer_rings(struct qwz_pci_softc *);
4314bba8532Spatrick void	qwz_mhi_init_event_rings(struct qwz_pci_softc *);
4324bba8532Spatrick void	qwz_mhi_init_cmd_ring(struct qwz_pci_softc *);
4334bba8532Spatrick void	qwz_mhi_init_dev_ctxt(struct qwz_pci_softc *);
4344bba8532Spatrick int	qwz_mhi_send_cmd(struct qwz_pci_softc *psc, uint32_t, uint32_t);
4354bba8532Spatrick void *	qwz_pci_xfer_ring_get_elem(struct qwz_pci_xfer_ring *, uint64_t);
4364bba8532Spatrick struct qwz_xfer_data *qwz_pci_xfer_ring_get_data(struct qwz_pci_xfer_ring *,
4374bba8532Spatrick 	    uint64_t);
4384bba8532Spatrick int	qwz_mhi_submit_xfer(struct qwz_softc *sc, struct mbuf *m);
4394bba8532Spatrick int	qwz_mhi_start_channel(struct qwz_pci_softc *,
4404bba8532Spatrick 	    struct qwz_pci_xfer_ring *);
4414bba8532Spatrick int	qwz_mhi_start_channels(struct qwz_pci_softc *);
4424bba8532Spatrick int	qwz_mhi_start(struct qwz_pci_softc *);
4434bba8532Spatrick void	qwz_mhi_stop(struct qwz_softc *);
4444bba8532Spatrick int	qwz_mhi_reset_device(struct qwz_softc *, int);
4454bba8532Spatrick void	qwz_mhi_clear_vector(struct qwz_softc *);
4464bba8532Spatrick int	qwz_mhi_fw_load_handler(struct qwz_pci_softc *);
4474bba8532Spatrick int	qwz_mhi_await_device_reset(struct qwz_softc *);
4484bba8532Spatrick int	qwz_mhi_await_device_ready(struct qwz_softc *);
4494bba8532Spatrick void	qwz_mhi_ready_state_transition(struct qwz_pci_softc *);
4504bba8532Spatrick void	qwz_mhi_mission_mode_state_transition(struct qwz_pci_softc *);
4514bba8532Spatrick void	qwz_mhi_low_power_mode_state_transition(struct qwz_pci_softc *);
4524bba8532Spatrick void	qwz_mhi_set_state(struct qwz_softc *, uint32_t);
4534bba8532Spatrick void	qwz_mhi_init_mmio(struct qwz_pci_softc *);
4544bba8532Spatrick int	qwz_mhi_fw_load_bhi(struct qwz_pci_softc *, uint8_t *, size_t);
4554bba8532Spatrick int	qwz_mhi_fw_load_bhie(struct qwz_pci_softc *, uint8_t *, size_t);
4564bba8532Spatrick void	qwz_rddm_prepare(struct qwz_pci_softc *);
4574bba8532Spatrick #ifdef QWZ_DEBUG
4584bba8532Spatrick void	qwz_rddm_task(void *);
4594bba8532Spatrick #endif
4604bba8532Spatrick void *	qwz_pci_event_ring_get_elem(struct qwz_pci_event_ring *, uint64_t);
4614bba8532Spatrick void	qwz_pci_intr_ctrl_event_mhi(struct qwz_pci_softc *, uint32_t);
4624bba8532Spatrick void	qwz_pci_intr_ctrl_event_ee(struct qwz_pci_softc *, uint32_t);
4634bba8532Spatrick void	qwz_pci_intr_ctrl_event_cmd_complete(struct qwz_pci_softc *,
4644bba8532Spatrick 	    uint64_t, uint32_t);
4654bba8532Spatrick int	qwz_pci_intr_ctrl_event(struct qwz_pci_softc *,
4664bba8532Spatrick 	    struct qwz_pci_event_ring *);
4674bba8532Spatrick void	qwz_pci_intr_data_event_tx(struct qwz_pci_softc *,
4684bba8532Spatrick 	    struct qwz_mhi_ring_element *);
4694bba8532Spatrick int	qwz_pci_intr_data_event(struct qwz_pci_softc *,
4704bba8532Spatrick 	    struct qwz_pci_event_ring *);
4714bba8532Spatrick int	qwz_pci_intr_mhi_ctrl(void *);
4724bba8532Spatrick int	qwz_pci_intr_mhi_data(void *);
4734bba8532Spatrick int	qwz_pci_intr(void *);
4744bba8532Spatrick 
4754bba8532Spatrick struct qwz_pci_ops {
4764bba8532Spatrick 	int	 (*wakeup)(struct qwz_softc *);
4774bba8532Spatrick 	void	 (*release)(struct qwz_softc *);
4784bba8532Spatrick 	int	 (*get_msi_irq)(struct qwz_softc *, unsigned int);
4794bba8532Spatrick 	void	 (*window_write32)(struct qwz_softc *, uint32_t, uint32_t);
4804bba8532Spatrick 	uint32_t (*window_read32)(struct qwz_softc *, uint32_t);
4814bba8532Spatrick 	int	 (*alloc_xfer_rings)(struct qwz_pci_softc *);
4824bba8532Spatrick };
4834bba8532Spatrick 
4844bba8532Spatrick 
48527c3d914Spatrick static const struct qwz_pci_ops qwz_pci_ops_wcn7850 = {
4864bba8532Spatrick 	.wakeup = qwz_pci_bus_wake_up,
4874bba8532Spatrick 	.release = qwz_pci_bus_release,
4884bba8532Spatrick 	.window_write32 = qwz_pci_window_write32,
4894bba8532Spatrick 	.window_read32 = qwz_pci_window_read32,
49009a673e5Spatrick 	.alloc_xfer_rings = qwz_pci_alloc_xfer_rings_wcn7850,
4914bba8532Spatrick };
4924bba8532Spatrick 
4934bba8532Spatrick const struct cfattach qwz_pci_ca = {
4944bba8532Spatrick 	sizeof(struct qwz_pci_softc),
4954bba8532Spatrick 	qwz_pci_match,
4964bba8532Spatrick 	qwz_pci_attach,
4974bba8532Spatrick 	qwz_pci_detach,
4984bba8532Spatrick 	qwz_activate
4994bba8532Spatrick };
5004bba8532Spatrick 
5014bba8532Spatrick static const struct pci_matchid qwz_pci_devices[] = {
50227c3d914Spatrick 	{ PCI_VENDOR_QUALCOMM, PCI_PRODUCT_QUALCOMM_WCN7850 }
5034bba8532Spatrick };
5044bba8532Spatrick 
5054bba8532Spatrick int
qwz_pci_match(struct device * parent,void * match,void * aux)5064bba8532Spatrick qwz_pci_match(struct device *parent, void *match, void *aux)
5074bba8532Spatrick {
5084bba8532Spatrick 	return pci_matchbyid(aux, qwz_pci_devices, nitems(qwz_pci_devices));
5094bba8532Spatrick }
5104bba8532Spatrick 
5114bba8532Spatrick void
qwz_pci_init_qmi_ce_config(struct qwz_softc * sc)5124bba8532Spatrick qwz_pci_init_qmi_ce_config(struct qwz_softc *sc)
5134bba8532Spatrick {
5144bba8532Spatrick 	struct qwz_qmi_ce_cfg *cfg = &sc->qmi_ce_cfg;
5154bba8532Spatrick 
51627c3d914Spatrick 	qwz_ce_get_shadow_config(sc, &cfg->shadow_reg_v3,
51727c3d914Spatrick 	    &cfg->shadow_reg_v3_len);
5184bba8532Spatrick }
5194bba8532Spatrick 
5204bba8532Spatrick const struct qwz_msi_config qwz_msi_config_one_msi = {
5214bba8532Spatrick 	.total_vectors = 1,
5224bba8532Spatrick 	.total_users = 4,
5234bba8532Spatrick 	.users = (struct qwz_msi_user[]) {
5244bba8532Spatrick 		{ .name = "MHI", .num_vectors = 1, .base_vector = 0 },
5254bba8532Spatrick 		{ .name = "CE", .num_vectors = 1, .base_vector = 0 },
5264bba8532Spatrick 		{ .name = "WAKE", .num_vectors = 1, .base_vector = 0 },
5274bba8532Spatrick 		{ .name = "DP", .num_vectors = 1, .base_vector = 0 },
5284bba8532Spatrick 	},
5294bba8532Spatrick };
5304bba8532Spatrick 
5314bba8532Spatrick const struct qwz_msi_config qwz_msi_config[] = {
5324bba8532Spatrick 	{
5334bba8532Spatrick 		.total_vectors = 16,
5344bba8532Spatrick 		.total_users = 3,
5354bba8532Spatrick 		.users = (struct qwz_msi_user[]) {
5364bba8532Spatrick 			{ .name = "MHI", .num_vectors = 3, .base_vector = 0 },
5374bba8532Spatrick 			{ .name = "CE", .num_vectors = 5, .base_vector = 3 },
5384bba8532Spatrick 			{ .name = "DP", .num_vectors = 8, .base_vector = 8 },
5394bba8532Spatrick 		},
54027c3d914Spatrick 		.hw_rev = ATH12K_HW_WCN7850_HW20,
5414bba8532Spatrick 	},
5424bba8532Spatrick };
5434bba8532Spatrick 
5444bba8532Spatrick int
qwz_pcic_init_msi_config(struct qwz_softc * sc)5454bba8532Spatrick qwz_pcic_init_msi_config(struct qwz_softc *sc)
5464bba8532Spatrick {
5474bba8532Spatrick 	const struct qwz_msi_config *msi_config;
5484bba8532Spatrick 	int i;
5494bba8532Spatrick 
5504bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags)) {
5514bba8532Spatrick 		sc->msi_cfg = &qwz_msi_config_one_msi;
5524bba8532Spatrick 		return 0;
5534bba8532Spatrick 	}
5544bba8532Spatrick 	for (i = 0; i < nitems(qwz_msi_config); i++) {
5554bba8532Spatrick 		msi_config = &qwz_msi_config[i];
5564bba8532Spatrick 
5574bba8532Spatrick 		if (msi_config->hw_rev == sc->sc_hw_rev)
5584bba8532Spatrick 			break;
5594bba8532Spatrick 	}
5604bba8532Spatrick 
5614bba8532Spatrick 	if (i == nitems(qwz_msi_config)) {
5624bba8532Spatrick 		printf("%s: failed to fetch msi config, "
5634bba8532Spatrick 		    "unsupported hw version: 0x%x\n",
5644bba8532Spatrick 		    sc->sc_dev.dv_xname, sc->sc_hw_rev);
5654bba8532Spatrick 		return EINVAL;
5664bba8532Spatrick 	}
5674bba8532Spatrick 
5684bba8532Spatrick 	sc->msi_cfg = msi_config;
5694bba8532Spatrick 	return 0;
5704bba8532Spatrick }
5714bba8532Spatrick 
5724bba8532Spatrick int
qwz_pci_alloc_msi(struct qwz_softc * sc)5734bba8532Spatrick qwz_pci_alloc_msi(struct qwz_softc *sc)
5744bba8532Spatrick {
5754bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
5764bba8532Spatrick 	uint64_t addr;
5774bba8532Spatrick 	pcireg_t data;
5784bba8532Spatrick 
5794bba8532Spatrick 	if (psc->sc_msi_cap & PCI_MSI_MC_C64) {
5804bba8532Spatrick 		uint64_t addr_hi;
5814bba8532Spatrick 		pcireg_t addr_lo;
5824bba8532Spatrick 
5834bba8532Spatrick 		addr_lo = pci_conf_read(psc->sc_pc, psc->sc_tag,
5844bba8532Spatrick 		    psc->sc_msi_off + PCI_MSI_MA);
5854bba8532Spatrick 		addr_hi = pci_conf_read(psc->sc_pc, psc->sc_tag,
5864bba8532Spatrick 		    psc->sc_msi_off + PCI_MSI_MAU32);
5874bba8532Spatrick 		addr = addr_hi << 32 | addr_lo;
5884bba8532Spatrick 		data = pci_conf_read(psc->sc_pc, psc->sc_tag,
5894bba8532Spatrick 		    psc->sc_msi_off + PCI_MSI_MD64);
5904bba8532Spatrick 	} else {
5914bba8532Spatrick 		addr = pci_conf_read(psc->sc_pc, psc->sc_tag,
5924bba8532Spatrick 		    psc->sc_msi_off + PCI_MSI_MA);
5934bba8532Spatrick 		data = pci_conf_read(psc->sc_pc, psc->sc_tag,
5944bba8532Spatrick 		    psc->sc_msi_off + PCI_MSI_MD32);
5954bba8532Spatrick 	}
5964bba8532Spatrick 
5974bba8532Spatrick 	sc->msi_addr_lo = addr & 0xffffffff;
5984bba8532Spatrick 	sc->msi_addr_hi = ((uint64_t)addr) >> 32;
5994bba8532Spatrick 	sc->msi_data_start = data;
6004bba8532Spatrick 
6014bba8532Spatrick 	DPRINTF("%s: MSI addr: 0x%llx MSI data: 0x%x\n", sc->sc_dev.dv_xname,
6024bba8532Spatrick 	    addr, data);
6034bba8532Spatrick 
6044bba8532Spatrick 	return 0;
6054bba8532Spatrick }
6064bba8532Spatrick 
6074bba8532Spatrick int
qwz_pcic_map_service_to_pipe(struct qwz_softc * sc,uint16_t service_id,uint8_t * ul_pipe,uint8_t * dl_pipe)6084bba8532Spatrick qwz_pcic_map_service_to_pipe(struct qwz_softc *sc, uint16_t service_id,
6094bba8532Spatrick     uint8_t *ul_pipe, uint8_t *dl_pipe)
6104bba8532Spatrick {
6114bba8532Spatrick 	const struct service_to_pipe *entry;
6124bba8532Spatrick 	int ul_set = 0, dl_set = 0;
6134bba8532Spatrick 	int i;
6144bba8532Spatrick 
6154bba8532Spatrick 	for (i = 0; i < sc->hw_params.svc_to_ce_map_len; i++) {
6164bba8532Spatrick 		entry = &sc->hw_params.svc_to_ce_map[i];
6174bba8532Spatrick 
6184bba8532Spatrick 		if (le32toh(entry->service_id) != service_id)
6194bba8532Spatrick 			continue;
6204bba8532Spatrick 
6214bba8532Spatrick 		switch (le32toh(entry->pipedir)) {
6224bba8532Spatrick 		case PIPEDIR_NONE:
6234bba8532Spatrick 			break;
6244bba8532Spatrick 		case PIPEDIR_IN:
6254bba8532Spatrick 			*dl_pipe = le32toh(entry->pipenum);
6264bba8532Spatrick 			dl_set = 1;
6274bba8532Spatrick 			break;
6284bba8532Spatrick 		case PIPEDIR_OUT:
6294bba8532Spatrick 			*ul_pipe = le32toh(entry->pipenum);
6304bba8532Spatrick 			ul_set = 1;
6314bba8532Spatrick 			break;
6324bba8532Spatrick 		case PIPEDIR_INOUT:
6334bba8532Spatrick 			*dl_pipe = le32toh(entry->pipenum);
6344bba8532Spatrick 			*ul_pipe = le32toh(entry->pipenum);
6354bba8532Spatrick 			dl_set = 1;
6364bba8532Spatrick 			ul_set = 1;
6374bba8532Spatrick 			break;
6384bba8532Spatrick 		}
6394bba8532Spatrick 	}
6404bba8532Spatrick 
6414bba8532Spatrick 	if (!ul_set || !dl_set) {
6424bba8532Spatrick 		DPRINTF("%s: found no uplink and no downlink\n", __func__);
6434bba8532Spatrick 		return ENOENT;
6444bba8532Spatrick 	}
6454bba8532Spatrick 
6464bba8532Spatrick 	return 0;
6474bba8532Spatrick }
6484bba8532Spatrick 
6494bba8532Spatrick int
qwz_pcic_get_user_msi_vector(struct qwz_softc * sc,char * user_name,int * num_vectors,uint32_t * user_base_data,uint32_t * base_vector)6504bba8532Spatrick qwz_pcic_get_user_msi_vector(struct qwz_softc *sc, char *user_name,
6514bba8532Spatrick     int *num_vectors, uint32_t *user_base_data, uint32_t *base_vector)
6524bba8532Spatrick {
6534bba8532Spatrick 	const struct qwz_msi_config *msi_config = sc->msi_cfg;
6544bba8532Spatrick 	int idx;
6554bba8532Spatrick 
6564bba8532Spatrick 	for (idx = 0; idx < msi_config->total_users; idx++) {
6574bba8532Spatrick 		if (strcmp(user_name, msi_config->users[idx].name) == 0) {
6584bba8532Spatrick 			*num_vectors = msi_config->users[idx].num_vectors;
6594bba8532Spatrick 			*base_vector =  msi_config->users[idx].base_vector;
6604bba8532Spatrick 			*user_base_data = *base_vector + sc->msi_data_start;
6614bba8532Spatrick 
6624bba8532Spatrick 			DPRINTF("%s: MSI assignment %s num_vectors %d "
6634bba8532Spatrick 			    "user_base_data %u base_vector %u\n", __func__,
6644bba8532Spatrick 			    user_name, *num_vectors, *user_base_data,
6654bba8532Spatrick 			    *base_vector);
6664bba8532Spatrick 			return 0;
6674bba8532Spatrick 		}
6684bba8532Spatrick 	}
6694bba8532Spatrick 
6704bba8532Spatrick 	DPRINTF("%s: Failed to find MSI assignment for %s\n",
6714bba8532Spatrick 	    sc->sc_dev.dv_xname, user_name);
6724bba8532Spatrick 
6734bba8532Spatrick 	return EINVAL;
6744bba8532Spatrick }
6754bba8532Spatrick 
6764bba8532Spatrick void
qwz_pci_attach(struct device * parent,struct device * self,void * aux)6774bba8532Spatrick qwz_pci_attach(struct device *parent, struct device *self, void *aux)
6784bba8532Spatrick {
6794bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)self;
6804bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
6814bba8532Spatrick 	struct ieee80211com *ic = &sc->sc_ic;
6824bba8532Spatrick 	struct ifnet *ifp = &ic->ic_if;
6834bba8532Spatrick 	uint32_t soc_hw_version_major, soc_hw_version_minor;
6844bba8532Spatrick 	struct pci_attach_args *pa = aux;
6854bba8532Spatrick 	pci_intr_handle_t ih;
6864bba8532Spatrick 	pcireg_t memtype, reg;
6874bba8532Spatrick 	const char *intrstr;
6884bba8532Spatrick 	int error;
6894bba8532Spatrick 	pcireg_t sreg;
6904bba8532Spatrick 
6914bba8532Spatrick 	sc->sc_dmat = pa->pa_dmat;
6924bba8532Spatrick 	psc->sc_pc = pa->pa_pc;
6934bba8532Spatrick 	psc->sc_tag = pa->pa_tag;
6944bba8532Spatrick 
6954bba8532Spatrick #ifdef __HAVE_FDT
6964bba8532Spatrick 	sc->sc_node = PCITAG_NODE(pa->pa_tag);
6974bba8532Spatrick #endif
6984bba8532Spatrick 
6994bba8532Spatrick 	rw_init(&sc->ioctl_rwl, "qwzioctl");
7004bba8532Spatrick 
7014bba8532Spatrick 	sreg = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_SUBSYS_ID_REG);
7024bba8532Spatrick 	sc->id.bdf_search = ATH12K_BDF_SEARCH_DEFAULT;
7034bba8532Spatrick 	sc->id.vendor = PCI_VENDOR(pa->pa_id);
7044bba8532Spatrick 	sc->id.device = PCI_PRODUCT(pa->pa_id);
7054bba8532Spatrick 	sc->id.subsystem_vendor = PCI_VENDOR(sreg);
7064bba8532Spatrick 	sc->id.subsystem_device = PCI_PRODUCT(sreg);
7074bba8532Spatrick 
7084bba8532Spatrick 	strlcpy(sc->sc_bus_str, "pci", sizeof(sc->sc_bus_str));
7094bba8532Spatrick 
7104bba8532Spatrick 	sc->ops.read32 = qwz_pcic_read32;
7114bba8532Spatrick 	sc->ops.write32 = qwz_pcic_write32;
7124bba8532Spatrick 	sc->ops.start = qwz_pci_start;
7134bba8532Spatrick 	sc->ops.stop = qwz_pci_stop;
7144bba8532Spatrick 	sc->ops.power_up = qwz_pci_power_up;
7154bba8532Spatrick 	sc->ops.power_down = qwz_pci_power_down;
7164bba8532Spatrick 	sc->ops.submit_xfer = qwz_mhi_submit_xfer;
7174bba8532Spatrick 	sc->ops.irq_enable = qwz_pcic_ext_irq_enable;
7184bba8532Spatrick 	sc->ops.irq_disable = qwz_pcic_ext_irq_disable;
7194bba8532Spatrick 	sc->ops.map_service_to_pipe = qwz_pcic_map_service_to_pipe;
7204bba8532Spatrick 	sc->ops.get_user_msi_vector = qwz_pcic_get_user_msi_vector;
7214bba8532Spatrick 
7224bba8532Spatrick 	if (pci_get_capability(psc->sc_pc, psc->sc_tag, PCI_CAP_PCIEXPRESS,
7234bba8532Spatrick 	    &psc->sc_cap_off, NULL) == 0) {
7244bba8532Spatrick 		printf(": can't find PCIe capability structure\n");
7254bba8532Spatrick 		return;
7264bba8532Spatrick 	}
7274bba8532Spatrick 
7284bba8532Spatrick 	if (pci_get_capability(psc->sc_pc, psc->sc_tag, PCI_CAP_MSI,
7294bba8532Spatrick 	    &psc->sc_msi_off, &psc->sc_msi_cap) == 0) {
7304bba8532Spatrick 		printf(": can't find MSI capability structure\n");
7314bba8532Spatrick 		return;
7324bba8532Spatrick 	}
7334bba8532Spatrick 
7344bba8532Spatrick 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
7354bba8532Spatrick 	reg |= PCI_COMMAND_MASTER_ENABLE;
7364bba8532Spatrick 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
7374bba8532Spatrick 
7384bba8532Spatrick 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
7394bba8532Spatrick 	if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0,
7404bba8532Spatrick 	    &psc->sc_st, &psc->sc_sh, &psc->sc_map, &psc->sc_mapsize, 0)) {
7414bba8532Spatrick 		printf(": can't map mem space\n");
7424bba8532Spatrick 		return;
7434bba8532Spatrick 	}
7444bba8532Spatrick 
7454bba8532Spatrick 	sc->mem = psc->sc_map;
7464bba8532Spatrick 
7474bba8532Spatrick 	sc->num_msivec = 32;
7484bba8532Spatrick 	if (pci_intr_enable_msivec(pa, sc->num_msivec) != 0) {
7494bba8532Spatrick 		sc->num_msivec = 1;
7504bba8532Spatrick 		if (pci_intr_map_msi(pa, &ih) != 0) {
7514bba8532Spatrick 			printf(": can't map interrupt\n");
7524bba8532Spatrick 			return;
7534bba8532Spatrick 		}
7544bba8532Spatrick 		clear_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags);
7554bba8532Spatrick 	} else {
7564bba8532Spatrick 		if (pci_intr_map_msivec(pa, 0, &ih) != 0 &&
7574bba8532Spatrick 		    pci_intr_map_msi(pa, &ih) != 0) {
7584bba8532Spatrick 			printf(": can't map interrupt\n");
7594bba8532Spatrick 			return;
7604bba8532Spatrick 		}
7614bba8532Spatrick 		set_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags);
7624bba8532Spatrick 		psc->mhi_irq[MHI_ER_CTRL] = 1;
7634bba8532Spatrick 		psc->mhi_irq[MHI_ER_DATA] = 2;
7644bba8532Spatrick 	}
7654bba8532Spatrick 
7664bba8532Spatrick 	intrstr = pci_intr_string(psc->sc_pc, ih);
7674bba8532Spatrick 	snprintf(psc->sc_ivname[0], sizeof(psc->sc_ivname[0]), "%s:bhi",
7684bba8532Spatrick 	    sc->sc_dev.dv_xname);
7694bba8532Spatrick 	psc->sc_ih[0] = pci_intr_establish(psc->sc_pc, ih, IPL_NET,
7704bba8532Spatrick 	    qwz_pci_intr, psc, psc->sc_ivname[0]);
7714bba8532Spatrick 	if (psc->sc_ih[0] == NULL) {
7724bba8532Spatrick 		printf(": can't establish interrupt");
7734bba8532Spatrick 		if (intrstr != NULL)
7744bba8532Spatrick 			printf(" at %s", intrstr);
7754bba8532Spatrick 		printf("\n");
7764bba8532Spatrick 		return;
7774bba8532Spatrick 	}
7784bba8532Spatrick 	printf(": %s\n", intrstr);
7794bba8532Spatrick 
7804bba8532Spatrick 	if (test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags)) {
7814bba8532Spatrick 		int msivec;
7824bba8532Spatrick 
7834bba8532Spatrick 		msivec = psc->mhi_irq[MHI_ER_CTRL];
7844bba8532Spatrick 		if (pci_intr_map_msivec(pa, msivec, &ih) != 0 &&
7854bba8532Spatrick 		    pci_intr_map_msi(pa, &ih) != 0) {
7864bba8532Spatrick 			printf(": can't map interrupt\n");
7874bba8532Spatrick 			return;
7884bba8532Spatrick 		}
7894bba8532Spatrick 		snprintf(psc->sc_ivname[msivec],
7904bba8532Spatrick 		    sizeof(psc->sc_ivname[msivec]),
7914bba8532Spatrick 		    "%s:mhic", sc->sc_dev.dv_xname);
7924bba8532Spatrick 		psc->sc_ih[msivec] = pci_intr_establish(psc->sc_pc, ih,
7934bba8532Spatrick 		    IPL_NET, qwz_pci_intr_mhi_ctrl, psc,
7944bba8532Spatrick 		    psc->sc_ivname[msivec]);
7954bba8532Spatrick 		if (psc->sc_ih[msivec] == NULL) {
7964bba8532Spatrick 			printf("%s: can't establish interrupt\n",
7974bba8532Spatrick 			    sc->sc_dev.dv_xname);
7984bba8532Spatrick 			return;
7994bba8532Spatrick 		}
8004bba8532Spatrick 
8014bba8532Spatrick 		msivec = psc->mhi_irq[MHI_ER_DATA];
8024bba8532Spatrick 		if (pci_intr_map_msivec(pa, msivec, &ih) != 0 &&
8034bba8532Spatrick 		    pci_intr_map_msi(pa, &ih) != 0) {
8044bba8532Spatrick 			printf(": can't map interrupt\n");
8054bba8532Spatrick 			return;
8064bba8532Spatrick 		}
8074bba8532Spatrick 		snprintf(psc->sc_ivname[msivec],
8084bba8532Spatrick 		    sizeof(psc->sc_ivname[msivec]),
8094bba8532Spatrick 		    "%s:mhid", sc->sc_dev.dv_xname);
8104bba8532Spatrick 		psc->sc_ih[msivec] = pci_intr_establish(psc->sc_pc, ih,
8114bba8532Spatrick 		    IPL_NET, qwz_pci_intr_mhi_data, psc,
8124bba8532Spatrick 		    psc->sc_ivname[msivec]);
8134bba8532Spatrick 		if (psc->sc_ih[msivec] == NULL) {
8144bba8532Spatrick 			printf("%s: can't establish interrupt\n",
8154bba8532Spatrick 			    sc->sc_dev.dv_xname);
8164bba8532Spatrick 			return;
8174bba8532Spatrick 		}
8184bba8532Spatrick 	}
8194bba8532Spatrick 
8204bba8532Spatrick 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
8214bba8532Spatrick 
8224bba8532Spatrick 	switch (PCI_PRODUCT(pa->pa_id)) {
82327c3d914Spatrick 	case PCI_PRODUCT_QUALCOMM_WCN7850:
82427c3d914Spatrick 		sc->static_window_map = 0;
82527c3d914Spatrick 		psc->sc_pci_ops = &qwz_pci_ops_wcn7850;
826*eb49c7f0Spatrick 		sc->hal_rx_ops = &hal_rx_wcn7850_ops;
8274bba8532Spatrick 		sc->id.bdf_search = ATH12K_BDF_SEARCH_BUS_AND_BOARD;
8284bba8532Spatrick 		qwz_pci_read_hw_version(sc, &soc_hw_version_major,
8294bba8532Spatrick 		    &soc_hw_version_minor);
8304bba8532Spatrick 		switch (soc_hw_version_major) {
8314bba8532Spatrick 		case 2:
83227c3d914Spatrick 			sc->sc_hw_rev = ATH12K_HW_WCN7850_HW20;
8334bba8532Spatrick 			break;
8344bba8532Spatrick 		default:
83509a673e5Spatrick 			printf(": unknown hardware version found for WCN7850: "
83627c3d914Spatrick 			    "%d\n", soc_hw_version_major);
8374bba8532Spatrick 			return;
8384bba8532Spatrick 		}
8394bba8532Spatrick 
84009a673e5Spatrick 		psc->max_chan = QWZ_MHI_CONFIG_WCN7850_MAX_CHANNELS;
8414bba8532Spatrick 		break;
8424bba8532Spatrick 	default:
8434bba8532Spatrick 		printf(": unsupported chip\n");
8444bba8532Spatrick 		return;
8454bba8532Spatrick 	}
8464bba8532Spatrick 
8474bba8532Spatrick 	error = qwz_pcic_init_msi_config(sc);
8484bba8532Spatrick 	if (error)
8494bba8532Spatrick 		goto err_pci_free_region;
8504bba8532Spatrick 
8514bba8532Spatrick 	error = qwz_pci_alloc_msi(sc);
8524bba8532Spatrick 	if (error) {
8534bba8532Spatrick 		printf("%s: failed to enable msi: %d\n", sc->sc_dev.dv_xname,
8544bba8532Spatrick 		    error);
8554bba8532Spatrick 		goto err_pci_free_region;
8564bba8532Spatrick 	}
8574bba8532Spatrick 
8584bba8532Spatrick 	error = qwz_init_hw_params(sc);
8594bba8532Spatrick 	if (error)
8604bba8532Spatrick 		goto err_pci_disable_msi;
8614bba8532Spatrick 
8624bba8532Spatrick 	psc->chan_ctxt = qwz_dmamem_alloc(sc->sc_dmat,
8634bba8532Spatrick 	    sizeof(struct qwz_mhi_chan_ctxt) * psc->max_chan, 0);
8644bba8532Spatrick 	if (psc->chan_ctxt == NULL) {
8654bba8532Spatrick 		printf("%s: could not allocate channel context array\n",
8664bba8532Spatrick 		    sc->sc_dev.dv_xname);
8674bba8532Spatrick 		goto err_pci_disable_msi;
8684bba8532Spatrick 	}
8694bba8532Spatrick 
8704bba8532Spatrick 	if (psc->sc_pci_ops->alloc_xfer_rings(psc)) {
8714bba8532Spatrick 		printf("%s: could not allocate transfer rings\n",
8724bba8532Spatrick 		    sc->sc_dev.dv_xname);
8734bba8532Spatrick 		goto err_pci_free_chan_ctxt;
8744bba8532Spatrick 	}
8754bba8532Spatrick 
8764bba8532Spatrick 	psc->event_ctxt = qwz_dmamem_alloc(sc->sc_dmat,
8774bba8532Spatrick 	    sizeof(struct qwz_mhi_event_ctxt) * QWZ_NUM_EVENT_CTX, 0);
8784bba8532Spatrick 	if (psc->event_ctxt == NULL) {
8794bba8532Spatrick 		printf("%s: could not allocate event context array\n",
8804bba8532Spatrick 		    sc->sc_dev.dv_xname);
8814bba8532Spatrick 		goto err_pci_free_xfer_rings;
8824bba8532Spatrick 	}
8834bba8532Spatrick 
8844bba8532Spatrick 	if (qwz_pci_alloc_event_rings(psc)) {
8854bba8532Spatrick 		printf("%s: could not allocate event rings\n",
8864bba8532Spatrick 		    sc->sc_dev.dv_xname);
8874bba8532Spatrick 		goto err_pci_free_event_ctxt;
8884bba8532Spatrick 	}
8894bba8532Spatrick 
8904bba8532Spatrick 	psc->cmd_ctxt = qwz_dmamem_alloc(sc->sc_dmat,
8914bba8532Spatrick 	    sizeof(struct qwz_mhi_cmd_ctxt), 0);
8924bba8532Spatrick 	if (psc->cmd_ctxt == NULL) {
8934bba8532Spatrick 		printf("%s: could not allocate command context array\n",
8944bba8532Spatrick 		    sc->sc_dev.dv_xname);
8954bba8532Spatrick 		goto err_pci_free_event_rings;
8964bba8532Spatrick 	}
8974bba8532Spatrick 
8984bba8532Spatrick 	if (qwz_pci_init_cmd_ring(sc, &psc->cmd_ring))  {
8994bba8532Spatrick 		printf("%s: could not allocate command ring\n",
9004bba8532Spatrick 		    sc->sc_dev.dv_xname);
9014bba8532Spatrick 		goto err_pci_free_cmd_ctxt;
9024bba8532Spatrick 	}
9034bba8532Spatrick 
9044bba8532Spatrick 	error = qwz_mhi_register(sc);
9054bba8532Spatrick 	if (error) {
9064bba8532Spatrick 		printf(": failed to register mhi: %d\n", error);
9074bba8532Spatrick 		goto err_pci_free_cmd_ring;
9084bba8532Spatrick 	}
9094bba8532Spatrick 
9104bba8532Spatrick 	error = qwz_hal_srng_init(sc);
9114bba8532Spatrick 	if (error)
9124bba8532Spatrick 		goto err_mhi_unregister;
9134bba8532Spatrick 
9144bba8532Spatrick 	error = qwz_ce_alloc_pipes(sc);
9154bba8532Spatrick 	if (error) {
9164bba8532Spatrick 		printf(": failed to allocate ce pipes: %d\n", error);
9174bba8532Spatrick 		goto err_hal_srng_deinit;
9184bba8532Spatrick 	}
9194bba8532Spatrick 
9204bba8532Spatrick 	sc->sc_nswq = taskq_create("qwzns", 1, IPL_NET, 0);
9214bba8532Spatrick 	if (sc->sc_nswq == NULL)
9224bba8532Spatrick 		goto err_ce_free;
9234bba8532Spatrick 
9244bba8532Spatrick 	error = qwz_pcic_config_irq(sc, pa);
9254bba8532Spatrick 	if (error) {
9264bba8532Spatrick 		printf("%s: failed to config irq: %d\n",
9274bba8532Spatrick 		    sc->sc_dev.dv_xname, error);
9284bba8532Spatrick 		goto err_ce_free;
9294bba8532Spatrick 	}
9304bba8532Spatrick #if notyet
9314bba8532Spatrick 	ret = ath12k_pci_set_irq_affinity_hint(ab_pci, cpumask_of(0));
9324bba8532Spatrick 	if (ret) {
9334bba8532Spatrick 		ath12k_err(ab, "failed to set irq affinity %d\n", ret);
9344bba8532Spatrick 		goto err_free_irq;
9354bba8532Spatrick 	}
9364bba8532Spatrick 
9374bba8532Spatrick 	/* kernel may allocate a dummy vector before request_irq and
9384bba8532Spatrick 	 * then allocate a real vector when request_irq is called.
9394bba8532Spatrick 	 * So get msi_data here again to avoid spurious interrupt
9404bba8532Spatrick 	 * as msi_data will configured to srngs.
9414bba8532Spatrick 	 */
9424bba8532Spatrick 	ret = ath12k_pci_config_msi_data(ab_pci);
9434bba8532Spatrick 	if (ret) {
9444bba8532Spatrick 		ath12k_err(ab, "failed to config msi_data: %d\n", ret);
9454bba8532Spatrick 		goto err_irq_affinity_cleanup;
9464bba8532Spatrick 	}
9474bba8532Spatrick #endif
9484bba8532Spatrick #ifdef QWZ_DEBUG
9494bba8532Spatrick 	task_set(&psc->rddm_task, qwz_rddm_task, psc);
9504bba8532Spatrick #endif
9514bba8532Spatrick 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
9524bba8532Spatrick 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
9534bba8532Spatrick 	ic->ic_state = IEEE80211_S_INIT;
9544bba8532Spatrick 
9554bba8532Spatrick 	/* Set device capabilities. */
9564bba8532Spatrick 	ic->ic_caps =
9574bba8532Spatrick #if 0
9584bba8532Spatrick 	    IEEE80211_C_QOS | IEEE80211_C_TX_AMPDU | /* A-MPDU */
9594bba8532Spatrick #endif
9604bba8532Spatrick 	    IEEE80211_C_ADDBA_OFFLOAD | /* device sends ADDBA/DELBA frames */
9614bba8532Spatrick 	    IEEE80211_C_WEP |		/* WEP */
9624bba8532Spatrick 	    IEEE80211_C_RSN |		/* WPA/RSN */
9634bba8532Spatrick 	    IEEE80211_C_SCANALL |	/* device scans all channels at once */
9644bba8532Spatrick 	    IEEE80211_C_SCANALLBAND |	/* device scans all bands at once */
9654bba8532Spatrick #if 0
9664bba8532Spatrick 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
9674bba8532Spatrick #endif
9684bba8532Spatrick 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
9694bba8532Spatrick 	    IEEE80211_C_SHPREAMBLE;	/* short preamble supported */
9704bba8532Spatrick 
9714bba8532Spatrick 	ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_std_rateset_11a;
9724bba8532Spatrick 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
9734bba8532Spatrick 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
9744bba8532Spatrick 
9754bba8532Spatrick 	/* IBSS channel undefined for now. */
9764bba8532Spatrick 	ic->ic_ibss_chan = &ic->ic_channels[1];
9774bba8532Spatrick 
9784bba8532Spatrick 	ifp->if_softc = sc;
9794bba8532Spatrick 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
9804bba8532Spatrick 	ifp->if_ioctl = qwz_ioctl;
9814bba8532Spatrick 	ifp->if_start = qwz_start;
9824bba8532Spatrick 	ifp->if_watchdog = qwz_watchdog;
9834bba8532Spatrick 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
9844bba8532Spatrick 	if_attach(ifp);
9854bba8532Spatrick 	ieee80211_ifattach(ifp);
9864bba8532Spatrick 	ieee80211_media_init(ifp, qwz_media_change, ieee80211_media_status);
9874bba8532Spatrick 
9884bba8532Spatrick 	ic->ic_node_alloc = qwz_node_alloc;
9894bba8532Spatrick 
9904bba8532Spatrick 	/* Override 802.11 state transition machine. */
9914bba8532Spatrick 	sc->sc_newstate = ic->ic_newstate;
9924bba8532Spatrick 	ic->ic_newstate = qwz_newstate;
9934bba8532Spatrick 	ic->ic_set_key = qwz_set_key;
9944bba8532Spatrick 	ic->ic_delete_key = qwz_delete_key;
9954bba8532Spatrick #if 0
9964bba8532Spatrick 	ic->ic_updatechan = qwz_updatechan;
9974bba8532Spatrick 	ic->ic_updateprot = qwz_updateprot;
9984bba8532Spatrick 	ic->ic_updateslot = qwz_updateslot;
9994bba8532Spatrick 	ic->ic_updateedca = qwz_updateedca;
10004bba8532Spatrick 	ic->ic_updatedtim = qwz_updatedtim;
10014bba8532Spatrick #endif
10024bba8532Spatrick 	/*
10034bba8532Spatrick 	 * We cannot read the MAC address without loading the
10044bba8532Spatrick 	 * firmware from disk. Postpone until mountroot is done.
10054bba8532Spatrick 	 */
10064bba8532Spatrick 	config_mountroot(self, qwz_pci_attach_hook);
10074bba8532Spatrick 	return;
10084bba8532Spatrick 
10094bba8532Spatrick err_ce_free:
10104bba8532Spatrick 	qwz_ce_free_pipes(sc);
10114bba8532Spatrick err_hal_srng_deinit:
10124bba8532Spatrick err_mhi_unregister:
10134bba8532Spatrick err_pci_free_cmd_ring:
10144bba8532Spatrick 	qwz_pci_free_cmd_ring(psc);
10154bba8532Spatrick err_pci_free_cmd_ctxt:
10164bba8532Spatrick 	qwz_dmamem_free(sc->sc_dmat, psc->cmd_ctxt);
10174bba8532Spatrick 	psc->cmd_ctxt = NULL;
10184bba8532Spatrick err_pci_free_event_rings:
10194bba8532Spatrick 	qwz_pci_free_event_rings(psc);
10204bba8532Spatrick err_pci_free_event_ctxt:
10214bba8532Spatrick 	qwz_dmamem_free(sc->sc_dmat, psc->event_ctxt);
10224bba8532Spatrick 	psc->event_ctxt = NULL;
10234bba8532Spatrick err_pci_free_xfer_rings:
10244bba8532Spatrick 	qwz_pci_free_xfer_rings(psc);
10254bba8532Spatrick err_pci_free_chan_ctxt:
10264bba8532Spatrick 	qwz_dmamem_free(sc->sc_dmat, psc->chan_ctxt);
10274bba8532Spatrick 	psc->chan_ctxt = NULL;
10284bba8532Spatrick err_pci_disable_msi:
10294bba8532Spatrick err_pci_free_region:
10304bba8532Spatrick 	pci_intr_disestablish(psc->sc_pc, psc->sc_ih[0]);
10314bba8532Spatrick 	return;
10324bba8532Spatrick }
10334bba8532Spatrick 
10344bba8532Spatrick int
qwz_pci_detach(struct device * self,int flags)10354bba8532Spatrick qwz_pci_detach(struct device *self, int flags)
10364bba8532Spatrick {
10374bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)self;
10384bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
10394bba8532Spatrick 
10404bba8532Spatrick 	if (psc->sc_ih[0]) {
10414bba8532Spatrick 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih[0]);
10424bba8532Spatrick 		psc->sc_ih[0] = NULL;
10434bba8532Spatrick 	}
10444bba8532Spatrick 
10454bba8532Spatrick 	qwz_detach(sc);
10464bba8532Spatrick 
10474bba8532Spatrick 	qwz_pci_free_event_rings(psc);
10484bba8532Spatrick 	qwz_pci_free_xfer_rings(psc);
10494bba8532Spatrick 	qwz_pci_free_cmd_ring(psc);
10504bba8532Spatrick 
10514bba8532Spatrick 	if (psc->event_ctxt) {
10524bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, psc->event_ctxt);
10534bba8532Spatrick 		psc->event_ctxt = NULL;
10544bba8532Spatrick 	}
10554bba8532Spatrick 	if (psc->chan_ctxt) {
10564bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, psc->chan_ctxt);
10574bba8532Spatrick 		psc->chan_ctxt = NULL;
10584bba8532Spatrick 	}
10594bba8532Spatrick 	if (psc->cmd_ctxt) {
10604bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, psc->cmd_ctxt);
10614bba8532Spatrick 		psc->cmd_ctxt = NULL;
10624bba8532Spatrick 	}
10634bba8532Spatrick 
10644bba8532Spatrick 	if (psc->amss_data) {
10654bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, psc->amss_data);
10664bba8532Spatrick 		psc->amss_data = NULL;
10674bba8532Spatrick 	}
10684bba8532Spatrick 	if (psc->amss_vec) {
10694bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, psc->amss_vec);
10704bba8532Spatrick 		psc->amss_vec = NULL;
10714bba8532Spatrick 	}
10724bba8532Spatrick 
10734bba8532Spatrick 	return 0;
10744bba8532Spatrick }
10754bba8532Spatrick 
10764bba8532Spatrick void
qwz_pci_attach_hook(struct device * self)10774bba8532Spatrick qwz_pci_attach_hook(struct device *self)
10784bba8532Spatrick {
10794bba8532Spatrick 	struct qwz_softc *sc = (void *)self;
10804bba8532Spatrick 	int s = splnet();
10814bba8532Spatrick 
10824bba8532Spatrick 	qwz_attach(sc);
10834bba8532Spatrick 
10844bba8532Spatrick 	splx(s);
10854bba8532Spatrick }
10864bba8532Spatrick 
10874bba8532Spatrick void
qwz_pci_free_xfer_rings(struct qwz_pci_softc * psc)10884bba8532Spatrick qwz_pci_free_xfer_rings(struct qwz_pci_softc *psc)
10894bba8532Spatrick {
10904bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
10914bba8532Spatrick 	int i;
10924bba8532Spatrick 
10934bba8532Spatrick 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
10944bba8532Spatrick 		struct qwz_pci_xfer_ring *ring = &psc->xfer_rings[i];
10954bba8532Spatrick 		if (ring->dmamem) {
10964bba8532Spatrick 			qwz_dmamem_free(sc->sc_dmat, ring->dmamem);
10974bba8532Spatrick 			ring->dmamem = NULL;
10984bba8532Spatrick 		}
10994bba8532Spatrick 		memset(ring, 0, sizeof(*ring));
11004bba8532Spatrick 	}
11014bba8532Spatrick }
11024bba8532Spatrick 
11034bba8532Spatrick int
qwz_pci_alloc_xfer_ring(struct qwz_softc * sc,struct qwz_pci_xfer_ring * ring,uint32_t id,uint32_t direction,uint32_t event_ring_index,size_t num_elements)11044bba8532Spatrick qwz_pci_alloc_xfer_ring(struct qwz_softc *sc, struct qwz_pci_xfer_ring *ring,
11054bba8532Spatrick     uint32_t id, uint32_t direction, uint32_t event_ring_index,
11064bba8532Spatrick     size_t num_elements)
11074bba8532Spatrick {
11084bba8532Spatrick 	bus_size_t size;
11094bba8532Spatrick 	int i, err;
11104bba8532Spatrick 
11114bba8532Spatrick 	memset(ring, 0, sizeof(*ring));
11124bba8532Spatrick 
11134bba8532Spatrick 	size = sizeof(struct qwz_mhi_ring_element) * num_elements;
11144bba8532Spatrick 	/* Hardware requires that rings are aligned to ring size. */
11154bba8532Spatrick 	ring->dmamem = qwz_dmamem_alloc(sc->sc_dmat, size, size);
11164bba8532Spatrick 	if (ring->dmamem == NULL)
11174bba8532Spatrick 		return ENOMEM;
11184bba8532Spatrick 
11194bba8532Spatrick 	ring->size = size;
11204bba8532Spatrick 	ring->mhi_chan_id = id;
11214bba8532Spatrick 	ring->mhi_chan_state = MHI_CH_STATE_DISABLED;
11224bba8532Spatrick 	ring->mhi_chan_direction = direction;
11234bba8532Spatrick 	ring->mhi_chan_event_ring_index = event_ring_index;
11244bba8532Spatrick 	ring->num_elements = num_elements;
11254bba8532Spatrick 
11264bba8532Spatrick 	memset(ring->data, 0, sizeof(ring->data));
11274bba8532Spatrick 	for (i = 0; i < ring->num_elements; i++) {
11284bba8532Spatrick 		struct qwz_xfer_data *xfer = &ring->data[i];
11294bba8532Spatrick 
11304bba8532Spatrick 		err = bus_dmamap_create(sc->sc_dmat, QWZ_PCI_XFER_MAX_DATA_SIZE,
11314bba8532Spatrick 		    1, QWZ_PCI_XFER_MAX_DATA_SIZE, 0, BUS_DMA_NOWAIT,
11324bba8532Spatrick 		    &xfer->map);
11334bba8532Spatrick 		if (err) {
11344bba8532Spatrick 			printf("%s: could not create xfer DMA map\n",
11354bba8532Spatrick 			    sc->sc_dev.dv_xname);
11364bba8532Spatrick 			goto fail;
11374bba8532Spatrick 		}
11384bba8532Spatrick 
11394bba8532Spatrick 		if (direction == MHI_CHAN_TYPE_INBOUND) {
11404bba8532Spatrick 			struct mbuf *m;
11414bba8532Spatrick 
11424bba8532Spatrick 			m = m_gethdr(M_DONTWAIT, MT_DATA);
11434bba8532Spatrick 			if (m == NULL) {
11444bba8532Spatrick 				err = ENOBUFS;
11454bba8532Spatrick 				goto fail;
11464bba8532Spatrick 			}
11474bba8532Spatrick 
11484bba8532Spatrick 			MCLGETL(m, M_DONTWAIT, QWZ_PCI_XFER_MAX_DATA_SIZE);
11494bba8532Spatrick 			if ((m->m_flags & M_EXT) == 0) {
11504bba8532Spatrick 				m_freem(m);
11514bba8532Spatrick 				err = ENOBUFS;
11524bba8532Spatrick 				goto fail;
11534bba8532Spatrick 			}
11544bba8532Spatrick 
11554bba8532Spatrick 			m->m_len = m->m_pkthdr.len = QWZ_PCI_XFER_MAX_DATA_SIZE;
11564bba8532Spatrick 			err = bus_dmamap_load_mbuf(sc->sc_dmat, xfer->map,
11574bba8532Spatrick 			    m, BUS_DMA_READ | BUS_DMA_NOWAIT);
11584bba8532Spatrick 			if (err) {
11594bba8532Spatrick 				printf("%s: can't map mbuf (error %d)\n",
11604bba8532Spatrick 				    sc->sc_dev.dv_xname, err);
11614bba8532Spatrick 				m_freem(m);
11624bba8532Spatrick 				goto fail;
11634bba8532Spatrick 			}
11644bba8532Spatrick 
11654bba8532Spatrick 			bus_dmamap_sync(sc->sc_dmat, xfer->map, 0,
11664bba8532Spatrick 			    QWZ_PCI_XFER_MAX_DATA_SIZE, BUS_DMASYNC_PREREAD);
11674bba8532Spatrick 			xfer->m = m;
11684bba8532Spatrick 		}
11694bba8532Spatrick 	}
11704bba8532Spatrick 
11714bba8532Spatrick 	return 0;
11724bba8532Spatrick fail:
11734bba8532Spatrick 	for (i = 0; i < ring->num_elements; i++) {
11744bba8532Spatrick 		struct qwz_xfer_data *xfer = &ring->data[i];
11754bba8532Spatrick 
11764bba8532Spatrick 		if (xfer->map) {
11774bba8532Spatrick 			bus_dmamap_sync(sc->sc_dmat, xfer->map, 0,
11784bba8532Spatrick 			    xfer->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
11794bba8532Spatrick 			bus_dmamap_unload(sc->sc_dmat, xfer->map);
11804bba8532Spatrick 			bus_dmamap_destroy(sc->sc_dmat, xfer->map);
11814bba8532Spatrick 			xfer->map = NULL;
11824bba8532Spatrick 		}
11834bba8532Spatrick 
11844bba8532Spatrick 		if (xfer->m) {
11854bba8532Spatrick 			m_freem(xfer->m);
11864bba8532Spatrick 			xfer->m = NULL;
11874bba8532Spatrick 		}
11884bba8532Spatrick 	}
11894bba8532Spatrick 	return 1;
11904bba8532Spatrick }
11914bba8532Spatrick 
11924bba8532Spatrick int
qwz_pci_alloc_xfer_rings_wcn7850(struct qwz_pci_softc * psc)119309a673e5Spatrick qwz_pci_alloc_xfer_rings_wcn7850(struct qwz_pci_softc *psc)
11944bba8532Spatrick {
11954bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
11964bba8532Spatrick 	int ret;
11974bba8532Spatrick 
11984bba8532Spatrick 	ret = qwz_pci_alloc_xfer_ring(sc,
11994bba8532Spatrick 	    &psc->xfer_rings[QWZ_PCI_XFER_RING_IPCR_OUTBOUND],
12004bba8532Spatrick 	    20, MHI_CHAN_TYPE_OUTBOUND, 1, 64);
12014bba8532Spatrick 	if (ret)
12024bba8532Spatrick 		goto fail;
12034bba8532Spatrick 
12044bba8532Spatrick 	ret = qwz_pci_alloc_xfer_ring(sc,
12054bba8532Spatrick 	    &psc->xfer_rings[QWZ_PCI_XFER_RING_IPCR_INBOUND],
12064bba8532Spatrick 	    21, MHI_CHAN_TYPE_INBOUND, 1, 64);
12074bba8532Spatrick 	if (ret)
12084bba8532Spatrick 		goto fail;
12094bba8532Spatrick 
12104bba8532Spatrick 	return 0;
12114bba8532Spatrick fail:
12124bba8532Spatrick 	qwz_pci_free_xfer_rings(psc);
12134bba8532Spatrick 	return ret;
12144bba8532Spatrick }
12154bba8532Spatrick 
12164bba8532Spatrick void
qwz_pci_free_event_rings(struct qwz_pci_softc * psc)12174bba8532Spatrick qwz_pci_free_event_rings(struct qwz_pci_softc *psc)
12184bba8532Spatrick {
12194bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
12204bba8532Spatrick 	int i;
12214bba8532Spatrick 
12224bba8532Spatrick 	for (i = 0; i < nitems(psc->event_rings); i++) {
12234bba8532Spatrick 		struct qwz_pci_event_ring *ring = &psc->event_rings[i];
12244bba8532Spatrick 		if (ring->dmamem) {
12254bba8532Spatrick 			qwz_dmamem_free(sc->sc_dmat, ring->dmamem);
12264bba8532Spatrick 			ring->dmamem = NULL;
12274bba8532Spatrick 		}
12284bba8532Spatrick 		memset(ring, 0, sizeof(*ring));
12294bba8532Spatrick 	}
12304bba8532Spatrick }
12314bba8532Spatrick 
12324bba8532Spatrick int
qwz_pci_alloc_event_ring(struct qwz_softc * sc,struct qwz_pci_event_ring * ring,uint32_t type,uint32_t irq,uint32_t intmod,size_t num_elements)12334bba8532Spatrick qwz_pci_alloc_event_ring(struct qwz_softc *sc, struct qwz_pci_event_ring *ring,
12344bba8532Spatrick     uint32_t type, uint32_t irq, uint32_t intmod, size_t num_elements)
12354bba8532Spatrick {
12364bba8532Spatrick 	bus_size_t size;
12374bba8532Spatrick 
12384bba8532Spatrick 	memset(ring, 0, sizeof(*ring));
12394bba8532Spatrick 
12404bba8532Spatrick 	size = sizeof(struct qwz_mhi_ring_element) * num_elements;
12414bba8532Spatrick 	/* Hardware requires that rings are aligned to ring size. */
12424bba8532Spatrick 	ring->dmamem = qwz_dmamem_alloc(sc->sc_dmat, size, size);
12434bba8532Spatrick 	if (ring->dmamem == NULL)
12444bba8532Spatrick 		return ENOMEM;
12454bba8532Spatrick 
12464bba8532Spatrick 	ring->size = size;
12474bba8532Spatrick 	ring->mhi_er_type = type;
12484bba8532Spatrick 	ring->mhi_er_irq = irq;
12494bba8532Spatrick 	ring->mhi_er_irq_moderation_ms = intmod;
12504bba8532Spatrick 	ring->num_elements = num_elements;
12514bba8532Spatrick 	return 0;
12524bba8532Spatrick }
12534bba8532Spatrick 
12544bba8532Spatrick int
qwz_pci_alloc_event_rings(struct qwz_pci_softc * psc)12554bba8532Spatrick qwz_pci_alloc_event_rings(struct qwz_pci_softc *psc)
12564bba8532Spatrick {
12574bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
12584bba8532Spatrick 	int ret;
12594bba8532Spatrick 
12604bba8532Spatrick 	ret = qwz_pci_alloc_event_ring(sc, &psc->event_rings[0],
12614bba8532Spatrick 	    MHI_ER_CTRL, psc->mhi_irq[MHI_ER_CTRL], 0, 32);
12624bba8532Spatrick 	if (ret)
12634bba8532Spatrick 		goto fail;
12644bba8532Spatrick 
12654bba8532Spatrick 	ret = qwz_pci_alloc_event_ring(sc, &psc->event_rings[1],
12664bba8532Spatrick 	    MHI_ER_DATA, psc->mhi_irq[MHI_ER_DATA], 1, 256);
12674bba8532Spatrick 	if (ret)
12684bba8532Spatrick 		goto fail;
12694bba8532Spatrick 
12704bba8532Spatrick 	return 0;
12714bba8532Spatrick fail:
12724bba8532Spatrick 	qwz_pci_free_event_rings(psc);
12734bba8532Spatrick 	return ret;
12744bba8532Spatrick }
12754bba8532Spatrick 
12764bba8532Spatrick void
qwz_pci_free_cmd_ring(struct qwz_pci_softc * psc)12774bba8532Spatrick qwz_pci_free_cmd_ring(struct qwz_pci_softc *psc)
12784bba8532Spatrick {
12794bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
12804bba8532Spatrick 	struct qwz_pci_cmd_ring *ring = &psc->cmd_ring;
12814bba8532Spatrick 
12824bba8532Spatrick 	if (ring->dmamem)
12834bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, ring->dmamem);
12844bba8532Spatrick 
12854bba8532Spatrick 	memset(ring, 0, sizeof(*ring));
12864bba8532Spatrick }
12874bba8532Spatrick 
12884bba8532Spatrick int
qwz_pci_init_cmd_ring(struct qwz_softc * sc,struct qwz_pci_cmd_ring * ring)12894bba8532Spatrick qwz_pci_init_cmd_ring(struct qwz_softc *sc, struct qwz_pci_cmd_ring *ring)
12904bba8532Spatrick {
12914bba8532Spatrick 	memset(ring, 0, sizeof(*ring));
12924bba8532Spatrick 
12934bba8532Spatrick 	ring->num_elements = QWZ_PCI_CMD_RING_MAX_ELEMENTS;
12944bba8532Spatrick 	ring->size = sizeof(struct qwz_mhi_ring_element) * ring->num_elements;
12954bba8532Spatrick 
12964bba8532Spatrick 	/* Hardware requires that rings are aligned to ring size. */
12974bba8532Spatrick 	ring->dmamem = qwz_dmamem_alloc(sc->sc_dmat, ring->size, ring->size);
12984bba8532Spatrick 	if (ring->dmamem == NULL)
12994bba8532Spatrick 		return ENOMEM;
13004bba8532Spatrick 
13014bba8532Spatrick 	return 0;
13024bba8532Spatrick }
13034bba8532Spatrick 
13044bba8532Spatrick uint32_t
qwz_pci_read(struct qwz_softc * sc,uint32_t addr)13054bba8532Spatrick qwz_pci_read(struct qwz_softc *sc, uint32_t addr)
13064bba8532Spatrick {
13074bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
13084bba8532Spatrick 
13094bba8532Spatrick 	return (bus_space_read_4(psc->sc_st, psc->sc_sh, addr));
13104bba8532Spatrick }
13114bba8532Spatrick 
13124bba8532Spatrick void
qwz_pci_write(struct qwz_softc * sc,uint32_t addr,uint32_t val)13134bba8532Spatrick qwz_pci_write(struct qwz_softc *sc, uint32_t addr, uint32_t val)
13144bba8532Spatrick {
13154bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
13164bba8532Spatrick 
13174bba8532Spatrick 	bus_space_write_4(psc->sc_st, psc->sc_sh, addr, val);
13184bba8532Spatrick }
13194bba8532Spatrick 
13204bba8532Spatrick void
qwz_pci_read_hw_version(struct qwz_softc * sc,uint32_t * major,uint32_t * minor)13214bba8532Spatrick qwz_pci_read_hw_version(struct qwz_softc *sc, uint32_t *major,
13224bba8532Spatrick     uint32_t *minor)
13234bba8532Spatrick {
13244bba8532Spatrick 	uint32_t soc_hw_version;
13254bba8532Spatrick 
13264bba8532Spatrick 	soc_hw_version = qwz_pcic_read32(sc, TCSR_SOC_HW_VERSION);
13274bba8532Spatrick 	*major = FIELD_GET(TCSR_SOC_HW_VERSION_MAJOR_MASK, soc_hw_version);
13284bba8532Spatrick 	*minor = FIELD_GET(TCSR_SOC_HW_VERSION_MINOR_MASK, soc_hw_version);
13294bba8532Spatrick 	DPRINTF("%s: pci tcsr_soc_hw_version major %d minor %d\n",
13304bba8532Spatrick 	    sc->sc_dev.dv_xname, *major, *minor);
13314bba8532Spatrick }
13324bba8532Spatrick 
13334bba8532Spatrick uint32_t
qwz_pcic_read32(struct qwz_softc * sc,uint32_t offset)13344bba8532Spatrick qwz_pcic_read32(struct qwz_softc *sc, uint32_t offset)
13354bba8532Spatrick {
13364bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
13374bba8532Spatrick 	int ret = 0;
13384bba8532Spatrick 	uint32_t val;
13394bba8532Spatrick 	bool wakeup_required;
13404bba8532Spatrick 
13414bba8532Spatrick 	/* for offset beyond BAR + 4K - 32, may
13424bba8532Spatrick 	 * need to wakeup the device to access.
13434bba8532Spatrick 	 */
13444bba8532Spatrick 	wakeup_required = test_bit(ATH12K_FLAG_DEVICE_INIT_DONE, sc->sc_flags)
13454bba8532Spatrick 	    && offset >= ATH12K_PCI_ACCESS_ALWAYS_OFF;
13464bba8532Spatrick 	if (wakeup_required && psc->sc_pci_ops->wakeup)
13474bba8532Spatrick 		ret = psc->sc_pci_ops->wakeup(sc);
13484bba8532Spatrick 
13494bba8532Spatrick 	if (offset < ATH12K_PCI_WINDOW_START)
13504bba8532Spatrick 		val = qwz_pci_read(sc, offset);
13514bba8532Spatrick 	else
13524bba8532Spatrick 		val = psc->sc_pci_ops->window_read32(sc, offset);
13534bba8532Spatrick 
13544bba8532Spatrick 	if (wakeup_required && !ret && psc->sc_pci_ops->release)
13554bba8532Spatrick 		psc->sc_pci_ops->release(sc);
13564bba8532Spatrick 
13574bba8532Spatrick 	return val;
13584bba8532Spatrick }
13594bba8532Spatrick 
13604bba8532Spatrick void
qwz_pcic_write32(struct qwz_softc * sc,uint32_t offset,uint32_t value)13614bba8532Spatrick qwz_pcic_write32(struct qwz_softc *sc, uint32_t offset, uint32_t value)
13624bba8532Spatrick {
13634bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
13644bba8532Spatrick 	int ret = 0;
13654bba8532Spatrick 	bool wakeup_required;
13664bba8532Spatrick 
13674bba8532Spatrick 	/* for offset beyond BAR + 4K - 32, may
13684bba8532Spatrick 	 * need to wakeup the device to access.
13694bba8532Spatrick 	 */
13704bba8532Spatrick 	wakeup_required = test_bit(ATH12K_FLAG_DEVICE_INIT_DONE, sc->sc_flags)
13714bba8532Spatrick 	    && offset >= ATH12K_PCI_ACCESS_ALWAYS_OFF;
13724bba8532Spatrick 	if (wakeup_required && psc->sc_pci_ops->wakeup)
13734bba8532Spatrick 		ret = psc->sc_pci_ops->wakeup(sc);
13744bba8532Spatrick 
13754bba8532Spatrick 	if (offset < ATH12K_PCI_WINDOW_START)
13764bba8532Spatrick 		qwz_pci_write(sc, offset, value);
13774bba8532Spatrick 	else
13784bba8532Spatrick 		psc->sc_pci_ops->window_write32(sc, offset, value);
13794bba8532Spatrick 
13804bba8532Spatrick 	if (wakeup_required && !ret && psc->sc_pci_ops->release)
13814bba8532Spatrick 		psc->sc_pci_ops->release(sc);
13824bba8532Spatrick }
13834bba8532Spatrick 
13844bba8532Spatrick void
qwz_pcic_ext_irq_disable(struct qwz_softc * sc)13854bba8532Spatrick qwz_pcic_ext_irq_disable(struct qwz_softc *sc)
13864bba8532Spatrick {
13874bba8532Spatrick 	clear_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, sc->sc_flags);
13884bba8532Spatrick 
13894bba8532Spatrick 	/* In case of one MSI vector, we handle irq enable/disable in a
13904bba8532Spatrick 	 * uniform way since we only have one irq
13914bba8532Spatrick 	 */
13924bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
13934bba8532Spatrick 		return;
13944bba8532Spatrick 
13954bba8532Spatrick 	DPRINTF("%s not implemented\n", __func__);
13964bba8532Spatrick }
13974bba8532Spatrick 
13984bba8532Spatrick void
qwz_pcic_ext_irq_enable(struct qwz_softc * sc)13994bba8532Spatrick qwz_pcic_ext_irq_enable(struct qwz_softc *sc)
14004bba8532Spatrick {
14014bba8532Spatrick 	set_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, sc->sc_flags);
14024bba8532Spatrick 
14034bba8532Spatrick 	/* In case of one MSI vector, we handle irq enable/disable in a
14044bba8532Spatrick 	 * uniform way since we only have one irq
14054bba8532Spatrick 	 */
14064bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
14074bba8532Spatrick 		return;
14084bba8532Spatrick 
14094bba8532Spatrick 	DPRINTF("%s not implemented\n", __func__);
14104bba8532Spatrick }
14114bba8532Spatrick 
14124bba8532Spatrick void
qwz_pcic_ce_irq_enable(struct qwz_softc * sc,uint16_t ce_id)14134bba8532Spatrick qwz_pcic_ce_irq_enable(struct qwz_softc *sc, uint16_t ce_id)
14144bba8532Spatrick {
14154bba8532Spatrick 	/* In case of one MSI vector, we handle irq enable/disable in a
14164bba8532Spatrick 	 * uniform way since we only have one irq
14174bba8532Spatrick 	 */
14184bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
14194bba8532Spatrick 		return;
14204bba8532Spatrick 
14214bba8532Spatrick 	/* OpenBSD PCI stack does not yet implement MSI interrupt masking. */
14224bba8532Spatrick 	sc->msi_ce_irqmask |= (1U << ce_id);
14234bba8532Spatrick }
14244bba8532Spatrick 
14254bba8532Spatrick void
qwz_pcic_ce_irq_disable(struct qwz_softc * sc,uint16_t ce_id)14264bba8532Spatrick qwz_pcic_ce_irq_disable(struct qwz_softc *sc, uint16_t ce_id)
14274bba8532Spatrick {
14284bba8532Spatrick 	/* In case of one MSI vector, we handle irq enable/disable in a
14294bba8532Spatrick 	 * uniform way since we only have one irq
14304bba8532Spatrick 	 */
14314bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
14324bba8532Spatrick 		return;
14334bba8532Spatrick 
14344bba8532Spatrick 	/* OpenBSD PCI stack does not yet implement MSI interrupt masking. */
14354bba8532Spatrick 	sc->msi_ce_irqmask &= ~(1U << ce_id);
14364bba8532Spatrick }
14374bba8532Spatrick 
14384bba8532Spatrick void
qwz_pcic_ext_grp_disable(struct qwz_ext_irq_grp * irq_grp)14394bba8532Spatrick qwz_pcic_ext_grp_disable(struct qwz_ext_irq_grp *irq_grp)
14404bba8532Spatrick {
14414bba8532Spatrick 	struct qwz_softc *sc = irq_grp->sc;
14424bba8532Spatrick 
14434bba8532Spatrick 	/* In case of one MSI vector, we handle irq enable/disable
14444bba8532Spatrick 	 * in a uniform way since we only have one irq
14454bba8532Spatrick 	 */
14464bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
14474bba8532Spatrick 		return;
14484bba8532Spatrick }
14494bba8532Spatrick 
14504bba8532Spatrick int
qwz_pcic_ext_irq_config(struct qwz_softc * sc,struct pci_attach_args * pa)14514bba8532Spatrick qwz_pcic_ext_irq_config(struct qwz_softc *sc, struct pci_attach_args *pa)
14524bba8532Spatrick {
14534bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
14544bba8532Spatrick 	int i, ret, num_vectors = 0;
14554bba8532Spatrick 	uint32_t msi_data_start = 0;
145627c3d914Spatrick 	uint32_t base_idx, base_vector = 0;
14574bba8532Spatrick 
14584bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
14594bba8532Spatrick 		return 0;
14604bba8532Spatrick 
146127c3d914Spatrick 	base_idx = ATH12K_PCI_IRQ_CE0_OFFSET + CE_COUNT_MAX;
146227c3d914Spatrick 
14634bba8532Spatrick 	ret = qwz_pcic_get_user_msi_vector(sc, "DP", &num_vectors,
14644bba8532Spatrick 	    &msi_data_start, &base_vector);
14654bba8532Spatrick 	if (ret < 0)
14664bba8532Spatrick 		return ret;
14674bba8532Spatrick 
14684bba8532Spatrick 	for (i = 0; i < nitems(sc->ext_irq_grp); i++) {
14694bba8532Spatrick 		struct qwz_ext_irq_grp *irq_grp = &sc->ext_irq_grp[i];
14704bba8532Spatrick 		uint32_t num_irq = 0;
14714bba8532Spatrick 
14724bba8532Spatrick 		irq_grp->sc = sc;
14734bba8532Spatrick 		irq_grp->grp_id = i;
14744bba8532Spatrick #if 0
14754bba8532Spatrick 		init_dummy_netdev(&irq_grp->napi_ndev);
14764bba8532Spatrick 		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
14774bba8532Spatrick 			       ath12k_pcic_ext_grp_napi_poll);
14784bba8532Spatrick #endif
14794bba8532Spatrick 		if (sc->hw_params.ring_mask->tx[i] ||
14804bba8532Spatrick 		    sc->hw_params.ring_mask->rx[i] ||
14814bba8532Spatrick 		    sc->hw_params.ring_mask->rx_err[i] ||
14824bba8532Spatrick 		    sc->hw_params.ring_mask->rx_wbm_rel[i] ||
14834bba8532Spatrick 		    sc->hw_params.ring_mask->reo_status[i] ||
14844bba8532Spatrick 		    sc->hw_params.ring_mask->host2rxdma[i] ||
1485c12edff5Spatrick 		    sc->hw_params.ring_mask->rx_mon_dest[i]) {
14864bba8532Spatrick 			num_irq = 1;
14874bba8532Spatrick 		}
14884bba8532Spatrick 
14894bba8532Spatrick 		irq_grp->num_irq = num_irq;
149027c3d914Spatrick 		irq_grp->irqs[0] = base_idx + i;
14914bba8532Spatrick 
14924bba8532Spatrick 		if (num_irq) {
14934bba8532Spatrick 			int irq_idx = irq_grp->irqs[0];
14944bba8532Spatrick 			pci_intr_handle_t ih;
14954bba8532Spatrick 
14964bba8532Spatrick 			if (pci_intr_map_msivec(pa, irq_idx, &ih) != 0 &&
14974bba8532Spatrick 			    pci_intr_map(pa, &ih) != 0) {
14984bba8532Spatrick 				printf("%s: can't map interrupt\n",
14994bba8532Spatrick 				    sc->sc_dev.dv_xname);
15004bba8532Spatrick 				return EIO;
15014bba8532Spatrick 			}
15024bba8532Spatrick 
15034bba8532Spatrick 			snprintf(psc->sc_ivname[irq_idx], sizeof(psc->sc_ivname[0]),
15044bba8532Spatrick 			    "%s:ex%d", sc->sc_dev.dv_xname, i);
15054bba8532Spatrick 			psc->sc_ih[irq_idx] = pci_intr_establish(psc->sc_pc, ih,
15064bba8532Spatrick 			    IPL_NET, qwz_ext_intr, irq_grp, psc->sc_ivname[irq_idx]);
15074bba8532Spatrick 			if (psc->sc_ih[irq_idx] == NULL) {
15084bba8532Spatrick 				printf("%s: failed to request irq %d\n",
15094bba8532Spatrick 				    sc->sc_dev.dv_xname, irq_idx);
15104bba8532Spatrick 				return EIO;
15114bba8532Spatrick 			}
15124bba8532Spatrick 		}
15134bba8532Spatrick 
15144bba8532Spatrick 		qwz_pcic_ext_grp_disable(irq_grp);
15154bba8532Spatrick 	}
15164bba8532Spatrick 
15174bba8532Spatrick 	return 0;
15184bba8532Spatrick }
15194bba8532Spatrick 
15204bba8532Spatrick int
qwz_pcic_config_irq(struct qwz_softc * sc,struct pci_attach_args * pa)15214bba8532Spatrick qwz_pcic_config_irq(struct qwz_softc *sc, struct pci_attach_args *pa)
15224bba8532Spatrick {
15234bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
15244bba8532Spatrick 	struct qwz_ce_pipe *ce_pipe;
15254bba8532Spatrick 	uint32_t msi_data_start;
15264bba8532Spatrick 	uint32_t msi_data_count, msi_data_idx;
15274bba8532Spatrick 	uint32_t msi_irq_start;
15284bba8532Spatrick 	int i, ret, irq_idx;
15294bba8532Spatrick 	pci_intr_handle_t ih;
15304bba8532Spatrick 
15314bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
15324bba8532Spatrick 		return 0;
15334bba8532Spatrick 
15344bba8532Spatrick 	ret = qwz_pcic_get_user_msi_vector(sc, "CE", &msi_data_count,
15354bba8532Spatrick 	    &msi_data_start, &msi_irq_start);
15364bba8532Spatrick 	if (ret)
15374bba8532Spatrick 		return ret;
15384bba8532Spatrick 
15394bba8532Spatrick 	/* Configure CE irqs */
15404bba8532Spatrick 	for (i = 0, msi_data_idx = 0; i < sc->hw_params.ce_count; i++) {
15414bba8532Spatrick 		if (qwz_ce_get_attr_flags(sc, i) & CE_ATTR_DIS_INTR)
15424bba8532Spatrick 			continue;
15434bba8532Spatrick 
15444bba8532Spatrick 		ce_pipe = &sc->ce.ce_pipe[i];
15454bba8532Spatrick 		irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
15464bba8532Spatrick 
15474bba8532Spatrick 		if (pci_intr_map_msivec(pa, irq_idx, &ih) != 0 &&
15484bba8532Spatrick 		    pci_intr_map(pa, &ih) != 0) {
15494bba8532Spatrick 			printf("%s: can't map interrupt\n",
15504bba8532Spatrick 			    sc->sc_dev.dv_xname);
15514bba8532Spatrick 			return EIO;
15524bba8532Spatrick 		}
15534bba8532Spatrick 
15544bba8532Spatrick 		snprintf(psc->sc_ivname[irq_idx], sizeof(psc->sc_ivname[0]),
15554bba8532Spatrick 		    "%s:ce%d", sc->sc_dev.dv_xname, ce_pipe->pipe_num);
15564bba8532Spatrick 		psc->sc_ih[irq_idx] = pci_intr_establish(psc->sc_pc, ih,
15574bba8532Spatrick 		    IPL_NET, qwz_ce_intr, ce_pipe, psc->sc_ivname[irq_idx]);
15584bba8532Spatrick 		if (psc->sc_ih[irq_idx] == NULL) {
15594bba8532Spatrick 			printf("%s: failed to request irq %d\n",
15604bba8532Spatrick 			    sc->sc_dev.dv_xname, irq_idx);
15614bba8532Spatrick 			return EIO;
15624bba8532Spatrick 		}
15634bba8532Spatrick 
15644bba8532Spatrick 		msi_data_idx++;
15654bba8532Spatrick 
15664bba8532Spatrick 		qwz_pcic_ce_irq_disable(sc, i);
15674bba8532Spatrick 	}
15684bba8532Spatrick 
15694bba8532Spatrick 	ret = qwz_pcic_ext_irq_config(sc, pa);
15704bba8532Spatrick 	if (ret)
15714bba8532Spatrick 		return ret;
15724bba8532Spatrick 
15734bba8532Spatrick 	return 0;
15744bba8532Spatrick }
15754bba8532Spatrick 
15764bba8532Spatrick void
qwz_pcic_ce_irqs_enable(struct qwz_softc * sc)15774bba8532Spatrick qwz_pcic_ce_irqs_enable(struct qwz_softc *sc)
15784bba8532Spatrick {
15794bba8532Spatrick 	int i;
15804bba8532Spatrick 
15814bba8532Spatrick 	set_bit(ATH12K_FLAG_CE_IRQ_ENABLED, sc->sc_flags);
15824bba8532Spatrick 
15834bba8532Spatrick 	for (i = 0; i < sc->hw_params.ce_count; i++) {
15844bba8532Spatrick 		if (qwz_ce_get_attr_flags(sc, i) & CE_ATTR_DIS_INTR)
15854bba8532Spatrick 			continue;
15864bba8532Spatrick 		qwz_pcic_ce_irq_enable(sc, i);
15874bba8532Spatrick 	}
15884bba8532Spatrick }
15894bba8532Spatrick 
15904bba8532Spatrick void
qwz_pcic_ce_irqs_disable(struct qwz_softc * sc)15914bba8532Spatrick qwz_pcic_ce_irqs_disable(struct qwz_softc *sc)
15924bba8532Spatrick {
15934bba8532Spatrick 	int i;
15944bba8532Spatrick 
15954bba8532Spatrick 	clear_bit(ATH12K_FLAG_CE_IRQ_ENABLED, sc->sc_flags);
15964bba8532Spatrick 
15974bba8532Spatrick 	for (i = 0; i < sc->hw_params.ce_count; i++) {
15984bba8532Spatrick 		if (qwz_ce_get_attr_flags(sc, i) & CE_ATTR_DIS_INTR)
15994bba8532Spatrick 			continue;
16004bba8532Spatrick 		qwz_pcic_ce_irq_disable(sc, i);
16014bba8532Spatrick 	}
16024bba8532Spatrick }
16034bba8532Spatrick 
16044bba8532Spatrick int
qwz_pci_start(struct qwz_softc * sc)16054bba8532Spatrick qwz_pci_start(struct qwz_softc *sc)
16064bba8532Spatrick {
16074bba8532Spatrick 	/* TODO: for now don't restore ASPM in case of single MSI
16084bba8532Spatrick 	 * vector as MHI register reading in M2 causes system hang.
16094bba8532Spatrick 	 */
16104bba8532Spatrick 	if (test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags))
16114bba8532Spatrick 		qwz_pci_aspm_restore(sc);
16124bba8532Spatrick 	else
16134bba8532Spatrick 		DPRINTF("%s: leaving PCI ASPM disabled to avoid MHI M2 problems"
16144bba8532Spatrick 		    "\n", sc->sc_dev.dv_xname);
16154bba8532Spatrick 
16164bba8532Spatrick 	set_bit(ATH12K_FLAG_DEVICE_INIT_DONE, sc->sc_flags);
16174bba8532Spatrick 
16184bba8532Spatrick 	qwz_ce_rx_post_buf(sc);
16194bba8532Spatrick 	qwz_pcic_ce_irqs_enable(sc);
16204bba8532Spatrick 
16214bba8532Spatrick 	return 0;
16224bba8532Spatrick }
16234bba8532Spatrick 
16244bba8532Spatrick void
qwz_pcic_ce_irq_disable_sync(struct qwz_softc * sc)16254bba8532Spatrick qwz_pcic_ce_irq_disable_sync(struct qwz_softc *sc)
16264bba8532Spatrick {
16274bba8532Spatrick 	qwz_pcic_ce_irqs_disable(sc);
16284bba8532Spatrick #if 0
16294bba8532Spatrick 	ath12k_pcic_sync_ce_irqs(ab);
16304bba8532Spatrick 	ath12k_pcic_kill_tasklets(ab);
16314bba8532Spatrick #endif
16324bba8532Spatrick }
16334bba8532Spatrick 
16344bba8532Spatrick void
qwz_pci_stop(struct qwz_softc * sc)16354bba8532Spatrick qwz_pci_stop(struct qwz_softc *sc)
16364bba8532Spatrick {
16374bba8532Spatrick 	qwz_pcic_ce_irq_disable_sync(sc);
16384bba8532Spatrick 	qwz_ce_cleanup_pipes(sc);
16394bba8532Spatrick }
16404bba8532Spatrick 
16414bba8532Spatrick int
qwz_pci_bus_wake_up(struct qwz_softc * sc)16424bba8532Spatrick qwz_pci_bus_wake_up(struct qwz_softc *sc)
16434bba8532Spatrick {
16444bba8532Spatrick 	if (qwz_mhi_wake_db_clear_valid(sc))
16454bba8532Spatrick 		qwz_mhi_device_wake(sc);
16464bba8532Spatrick 
16474bba8532Spatrick 	return 0;
16484bba8532Spatrick }
16494bba8532Spatrick 
16504bba8532Spatrick void
qwz_pci_bus_release(struct qwz_softc * sc)16514bba8532Spatrick qwz_pci_bus_release(struct qwz_softc *sc)
16524bba8532Spatrick {
16534bba8532Spatrick 	if (qwz_mhi_wake_db_clear_valid(sc))
16544bba8532Spatrick 		qwz_mhi_device_zzz(sc);
16554bba8532Spatrick }
16564bba8532Spatrick 
16574bba8532Spatrick uint32_t
qwz_pci_get_window_start(struct qwz_softc * sc,uint32_t offset)16584bba8532Spatrick qwz_pci_get_window_start(struct qwz_softc *sc, uint32_t offset)
16594bba8532Spatrick {
166027c3d914Spatrick 	if (!sc->static_window_map)
16614bba8532Spatrick 		return ATH12K_PCI_WINDOW_START;
16624bba8532Spatrick 
16634bba8532Spatrick 	if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < ATH12K_PCI_WINDOW_RANGE_MASK)
16644bba8532Spatrick 		/* if offset lies within DP register range, use 3rd window */
16654bba8532Spatrick 		return 3 * ATH12K_PCI_WINDOW_START;
1666b8a42cceSpatrick 	else if ((offset ^ HAL_SEQ_WCSS_UMAC_CE0_SRC_REG) <
16674bba8532Spatrick 		 ATH12K_PCI_WINDOW_RANGE_MASK)
16684bba8532Spatrick 		 /* if offset lies within CE register range, use 2nd window */
16694bba8532Spatrick 		return 2 * ATH12K_PCI_WINDOW_START;
16704bba8532Spatrick 	else
16714bba8532Spatrick 		return ATH12K_PCI_WINDOW_START;
16724bba8532Spatrick }
16734bba8532Spatrick 
16744bba8532Spatrick void
qwz_pci_select_window(struct qwz_softc * sc,uint32_t offset)16754bba8532Spatrick qwz_pci_select_window(struct qwz_softc *sc, uint32_t offset)
16764bba8532Spatrick {
16774bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
16784bba8532Spatrick 	uint32_t window = FIELD_GET(ATH12K_PCI_WINDOW_VALUE_MASK, offset);
16794bba8532Spatrick 
16804bba8532Spatrick #if notyet
16814bba8532Spatrick 	lockdep_assert_held(&ab_pci->window_lock);
16824bba8532Spatrick #endif
16834bba8532Spatrick 
168427c3d914Spatrick 	/*
168527c3d914Spatrick 	 * Preserve the static window configuration and reset only
168627c3d914Spatrick 	 * dynamic window.
168727c3d914Spatrick 	 */
168827c3d914Spatrick 	window |= psc->register_window & ATH12K_PCI_WINDOW_STATIC_MASK;
168927c3d914Spatrick 
16904bba8532Spatrick 	if (window != psc->register_window) {
16914bba8532Spatrick 		qwz_pci_write(sc, ATH12K_PCI_WINDOW_REG_ADDRESS,
16924bba8532Spatrick 		    ATH12K_PCI_WINDOW_ENABLE_BIT | window);
16934bba8532Spatrick 		(void) qwz_pci_read(sc, ATH12K_PCI_WINDOW_REG_ADDRESS);
16944bba8532Spatrick 		psc->register_window = window;
16954bba8532Spatrick 	}
16964bba8532Spatrick }
16974bba8532Spatrick 
169827c3d914Spatrick static inline bool
qwz_pci_is_offset_within_mhi_region(uint32_t offset)169927c3d914Spatrick qwz_pci_is_offset_within_mhi_region(uint32_t offset)
170027c3d914Spatrick {
170127c3d914Spatrick 	return (offset >= PCI_MHIREGLEN_REG && offset <= PCI_MHI_REGION_END);
170227c3d914Spatrick }
170327c3d914Spatrick 
17044bba8532Spatrick void
qwz_pci_window_write32(struct qwz_softc * sc,uint32_t offset,uint32_t value)17054bba8532Spatrick qwz_pci_window_write32(struct qwz_softc *sc, uint32_t offset, uint32_t value)
17064bba8532Spatrick {
17074bba8532Spatrick 	uint32_t window_start;
17084bba8532Spatrick 
17094bba8532Spatrick 	window_start = qwz_pci_get_window_start(sc, offset);
17104bba8532Spatrick 
17114bba8532Spatrick 	if (window_start == ATH12K_PCI_WINDOW_START) {
17124bba8532Spatrick #if notyet
17134bba8532Spatrick 		spin_lock_bh(&ab_pci->window_lock);
17144bba8532Spatrick #endif
17154bba8532Spatrick 		qwz_pci_select_window(sc, offset);
171627c3d914Spatrick 
171727c3d914Spatrick 		if (qwz_pci_is_offset_within_mhi_region(offset)) {
171827c3d914Spatrick 			offset = offset - PCI_MHIREGLEN_REG;
171927c3d914Spatrick 			qwz_pci_write(sc, offset & ATH12K_PCI_WINDOW_RANGE_MASK,
172027c3d914Spatrick 			    value);
172127c3d914Spatrick 		} else {
17224bba8532Spatrick 			qwz_pci_write(sc, window_start +
17234bba8532Spatrick 			    (offset & ATH12K_PCI_WINDOW_RANGE_MASK), value);
172427c3d914Spatrick 		}
17254bba8532Spatrick #if notyet
17264bba8532Spatrick 		spin_unlock_bh(&ab_pci->window_lock);
17274bba8532Spatrick #endif
17284bba8532Spatrick 	} else {
17294bba8532Spatrick 		qwz_pci_write(sc, window_start +
17304bba8532Spatrick 		    (offset & ATH12K_PCI_WINDOW_RANGE_MASK), value);
17314bba8532Spatrick 	}
17324bba8532Spatrick }
17334bba8532Spatrick 
17344bba8532Spatrick uint32_t
qwz_pci_window_read32(struct qwz_softc * sc,uint32_t offset)17354bba8532Spatrick qwz_pci_window_read32(struct qwz_softc *sc, uint32_t offset)
17364bba8532Spatrick {
17374bba8532Spatrick 	uint32_t window_start, val;
17384bba8532Spatrick 
17394bba8532Spatrick 	window_start = qwz_pci_get_window_start(sc, offset);
17404bba8532Spatrick 
17414bba8532Spatrick 	if (window_start == ATH12K_PCI_WINDOW_START) {
17424bba8532Spatrick #if notyet
17434bba8532Spatrick 		spin_lock_bh(&ab_pci->window_lock);
17444bba8532Spatrick #endif
17454bba8532Spatrick 		qwz_pci_select_window(sc, offset);
174627c3d914Spatrick 
174727c3d914Spatrick 		if (qwz_pci_is_offset_within_mhi_region(offset)) {
174827c3d914Spatrick 			offset = offset - PCI_MHIREGLEN_REG;
174927c3d914Spatrick 			val = qwz_pci_read(sc,
175027c3d914Spatrick 			    offset & ATH12K_PCI_WINDOW_RANGE_MASK);
175127c3d914Spatrick 		} else {
17524bba8532Spatrick 			val = qwz_pci_read(sc, window_start +
17534bba8532Spatrick 			    (offset & ATH12K_PCI_WINDOW_RANGE_MASK));
175427c3d914Spatrick 		}
17554bba8532Spatrick #if notyet
17564bba8532Spatrick 		spin_unlock_bh(&ab_pci->window_lock);
17574bba8532Spatrick #endif
17584bba8532Spatrick 	} else {
17594bba8532Spatrick 		val = qwz_pci_read(sc, window_start +
17604bba8532Spatrick 		    (offset & ATH12K_PCI_WINDOW_RANGE_MASK));
17614bba8532Spatrick 	}
17624bba8532Spatrick 
17634bba8532Spatrick 	return val;
17644bba8532Spatrick }
17654bba8532Spatrick 
17664bba8532Spatrick void
qwz_pci_select_static_window(struct qwz_softc * sc)17674bba8532Spatrick qwz_pci_select_static_window(struct qwz_softc *sc)
17684bba8532Spatrick {
17694bba8532Spatrick 	uint32_t umac_window;
17704bba8532Spatrick 	uint32_t ce_window;
17714bba8532Spatrick 	uint32_t window;
17724bba8532Spatrick 
17734bba8532Spatrick 	umac_window = FIELD_GET(ATH12K_PCI_WINDOW_VALUE_MASK, HAL_SEQ_WCSS_UMAC_OFFSET);
17744bba8532Spatrick 	ce_window = FIELD_GET(ATH12K_PCI_WINDOW_VALUE_MASK, HAL_CE_WFSS_CE_REG_BASE);
17754bba8532Spatrick 	window = (umac_window << 12) | (ce_window << 6);
17764bba8532Spatrick 
17774bba8532Spatrick 	qwz_pci_write(sc, ATH12K_PCI_WINDOW_REG_ADDRESS,
17784bba8532Spatrick 	    ATH12K_PCI_WINDOW_ENABLE_BIT | window);
17794bba8532Spatrick }
17804bba8532Spatrick 
17814bba8532Spatrick void
qwz_pci_soc_global_reset(struct qwz_softc * sc)17824bba8532Spatrick qwz_pci_soc_global_reset(struct qwz_softc *sc)
17834bba8532Spatrick {
17844bba8532Spatrick 	uint32_t val, msecs;
17854bba8532Spatrick 
17864bba8532Spatrick 	val = qwz_pcic_read32(sc, PCIE_SOC_GLOBAL_RESET);
17874bba8532Spatrick 
17884bba8532Spatrick 	val |= PCIE_SOC_GLOBAL_RESET_V;
17894bba8532Spatrick 
17904bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_SOC_GLOBAL_RESET, val);
17914bba8532Spatrick 
17924bba8532Spatrick 	/* TODO: exact time to sleep is uncertain */
17934bba8532Spatrick 	msecs = 10;
17944bba8532Spatrick 	DELAY(msecs * 1000);
17954bba8532Spatrick 
17964bba8532Spatrick 	/* Need to toggle V bit back otherwise stuck in reset status */
17974bba8532Spatrick 	val &= ~PCIE_SOC_GLOBAL_RESET_V;
17984bba8532Spatrick 
17994bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_SOC_GLOBAL_RESET, val);
18004bba8532Spatrick 
18014bba8532Spatrick 	DELAY(msecs * 1000);
18024bba8532Spatrick 
18034bba8532Spatrick 	val = qwz_pcic_read32(sc, PCIE_SOC_GLOBAL_RESET);
18044bba8532Spatrick 	if (val == 0xffffffff)
18054bba8532Spatrick 		printf("%s: link down error during global reset\n",
18064bba8532Spatrick 		    sc->sc_dev.dv_xname);
18074bba8532Spatrick }
18084bba8532Spatrick 
18094bba8532Spatrick void
qwz_pci_clear_dbg_registers(struct qwz_softc * sc)18104bba8532Spatrick qwz_pci_clear_dbg_registers(struct qwz_softc *sc)
18114bba8532Spatrick {
18124bba8532Spatrick 	uint32_t val;
18134bba8532Spatrick 
18144bba8532Spatrick 	/* read cookie */
18154bba8532Spatrick 	val = qwz_pcic_read32(sc, PCIE_Q6_COOKIE_ADDR);
18164bba8532Spatrick 	DPRINTF("%s: cookie:0x%x\n", sc->sc_dev.dv_xname, val);
18174bba8532Spatrick 
18184bba8532Spatrick 	val = qwz_pcic_read32(sc, WLAON_WARM_SW_ENTRY);
18194bba8532Spatrick 	DPRINTF("%s: WLAON_WARM_SW_ENTRY 0x%x\n", sc->sc_dev.dv_xname, val);
18204bba8532Spatrick 
18214bba8532Spatrick 	/* TODO: exact time to sleep is uncertain */
18224bba8532Spatrick 	DELAY(10 * 1000);
18234bba8532Spatrick 
18244bba8532Spatrick 	/* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
18254bba8532Spatrick 	 * continuing warm path and entering dead loop.
18264bba8532Spatrick 	 */
18274bba8532Spatrick 	qwz_pcic_write32(sc, WLAON_WARM_SW_ENTRY, 0);
18284bba8532Spatrick 	DELAY(10 * 1000);
18294bba8532Spatrick 
18304bba8532Spatrick 	val = qwz_pcic_read32(sc, WLAON_WARM_SW_ENTRY);
18314bba8532Spatrick 	DPRINTF("%s: WLAON_WARM_SW_ENTRY 0x%x\n", sc->sc_dev.dv_xname, val);
18324bba8532Spatrick 
18334bba8532Spatrick 	/* A read clear register. clear the register to prevent
18344bba8532Spatrick 	 * Q6 from entering wrong code path.
18354bba8532Spatrick 	 */
18364bba8532Spatrick 	val = qwz_pcic_read32(sc, WLAON_SOC_RESET_CAUSE_REG);
18374bba8532Spatrick 	DPRINTF("%s: soc reset cause:%d\n", sc->sc_dev.dv_xname, val);
18384bba8532Spatrick }
18394bba8532Spatrick 
18404bba8532Spatrick int
qwz_pci_set_link_reg(struct qwz_softc * sc,uint32_t offset,uint32_t value,uint32_t mask)18414bba8532Spatrick qwz_pci_set_link_reg(struct qwz_softc *sc, uint32_t offset, uint32_t value,
18424bba8532Spatrick     uint32_t mask)
18434bba8532Spatrick {
18444bba8532Spatrick 	uint32_t v;
18454bba8532Spatrick 	int i;
18464bba8532Spatrick 
18474bba8532Spatrick 	v = qwz_pcic_read32(sc, offset);
18484bba8532Spatrick 	if ((v & mask) == value)
18494bba8532Spatrick 		return 0;
18504bba8532Spatrick 
18514bba8532Spatrick 	for (i = 0; i < 10; i++) {
18524bba8532Spatrick 		qwz_pcic_write32(sc, offset, (v & ~mask) | value);
18534bba8532Spatrick 
18544bba8532Spatrick 		v = qwz_pcic_read32(sc, offset);
18554bba8532Spatrick 		if ((v & mask) == value)
18564bba8532Spatrick 			return 0;
18574bba8532Spatrick 
18584bba8532Spatrick 		delay((2 * 1000));
18594bba8532Spatrick 	}
18604bba8532Spatrick 
18614bba8532Spatrick 	DPRINTF("failed to set pcie link register 0x%08x: 0x%08x != 0x%08x\n",
18624bba8532Spatrick 	    offset, v & mask, value);
18634bba8532Spatrick 
18644bba8532Spatrick 	return ETIMEDOUT;
18654bba8532Spatrick }
18664bba8532Spatrick 
18674bba8532Spatrick int
qwz_pci_fix_l1ss(struct qwz_softc * sc)18684bba8532Spatrick qwz_pci_fix_l1ss(struct qwz_softc *sc)
18694bba8532Spatrick {
18704bba8532Spatrick 	int ret;
18714bba8532Spatrick 
18724bba8532Spatrick 	ret = qwz_pci_set_link_reg(sc,
18734bba8532Spatrick 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_REG(sc),
18744bba8532Spatrick 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_VAL,
18754bba8532Spatrick 				      PCIE_QSERDES_COM_SYSCLK_EN_SEL_MSK);
18764bba8532Spatrick 	if (ret) {
18774bba8532Spatrick 		DPRINTF("failed to set sysclk: %d\n", ret);
18784bba8532Spatrick 		return ret;
18794bba8532Spatrick 	}
18804bba8532Spatrick 
18814bba8532Spatrick 	ret = qwz_pci_set_link_reg(sc,
18824bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG1_REG(sc),
18834bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG1_VAL,
18844bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
18854bba8532Spatrick 	if (ret) {
18864bba8532Spatrick 		DPRINTF("failed to set dtct config1 error: %d\n", ret);
18874bba8532Spatrick 		return ret;
18884bba8532Spatrick 	}
18894bba8532Spatrick 
18904bba8532Spatrick 	ret = qwz_pci_set_link_reg(sc,
18914bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG2_REG(sc),
18924bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG2_VAL,
18934bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
18944bba8532Spatrick 	if (ret) {
18954bba8532Spatrick 		DPRINTF("failed to set dtct config2: %d\n", ret);
18964bba8532Spatrick 		return ret;
18974bba8532Spatrick 	}
18984bba8532Spatrick 
18994bba8532Spatrick 	ret = qwz_pci_set_link_reg(sc,
19004bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG4_REG(sc),
19014bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG4_VAL,
19024bba8532Spatrick 				      PCIE_PCS_OSC_DTCT_CONFIG_MSK);
19034bba8532Spatrick 	if (ret) {
19044bba8532Spatrick 		DPRINTF("failed to set dtct config4: %d\n", ret);
19054bba8532Spatrick 		return ret;
19064bba8532Spatrick 	}
19074bba8532Spatrick 
19084bba8532Spatrick 	return 0;
19094bba8532Spatrick }
19104bba8532Spatrick 
19114bba8532Spatrick void
qwz_pci_enable_ltssm(struct qwz_softc * sc)19124bba8532Spatrick qwz_pci_enable_ltssm(struct qwz_softc *sc)
19134bba8532Spatrick {
19144bba8532Spatrick 	uint32_t val;
19154bba8532Spatrick 	int i;
19164bba8532Spatrick 
19174bba8532Spatrick 	val = qwz_pcic_read32(sc, PCIE_PCIE_PARF_LTSSM);
19184bba8532Spatrick 
19194bba8532Spatrick 	/* PCIE link seems very unstable after the Hot Reset*/
19204bba8532Spatrick 	for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
19214bba8532Spatrick 		if (val == 0xffffffff)
19224bba8532Spatrick 			DELAY(5 * 1000);
19234bba8532Spatrick 
19244bba8532Spatrick 		qwz_pcic_write32(sc, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
19254bba8532Spatrick 		val = qwz_pcic_read32(sc, PCIE_PCIE_PARF_LTSSM);
19264bba8532Spatrick 	}
19274bba8532Spatrick 
19284bba8532Spatrick 	DPRINTF("%s: pci ltssm 0x%x\n", sc->sc_dev.dv_xname, val);
19294bba8532Spatrick 
19304bba8532Spatrick 	val = qwz_pcic_read32(sc, GCC_GCC_PCIE_HOT_RST);
19314bba8532Spatrick 	val |= GCC_GCC_PCIE_HOT_RST_VAL;
19324bba8532Spatrick 	qwz_pcic_write32(sc, GCC_GCC_PCIE_HOT_RST, val);
19334bba8532Spatrick 	val = qwz_pcic_read32(sc, GCC_GCC_PCIE_HOT_RST);
19344bba8532Spatrick 
19354bba8532Spatrick 	DPRINTF("%s: pci pcie_hot_rst 0x%x\n", sc->sc_dev.dv_xname, val);
19364bba8532Spatrick 
19374bba8532Spatrick 	DELAY(5 * 1000);
19384bba8532Spatrick }
19394bba8532Spatrick 
19404bba8532Spatrick void
qwz_pci_clear_all_intrs(struct qwz_softc * sc)19414bba8532Spatrick qwz_pci_clear_all_intrs(struct qwz_softc *sc)
19424bba8532Spatrick {
19434bba8532Spatrick 	/* This is a WAR for PCIE Hotreset.
19444bba8532Spatrick 	 * When target receive Hotreset, but will set the interrupt.
19454bba8532Spatrick 	 * So when download SBL again, SBL will open Interrupt and
19464bba8532Spatrick 	 * receive it, and crash immediately.
19474bba8532Spatrick 	 */
19484bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
19494bba8532Spatrick }
19504bba8532Spatrick 
19514bba8532Spatrick void
qwz_pci_set_wlaon_pwr_ctrl(struct qwz_softc * sc)19524bba8532Spatrick qwz_pci_set_wlaon_pwr_ctrl(struct qwz_softc *sc)
19534bba8532Spatrick {
19544bba8532Spatrick 	uint32_t val;
19554bba8532Spatrick 
19564bba8532Spatrick 	val = qwz_pcic_read32(sc, WLAON_QFPROM_PWR_CTRL_REG);
19574bba8532Spatrick 	val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
19584bba8532Spatrick 	qwz_pcic_write32(sc, WLAON_QFPROM_PWR_CTRL_REG, val);
19594bba8532Spatrick }
19604bba8532Spatrick 
19614bba8532Spatrick void
qwz_pci_force_wake(struct qwz_softc * sc)19624bba8532Spatrick qwz_pci_force_wake(struct qwz_softc *sc)
19634bba8532Spatrick {
19644bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
19654bba8532Spatrick 	DELAY(5 * 1000);
19664bba8532Spatrick }
19674bba8532Spatrick 
19684bba8532Spatrick void
qwz_pci_sw_reset(struct qwz_softc * sc,bool power_on)19694bba8532Spatrick qwz_pci_sw_reset(struct qwz_softc *sc, bool power_on)
19704bba8532Spatrick {
19714bba8532Spatrick 	DELAY(100 * 1000); /* msecs */
19724bba8532Spatrick 
19734bba8532Spatrick 	if (power_on) {
19744bba8532Spatrick 		qwz_pci_enable_ltssm(sc);
19754bba8532Spatrick 		qwz_pci_clear_all_intrs(sc);
19764bba8532Spatrick 		qwz_pci_set_wlaon_pwr_ctrl(sc);
19774bba8532Spatrick 		if (sc->hw_params.fix_l1ss)
19784bba8532Spatrick 			qwz_pci_fix_l1ss(sc);
19794bba8532Spatrick 	}
19804bba8532Spatrick 
19814bba8532Spatrick 	qwz_mhi_clear_vector(sc);
19824bba8532Spatrick 	qwz_pci_clear_dbg_registers(sc);
19834bba8532Spatrick 	qwz_pci_soc_global_reset(sc);
19844bba8532Spatrick 	qwz_mhi_reset_device(sc, 0);
19854bba8532Spatrick }
19864bba8532Spatrick 
19874bba8532Spatrick void
qwz_pci_msi_config(struct qwz_softc * sc,bool enable)19884bba8532Spatrick qwz_pci_msi_config(struct qwz_softc *sc, bool enable)
19894bba8532Spatrick {
19904bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
19914bba8532Spatrick 	uint32_t val;
19924bba8532Spatrick 
19934bba8532Spatrick 	val = pci_conf_read(psc->sc_pc, psc->sc_tag,
19944bba8532Spatrick 	    psc->sc_msi_off + PCI_MSI_MC);
19954bba8532Spatrick 
19964bba8532Spatrick 	if (enable)
19974bba8532Spatrick 		val |= PCI_MSI_MC_MSIE;
19984bba8532Spatrick 	else
19994bba8532Spatrick 		val &= ~PCI_MSI_MC_MSIE;
20004bba8532Spatrick 
20014bba8532Spatrick 	pci_conf_write(psc->sc_pc, psc->sc_tag, psc->sc_msi_off + PCI_MSI_MC,
20024bba8532Spatrick 	    val);
20034bba8532Spatrick }
20044bba8532Spatrick 
20054bba8532Spatrick void
qwz_pci_msi_enable(struct qwz_softc * sc)20064bba8532Spatrick qwz_pci_msi_enable(struct qwz_softc *sc)
20074bba8532Spatrick {
20084bba8532Spatrick 	qwz_pci_msi_config(sc, true);
20094bba8532Spatrick }
20104bba8532Spatrick 
20114bba8532Spatrick void
qwz_pci_msi_disable(struct qwz_softc * sc)20124bba8532Spatrick qwz_pci_msi_disable(struct qwz_softc *sc)
20134bba8532Spatrick {
20144bba8532Spatrick 	qwz_pci_msi_config(sc, false);
20154bba8532Spatrick }
20164bba8532Spatrick 
20174bba8532Spatrick void
qwz_pci_aspm_disable(struct qwz_softc * sc)20184bba8532Spatrick qwz_pci_aspm_disable(struct qwz_softc *sc)
20194bba8532Spatrick {
20204bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
20214bba8532Spatrick 
20224bba8532Spatrick 	psc->sc_lcsr = pci_conf_read(psc->sc_pc, psc->sc_tag,
20234bba8532Spatrick 	    psc->sc_cap_off + PCI_PCIE_LCSR);
20244bba8532Spatrick 
20254bba8532Spatrick 	DPRINTF("%s: pci link_ctl 0x%04x L0s %d L1 %d\n", sc->sc_dev.dv_xname,
20264bba8532Spatrick 	    (uint16_t)psc->sc_lcsr, (psc->sc_lcsr & PCI_PCIE_LCSR_ASPM_L0S),
20274bba8532Spatrick 	    (psc->sc_lcsr & PCI_PCIE_LCSR_ASPM_L1));
20284bba8532Spatrick 
20294bba8532Spatrick 	/* disable L0s and L1 */
20304bba8532Spatrick 	pci_conf_write(psc->sc_pc, psc->sc_tag, psc->sc_cap_off + PCI_PCIE_LCSR,
20314bba8532Spatrick 	    psc->sc_lcsr & ~(PCI_PCIE_LCSR_ASPM_L0S | PCI_PCIE_LCSR_ASPM_L1));
20324bba8532Spatrick 
20334bba8532Spatrick 	psc->sc_flags |= ATH12K_PCI_ASPM_RESTORE;
20344bba8532Spatrick }
20354bba8532Spatrick 
20364bba8532Spatrick void
qwz_pci_aspm_restore(struct qwz_softc * sc)20374bba8532Spatrick qwz_pci_aspm_restore(struct qwz_softc *sc)
20384bba8532Spatrick {
20394bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
20404bba8532Spatrick 
20414bba8532Spatrick 	if (psc->sc_flags & ATH12K_PCI_ASPM_RESTORE) {
20424bba8532Spatrick 		pci_conf_write(psc->sc_pc, psc->sc_tag,
20434bba8532Spatrick 		    psc->sc_cap_off + PCI_PCIE_LCSR, psc->sc_lcsr);
20444bba8532Spatrick 		psc->sc_flags &= ~ATH12K_PCI_ASPM_RESTORE;
20454bba8532Spatrick 	}
20464bba8532Spatrick }
20474bba8532Spatrick 
20484bba8532Spatrick int
qwz_pci_power_up(struct qwz_softc * sc)20494bba8532Spatrick qwz_pci_power_up(struct qwz_softc *sc)
20504bba8532Spatrick {
20514bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
20524bba8532Spatrick 	int error;
20534bba8532Spatrick 
20544bba8532Spatrick 	psc->register_window = 0;
20554bba8532Spatrick 	clear_bit(ATH12K_FLAG_DEVICE_INIT_DONE, sc->sc_flags);
20564bba8532Spatrick 
20574bba8532Spatrick 	qwz_pci_sw_reset(sc, true);
20584bba8532Spatrick 
20594bba8532Spatrick 	/* Disable ASPM during firmware download due to problems switching
20604bba8532Spatrick 	 * to AMSS state.
20614bba8532Spatrick 	 */
20624bba8532Spatrick 	qwz_pci_aspm_disable(sc);
20634bba8532Spatrick 
20644bba8532Spatrick 	qwz_pci_msi_enable(sc);
20654bba8532Spatrick 
20664bba8532Spatrick 	error = qwz_mhi_start(psc);
20674bba8532Spatrick 	if (error)
20684bba8532Spatrick 		return error;
20694bba8532Spatrick 
207027c3d914Spatrick 	if (sc->static_window_map)
20714bba8532Spatrick 		qwz_pci_select_static_window(sc);
20724bba8532Spatrick 
20734bba8532Spatrick 	return 0;
20744bba8532Spatrick }
20754bba8532Spatrick 
20764bba8532Spatrick void
qwz_pci_power_down(struct qwz_softc * sc)20774bba8532Spatrick qwz_pci_power_down(struct qwz_softc *sc)
20784bba8532Spatrick {
20794bba8532Spatrick 	/* restore aspm in case firmware bootup fails */
20804bba8532Spatrick 	qwz_pci_aspm_restore(sc);
20814bba8532Spatrick 
20824bba8532Spatrick 	qwz_pci_force_wake(sc);
20834bba8532Spatrick 
20844bba8532Spatrick 	qwz_pci_msi_disable(sc);
20854bba8532Spatrick 
20864bba8532Spatrick 	qwz_mhi_stop(sc);
20874bba8532Spatrick 	clear_bit(ATH12K_FLAG_DEVICE_INIT_DONE, sc->sc_flags);
20884bba8532Spatrick 	qwz_pci_sw_reset(sc, false);
20894bba8532Spatrick }
20904bba8532Spatrick 
20914bba8532Spatrick /*
20924bba8532Spatrick  * MHI
20934bba8532Spatrick  */
20944bba8532Spatrick int
qwz_mhi_register(struct qwz_softc * sc)20954bba8532Spatrick qwz_mhi_register(struct qwz_softc *sc)
20964bba8532Spatrick {
20974bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: STUB %s()\n", sc->sc_dev.dv_xname, __func__);
20984bba8532Spatrick 	return 0;
20994bba8532Spatrick }
21004bba8532Spatrick 
21014bba8532Spatrick void
qwz_mhi_unregister(struct qwz_softc * sc)21024bba8532Spatrick qwz_mhi_unregister(struct qwz_softc *sc)
21034bba8532Spatrick {
21044bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: STUB %s()\n", sc->sc_dev.dv_xname, __func__);
21054bba8532Spatrick }
21064bba8532Spatrick 
21074bba8532Spatrick // XXX MHI is GPLd - we provide a compatible bare-bones implementation
21084bba8532Spatrick #define MHI_CFG				0x10
21094bba8532Spatrick #define   MHI_CFG_NHWER_MASK		GENMASK(31, 24)
21104bba8532Spatrick #define   MHI_CFG_NHWER_SHFT		24
21114bba8532Spatrick #define   MHI_CFG_NER_MASK		GENMASK(23, 16)
21124bba8532Spatrick #define   MHI_CFG_NER_SHFT		16
21134bba8532Spatrick #define   MHI_CFG_NHWCH_MASK		GENMASK(15, 8)
21144bba8532Spatrick #define   MHI_CFG_NHWCH_SHFT		8
21154bba8532Spatrick #define   MHI_CFG_NCH_MASK		GENMASK(7, 0)
21164bba8532Spatrick #define MHI_CHDBOFF			0x18
21174bba8532Spatrick #define MHI_DEV_WAKE_DB			127
21184bba8532Spatrick #define MHI_ERDBOFF			0x20
21194bba8532Spatrick #define MHI_BHI_OFFSET			0x28
21204bba8532Spatrick #define   MHI_BHI_IMGADDR_LOW			0x08
21214bba8532Spatrick #define   MHI_BHI_IMGADDR_HIGH			0x0c
21224bba8532Spatrick #define   MHI_BHI_IMGSIZE			0x10
21234bba8532Spatrick #define   MHI_BHI_IMGTXDB			0x18
21244bba8532Spatrick #define   MHI_BHI_INTVEC			0x20
21254bba8532Spatrick #define   MHI_BHI_EXECENV			0x28
21264bba8532Spatrick #define   MHI_BHI_STATUS			0x2c
21274bba8532Spatrick #define	  MHI_BHI_SERIALNU			0x40
21284bba8532Spatrick #define MHI_BHIE_OFFSET			0x2c
21294bba8532Spatrick #define   MHI_BHIE_TXVECADDR_LOW_OFFS		0x2c
21304bba8532Spatrick #define   MHI_BHIE_TXVECADDR_HIGH_OFFS		0x30
21314bba8532Spatrick #define   MHI_BHIE_TXVECSIZE_OFFS		0x34
21324bba8532Spatrick #define   MHI_BHIE_TXVECDB_OFFS			0x3c
21334bba8532Spatrick #define   MHI_BHIE_TXVECSTATUS_OFFS		0x44
21344bba8532Spatrick #define   MHI_BHIE_RXVECADDR_LOW_OFFS		0x60
21354bba8532Spatrick #define   MHI_BHIE_RXVECSTATUS_OFFS		0x78
21364bba8532Spatrick #define MHI_CTRL			0x38
21374bba8532Spatrick #define    MHI_CTRL_READY_MASK			0x1
21384bba8532Spatrick #define    MHI_CTRL_RESET_MASK			0x2
21394bba8532Spatrick #define    MHI_CTRL_MHISTATE_MASK		GENMASK(15, 8)
21404bba8532Spatrick #define    MHI_CTRL_MHISTATE_SHFT		8
21414bba8532Spatrick #define MHI_STATUS			0x48
21424bba8532Spatrick #define    MHI_STATUS_MHISTATE_MASK		GENMASK(15, 8)
21434bba8532Spatrick #define    MHI_STATUS_MHISTATE_SHFT		8
21444bba8532Spatrick #define        MHI_STATE_RESET			0x0
21454bba8532Spatrick #define        MHI_STATE_READY			0x1
21464bba8532Spatrick #define        MHI_STATE_M0			0x2
21474bba8532Spatrick #define        MHI_STATE_M1			0x3
21484bba8532Spatrick #define        MHI_STATE_M2			0x4
21494bba8532Spatrick #define        MHI_STATE_M3			0x5
21504bba8532Spatrick #define        MHI_STATE_M3_FAST		0x6
21514bba8532Spatrick #define        MHI_STATE_BHI			0x7
21524bba8532Spatrick #define        MHI_STATE_SYS_ERR		0xff
21534bba8532Spatrick #define    MHI_STATUS_READY_MASK		0x1
21544bba8532Spatrick #define    MHI_STATUS_SYSERR_MASK		0x4
21554bba8532Spatrick #define MHI_CCABAP_LOWER		0x58
21564bba8532Spatrick #define MHI_CCABAP_HIGHER		0x5c
21574bba8532Spatrick #define MHI_ECABAP_LOWER		0x60
21584bba8532Spatrick #define MHI_ECABAP_HIGHER		0x64
21594bba8532Spatrick #define MHI_CRCBAP_LOWER		0x68
21604bba8532Spatrick #define MHI_CRCBAP_HIGHER		0x6c
21614bba8532Spatrick #define MHI_CRDB_LOWER			0x70
21624bba8532Spatrick #define MHI_CRDB_HIGHER			0x74
21634bba8532Spatrick #define MHI_CTRLBASE_LOWER		0x80
21644bba8532Spatrick #define MHI_CTRLBASE_HIGHER		0x84
21654bba8532Spatrick #define MHI_CTRLLIMIT_LOWER		0x88
21664bba8532Spatrick #define MHI_CTRLLIMIT_HIGHER		0x8c
21674bba8532Spatrick #define MHI_DATABASE_LOWER		0x98
21684bba8532Spatrick #define MHI_DATABASE_HIGHER		0x9c
21694bba8532Spatrick #define MHI_DATALIMIT_LOWER		0xa0
21704bba8532Spatrick #define MHI_DATALIMIT_HIGHER		0xa4
21714bba8532Spatrick 
21724bba8532Spatrick #define MHI_EE_PBL	0x0	/* Primary Bootloader */
21734bba8532Spatrick #define MHI_EE_SBL	0x1	/* Secondary Bootloader */
21744bba8532Spatrick #define MHI_EE_AMSS	0x2	/* Modem, aka the primary runtime EE */
21754bba8532Spatrick #define MHI_EE_RDDM	0x3	/* Ram dump download mode */
21764bba8532Spatrick #define MHI_EE_WFW	0x4	/* WLAN firmware mode */
21774bba8532Spatrick #define MHI_EE_PTHRU	0x5	/* Passthrough */
21784bba8532Spatrick #define MHI_EE_EDL	0x6	/* Embedded downloader */
21794bba8532Spatrick #define MHI_EE_FP	0x7	/* Flash Programmer Environment */
21804bba8532Spatrick 
21814bba8532Spatrick #define MHI_IN_PBL(e) (e == MHI_EE_PBL || e == MHI_EE_PTHRU || e == MHI_EE_EDL)
21824bba8532Spatrick #define MHI_POWER_UP_CAPABLE(e) (MHI_IN_PBL(e) || e == MHI_EE_AMSS)
21834bba8532Spatrick #define MHI_IN_MISSION_MODE(e) \
21844bba8532Spatrick 	(e == MHI_EE_AMSS || e == MHI_EE_WFW || e == MHI_EE_FP)
21854bba8532Spatrick 
21864bba8532Spatrick /* BHI register bits */
21874bba8532Spatrick #define MHI_BHI_TXDB_SEQNUM_BMSK	GENMASK(29, 0)
21884bba8532Spatrick #define MHI_BHI_TXDB_SEQNUM_SHFT	0
21894bba8532Spatrick #define MHI_BHI_STATUS_MASK		GENMASK(31, 30)
21904bba8532Spatrick #define MHI_BHI_STATUS_SHFT		30
21914bba8532Spatrick #define MHI_BHI_STATUS_ERROR		0x03
21924bba8532Spatrick #define MHI_BHI_STATUS_SUCCESS		0x02
21934bba8532Spatrick #define MHI_BHI_STATUS_RESET		0x00
21944bba8532Spatrick 
21954bba8532Spatrick /* MHI BHIE registers */
21964bba8532Spatrick #define MHI_BHIE_MSMSOCID_OFFS		0x00
21974bba8532Spatrick #define MHI_BHIE_RXVECADDR_LOW_OFFS	0x60
21984bba8532Spatrick #define MHI_BHIE_RXVECADDR_HIGH_OFFS	0x64
21994bba8532Spatrick #define MHI_BHIE_RXVECSIZE_OFFS		0x68
22004bba8532Spatrick #define MHI_BHIE_RXVECDB_OFFS		0x70
22014bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_OFFS	0x78
22024bba8532Spatrick 
22034bba8532Spatrick /* BHIE register bits */
22044bba8532Spatrick #define MHI_BHIE_TXVECDB_SEQNUM_BMSK		GENMASK(29, 0)
22054bba8532Spatrick #define MHI_BHIE_TXVECDB_SEQNUM_SHFT		0
22064bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_SEQNUM_BMSK	GENMASK(29, 0)
22074bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_SEQNUM_SHFT	0
22084bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_STATUS_BMSK	GENMASK(31, 30)
22094bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_STATUS_SHFT	30
22104bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_STATUS_RESET	0x00
22114bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_STATUS_XFER_COMPL	0x02
22124bba8532Spatrick #define MHI_BHIE_TXVECSTATUS_STATUS_ERROR	0x03
22134bba8532Spatrick #define MHI_BHIE_RXVECDB_SEQNUM_BMSK		GENMASK(29, 0)
22144bba8532Spatrick #define MHI_BHIE_RXVECDB_SEQNUM_SHFT		0
22154bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_SEQNUM_BMSK	GENMASK(29, 0)
22164bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_SEQNUM_SHFT	0
22174bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_STATUS_BMSK	GENMASK(31, 30)
22184bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_STATUS_SHFT	30
22194bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_STATUS_RESET	0x00
22204bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_STATUS_XFER_COMPL	0x02
22214bba8532Spatrick #define MHI_BHIE_RXVECSTATUS_STATUS_ERROR	0x03
22224bba8532Spatrick 
22234bba8532Spatrick #define MHI_EV_CC_INVALID	0x0
22244bba8532Spatrick #define MHI_EV_CC_SUCCESS	0x1
22254bba8532Spatrick #define MHI_EV_CC_EOT		0x2
22264bba8532Spatrick #define MHI_EV_CC_OVERFLOW	0x3
22274bba8532Spatrick #define MHI_EV_CC_EOB		0x4
22284bba8532Spatrick #define MHI_EV_CC_OOB		0x5
22294bba8532Spatrick #define MHI_EV_CC_DB_MODE	0x6
22304bba8532Spatrick #define MHI_EV_CC_UNDEFINED_ERR	0x10
22314bba8532Spatrick #define MHI_EV_CC_BAD_TRE	0x11
22324bba8532Spatrick 
22334bba8532Spatrick #define MHI_CMD_NOP		01
22344bba8532Spatrick #define MHI_CMD_RESET_CHAN	16
22354bba8532Spatrick #define MHI_CMD_STOP_CHAN	17
22364bba8532Spatrick #define MHI_CMD_START_CHAN	18
22374bba8532Spatrick 
22384bba8532Spatrick #define MHI_TRE_CMD_CHID_MASK	GENMASK(31, 24)
22394bba8532Spatrick #define MHI_TRE_CMD_CHID_SHFT	24
22404bba8532Spatrick #define MHI_TRE_CMD_CMDID_MASK	GENMASK(23, 16)
22414bba8532Spatrick #define MHI_TRE_CMD_CMDID_SHFT	16
22424bba8532Spatrick 
22434bba8532Spatrick #define MHI_TRE0_EV_LEN_MASK	GENMASK(15, 0)
22444bba8532Spatrick #define MHI_TRE0_EV_LEN_SHFT	0
22454bba8532Spatrick #define MHI_TRE0_EV_CODE_MASK	GENMASK(31, 24)
22464bba8532Spatrick #define MHI_TRE0_EV_CODE_SHFT	24
22474bba8532Spatrick #define MHI_TRE1_EV_TYPE_MASK	GENMASK(23, 16)
22484bba8532Spatrick #define MHI_TRE1_EV_TYPE_SHFT	16
22494bba8532Spatrick #define MHI_TRE1_EV_CHID_MASK	GENMASK(31, 24)
22504bba8532Spatrick #define MHI_TRE1_EV_CHID_SHFT	24
22514bba8532Spatrick 
22524bba8532Spatrick #define MHI_TRE0_DATA_LEN_MASK	GENMASK(15, 0)
22534bba8532Spatrick #define MHI_TRE0_DATA_LEN_SHFT	0
22544bba8532Spatrick #define MHI_TRE1_DATA_CHAIN	(1 << 0)
22554bba8532Spatrick #define MHI_TRE1_DATA_IEOB	(1 << 8)
22564bba8532Spatrick #define MHI_TRE1_DATA_IEOT	(1 << 9)
22574bba8532Spatrick #define MHI_TRE1_DATA_BEI	(1 << 10)
22584bba8532Spatrick #define MHI_TRE1_DATA_TYPE_MASK		GENMASK(23, 16)
22594bba8532Spatrick #define MHI_TRE1_DATA_TYPE_SHIFT	16
22604bba8532Spatrick #define MHI_TRE1_DATA_TYPE_TRANSFER	0x2
22614bba8532Spatrick 
22624bba8532Spatrick #define MHI_PKT_TYPE_INVALID			0x00
22634bba8532Spatrick #define MHI_PKT_TYPE_NOOP_CMD			0x01
22644bba8532Spatrick #define MHI_PKT_TYPE_TRANSFER			0x02
22654bba8532Spatrick #define MHI_PKT_TYPE_COALESCING			0x08
22664bba8532Spatrick #define MHI_PKT_TYPE_RESET_CHAN_CMD		0x10
22674bba8532Spatrick #define MHI_PKT_TYPE_STOP_CHAN_CMD		0x11
22684bba8532Spatrick #define MHI_PKT_TYPE_START_CHAN_CMD		0x12
22694bba8532Spatrick #define MHI_PKT_TYPE_STATE_CHANGE_EVENT		0x20
22704bba8532Spatrick #define MHI_PKT_TYPE_CMD_COMPLETION_EVENT	0x21
22714bba8532Spatrick #define MHI_PKT_TYPE_TX_EVENT			0x22
22724bba8532Spatrick #define MHI_PKT_TYPE_RSC_TX_EVENT		0x28
22734bba8532Spatrick #define MHI_PKT_TYPE_EE_EVENT			0x40
22744bba8532Spatrick #define MHI_PKT_TYPE_TSYNC_EVENT		0x48
22754bba8532Spatrick #define MHI_PKT_TYPE_BW_REQ_EVENT		0x50
22764bba8532Spatrick 
22774bba8532Spatrick 
22784bba8532Spatrick #define MHI_DMA_VEC_CHUNK_SIZE			524288 /* 512 KB */
22794bba8532Spatrick struct qwz_dma_vec_entry {
22804bba8532Spatrick 	uint64_t paddr;
22814bba8532Spatrick 	uint64_t size;
22824bba8532Spatrick };
22834bba8532Spatrick 
22844bba8532Spatrick void
qwz_mhi_ring_doorbell(struct qwz_softc * sc,uint64_t db_addr,uint64_t val)22854bba8532Spatrick qwz_mhi_ring_doorbell(struct qwz_softc *sc, uint64_t db_addr, uint64_t val)
22864bba8532Spatrick {
22874bba8532Spatrick 	qwz_pci_write(sc, db_addr + 4, val >> 32);
22884bba8532Spatrick 	qwz_pci_write(sc, db_addr, val & 0xffffffff);
22894bba8532Spatrick }
22904bba8532Spatrick 
22914bba8532Spatrick void
qwz_mhi_device_wake(struct qwz_softc * sc)22924bba8532Spatrick qwz_mhi_device_wake(struct qwz_softc *sc)
22934bba8532Spatrick {
22944bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
22954bba8532Spatrick 
22964bba8532Spatrick 	/*
22974bba8532Spatrick 	 * Device wake is async only for now because we do not
22984bba8532Spatrick 	 * keep track of PM state in software.
22994bba8532Spatrick 	 */
23004bba8532Spatrick 	qwz_mhi_ring_doorbell(sc, psc->wake_db, 1);
23014bba8532Spatrick }
23024bba8532Spatrick 
23034bba8532Spatrick void
qwz_mhi_device_zzz(struct qwz_softc * sc)23044bba8532Spatrick qwz_mhi_device_zzz(struct qwz_softc *sc)
23054bba8532Spatrick {
23064bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
23074bba8532Spatrick 
23084bba8532Spatrick 	qwz_mhi_ring_doorbell(sc, psc->wake_db, 0);
23094bba8532Spatrick }
23104bba8532Spatrick 
23114bba8532Spatrick int
qwz_mhi_wake_db_clear_valid(struct qwz_softc * sc)23124bba8532Spatrick qwz_mhi_wake_db_clear_valid(struct qwz_softc *sc)
23134bba8532Spatrick {
23144bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
23154bba8532Spatrick 
23164bba8532Spatrick 	return (psc->mhi_state == MHI_STATE_M0); /* TODO other states? */
23174bba8532Spatrick }
23184bba8532Spatrick 
23194bba8532Spatrick void
qwz_mhi_init_xfer_rings(struct qwz_pci_softc * psc)23204bba8532Spatrick qwz_mhi_init_xfer_rings(struct qwz_pci_softc *psc)
23214bba8532Spatrick {
23224bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
23234bba8532Spatrick 	int i;
23244bba8532Spatrick 	uint32_t chcfg;
23254bba8532Spatrick 	struct qwz_pci_xfer_ring *ring;
23264bba8532Spatrick 	struct qwz_mhi_chan_ctxt *cbase, *c;
23274bba8532Spatrick 
23284bba8532Spatrick 	cbase = (struct qwz_mhi_chan_ctxt *)QWZ_DMA_KVA(psc->chan_ctxt);
23294bba8532Spatrick 	for (i = 0; i < psc->max_chan; i++) {
23304bba8532Spatrick 		c = &cbase[i];
23314bba8532Spatrick 		chcfg = le32toh(c->chcfg);
23324bba8532Spatrick 		chcfg &= ~(MHI_CHAN_CTX_CHSTATE_MASK |
23334bba8532Spatrick 		    MHI_CHAN_CTX_BRSTMODE_MASK |
23344bba8532Spatrick 		    MHI_CHAN_CTX_POLLCFG_MASK);
23354bba8532Spatrick 		chcfg |= (MHI_CHAN_CTX_CHSTATE_DISABLED |
23364bba8532Spatrick 		    (MHI_CHAN_CTX_BRSTMODE_DISABLE <<
23374bba8532Spatrick 		    MHI_CHAN_CTX_BRSTMODE_SHFT));
23384bba8532Spatrick 		c->chcfg = htole32(chcfg);
23394bba8532Spatrick 		c->chtype = htole32(MHI_CHAN_TYPE_INVALID);
23404bba8532Spatrick 		c->erindex = 0;
23414bba8532Spatrick 	}
23424bba8532Spatrick 
23434bba8532Spatrick 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
23444bba8532Spatrick 		ring = &psc->xfer_rings[i];
23454bba8532Spatrick 		KASSERT(ring->mhi_chan_id < psc->max_chan);
23464bba8532Spatrick 		c = &cbase[ring->mhi_chan_id];
23474bba8532Spatrick 		c->chtype = htole32(ring->mhi_chan_direction);
23484bba8532Spatrick 		c->erindex = htole32(ring->mhi_chan_event_ring_index);
23494bba8532Spatrick 		ring->chan_ctxt = c;
23504bba8532Spatrick 	}
23514bba8532Spatrick 
23524bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->chan_ctxt), 0,
23534bba8532Spatrick 	    QWZ_DMA_LEN(psc->chan_ctxt), BUS_DMASYNC_PREWRITE);
23544bba8532Spatrick }
23554bba8532Spatrick 
23564bba8532Spatrick void
qwz_mhi_init_event_rings(struct qwz_pci_softc * psc)23574bba8532Spatrick qwz_mhi_init_event_rings(struct qwz_pci_softc *psc)
23584bba8532Spatrick {
23594bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
23604bba8532Spatrick 	int i;
23614bba8532Spatrick 	uint32_t intmod;
23624bba8532Spatrick 	uint64_t paddr, len;
23634bba8532Spatrick 	struct qwz_pci_event_ring *ring;
23644bba8532Spatrick 	struct qwz_mhi_event_ctxt *c;
23654bba8532Spatrick 
23664bba8532Spatrick 	c = (struct qwz_mhi_event_ctxt *)QWZ_DMA_KVA(psc->event_ctxt);
23674bba8532Spatrick 	for (i = 0; i < nitems(psc->event_rings); i++, c++) {
23684bba8532Spatrick 		ring = &psc->event_rings[i];
23694bba8532Spatrick 
23704bba8532Spatrick 		ring->event_ctxt = c;
23714bba8532Spatrick 
23724bba8532Spatrick 		intmod = le32toh(c->intmod);
23734bba8532Spatrick 		intmod &= ~(MHI_EV_CTX_INTMODC_MASK | MHI_EV_CTX_INTMODT_MASK);
23744bba8532Spatrick 		intmod |= (ring->mhi_er_irq_moderation_ms <<
23754bba8532Spatrick 		    MHI_EV_CTX_INTMODT_SHFT) & MHI_EV_CTX_INTMODT_MASK;
23764bba8532Spatrick 		c->intmod = htole32(intmod);
23774bba8532Spatrick 
23784bba8532Spatrick 		c->ertype = htole32(MHI_ER_TYPE_VALID);
23794bba8532Spatrick 		c->msivec = htole32(ring->mhi_er_irq);
23804bba8532Spatrick 
23814bba8532Spatrick 		paddr = QWZ_DMA_DVA(ring->dmamem);
23824bba8532Spatrick 		ring->rp = paddr;
23834bba8532Spatrick 		ring->wp = paddr + ring->size -
23844bba8532Spatrick 		    sizeof(struct qwz_mhi_ring_element);
23854bba8532Spatrick 		c->rbase = htole64(paddr);
23864bba8532Spatrick 		c->rp = htole64(ring->rp);
23874bba8532Spatrick 		c->wp = htole64(ring->wp);
23884bba8532Spatrick 
23894bba8532Spatrick 		len = sizeof(struct qwz_mhi_ring_element) * ring->num_elements;
23904bba8532Spatrick 		c->rlen = htole64(len);
23914bba8532Spatrick 	}
23924bba8532Spatrick 
23934bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->event_ctxt), 0,
23944bba8532Spatrick 	    QWZ_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_PREWRITE);
23954bba8532Spatrick }
23964bba8532Spatrick 
23974bba8532Spatrick void
qwz_mhi_init_cmd_ring(struct qwz_pci_softc * psc)23984bba8532Spatrick qwz_mhi_init_cmd_ring(struct qwz_pci_softc *psc)
23994bba8532Spatrick {
24004bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
24014bba8532Spatrick 	struct qwz_pci_cmd_ring *ring = &psc->cmd_ring;
24024bba8532Spatrick 	struct qwz_mhi_cmd_ctxt *c;
24034bba8532Spatrick 	uint64_t paddr, len;
24044bba8532Spatrick 
24054bba8532Spatrick 	paddr = QWZ_DMA_DVA(ring->dmamem);
24064bba8532Spatrick 	len = ring->size;
24074bba8532Spatrick 
24084bba8532Spatrick 	ring->rp = ring->wp = paddr;
24094bba8532Spatrick 
24104bba8532Spatrick 	c = (struct qwz_mhi_cmd_ctxt *)QWZ_DMA_KVA(psc->cmd_ctxt);
24114bba8532Spatrick 	c->rbase = htole64(paddr);
24124bba8532Spatrick 	c->rp = htole64(paddr);
24134bba8532Spatrick 	c->wp = htole64(paddr);
24144bba8532Spatrick 	c->rlen = htole64(len);
24154bba8532Spatrick 
24164bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->cmd_ctxt), 0,
24174bba8532Spatrick 	    QWZ_DMA_LEN(psc->cmd_ctxt), BUS_DMASYNC_PREWRITE);
24184bba8532Spatrick }
24194bba8532Spatrick 
24204bba8532Spatrick void
qwz_mhi_init_dev_ctxt(struct qwz_pci_softc * psc)24214bba8532Spatrick qwz_mhi_init_dev_ctxt(struct qwz_pci_softc *psc)
24224bba8532Spatrick {
24234bba8532Spatrick 	qwz_mhi_init_xfer_rings(psc);
24244bba8532Spatrick 	qwz_mhi_init_event_rings(psc);
24254bba8532Spatrick 	qwz_mhi_init_cmd_ring(psc);
24264bba8532Spatrick }
24274bba8532Spatrick 
24284bba8532Spatrick void *
qwz_pci_cmd_ring_get_elem(struct qwz_pci_cmd_ring * ring,uint64_t ptr)24294bba8532Spatrick qwz_pci_cmd_ring_get_elem(struct qwz_pci_cmd_ring *ring, uint64_t ptr)
24304bba8532Spatrick {
24314bba8532Spatrick 	uint64_t base = QWZ_DMA_DVA(ring->dmamem), offset;
24324bba8532Spatrick 
24334bba8532Spatrick 	if (ptr < base || ptr >= base + ring->size)
24344bba8532Spatrick 		return NULL;
24354bba8532Spatrick 
24364bba8532Spatrick 	offset = ptr - base;
24374bba8532Spatrick 	if (offset >= ring->size)
24384bba8532Spatrick 		return NULL;
24394bba8532Spatrick 
24404bba8532Spatrick 	return QWZ_DMA_KVA(ring->dmamem) + offset;
24414bba8532Spatrick }
24424bba8532Spatrick 
24434bba8532Spatrick int
qwz_mhi_cmd_ring_submit(struct qwz_pci_softc * psc,struct qwz_pci_cmd_ring * ring)24444bba8532Spatrick qwz_mhi_cmd_ring_submit(struct qwz_pci_softc *psc,
24454bba8532Spatrick     struct qwz_pci_cmd_ring *ring)
24464bba8532Spatrick {
24474bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
24484bba8532Spatrick 	uint64_t base = QWZ_DMA_DVA(ring->dmamem);
24494bba8532Spatrick 	struct qwz_mhi_cmd_ctxt *c;
24504bba8532Spatrick 
24514bba8532Spatrick 	if (ring->queued >= ring->num_elements)
24524bba8532Spatrick 		return 1;
24534bba8532Spatrick 
24544bba8532Spatrick 	if (ring->wp + sizeof(struct qwz_mhi_ring_element) >= base + ring->size)
24554bba8532Spatrick 		ring->wp = base;
24564bba8532Spatrick 	else
24574bba8532Spatrick 		ring->wp += sizeof(struct qwz_mhi_ring_element);
24584bba8532Spatrick 
24594bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->cmd_ctxt), 0,
24604bba8532Spatrick 	    QWZ_DMA_LEN(psc->cmd_ctxt), BUS_DMASYNC_POSTREAD);
24614bba8532Spatrick 
24624bba8532Spatrick 	c = (struct qwz_mhi_cmd_ctxt *)QWZ_DMA_KVA(psc->cmd_ctxt);
24634bba8532Spatrick 	c->wp = htole64(ring->wp);
24644bba8532Spatrick 
24654bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->cmd_ctxt), 0,
24664bba8532Spatrick 	    QWZ_DMA_LEN(psc->cmd_ctxt), BUS_DMASYNC_PREWRITE);
24674bba8532Spatrick 
24684bba8532Spatrick 	ring->queued++;
24694bba8532Spatrick 	qwz_mhi_ring_doorbell(sc, MHI_CRDB_LOWER, ring->wp);
24704bba8532Spatrick 	return 0;
24714bba8532Spatrick }
24724bba8532Spatrick 
24734bba8532Spatrick int
qwz_mhi_send_cmd(struct qwz_pci_softc * psc,uint32_t cmd,uint32_t chan)24744bba8532Spatrick qwz_mhi_send_cmd(struct qwz_pci_softc *psc, uint32_t cmd, uint32_t chan)
24754bba8532Spatrick {
24764bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
24774bba8532Spatrick 	struct qwz_pci_cmd_ring	*ring = &psc->cmd_ring;
24784bba8532Spatrick 	struct qwz_mhi_ring_element *e;
24794bba8532Spatrick 
24804bba8532Spatrick 	if (ring->queued >= ring->num_elements) {
24814bba8532Spatrick 		printf("%s: command ring overflow\n", sc->sc_dev.dv_xname);
24824bba8532Spatrick 		return 1;
24834bba8532Spatrick 	}
24844bba8532Spatrick 
24854bba8532Spatrick 	e = qwz_pci_cmd_ring_get_elem(ring, ring->wp);
24864bba8532Spatrick 	if (e == NULL)
24874bba8532Spatrick 		return 1;
24884bba8532Spatrick 
24894bba8532Spatrick 	e->ptr = 0ULL;
24904bba8532Spatrick 	e->dword[0] = 0;
24914bba8532Spatrick 	e->dword[1] = htole32(
24924bba8532Spatrick 	    ((chan << MHI_TRE_CMD_CHID_SHFT) & MHI_TRE_CMD_CHID_MASK) |
24934bba8532Spatrick 	    ((cmd << MHI_TRE_CMD_CMDID_SHFT) & MHI_TRE_CMD_CMDID_MASK));
24944bba8532Spatrick 
24954bba8532Spatrick 	return qwz_mhi_cmd_ring_submit(psc, ring);
24964bba8532Spatrick }
24974bba8532Spatrick 
24984bba8532Spatrick void *
qwz_pci_xfer_ring_get_elem(struct qwz_pci_xfer_ring * ring,uint64_t wp)24994bba8532Spatrick qwz_pci_xfer_ring_get_elem(struct qwz_pci_xfer_ring *ring, uint64_t wp)
25004bba8532Spatrick {
25014bba8532Spatrick 	uint64_t base = QWZ_DMA_DVA(ring->dmamem), offset;
25024bba8532Spatrick 	void *addr = QWZ_DMA_KVA(ring->dmamem);
25034bba8532Spatrick 
25044bba8532Spatrick 	if (wp < base)
25054bba8532Spatrick 		return NULL;
25064bba8532Spatrick 
25074bba8532Spatrick 	offset = wp - base;
25084bba8532Spatrick 	if (offset >= ring->size)
25094bba8532Spatrick 		return NULL;
25104bba8532Spatrick 
25114bba8532Spatrick 	return addr + offset;
25124bba8532Spatrick }
25134bba8532Spatrick 
25144bba8532Spatrick struct qwz_xfer_data *
qwz_pci_xfer_ring_get_data(struct qwz_pci_xfer_ring * ring,uint64_t wp)25154bba8532Spatrick qwz_pci_xfer_ring_get_data(struct qwz_pci_xfer_ring *ring, uint64_t wp)
25164bba8532Spatrick {
25174bba8532Spatrick 	uint64_t base = QWZ_DMA_DVA(ring->dmamem), offset;
25184bba8532Spatrick 
25194bba8532Spatrick 	if (wp < base)
25204bba8532Spatrick 		return NULL;
25214bba8532Spatrick 
25224bba8532Spatrick 	offset = wp - base;
25234bba8532Spatrick 	if (offset >= ring->size)
25244bba8532Spatrick 		return NULL;
25254bba8532Spatrick 
25264bba8532Spatrick 	return &ring->data[offset / sizeof(ring->data[0])];
25274bba8532Spatrick }
25284bba8532Spatrick 
25294bba8532Spatrick int
qwz_mhi_submit_xfer(struct qwz_softc * sc,struct mbuf * m)25304bba8532Spatrick qwz_mhi_submit_xfer(struct qwz_softc *sc, struct mbuf *m)
25314bba8532Spatrick {
25324bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
25334bba8532Spatrick 	struct qwz_pci_xfer_ring *ring;
25344bba8532Spatrick 	struct qwz_mhi_ring_element *e;
25354bba8532Spatrick 	struct qwz_xfer_data *xfer;
25364bba8532Spatrick 	uint64_t paddr, base;
25374bba8532Spatrick 	int err;
25384bba8532Spatrick 
25394bba8532Spatrick 	ring = &psc->xfer_rings[QWZ_PCI_XFER_RING_IPCR_OUTBOUND];
25404bba8532Spatrick 
25414bba8532Spatrick 	if (ring->queued >= ring->num_elements)
25424bba8532Spatrick 		return 1;
25434bba8532Spatrick 
25444bba8532Spatrick 	if (m->m_pkthdr.len > QWZ_PCI_XFER_MAX_DATA_SIZE) {
25454bba8532Spatrick 		/* TODO: chunk xfers */
25464bba8532Spatrick 		printf("%s: xfer too large: %d bytes\n", __func__, m->m_pkthdr.len);
25474bba8532Spatrick 		return 1;
25484bba8532Spatrick 
25494bba8532Spatrick 	}
25504bba8532Spatrick 
25514bba8532Spatrick 	e = qwz_pci_xfer_ring_get_elem(ring, ring->wp);
25524bba8532Spatrick 	if (e == NULL)
25534bba8532Spatrick 		return 1;
25544bba8532Spatrick 
25554bba8532Spatrick 	xfer = qwz_pci_xfer_ring_get_data(ring, ring->wp);
25564bba8532Spatrick 	if (xfer == NULL || xfer->m != NULL)
25574bba8532Spatrick 		return 1;
25584bba8532Spatrick 
25594bba8532Spatrick 	err = bus_dmamap_load_mbuf(sc->sc_dmat, xfer->map, m,
25604bba8532Spatrick 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
25614bba8532Spatrick 	if (err && err != EFBIG) {
25624bba8532Spatrick 		printf("%s: can't map mbuf (error %d)\n",
25634bba8532Spatrick 		    sc->sc_dev.dv_xname, err);
25644bba8532Spatrick 		return err;
25654bba8532Spatrick 	}
25664bba8532Spatrick 	if (err) {
25674bba8532Spatrick 		/* Too many DMA segments, linearize mbuf. */
25684bba8532Spatrick 		if (m_defrag(m, M_DONTWAIT))
25694bba8532Spatrick 			return ENOBUFS;
25704bba8532Spatrick 		err = bus_dmamap_load_mbuf(sc->sc_dmat, xfer->map, m,
25714bba8532Spatrick 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
25724bba8532Spatrick 		if (err) {
25734bba8532Spatrick 			printf("%s: can't map mbuf (error %d)\n",
25744bba8532Spatrick 			    sc->sc_dev.dv_xname, err);
25754bba8532Spatrick 			return err;
25764bba8532Spatrick 		}
25774bba8532Spatrick 	}
25784bba8532Spatrick 
25794bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, xfer->map, 0, m->m_pkthdr.len,
25804bba8532Spatrick 	    BUS_DMASYNC_PREWRITE);
25814bba8532Spatrick 
25824bba8532Spatrick 	xfer->m = m;
25834bba8532Spatrick 	paddr = xfer->map->dm_segs[0].ds_addr;
25844bba8532Spatrick 
25854bba8532Spatrick 	e->ptr = htole64(paddr);
25864bba8532Spatrick 	e->dword[0] = htole32((m->m_pkthdr.len << MHI_TRE0_DATA_LEN_SHFT) &
25874bba8532Spatrick 	    MHI_TRE0_DATA_LEN_MASK);
25884bba8532Spatrick 	e->dword[1] = htole32(MHI_TRE1_DATA_IEOT |
25894bba8532Spatrick 	    MHI_TRE1_DATA_TYPE_TRANSFER << MHI_TRE1_DATA_TYPE_SHIFT);
25904bba8532Spatrick 
25914bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(ring->dmamem),
25924bba8532Spatrick 	    0, QWZ_DMA_LEN(ring->dmamem), BUS_DMASYNC_PREWRITE);
25934bba8532Spatrick 
25944bba8532Spatrick 	base = QWZ_DMA_DVA(ring->dmamem);
25954bba8532Spatrick 	if (ring->wp + sizeof(struct qwz_mhi_ring_element) >= base + ring->size)
25964bba8532Spatrick 		ring->wp = base;
25974bba8532Spatrick 	else
25984bba8532Spatrick 		ring->wp += sizeof(struct qwz_mhi_ring_element);
25994bba8532Spatrick 	ring->queued++;
26004bba8532Spatrick 
26014bba8532Spatrick 	ring->chan_ctxt->wp = htole64(ring->wp);
26024bba8532Spatrick 
26034bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->chan_ctxt), 0,
26044bba8532Spatrick 	    QWZ_DMA_LEN(psc->chan_ctxt), BUS_DMASYNC_PREWRITE);
26054bba8532Spatrick 
26064bba8532Spatrick 	qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
26074bba8532Spatrick 	return 0;
26084bba8532Spatrick }
26094bba8532Spatrick 
26104bba8532Spatrick int
qwz_mhi_start_channel(struct qwz_pci_softc * psc,struct qwz_pci_xfer_ring * ring)26114bba8532Spatrick qwz_mhi_start_channel(struct qwz_pci_softc *psc,
26124bba8532Spatrick 	struct qwz_pci_xfer_ring *ring)
26134bba8532Spatrick {
26144bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
26154bba8532Spatrick 	struct qwz_mhi_chan_ctxt *c;
26164bba8532Spatrick 	int ret = 0;
26174bba8532Spatrick 	uint32_t chcfg;
26184bba8532Spatrick 	uint64_t paddr, len;
26194bba8532Spatrick 
26204bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: start MHI channel %d in state %d\n", __func__,
26214bba8532Spatrick 	    ring->mhi_chan_id, ring->mhi_chan_state);
26224bba8532Spatrick 
26234bba8532Spatrick 	c = ring->chan_ctxt;
26244bba8532Spatrick 
26254bba8532Spatrick 	chcfg = le32toh(c->chcfg);
26264bba8532Spatrick 	chcfg &= ~MHI_CHAN_CTX_CHSTATE_MASK;
26274bba8532Spatrick 	chcfg |= MHI_CHAN_CTX_CHSTATE_ENABLED;
26284bba8532Spatrick 	c->chcfg = htole32(chcfg);
26294bba8532Spatrick 
26304bba8532Spatrick 	paddr = QWZ_DMA_DVA(ring->dmamem);
26314bba8532Spatrick 	ring->rp = ring->wp = paddr;
26324bba8532Spatrick 	c->rbase = htole64(paddr);
26334bba8532Spatrick 	c->rp = htole64(ring->rp);
26344bba8532Spatrick 	c->wp = htole64(ring->wp);
26354bba8532Spatrick 	len = sizeof(struct qwz_mhi_ring_element) * ring->num_elements;
26364bba8532Spatrick 	c->rlen = htole64(len);
26374bba8532Spatrick 
26384bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->chan_ctxt), 0,
26394bba8532Spatrick 	    QWZ_DMA_LEN(psc->chan_ctxt), BUS_DMASYNC_PREWRITE);
26404bba8532Spatrick 
26414bba8532Spatrick 	ring->cmd_status = MHI_EV_CC_INVALID;
26424bba8532Spatrick 	if (qwz_mhi_send_cmd(psc, MHI_CMD_START_CHAN, ring->mhi_chan_id))
26434bba8532Spatrick 		return 1;
26444bba8532Spatrick 
26454bba8532Spatrick 	while (ring->cmd_status != MHI_EV_CC_SUCCESS) {
26464bba8532Spatrick 		ret = tsleep_nsec(&ring->cmd_status, 0, "qwzcmd",
26474bba8532Spatrick 		    SEC_TO_NSEC(5));
26484bba8532Spatrick 		if (ret)
26494bba8532Spatrick 			break;
26504bba8532Spatrick 	}
26514bba8532Spatrick 
26524bba8532Spatrick 	if (ret) {
26534bba8532Spatrick 		printf("%s: could not start MHI channel %d in state %d: status 0x%x\n",
26544bba8532Spatrick 		    sc->sc_dev.dv_xname, ring->mhi_chan_id,
26554bba8532Spatrick 		    ring->mhi_chan_state, ring->cmd_status);
26564bba8532Spatrick 		return 1;
26574bba8532Spatrick 	}
26584bba8532Spatrick 
26594bba8532Spatrick 	if (ring->mhi_chan_direction == MHI_CHAN_TYPE_INBOUND) {
26604bba8532Spatrick 		uint64_t wp = QWZ_DMA_DVA(ring->dmamem);
26614bba8532Spatrick 		int i;
26624bba8532Spatrick 
26634bba8532Spatrick 		for (i = 0; i < ring->num_elements; i++) {
26644bba8532Spatrick 			struct qwz_mhi_ring_element *e;
26654bba8532Spatrick 			struct qwz_xfer_data *xfer;
26664bba8532Spatrick 			uint64_t paddr;
26674bba8532Spatrick 
26684bba8532Spatrick 			e = qwz_pci_xfer_ring_get_elem(ring, wp);
26694bba8532Spatrick 			xfer = qwz_pci_xfer_ring_get_data(ring, wp);
26704bba8532Spatrick 			paddr = xfer->map->dm_segs[0].ds_addr;
26714bba8532Spatrick 
26724bba8532Spatrick 			e->ptr = htole64(paddr);
26734bba8532Spatrick 			e->dword[0] = htole32((QWZ_PCI_XFER_MAX_DATA_SIZE <<
26744bba8532Spatrick 			    MHI_TRE0_DATA_LEN_SHFT) &
26754bba8532Spatrick 			    MHI_TRE0_DATA_LEN_MASK);
26764bba8532Spatrick 			e->dword[1] = htole32(MHI_TRE1_DATA_IEOT |
26774bba8532Spatrick 			    MHI_TRE1_DATA_BEI |
26784bba8532Spatrick 			    MHI_TRE1_DATA_TYPE_TRANSFER <<
26794bba8532Spatrick 			    MHI_TRE1_DATA_TYPE_SHIFT);
26804bba8532Spatrick 
26814bba8532Spatrick 			ring->wp = wp;
26824bba8532Spatrick 			wp += sizeof(*e);
26834bba8532Spatrick 		}
26844bba8532Spatrick 
26854bba8532Spatrick 		bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(ring->dmamem), 0,
26864bba8532Spatrick 		    QWZ_DMA_LEN(ring->dmamem), BUS_DMASYNC_PREWRITE);
26874bba8532Spatrick 
26884bba8532Spatrick 		qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
26894bba8532Spatrick 	}
26904bba8532Spatrick 
26914bba8532Spatrick 	return 0;
26924bba8532Spatrick }
26934bba8532Spatrick 
26944bba8532Spatrick int
qwz_mhi_start_channels(struct qwz_pci_softc * psc)26954bba8532Spatrick qwz_mhi_start_channels(struct qwz_pci_softc *psc)
26964bba8532Spatrick {
26974bba8532Spatrick 	struct qwz_pci_xfer_ring *ring;
26984bba8532Spatrick 	int ret = 0;
26994bba8532Spatrick 
27004bba8532Spatrick 	qwz_mhi_device_wake(&psc->sc_sc);
27014bba8532Spatrick 
27024bba8532Spatrick 	ring = &psc->xfer_rings[QWZ_PCI_XFER_RING_IPCR_OUTBOUND];
27034bba8532Spatrick 	if (qwz_mhi_start_channel(psc, ring)) {
27044bba8532Spatrick 		ret = 1;
27054bba8532Spatrick 		goto done;
27064bba8532Spatrick 	}
27074bba8532Spatrick 
27084bba8532Spatrick 	ring = &psc->xfer_rings[QWZ_PCI_XFER_RING_IPCR_INBOUND];
27094bba8532Spatrick 	if (qwz_mhi_start_channel(psc, ring))
27104bba8532Spatrick 		ret = 1;
27114bba8532Spatrick done:
27124bba8532Spatrick 	qwz_mhi_device_zzz(&psc->sc_sc);
27134bba8532Spatrick 	return ret;
27144bba8532Spatrick }
27154bba8532Spatrick 
27164bba8532Spatrick int
qwz_mhi_start(struct qwz_pci_softc * psc)27174bba8532Spatrick qwz_mhi_start(struct qwz_pci_softc *psc)
27184bba8532Spatrick {
27194bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
27204bba8532Spatrick 	uint32_t off;
27214bba8532Spatrick 	uint32_t ee, state;
27224bba8532Spatrick 	int ret;
27234bba8532Spatrick 
27244bba8532Spatrick 	qwz_mhi_init_dev_ctxt(psc);
27254bba8532Spatrick 
27264bba8532Spatrick 	psc->bhi_off = qwz_pci_read(sc, MHI_BHI_OFFSET);
27274bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: BHI offset 0x%x\n", __func__, psc->bhi_off);
27284bba8532Spatrick 
27294bba8532Spatrick 	psc->bhie_off = qwz_pci_read(sc, MHI_BHIE_OFFSET);
27304bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: BHIE offset 0x%x\n", __func__, psc->bhie_off);
27314bba8532Spatrick 
27324bba8532Spatrick 	/* Clean BHIE RX registers */
27334bba8532Spatrick 	for (off = MHI_BHIE_RXVECADDR_LOW_OFFS;
27344bba8532Spatrick 	     off < (MHI_BHIE_RXVECSTATUS_OFFS - 4);
27354bba8532Spatrick 	     off += 4)
27364bba8532Spatrick 	     	qwz_pci_write(sc, psc->bhie_off + off, 0x0);
27374bba8532Spatrick 
27384bba8532Spatrick 	qwz_rddm_prepare(psc);
27394bba8532Spatrick 
27404bba8532Spatrick 	/* Program BHI INTVEC */
27414bba8532Spatrick 	qwz_pci_write(sc, psc->bhi_off + MHI_BHI_INTVEC, 0x00);
27424bba8532Spatrick 
27434bba8532Spatrick 	/*
27444bba8532Spatrick 	 * Get BHI execution environment and confirm that it is valid
27454bba8532Spatrick 	 * for power on.
27464bba8532Spatrick 	 */
27474bba8532Spatrick 	ee = qwz_pci_read(sc, psc->bhi_off + MHI_BHI_EXECENV);
27484bba8532Spatrick 	if (!MHI_POWER_UP_CAPABLE(ee)) {
27494bba8532Spatrick 		printf("%s: invalid EE for power on: 0x%x\n",
27504bba8532Spatrick 		     sc->sc_dev.dv_xname, ee);
27514bba8532Spatrick 		return 1;
27524bba8532Spatrick 	}
27534bba8532Spatrick 
27544bba8532Spatrick 	/*
27554bba8532Spatrick 	 * Get MHI state of the device and reset it if it is in system
27564bba8532Spatrick 	 * error.
27574bba8532Spatrick 	 */
27584bba8532Spatrick 	state = qwz_pci_read(sc, MHI_STATUS);
27594bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: MHI power on with EE: 0x%x, status: 0x%x\n",
27604bba8532Spatrick 	     sc->sc_dev.dv_xname, ee, state);
27614bba8532Spatrick 	state = (state & MHI_STATUS_MHISTATE_MASK) >> MHI_STATUS_MHISTATE_SHFT;
27624bba8532Spatrick 	if (state == MHI_STATE_SYS_ERR) {
27634bba8532Spatrick 		if (qwz_mhi_reset_device(sc, 0))
27644bba8532Spatrick 			return 1;
27654bba8532Spatrick 		state = qwz_pci_read(sc, MHI_STATUS);
27664bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: MHI state after reset: 0x%x\n",
27674bba8532Spatrick 		    sc->sc_dev.dv_xname, state);
27684bba8532Spatrick 		state = (state & MHI_STATUS_MHISTATE_MASK) >>
27694bba8532Spatrick 		    MHI_STATUS_MHISTATE_SHFT;
27704bba8532Spatrick 		if (state == MHI_STATE_SYS_ERR) {
27714bba8532Spatrick 			printf("%s: MHI stuck in system error state\n",
27724bba8532Spatrick 			    sc->sc_dev.dv_xname);
27734bba8532Spatrick 			return 1;
27744bba8532Spatrick 		}
27754bba8532Spatrick 	}
27764bba8532Spatrick 
27774bba8532Spatrick 	psc->bhi_ee = ee;
27784bba8532Spatrick 	psc->mhi_state = state;
27794bba8532Spatrick 
27804bba8532Spatrick #if notyet
27814bba8532Spatrick 	/* Enable IRQs */
27824bba8532Spatrick 	//  XXX todo?
27834bba8532Spatrick #endif
27844bba8532Spatrick 
27854bba8532Spatrick 	/* Transition to primary runtime. */
27864bba8532Spatrick 	if (MHI_IN_PBL(ee)) {
27874bba8532Spatrick 		ret = qwz_mhi_fw_load_handler(psc);
27884bba8532Spatrick 		if (ret)
27894bba8532Spatrick 			return ret;
27904bba8532Spatrick 
27914bba8532Spatrick 		/* XXX without this delay starting the channels may fail */
27924bba8532Spatrick 		delay(1000);
27934bba8532Spatrick 		qwz_mhi_start_channels(psc);
27944bba8532Spatrick 	} else {
27954bba8532Spatrick 		/* XXX Handle partially initialized device...?!? */
27964bba8532Spatrick 		ee = qwz_pci_read(sc, psc->bhi_off + MHI_BHI_EXECENV);
27974bba8532Spatrick 		if (!MHI_IN_MISSION_MODE(ee)) {
27984bba8532Spatrick 			printf("%s: failed to power up MHI, ee=0x%x\n",
27994bba8532Spatrick 			    sc->sc_dev.dv_xname, ee);
28004bba8532Spatrick 			return EIO;
28014bba8532Spatrick 		}
28024bba8532Spatrick 	}
28034bba8532Spatrick 
28044bba8532Spatrick 	return 0;
28054bba8532Spatrick }
28064bba8532Spatrick 
28074bba8532Spatrick void
qwz_mhi_stop(struct qwz_softc * sc)28084bba8532Spatrick qwz_mhi_stop(struct qwz_softc *sc)
28094bba8532Spatrick {
28104bba8532Spatrick 	qwz_mhi_reset_device(sc, 1);
28114bba8532Spatrick }
28124bba8532Spatrick 
28134bba8532Spatrick int
qwz_mhi_reset_device(struct qwz_softc * sc,int force)28144bba8532Spatrick qwz_mhi_reset_device(struct qwz_softc *sc, int force)
28154bba8532Spatrick {
28164bba8532Spatrick 	struct qwz_pci_softc *psc = (struct qwz_pci_softc *)sc;
28174bba8532Spatrick 	uint32_t reg;
28184bba8532Spatrick 	int ret = 0;
28194bba8532Spatrick 
28204bba8532Spatrick 	reg = qwz_pcic_read32(sc, MHI_STATUS);
28214bba8532Spatrick 
28224bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: MHISTATUS 0x%x\n", sc->sc_dev.dv_xname, reg);
28234bba8532Spatrick 	/*
28244bba8532Spatrick 	 * Observed on QCA6390 that after SOC_GLOBAL_RESET, MHISTATUS
28254bba8532Spatrick 	 * has SYSERR bit set and thus need to set MHICTRL_RESET
28264bba8532Spatrick 	 * to clear SYSERR.
28274bba8532Spatrick 	 */
28284bba8532Spatrick 	if (force || (reg & MHI_STATUS_SYSERR_MASK)) {
28294bba8532Spatrick 		/* Trigger MHI Reset in device. */
28304bba8532Spatrick 		qwz_pcic_write32(sc, MHI_CTRL, MHI_CTRL_RESET_MASK);
28314bba8532Spatrick 
28324bba8532Spatrick 		/* Wait for the reset bit to be cleared by the device. */
28334bba8532Spatrick 		ret = qwz_mhi_await_device_reset(sc);
28344bba8532Spatrick 		if (ret)
28354bba8532Spatrick 			return ret;
28364bba8532Spatrick 
28374bba8532Spatrick 		if (psc->bhi_off == 0)
28384bba8532Spatrick 			psc->bhi_off = qwz_pci_read(sc, MHI_BHI_OFFSET);
28394bba8532Spatrick 
28404bba8532Spatrick 		/* Device clear BHI INTVEC so re-program it. */
28414bba8532Spatrick 		qwz_pci_write(sc, psc->bhi_off + MHI_BHI_INTVEC, 0x00);
28424bba8532Spatrick 	}
28434bba8532Spatrick 
28444bba8532Spatrick 	return 0;
28454bba8532Spatrick }
28464bba8532Spatrick 
28474bba8532Spatrick static inline void
qwz_mhi_reset_txvecdb(struct qwz_softc * sc)28484bba8532Spatrick qwz_mhi_reset_txvecdb(struct qwz_softc *sc)
28494bba8532Spatrick {
28504bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_TXVECDB, 0);
28514bba8532Spatrick }
28524bba8532Spatrick 
28534bba8532Spatrick static inline void
qwz_mhi_reset_txvecstatus(struct qwz_softc * sc)28544bba8532Spatrick qwz_mhi_reset_txvecstatus(struct qwz_softc *sc)
28554bba8532Spatrick {
28564bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_TXVECSTATUS, 0);
28574bba8532Spatrick }
28584bba8532Spatrick 
28594bba8532Spatrick static inline void
qwz_mhi_reset_rxvecdb(struct qwz_softc * sc)28604bba8532Spatrick qwz_mhi_reset_rxvecdb(struct qwz_softc *sc)
28614bba8532Spatrick {
28624bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_RXVECDB, 0);
28634bba8532Spatrick }
28644bba8532Spatrick 
28654bba8532Spatrick static inline void
qwz_mhi_reset_rxvecstatus(struct qwz_softc * sc)28664bba8532Spatrick qwz_mhi_reset_rxvecstatus(struct qwz_softc *sc)
28674bba8532Spatrick {
28684bba8532Spatrick 	qwz_pcic_write32(sc, PCIE_RXVECSTATUS, 0);
28694bba8532Spatrick }
28704bba8532Spatrick 
28714bba8532Spatrick void
qwz_mhi_clear_vector(struct qwz_softc * sc)28724bba8532Spatrick qwz_mhi_clear_vector(struct qwz_softc *sc)
28734bba8532Spatrick {
28744bba8532Spatrick 	qwz_mhi_reset_txvecdb(sc);
28754bba8532Spatrick 	qwz_mhi_reset_txvecstatus(sc);
28764bba8532Spatrick 	qwz_mhi_reset_rxvecdb(sc);
28774bba8532Spatrick 	qwz_mhi_reset_rxvecstatus(sc);
28784bba8532Spatrick }
28794bba8532Spatrick 
28804bba8532Spatrick int
qwz_mhi_fw_load_handler(struct qwz_pci_softc * psc)28814bba8532Spatrick qwz_mhi_fw_load_handler(struct qwz_pci_softc *psc)
28824bba8532Spatrick {
28834bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
28844bba8532Spatrick 	int ret;
28854bba8532Spatrick 	char amss_path[PATH_MAX];
28864bba8532Spatrick 	u_char *data;
28874bba8532Spatrick 	size_t len;
28884bba8532Spatrick 
28894bba8532Spatrick 	if (sc->fw_img[QWZ_FW_AMSS].data) {
28904bba8532Spatrick 		data = sc->fw_img[QWZ_FW_AMSS].data;
28914bba8532Spatrick 		len = sc->fw_img[QWZ_FW_AMSS].size;
28924bba8532Spatrick 	} else {
28934bba8532Spatrick 		ret = snprintf(amss_path, sizeof(amss_path), "%s-%s-%s",
28944bba8532Spatrick 		    ATH12K_FW_DIR, sc->hw_params.fw.dir, ATH12K_AMSS_FILE);
28954bba8532Spatrick 		if (ret < 0 || ret >= sizeof(amss_path))
28964bba8532Spatrick 			return ENOSPC;
28974bba8532Spatrick 
28984bba8532Spatrick 		ret = loadfirmware(amss_path, &data, &len);
28994bba8532Spatrick 		if (ret) {
29004bba8532Spatrick 			printf("%s: could not read %s (error %d)\n",
29014bba8532Spatrick 			    sc->sc_dev.dv_xname, amss_path, ret);
29024bba8532Spatrick 			return ret;
29034bba8532Spatrick 		}
29044bba8532Spatrick 
29054bba8532Spatrick 		if (len < MHI_DMA_VEC_CHUNK_SIZE) {
29064bba8532Spatrick 			printf("%s: %s is too short, have only %zu bytes\n",
29074bba8532Spatrick 			    sc->sc_dev.dv_xname, amss_path, len);
29084bba8532Spatrick 			free(data, M_DEVBUF, len);
29094bba8532Spatrick 			return EINVAL;
29104bba8532Spatrick 		}
29114bba8532Spatrick 
29124bba8532Spatrick 		sc->fw_img[QWZ_FW_AMSS].data = data;
29134bba8532Spatrick 		sc->fw_img[QWZ_FW_AMSS].size = len;
29144bba8532Spatrick 	}
29154bba8532Spatrick 
29164bba8532Spatrick 	/* Second-stage boot loader sits in the first 512 KB of image. */
29174bba8532Spatrick 	ret = qwz_mhi_fw_load_bhi(psc, data, MHI_DMA_VEC_CHUNK_SIZE);
29184bba8532Spatrick 	if (ret != 0) {
29194bba8532Spatrick 		printf("%s: could not load firmware %s\n",
29204bba8532Spatrick 		    sc->sc_dev.dv_xname, amss_path);
29214bba8532Spatrick 		return ret;
29224bba8532Spatrick 	}
29234bba8532Spatrick 
29244bba8532Spatrick 	/* Now load the full image. */
29254bba8532Spatrick 	ret = qwz_mhi_fw_load_bhie(psc, data, len);
29264bba8532Spatrick 	if (ret != 0) {
29274bba8532Spatrick 		printf("%s: could not load firmware %s\n",
29284bba8532Spatrick 		    sc->sc_dev.dv_xname, amss_path);
29294bba8532Spatrick 		return ret;
29304bba8532Spatrick 	}
29314bba8532Spatrick 
29324bba8532Spatrick 	while (psc->bhi_ee < MHI_EE_AMSS) {
29334bba8532Spatrick 		ret = tsleep_nsec(&psc->bhi_ee, 0, "qwzamss",
29344bba8532Spatrick 		    SEC_TO_NSEC(5));
29354bba8532Spatrick 		if (ret)
29364bba8532Spatrick 			break;
29374bba8532Spatrick 	}
29384bba8532Spatrick 	if (ret != 0) {
29394bba8532Spatrick 		printf("%s: device failed to enter AMSS EE\n",
29404bba8532Spatrick 		    sc->sc_dev.dv_xname);
29414bba8532Spatrick 	}
29424bba8532Spatrick 
29434bba8532Spatrick 	return ret;
29444bba8532Spatrick }
29454bba8532Spatrick 
29464bba8532Spatrick int
qwz_mhi_await_device_reset(struct qwz_softc * sc)29474bba8532Spatrick qwz_mhi_await_device_reset(struct qwz_softc *sc)
29484bba8532Spatrick {
29494bba8532Spatrick 	const uint32_t msecs = 24, retries = 2;
29504bba8532Spatrick 	uint32_t reg;
29514bba8532Spatrick 	int timeout;
29524bba8532Spatrick 
29534bba8532Spatrick 	/* Poll for CTRL RESET to clear. */
29544bba8532Spatrick 	timeout = retries;
29554bba8532Spatrick 	while (timeout > 0) {
29564bba8532Spatrick 		reg = qwz_pci_read(sc, MHI_CTRL);
29574bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: MHI_CTRL is 0x%x\n", __func__, reg);
29584bba8532Spatrick 		if ((reg & MHI_CTRL_RESET_MASK) == 0)
29594bba8532Spatrick 			break;
29604bba8532Spatrick 		DELAY((msecs / retries) * 1000);
29614bba8532Spatrick 		timeout--;
29624bba8532Spatrick 	}
29634bba8532Spatrick 	if (timeout == 0) {
29644bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: MHI reset failed\n", __func__);
29654bba8532Spatrick 		return ETIMEDOUT;
29664bba8532Spatrick 	}
29674bba8532Spatrick 
29684bba8532Spatrick 	return 0;
29694bba8532Spatrick }
29704bba8532Spatrick 
29714bba8532Spatrick int
qwz_mhi_await_device_ready(struct qwz_softc * sc)29724bba8532Spatrick qwz_mhi_await_device_ready(struct qwz_softc *sc)
29734bba8532Spatrick {
29744bba8532Spatrick 	uint32_t reg;
29754bba8532Spatrick 	int timeout;
29764bba8532Spatrick 	const uint32_t msecs = 2000, retries = 4;
29774bba8532Spatrick 
29784bba8532Spatrick 
29794bba8532Spatrick 	/* Poll for READY to be set. */
29804bba8532Spatrick 	timeout = retries;
29814bba8532Spatrick 	while (timeout > 0) {
29824bba8532Spatrick 		reg = qwz_pci_read(sc, MHI_STATUS);
29834bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: MHI_STATUS is 0x%x\n", __func__, reg);
29844bba8532Spatrick 		if (reg & MHI_STATUS_READY_MASK) {
29854bba8532Spatrick 			reg &= ~MHI_STATUS_READY_MASK;
29864bba8532Spatrick 			qwz_pci_write(sc, MHI_STATUS, reg);
29874bba8532Spatrick 			break;
29884bba8532Spatrick 		}
29894bba8532Spatrick 		DELAY((msecs / retries) * 1000);
29904bba8532Spatrick 		timeout--;
29914bba8532Spatrick 	}
29924bba8532Spatrick 	if (timeout == 0) {
29934bba8532Spatrick 		printf("%s: MHI not ready\n", sc->sc_dev.dv_xname);
29944bba8532Spatrick 		return ETIMEDOUT;
29954bba8532Spatrick 	}
29964bba8532Spatrick 
29974bba8532Spatrick 	return 0;
29984bba8532Spatrick }
29994bba8532Spatrick 
30004bba8532Spatrick void
qwz_mhi_ready_state_transition(struct qwz_pci_softc * psc)30014bba8532Spatrick qwz_mhi_ready_state_transition(struct qwz_pci_softc *psc)
30024bba8532Spatrick {
30034bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
30044bba8532Spatrick 	int ret, i;
30054bba8532Spatrick 
30064bba8532Spatrick 	ret = qwz_mhi_await_device_reset(sc);
30074bba8532Spatrick 	if (ret)
30084bba8532Spatrick 		return;
30094bba8532Spatrick 
30104bba8532Spatrick 	ret = qwz_mhi_await_device_ready(sc);
30114bba8532Spatrick 	if (ret)
30124bba8532Spatrick 		return;
30134bba8532Spatrick 
30144bba8532Spatrick 	/* Set up memory-mapped IO for channels, events, etc. */
30154bba8532Spatrick 	qwz_mhi_init_mmio(psc);
30164bba8532Spatrick 
30174bba8532Spatrick 	/* Notify event rings. */
30184bba8532Spatrick 	for (i = 0; i < nitems(psc->event_rings); i++) {
30194bba8532Spatrick 		struct qwz_pci_event_ring *ring = &psc->event_rings[i];
30204bba8532Spatrick 		qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
30214bba8532Spatrick 	}
30224bba8532Spatrick 
30234bba8532Spatrick 	/*
30244bba8532Spatrick 	 * Set the device into M0 state. The device will transition
30254bba8532Spatrick 	 * into M0 and the execution environment will switch to SBL.
30264bba8532Spatrick 	 */
30274bba8532Spatrick 	qwz_mhi_set_state(sc, MHI_STATE_M0);
30284bba8532Spatrick }
30294bba8532Spatrick 
30304bba8532Spatrick void
qwz_mhi_mission_mode_state_transition(struct qwz_pci_softc * psc)30314bba8532Spatrick qwz_mhi_mission_mode_state_transition(struct qwz_pci_softc *psc)
30324bba8532Spatrick {
30334bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
30344bba8532Spatrick 	int i;
30354bba8532Spatrick 
30364bba8532Spatrick 	qwz_mhi_device_wake(sc);
30374bba8532Spatrick 
30384bba8532Spatrick 	/* Notify event rings. */
30394bba8532Spatrick 	for (i = 0; i < nitems(psc->event_rings); i++) {
30404bba8532Spatrick 		struct qwz_pci_event_ring *ring = &psc->event_rings[i];
30414bba8532Spatrick 		qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
30424bba8532Spatrick 	}
30434bba8532Spatrick 
30444bba8532Spatrick 	/* TODO: Notify transfer/command rings? */
30454bba8532Spatrick 
30464bba8532Spatrick 	qwz_mhi_device_zzz(sc);
30474bba8532Spatrick }
30484bba8532Spatrick 
30494bba8532Spatrick void
qwz_mhi_low_power_mode_state_transition(struct qwz_pci_softc * psc)30504bba8532Spatrick qwz_mhi_low_power_mode_state_transition(struct qwz_pci_softc *psc)
30514bba8532Spatrick {
30524bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
30534bba8532Spatrick 
30544bba8532Spatrick 	qwz_mhi_set_state(sc, MHI_STATE_M2);
30554bba8532Spatrick }
30564bba8532Spatrick 
30574bba8532Spatrick void
qwz_mhi_set_state(struct qwz_softc * sc,uint32_t state)30584bba8532Spatrick qwz_mhi_set_state(struct qwz_softc *sc, uint32_t state)
30594bba8532Spatrick {
30604bba8532Spatrick 	uint32_t reg;
30614bba8532Spatrick 
30624bba8532Spatrick 	reg = qwz_pci_read(sc, MHI_CTRL);
30634bba8532Spatrick 
30644bba8532Spatrick 	if (state != MHI_STATE_RESET) {
30654bba8532Spatrick 		reg &= ~MHI_CTRL_MHISTATE_MASK;
30664bba8532Spatrick 		reg |= (state << MHI_CTRL_MHISTATE_SHFT) & MHI_CTRL_MHISTATE_MASK;
30674bba8532Spatrick 	} else
30684bba8532Spatrick 		reg |= MHI_CTRL_RESET_MASK;
30694bba8532Spatrick 
30704bba8532Spatrick 	qwz_pci_write(sc, MHI_CTRL, reg);
30714bba8532Spatrick }
30724bba8532Spatrick 
30734bba8532Spatrick void
qwz_mhi_init_mmio(struct qwz_pci_softc * psc)30744bba8532Spatrick qwz_mhi_init_mmio(struct qwz_pci_softc *psc)
30754bba8532Spatrick {
30764bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
30774bba8532Spatrick 	uint64_t paddr;
30784bba8532Spatrick 	uint32_t reg;
30794bba8532Spatrick 	int i;
30804bba8532Spatrick 
30814bba8532Spatrick 	reg = qwz_pci_read(sc, MHI_CHDBOFF);
30824bba8532Spatrick 
30834bba8532Spatrick 	/* Set device wake doorbell address. */
30844bba8532Spatrick 	psc->wake_db = reg + 8 * MHI_DEV_WAKE_DB;
30854bba8532Spatrick 
30864bba8532Spatrick 	/* Set doorbell address for each transfer ring. */
30874bba8532Spatrick 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
30884bba8532Spatrick 		struct qwz_pci_xfer_ring *ring = &psc->xfer_rings[i];
30894bba8532Spatrick 		ring->db_addr = reg + (8 * ring->mhi_chan_id);
30904bba8532Spatrick 	}
30914bba8532Spatrick 
30924bba8532Spatrick 	reg = qwz_pci_read(sc, MHI_ERDBOFF);
30934bba8532Spatrick 	/* Set doorbell address for each event ring. */
30944bba8532Spatrick 	for (i = 0; i < nitems(psc->event_rings); i++) {
30954bba8532Spatrick 		struct qwz_pci_event_ring *ring = &psc->event_rings[i];
30964bba8532Spatrick 		ring->db_addr = reg + (8 * i);
30974bba8532Spatrick 	}
30984bba8532Spatrick 
30994bba8532Spatrick 	paddr = QWZ_DMA_DVA(psc->chan_ctxt);
31004bba8532Spatrick 	qwz_pci_write(sc, MHI_CCABAP_HIGHER, paddr >> 32);
31014bba8532Spatrick 	qwz_pci_write(sc, MHI_CCABAP_LOWER, paddr & 0xffffffff);
31024bba8532Spatrick 
31034bba8532Spatrick 	paddr = QWZ_DMA_DVA(psc->event_ctxt);
31044bba8532Spatrick 	qwz_pci_write(sc, MHI_ECABAP_HIGHER, paddr >> 32);
31054bba8532Spatrick 	qwz_pci_write(sc, MHI_ECABAP_LOWER, paddr & 0xffffffff);
31064bba8532Spatrick 
31074bba8532Spatrick 	paddr = QWZ_DMA_DVA(psc->cmd_ctxt);
31084bba8532Spatrick 	qwz_pci_write(sc, MHI_CRCBAP_HIGHER, paddr >> 32);
31094bba8532Spatrick 	qwz_pci_write(sc, MHI_CRCBAP_LOWER, paddr & 0xffffffff);
31104bba8532Spatrick 
31114bba8532Spatrick 	/* Not (yet?) using fixed memory space from a device-tree. */
31124bba8532Spatrick 	qwz_pci_write(sc, MHI_CTRLBASE_HIGHER, 0);
31134bba8532Spatrick 	qwz_pci_write(sc, MHI_CTRLBASE_LOWER, 0);
31144bba8532Spatrick 	qwz_pci_write(sc, MHI_DATABASE_HIGHER, 0);
31154bba8532Spatrick 	qwz_pci_write(sc, MHI_DATABASE_LOWER, 0);
31164bba8532Spatrick 	qwz_pci_write(sc, MHI_CTRLLIMIT_HIGHER, 0x0);
31174bba8532Spatrick 	qwz_pci_write(sc, MHI_CTRLLIMIT_LOWER, 0xffffffff);
31184bba8532Spatrick 	qwz_pci_write(sc, MHI_DATALIMIT_HIGHER, 0x0);
31194bba8532Spatrick 	qwz_pci_write(sc, MHI_DATALIMIT_LOWER, 0xffffffff);
31204bba8532Spatrick 
31214bba8532Spatrick 	reg = qwz_pci_read(sc, MHI_CFG);
31224bba8532Spatrick 	reg &= ~(MHI_CFG_NER_MASK | MHI_CFG_NHWER_MASK);
31234bba8532Spatrick 	reg |= QWZ_NUM_EVENT_CTX << MHI_CFG_NER_SHFT;
31244bba8532Spatrick 	qwz_pci_write(sc, MHI_CFG, reg);
31254bba8532Spatrick }
31264bba8532Spatrick 
31274bba8532Spatrick int
qwz_mhi_fw_load_bhi(struct qwz_pci_softc * psc,uint8_t * data,size_t len)31284bba8532Spatrick qwz_mhi_fw_load_bhi(struct qwz_pci_softc *psc, uint8_t *data, size_t len)
31294bba8532Spatrick {
31304bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
31314bba8532Spatrick 	struct qwz_dmamem *data_adm;
31324bba8532Spatrick 	uint32_t seq, reg, status = MHI_BHI_STATUS_RESET;
31334bba8532Spatrick 	uint64_t paddr;
31344bba8532Spatrick 	int ret;
31354bba8532Spatrick 
31364bba8532Spatrick 	data_adm = qwz_dmamem_alloc(sc->sc_dmat, len, 0);
31374bba8532Spatrick 	if (data_adm == NULL) {
31384bba8532Spatrick 		printf("%s: could not allocate BHI DMA data buffer\n",
31394bba8532Spatrick 		    sc->sc_dev.dv_xname);
31404bba8532Spatrick 		return 1;
31414bba8532Spatrick 	}
31424bba8532Spatrick 
31434bba8532Spatrick 	/* Copy firmware image to DMA memory. */
31444bba8532Spatrick 	memcpy(QWZ_DMA_KVA(data_adm), data, len);
31454bba8532Spatrick 
31464bba8532Spatrick 	qwz_pci_write(sc, psc->bhi_off + MHI_BHI_STATUS, 0);
31474bba8532Spatrick 
31484bba8532Spatrick 	/* Set data physical address and length. */
31494bba8532Spatrick 	paddr = QWZ_DMA_DVA(data_adm);
31504bba8532Spatrick 	qwz_pci_write(sc, psc->bhi_off + MHI_BHI_IMGADDR_HIGH, paddr >> 32);
31514bba8532Spatrick 	qwz_pci_write(sc, psc->bhi_off + MHI_BHI_IMGADDR_LOW,
31524bba8532Spatrick 	    paddr & 0xffffffff);
31534bba8532Spatrick 	qwz_pci_write(sc, psc->bhi_off + MHI_BHI_IMGSIZE, len);
31544bba8532Spatrick 
31554bba8532Spatrick 	/* Set a random transaction sequence number. */
31564bba8532Spatrick 	do {
31574bba8532Spatrick 		seq = arc4random_uniform(MHI_BHI_TXDB_SEQNUM_BMSK);
31584bba8532Spatrick 	} while (seq == 0);
31594bba8532Spatrick 	qwz_pci_write(sc, psc->bhi_off + MHI_BHI_IMGTXDB, seq);
31604bba8532Spatrick 
31614bba8532Spatrick 	/* Wait for completion. */
31624bba8532Spatrick 	ret = 0;
31634bba8532Spatrick 	while (status != MHI_BHI_STATUS_SUCCESS && psc->bhi_ee < MHI_EE_SBL) {
31644bba8532Spatrick 		ret = tsleep_nsec(&psc->bhi_ee, 0, "qwzbhi", SEC_TO_NSEC(5));
31654bba8532Spatrick 		if (ret)
31664bba8532Spatrick 			break;
31674bba8532Spatrick 		reg = qwz_pci_read(sc, psc->bhi_off + MHI_BHI_STATUS);
31684bba8532Spatrick 		status = (reg & MHI_BHI_STATUS_MASK) >> MHI_BHI_STATUS_SHFT;
31694bba8532Spatrick 	}
31704bba8532Spatrick 
31714bba8532Spatrick 	if (ret) {
31724bba8532Spatrick 		printf("%s: BHI load timeout\n", sc->sc_dev.dv_xname);
31734bba8532Spatrick 		reg = qwz_pci_read(sc, psc->bhi_off + MHI_BHI_STATUS);
31744bba8532Spatrick 		status = (reg & MHI_BHI_STATUS_MASK) >> MHI_BHI_STATUS_SHFT;
31754bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: BHI status is 0x%x EE is 0x%x\n",
31764bba8532Spatrick 		    __func__, status, psc->bhi_ee);
31774bba8532Spatrick 	}
31784bba8532Spatrick 
31794bba8532Spatrick 	qwz_dmamem_free(sc->sc_dmat, data_adm);
31804bba8532Spatrick 	return ret;
31814bba8532Spatrick }
31824bba8532Spatrick 
31834bba8532Spatrick int
qwz_mhi_fw_load_bhie(struct qwz_pci_softc * psc,uint8_t * data,size_t len)31844bba8532Spatrick qwz_mhi_fw_load_bhie(struct qwz_pci_softc *psc, uint8_t *data, size_t len)
31854bba8532Spatrick {
31864bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
31874bba8532Spatrick 	struct qwz_dma_vec_entry *vec;
31884bba8532Spatrick 	uint32_t seq, reg, state = MHI_BHIE_TXVECSTATUS_STATUS_RESET;
31894bba8532Spatrick 	uint64_t paddr;
31904bba8532Spatrick 	const size_t chunk_size = MHI_DMA_VEC_CHUNK_SIZE;
31914bba8532Spatrick 	size_t nseg, remain, vec_size;
31924bba8532Spatrick 	int i, ret;
31934bba8532Spatrick 
31944bba8532Spatrick 	nseg = howmany(len, chunk_size);
31954bba8532Spatrick 	if (nseg == 0) {
31964bba8532Spatrick 		printf("%s: BHIE data too short, have only %zu bytes\n",
31974bba8532Spatrick 		    sc->sc_dev.dv_xname, len);
31984bba8532Spatrick 		return 1;
31994bba8532Spatrick 	}
32004bba8532Spatrick 
32014bba8532Spatrick 	if (psc->amss_data == NULL || QWZ_DMA_LEN(psc->amss_data) < len) {
32024bba8532Spatrick 		if (psc->amss_data)
32034bba8532Spatrick 			qwz_dmamem_free(sc->sc_dmat, psc->amss_data);
32044bba8532Spatrick 		psc->amss_data = qwz_dmamem_alloc(sc->sc_dmat, len, 0);
32054bba8532Spatrick 		if (psc->amss_data == NULL) {
32064bba8532Spatrick 			printf("%s: could not allocate BHIE DMA data buffer\n",
32074bba8532Spatrick 			    sc->sc_dev.dv_xname);
32084bba8532Spatrick 			return 1;
32094bba8532Spatrick 		}
32104bba8532Spatrick 	}
32114bba8532Spatrick 
32124bba8532Spatrick 	vec_size = nseg * sizeof(*vec);
32134bba8532Spatrick 	if (psc->amss_vec == NULL || QWZ_DMA_LEN(psc->amss_vec) < vec_size) {
32144bba8532Spatrick 		if (psc->amss_vec)
32154bba8532Spatrick 			qwz_dmamem_free(sc->sc_dmat, psc->amss_vec);
32164bba8532Spatrick 		psc->amss_vec = qwz_dmamem_alloc(sc->sc_dmat, vec_size, 0);
32174bba8532Spatrick 		if (psc->amss_vec == NULL) {
32184bba8532Spatrick 			printf("%s: could not allocate BHIE DMA vec buffer\n",
32194bba8532Spatrick 			    sc->sc_dev.dv_xname);
32204bba8532Spatrick 			qwz_dmamem_free(sc->sc_dmat, psc->amss_data);
32214bba8532Spatrick 			psc->amss_data = NULL;
32224bba8532Spatrick 			return 1;
32234bba8532Spatrick 		}
32244bba8532Spatrick 	}
32254bba8532Spatrick 
32264bba8532Spatrick 	/* Copy firmware image to DMA memory. */
32274bba8532Spatrick 	memcpy(QWZ_DMA_KVA(psc->amss_data), data, len);
32284bba8532Spatrick 
32294bba8532Spatrick 	/* Create vector which controls chunk-wise DMA copy in hardware. */
32304bba8532Spatrick 	paddr = QWZ_DMA_DVA(psc->amss_data);
32314bba8532Spatrick 	vec = QWZ_DMA_KVA(psc->amss_vec);
32324bba8532Spatrick 	remain = len;
32334bba8532Spatrick 	for (i = 0; i < nseg; i++) {
32344bba8532Spatrick 		vec[i].paddr = paddr;
32354bba8532Spatrick 		if (remain >= chunk_size) {
32364bba8532Spatrick 			vec[i].size = chunk_size;
32374bba8532Spatrick 			remain -= chunk_size;
32384bba8532Spatrick 			paddr += chunk_size;
32394bba8532Spatrick 		} else
32404bba8532Spatrick 			vec[i].size = remain;
32414bba8532Spatrick 	}
32424bba8532Spatrick 
32434bba8532Spatrick 	/* Set vector physical address and length. */
32444bba8532Spatrick 	paddr = QWZ_DMA_DVA(psc->amss_vec);
32454bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECADDR_HIGH_OFFS,
32464bba8532Spatrick 	    paddr >> 32);
32474bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECADDR_LOW_OFFS,
32484bba8532Spatrick 	    paddr & 0xffffffff);
32494bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECSIZE_OFFS, vec_size);
32504bba8532Spatrick 
32514bba8532Spatrick 	/* Set a random transaction sequence number. */
32524bba8532Spatrick 	do {
32534bba8532Spatrick 		seq = arc4random_uniform(MHI_BHIE_TXVECSTATUS_SEQNUM_BMSK);
32544bba8532Spatrick 	} while (seq == 0);
32554bba8532Spatrick 	reg = qwz_pci_read(sc, psc->bhie_off + MHI_BHIE_TXVECDB_OFFS);
32564bba8532Spatrick 	reg &= ~MHI_BHIE_TXVECDB_SEQNUM_BMSK;
32574bba8532Spatrick 	reg |= seq << MHI_BHIE_TXVECDB_SEQNUM_SHFT;
32584bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_TXVECDB_OFFS, reg);
32594bba8532Spatrick 
32604bba8532Spatrick 	/* Wait for completion. */
32614bba8532Spatrick 	ret = 0;
32624bba8532Spatrick 	while (state != MHI_BHIE_TXVECSTATUS_STATUS_XFER_COMPL) {
32634bba8532Spatrick 		ret = tsleep_nsec(&psc->bhie_off, 0, "qwzbhie",
32644bba8532Spatrick 		    SEC_TO_NSEC(5));
32654bba8532Spatrick 		if (ret)
32664bba8532Spatrick 			break;
32674bba8532Spatrick 		reg = qwz_pci_read(sc,
32684bba8532Spatrick 		    psc->bhie_off + MHI_BHIE_TXVECSTATUS_OFFS);
32694bba8532Spatrick 		state = (reg & MHI_BHIE_TXVECSTATUS_STATUS_BMSK) >>
32704bba8532Spatrick 		    MHI_BHIE_TXVECSTATUS_STATUS_SHFT;
32714bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: txvec state is 0x%x\n", __func__,
32724bba8532Spatrick 		    state);
32734bba8532Spatrick 	}
32744bba8532Spatrick 
32754bba8532Spatrick 	if (ret) {
32764bba8532Spatrick 		printf("%s: BHIE load timeout\n", sc->sc_dev.dv_xname);
32774bba8532Spatrick 		return ret;
32784bba8532Spatrick 	}
32794bba8532Spatrick 	return 0;
32804bba8532Spatrick }
32814bba8532Spatrick 
32824bba8532Spatrick void
qwz_rddm_prepare(struct qwz_pci_softc * psc)32834bba8532Spatrick qwz_rddm_prepare(struct qwz_pci_softc *psc)
32844bba8532Spatrick {
32854bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
32864bba8532Spatrick 	struct qwz_dma_vec_entry *vec;
32874bba8532Spatrick 	struct qwz_dmamem *data_adm, *vec_adm;
32884bba8532Spatrick 	uint32_t seq, reg;
32894bba8532Spatrick 	uint64_t paddr;
32904bba8532Spatrick 	const size_t len = QWZ_RDDM_DUMP_SIZE;
32914bba8532Spatrick 	const size_t chunk_size = MHI_DMA_VEC_CHUNK_SIZE;
32924bba8532Spatrick 	size_t nseg, remain, vec_size;
32934bba8532Spatrick 	int i;
32944bba8532Spatrick 
32954bba8532Spatrick 	nseg = howmany(len, chunk_size);
32964bba8532Spatrick 	if (nseg == 0) {
32974bba8532Spatrick 		printf("%s: RDDM data too short, have only %zu bytes\n",
32984bba8532Spatrick 		    sc->sc_dev.dv_xname, len);
32994bba8532Spatrick 		return;
33004bba8532Spatrick 	}
33014bba8532Spatrick 
33024bba8532Spatrick 	data_adm = qwz_dmamem_alloc(sc->sc_dmat, len, 0);
33034bba8532Spatrick 	if (data_adm == NULL) {
33044bba8532Spatrick 		printf("%s: could not allocate BHIE DMA data buffer\n",
33054bba8532Spatrick 		    sc->sc_dev.dv_xname);
33064bba8532Spatrick 		return;
33074bba8532Spatrick 	}
33084bba8532Spatrick 
33094bba8532Spatrick 	vec_size = nseg * sizeof(*vec);
33104bba8532Spatrick 	vec_adm = qwz_dmamem_alloc(sc->sc_dmat, vec_size, 0);
33114bba8532Spatrick 	if (vec_adm == NULL) {
33124bba8532Spatrick 		printf("%s: could not allocate BHIE DMA vector buffer\n",
33134bba8532Spatrick 		    sc->sc_dev.dv_xname);
33144bba8532Spatrick 		qwz_dmamem_free(sc->sc_dmat, data_adm);
33154bba8532Spatrick 		return;
33164bba8532Spatrick 	}
33174bba8532Spatrick 
33184bba8532Spatrick 	/* Create vector which controls chunk-wise DMA copy from hardware. */
33194bba8532Spatrick 	paddr = QWZ_DMA_DVA(data_adm);
33204bba8532Spatrick 	vec = QWZ_DMA_KVA(vec_adm);
33214bba8532Spatrick 	remain = len;
33224bba8532Spatrick 	for (i = 0; i < nseg; i++) {
33234bba8532Spatrick 		vec[i].paddr = paddr;
33244bba8532Spatrick 		if (remain >= chunk_size) {
33254bba8532Spatrick 			vec[i].size = chunk_size;
33264bba8532Spatrick 			remain -= chunk_size;
33274bba8532Spatrick 			paddr += chunk_size;
33284bba8532Spatrick 		} else
33294bba8532Spatrick 			vec[i].size = remain;
33304bba8532Spatrick 	}
33314bba8532Spatrick 
33324bba8532Spatrick 	/* Set vector physical address and length. */
33334bba8532Spatrick 	paddr = QWZ_DMA_DVA(vec_adm);
33344bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECADDR_HIGH_OFFS,
33354bba8532Spatrick 	    paddr >> 32);
33364bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECADDR_LOW_OFFS,
33374bba8532Spatrick 	    paddr & 0xffffffff);
33384bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECSIZE_OFFS, vec_size);
33394bba8532Spatrick 
33404bba8532Spatrick 	/* Set a random transaction sequence number. */
33414bba8532Spatrick 	do {
33424bba8532Spatrick 		seq = arc4random_uniform(MHI_BHIE_RXVECSTATUS_SEQNUM_BMSK);
33434bba8532Spatrick 	} while (seq == 0);
33444bba8532Spatrick 
33454bba8532Spatrick 	reg = qwz_pci_read(sc, psc->bhie_off + MHI_BHIE_RXVECDB_OFFS);
33464bba8532Spatrick 	reg &= ~MHI_BHIE_RXVECDB_SEQNUM_BMSK;
33474bba8532Spatrick 	reg |= seq << MHI_BHIE_RXVECDB_SEQNUM_SHFT;
33484bba8532Spatrick 	qwz_pci_write(sc, psc->bhie_off + MHI_BHIE_RXVECDB_OFFS, reg);
33494bba8532Spatrick 
33504bba8532Spatrick 	psc->rddm_data = data_adm;
33514bba8532Spatrick 	psc->rddm_vec = vec_adm;
33524bba8532Spatrick }
33534bba8532Spatrick 
33544bba8532Spatrick #ifdef QWZ_DEBUG
33554bba8532Spatrick void
qwz_rddm_task(void * arg)33564bba8532Spatrick qwz_rddm_task(void *arg)
33574bba8532Spatrick {
33584bba8532Spatrick 	struct qwz_pci_softc *psc = arg;
33594bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
33604bba8532Spatrick 	uint32_t reg, state = MHI_BHIE_RXVECSTATUS_STATUS_RESET;
33614bba8532Spatrick 	const size_t len = QWZ_RDDM_DUMP_SIZE;
33624bba8532Spatrick 	int i, timeout;
33634bba8532Spatrick 	const uint32_t msecs = 100, retries = 20;
33644bba8532Spatrick 	uint8_t *rddm;
33654bba8532Spatrick 	struct nameidata nd;
33664bba8532Spatrick 	struct vnode *vp = NULL;
33674bba8532Spatrick 	struct iovec iov[3];
33684bba8532Spatrick 	struct uio uio;
33694bba8532Spatrick 	char path[PATH_MAX];
33704bba8532Spatrick 	int error = 0;
33714bba8532Spatrick 
33724bba8532Spatrick 	if (psc->rddm_data == NULL) {
33734bba8532Spatrick 		DPRINTF("%s: RDDM not prepared\n", __func__);
33744bba8532Spatrick 		return;
33754bba8532Spatrick 	}
33764bba8532Spatrick 
33774bba8532Spatrick 	/* Poll for completion */
33784bba8532Spatrick 	timeout = retries;
33794bba8532Spatrick 	while (timeout > 0 && state != MHI_BHIE_RXVECSTATUS_STATUS_XFER_COMPL) {
33804bba8532Spatrick 		reg = qwz_pci_read(sc,
33814bba8532Spatrick 		    psc->bhie_off + MHI_BHIE_RXVECSTATUS_OFFS);
33824bba8532Spatrick 		state = (reg & MHI_BHIE_RXVECSTATUS_STATUS_BMSK) >>
33834bba8532Spatrick 		    MHI_BHIE_RXVECSTATUS_STATUS_SHFT;
33844bba8532Spatrick 		DPRINTF("%s: txvec state is 0x%x\n", __func__, state);
33854bba8532Spatrick 		DELAY((msecs / retries) * 1000);
33864bba8532Spatrick 		timeout--;
33874bba8532Spatrick 	}
33884bba8532Spatrick 
33894bba8532Spatrick 	if (timeout == 0) {
33904bba8532Spatrick 		DPRINTF("%s: RDDM dump failed\n", sc->sc_dev.dv_xname);
33914bba8532Spatrick 		return;
33924bba8532Spatrick 	}
33934bba8532Spatrick 
33944bba8532Spatrick 	rddm = QWZ_DMA_KVA(psc->rddm_data);
33954bba8532Spatrick 	DPRINTF("%s: RDDM snippet:\n", __func__);
33964bba8532Spatrick 	for (i = 0; i < MIN(64, len); i++) {
33974bba8532Spatrick 		DPRINTF("%s %.2x", i % 16 == 0 ? "\n" : "", rddm[i]);
33984bba8532Spatrick 	}
33994bba8532Spatrick 	DPRINTF("\n");
34004bba8532Spatrick 
34014bba8532Spatrick 	DPRINTF("%s: sleeping for 30 seconds to allow userland to boot\n", __func__);
34024bba8532Spatrick 	tsleep_nsec(&psc->rddm_data, 0, "qwzrddm", SEC_TO_NSEC(30));
34034bba8532Spatrick 
34044bba8532Spatrick 	snprintf(path, sizeof(path), "/root/%s-rddm.bin", sc->sc_dev.dv_xname);
34054bba8532Spatrick 	DPRINTF("%s: saving RDDM to %s\n", __func__, path);
34064bba8532Spatrick 	NDINIT(&nd, 0, 0, UIO_SYSSPACE, path, curproc);
34074bba8532Spatrick 	nd.ni_pledge = PLEDGE_CPATH | PLEDGE_WPATH;
34084bba8532Spatrick 	nd.ni_unveil = UNVEIL_CREATE | UNVEIL_WRITE;
34094bba8532Spatrick 	error = vn_open(&nd, FWRITE | O_CREAT | O_NOFOLLOW | O_TRUNC,
34104bba8532Spatrick 	    S_IRUSR | S_IWUSR);
34114bba8532Spatrick 	if (error) {
34124bba8532Spatrick 		DPRINTF("%s: vn_open: error %d\n", __func__, error);
34134bba8532Spatrick 		goto done;
34144bba8532Spatrick 	}
34154bba8532Spatrick 	vp = nd.ni_vp;
34164bba8532Spatrick 	VOP_UNLOCK(vp);
34174bba8532Spatrick 
34184bba8532Spatrick 	iov[0].iov_base = (void *)rddm;
34194bba8532Spatrick 	iov[0].iov_len = len;
34204bba8532Spatrick 	iov[1].iov_len = 0;
34214bba8532Spatrick 	uio.uio_iov = &iov[0];
34224bba8532Spatrick 	uio.uio_offset = 0;
34234bba8532Spatrick 	uio.uio_segflg = UIO_SYSSPACE;
34244bba8532Spatrick 	uio.uio_rw = UIO_WRITE;
34254bba8532Spatrick 	uio.uio_resid = len;
34264bba8532Spatrick 	uio.uio_iovcnt = 1;
34274bba8532Spatrick 	uio.uio_procp = curproc;
34284bba8532Spatrick 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
34294bba8532Spatrick 	if (error) {
34304bba8532Spatrick 		DPRINTF("%s: vget: error %d\n", __func__, error);
34314bba8532Spatrick 		goto done;
34324bba8532Spatrick 	}
34334bba8532Spatrick 	error = VOP_WRITE(vp, &uio, IO_UNIT|IO_APPEND, curproc->p_ucred);
34344bba8532Spatrick 	vput(vp);
34354bba8532Spatrick 	if (error)
34364bba8532Spatrick 		DPRINTF("%s: VOP_WRITE: error %d\n", __func__, error);
34374bba8532Spatrick 	#if 0
34384bba8532Spatrick 	error = vn_close(vp, FWRITE, curproc->p_ucred, curproc);
34394bba8532Spatrick 	if (error)
34404bba8532Spatrick 		DPRINTF("%s: vn_close: error %d\n", __func__, error);
34414bba8532Spatrick 	#endif
34424bba8532Spatrick done:
34434bba8532Spatrick 	qwz_dmamem_free(sc->sc_dmat, psc->rddm_data);
34444bba8532Spatrick 	qwz_dmamem_free(sc->sc_dmat, psc->rddm_vec);
34454bba8532Spatrick 	psc->rddm_data = NULL;
34464bba8532Spatrick 	psc->rddm_vec = NULL;
34474bba8532Spatrick 	DPRINTF("%s: done, error %d\n", __func__, error);
34484bba8532Spatrick }
34494bba8532Spatrick #endif
34504bba8532Spatrick 
34514bba8532Spatrick void *
qwz_pci_event_ring_get_elem(struct qwz_pci_event_ring * ring,uint64_t rp)34524bba8532Spatrick qwz_pci_event_ring_get_elem(struct qwz_pci_event_ring *ring, uint64_t rp)
34534bba8532Spatrick {
34544bba8532Spatrick 	uint64_t base = QWZ_DMA_DVA(ring->dmamem), offset;
34554bba8532Spatrick 	void *addr = QWZ_DMA_KVA(ring->dmamem);
34564bba8532Spatrick 
34574bba8532Spatrick 	if (rp < base)
34584bba8532Spatrick 		return NULL;
34594bba8532Spatrick 
34604bba8532Spatrick 	offset = rp - base;
34614bba8532Spatrick 	if (offset >= ring->size)
34624bba8532Spatrick 		return NULL;
34634bba8532Spatrick 
34644bba8532Spatrick 	return addr + offset;
34654bba8532Spatrick }
34664bba8532Spatrick 
34674bba8532Spatrick void
qwz_mhi_state_change(struct qwz_pci_softc * psc,int ee,int mhi_state)34684bba8532Spatrick qwz_mhi_state_change(struct qwz_pci_softc *psc, int ee, int mhi_state)
34694bba8532Spatrick {
34704bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
34714bba8532Spatrick 	uint32_t old_ee = psc->bhi_ee;
34724bba8532Spatrick 	uint32_t old_mhi_state = psc->mhi_state;
34734bba8532Spatrick 
34744bba8532Spatrick 	if (ee != -1 && psc->bhi_ee != ee) {
34754bba8532Spatrick 		switch (ee) {
34764bba8532Spatrick 		case MHI_EE_PBL:
34774bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new EE PBL\n",
34784bba8532Spatrick 			    sc->sc_dev.dv_xname);
34794bba8532Spatrick 			psc->bhi_ee = ee;
34804bba8532Spatrick 			break;
34814bba8532Spatrick 		case MHI_EE_SBL:
34824bba8532Spatrick 			psc->bhi_ee = ee;
34834bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new EE SBL\n",
34844bba8532Spatrick 			    sc->sc_dev.dv_xname);
34854bba8532Spatrick 			break;
34864bba8532Spatrick 		case MHI_EE_AMSS:
34874bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new EE AMSS\n",
34884bba8532Spatrick 			    sc->sc_dev.dv_xname);
34894bba8532Spatrick 			psc->bhi_ee = ee;
34904bba8532Spatrick 			/* Wake thread loading the full AMSS image. */
34914bba8532Spatrick 			wakeup(&psc->bhie_off);
34924bba8532Spatrick 			break;
34934bba8532Spatrick 		case MHI_EE_WFW:
34944bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new EE WFW\n",
34954bba8532Spatrick 			    sc->sc_dev.dv_xname);
34964bba8532Spatrick 			psc->bhi_ee = ee;
34974bba8532Spatrick 			break;
34984bba8532Spatrick 		default:
34994bba8532Spatrick 			printf("%s: unhandled EE change to %x\n",
35004bba8532Spatrick 			    sc->sc_dev.dv_xname, ee);
35014bba8532Spatrick 			break;
35024bba8532Spatrick 		}
35034bba8532Spatrick 	}
35044bba8532Spatrick 
35054bba8532Spatrick 	if (mhi_state != -1 && psc->mhi_state != mhi_state) {
35064bba8532Spatrick 		switch (mhi_state) {
35074bba8532Spatrick 		case -1:
35084bba8532Spatrick 			break;
35094bba8532Spatrick 		case MHI_STATE_RESET:
35104bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new MHI state RESET\n",
35114bba8532Spatrick 			    sc->sc_dev.dv_xname);
35124bba8532Spatrick 			psc->mhi_state = mhi_state;
35134bba8532Spatrick 			break;
35144bba8532Spatrick 		case MHI_STATE_READY:
35154bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new MHI state READY\n",
35164bba8532Spatrick 			    sc->sc_dev.dv_xname);
35174bba8532Spatrick 			psc->mhi_state = mhi_state;
35184bba8532Spatrick 			qwz_mhi_ready_state_transition(psc);
35194bba8532Spatrick 			break;
35204bba8532Spatrick 		case MHI_STATE_M0:
35214bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new MHI state M0\n",
35224bba8532Spatrick 			    sc->sc_dev.dv_xname);
35234bba8532Spatrick 			psc->mhi_state = mhi_state;
35244bba8532Spatrick 			qwz_mhi_mission_mode_state_transition(psc);
35254bba8532Spatrick 			break;
35264bba8532Spatrick 		case MHI_STATE_M1:
35274bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: new MHI state M1\n",
35284bba8532Spatrick 			    sc->sc_dev.dv_xname);
35294bba8532Spatrick 			psc->mhi_state = mhi_state;
35304bba8532Spatrick 			qwz_mhi_low_power_mode_state_transition(psc);
35314bba8532Spatrick 			break;
35324bba8532Spatrick 		case MHI_STATE_SYS_ERR:
35334bba8532Spatrick 			DNPRINTF(QWZ_D_MHI,
35344bba8532Spatrick 			    "%s: new MHI state SYS ERR\n",
35354bba8532Spatrick 			    sc->sc_dev.dv_xname);
35364bba8532Spatrick 			psc->mhi_state = mhi_state;
35374bba8532Spatrick 			break;
35384bba8532Spatrick 		default:
35394bba8532Spatrick 			printf("%s: unhandled MHI state change to %x\n",
35404bba8532Spatrick 			    sc->sc_dev.dv_xname, mhi_state);
35414bba8532Spatrick 			break;
35424bba8532Spatrick 		}
35434bba8532Spatrick 	}
35444bba8532Spatrick 
35454bba8532Spatrick 	if (old_ee != psc->bhi_ee)
35464bba8532Spatrick 		wakeup(&psc->bhi_ee);
35474bba8532Spatrick 	if (old_mhi_state != psc->mhi_state)
35484bba8532Spatrick 		wakeup(&psc->mhi_state);
35494bba8532Spatrick }
35504bba8532Spatrick 
35514bba8532Spatrick void
qwz_pci_intr_ctrl_event_mhi(struct qwz_pci_softc * psc,uint32_t mhi_state)35524bba8532Spatrick qwz_pci_intr_ctrl_event_mhi(struct qwz_pci_softc *psc, uint32_t mhi_state)
35534bba8532Spatrick {
35544bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: MHI state change 0x%x -> 0x%x\n", __func__,
35554bba8532Spatrick 	    psc->mhi_state, mhi_state);
35564bba8532Spatrick 
35574bba8532Spatrick 	if (psc->mhi_state != mhi_state)
35584bba8532Spatrick 		qwz_mhi_state_change(psc, -1, mhi_state);
35594bba8532Spatrick }
35604bba8532Spatrick 
35614bba8532Spatrick void
qwz_pci_intr_ctrl_event_ee(struct qwz_pci_softc * psc,uint32_t ee)35624bba8532Spatrick qwz_pci_intr_ctrl_event_ee(struct qwz_pci_softc *psc, uint32_t ee)
35634bba8532Spatrick {
35644bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: EE change 0x%x to 0x%x\n", __func__,
35654bba8532Spatrick 	    psc->bhi_ee, ee);
35664bba8532Spatrick 
35674bba8532Spatrick 	if (psc->bhi_ee != ee)
35684bba8532Spatrick 		qwz_mhi_state_change(psc, ee, -1);
35694bba8532Spatrick }
35704bba8532Spatrick 
35714bba8532Spatrick void
qwz_pci_intr_ctrl_event_cmd_complete(struct qwz_pci_softc * psc,uint64_t ptr,uint32_t cmd_status)35724bba8532Spatrick qwz_pci_intr_ctrl_event_cmd_complete(struct qwz_pci_softc *psc,
35734bba8532Spatrick     uint64_t ptr, uint32_t cmd_status)
35744bba8532Spatrick {
35754bba8532Spatrick 	struct qwz_pci_cmd_ring	*cmd_ring = &psc->cmd_ring;
35764bba8532Spatrick 	uint64_t base = QWZ_DMA_DVA(cmd_ring->dmamem);
35774bba8532Spatrick 	struct qwz_pci_xfer_ring *xfer_ring = NULL;
35784bba8532Spatrick 	struct qwz_mhi_ring_element *e;
35794bba8532Spatrick 	uint32_t tre1, chid;
35804bba8532Spatrick 	size_t i;
35814bba8532Spatrick 
35824bba8532Spatrick 	e = qwz_pci_cmd_ring_get_elem(cmd_ring, ptr);
35834bba8532Spatrick 	if (e == NULL)
35844bba8532Spatrick 		return;
35854bba8532Spatrick 
35864bba8532Spatrick 	tre1 = le32toh(e->dword[1]);
35874bba8532Spatrick 	chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
35884bba8532Spatrick 
35894bba8532Spatrick 	for (i = 0; i < nitems(psc->xfer_rings); i++) {
35904bba8532Spatrick 		if (psc->xfer_rings[i].mhi_chan_id == chid) {
35914bba8532Spatrick 			xfer_ring = &psc->xfer_rings[i];
35924bba8532Spatrick 			break;
35934bba8532Spatrick 		}
35944bba8532Spatrick 	}
35954bba8532Spatrick 	if (xfer_ring == NULL) {
35964bba8532Spatrick 		printf("%s: no transfer ring found for command completion "
35974bba8532Spatrick 		    "on channel %u\n", __func__, chid);
35984bba8532Spatrick 		return;
35994bba8532Spatrick 	}
36004bba8532Spatrick 
36014bba8532Spatrick 	xfer_ring->cmd_status = cmd_status;
36024bba8532Spatrick 	wakeup(&xfer_ring->cmd_status);
36034bba8532Spatrick 
36044bba8532Spatrick 	if (cmd_ring->rp + sizeof(*e) >= base + cmd_ring->size)
36054bba8532Spatrick 		cmd_ring->rp = base;
36064bba8532Spatrick 	else
36074bba8532Spatrick 		cmd_ring->rp += sizeof(*e);
36084bba8532Spatrick }
36094bba8532Spatrick 
36104bba8532Spatrick int
qwz_pci_intr_ctrl_event(struct qwz_pci_softc * psc,struct qwz_pci_event_ring * ring)36114bba8532Spatrick qwz_pci_intr_ctrl_event(struct qwz_pci_softc *psc, struct qwz_pci_event_ring *ring)
36124bba8532Spatrick {
36134bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
36144bba8532Spatrick 	struct qwz_mhi_event_ctxt *c;
36154bba8532Spatrick 	uint64_t rp, wp, base;
36164bba8532Spatrick 	struct qwz_mhi_ring_element *e;
36174bba8532Spatrick 	uint32_t tre0, tre1, type, code, chid, len;
36184bba8532Spatrick 
36194bba8532Spatrick 	c = ring->event_ctxt;
36204bba8532Spatrick 	if (c == NULL) {
36214bba8532Spatrick 		/*
36224bba8532Spatrick 		 * Interrupts can trigger before mhi_init_event_rings()
36234bba8532Spatrick 		 * if the device is still active after a warm reboot.
36244bba8532Spatrick 		 */
36254bba8532Spatrick 		return 0;
36264bba8532Spatrick 	}
36274bba8532Spatrick 
36284bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->event_ctxt), 0,
36294bba8532Spatrick 	    QWZ_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_POSTREAD);
36304bba8532Spatrick 
36314bba8532Spatrick 	rp = le64toh(c->rp);
36324bba8532Spatrick 	wp = le64toh(c->wp);
36334bba8532Spatrick 
36344bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: kernel rp=0x%llx\n", __func__, ring->rp);
36354bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: device rp=0x%llx\n", __func__, rp);
36364bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: kernel wp=0x%llx\n", __func__, ring->wp);
36374bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: device wp=0x%llx\n", __func__, wp);
36384bba8532Spatrick 
36394bba8532Spatrick 	base = QWZ_DMA_DVA(ring->dmamem);
36404bba8532Spatrick 	if (ring->rp == rp || rp < base || rp >= base + ring->size)
36414bba8532Spatrick 		return 0;
36424bba8532Spatrick 	if (wp < base || wp >= base + ring->size)
36434bba8532Spatrick 		return 0;
36444bba8532Spatrick 
36454bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(ring->dmamem),
36464bba8532Spatrick 	    0, QWZ_DMA_LEN(ring->dmamem), BUS_DMASYNC_POSTREAD);
36474bba8532Spatrick 
36484bba8532Spatrick 	while (ring->rp != rp) {
36494bba8532Spatrick 		e = qwz_pci_event_ring_get_elem(ring, ring->rp);
36504bba8532Spatrick 		if (e == NULL)
36514bba8532Spatrick 			return 0;
36524bba8532Spatrick 
36534bba8532Spatrick 		tre0 = le32toh(e->dword[0]);
36544bba8532Spatrick 		tre1 = le32toh(e->dword[1]);
36554bba8532Spatrick 
36564bba8532Spatrick 		len = (tre0 & MHI_TRE0_EV_LEN_MASK) >> MHI_TRE0_EV_LEN_SHFT;
36574bba8532Spatrick 		code = (tre0 & MHI_TRE0_EV_CODE_MASK) >> MHI_TRE0_EV_CODE_SHFT;
36584bba8532Spatrick 		type = (tre1 & MHI_TRE1_EV_TYPE_MASK) >> MHI_TRE1_EV_TYPE_SHFT;
36594bba8532Spatrick 		chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
36604bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: len=%u code=0x%x type=0x%x chid=%d\n",
36614bba8532Spatrick 		    __func__, len, code, type, chid);
36624bba8532Spatrick 
36634bba8532Spatrick 		switch (type) {
36644bba8532Spatrick 		case MHI_PKT_TYPE_STATE_CHANGE_EVENT:
36654bba8532Spatrick 			qwz_pci_intr_ctrl_event_mhi(psc, code);
36664bba8532Spatrick 			break;
36674bba8532Spatrick 		case MHI_PKT_TYPE_EE_EVENT:
36684bba8532Spatrick 			qwz_pci_intr_ctrl_event_ee(psc, code);
36694bba8532Spatrick 			break;
36704bba8532Spatrick 		case MHI_PKT_TYPE_CMD_COMPLETION_EVENT:
36714bba8532Spatrick 			qwz_pci_intr_ctrl_event_cmd_complete(psc,
36724bba8532Spatrick 			    le64toh(e->ptr), code);
36734bba8532Spatrick 			break;
36744bba8532Spatrick 		default:
36754bba8532Spatrick 			printf("%s: unhandled event type 0x%x\n",
36764bba8532Spatrick 			    __func__, type);
36774bba8532Spatrick 			break;
36784bba8532Spatrick 		}
36794bba8532Spatrick 
36804bba8532Spatrick 		if (ring->rp + sizeof(*e) >= base + ring->size)
36814bba8532Spatrick 			ring->rp = base;
36824bba8532Spatrick 		else
36834bba8532Spatrick 			ring->rp += sizeof(*e);
36844bba8532Spatrick 
36854bba8532Spatrick 		if (ring->wp + sizeof(*e) >= base + ring->size)
36864bba8532Spatrick 			ring->wp = base;
36874bba8532Spatrick 		else
36884bba8532Spatrick 			ring->wp += sizeof(*e);
36894bba8532Spatrick 	}
36904bba8532Spatrick 
36914bba8532Spatrick 	c->wp = htole64(ring->wp);
36924bba8532Spatrick 
36934bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->event_ctxt), 0,
36944bba8532Spatrick 	    QWZ_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_PREWRITE);
36954bba8532Spatrick 
36964bba8532Spatrick 	qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
36974bba8532Spatrick 	return 1;
36984bba8532Spatrick }
36994bba8532Spatrick 
37004bba8532Spatrick void
qwz_pci_intr_data_event_tx(struct qwz_pci_softc * psc,struct qwz_mhi_ring_element * e)37014bba8532Spatrick qwz_pci_intr_data_event_tx(struct qwz_pci_softc *psc, struct qwz_mhi_ring_element *e)
37024bba8532Spatrick {
37034bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
37044bba8532Spatrick 	struct qwz_pci_xfer_ring *ring;
37054bba8532Spatrick 	struct qwz_xfer_data *xfer;
37064bba8532Spatrick 	uint64_t rp, evrp, base, paddr;
37074bba8532Spatrick 	uint32_t tre0, tre1, code, chid, evlen, len;
37084bba8532Spatrick 	int i;
37094bba8532Spatrick 
37104bba8532Spatrick 	tre0 = le32toh(e->dword[0]);
37114bba8532Spatrick 	tre1 = le32toh(e->dword[1]);
37124bba8532Spatrick 
37134bba8532Spatrick 	evlen = (tre0 & MHI_TRE0_EV_LEN_MASK) >> MHI_TRE0_EV_LEN_SHFT;
37144bba8532Spatrick 	code = (tre0 & MHI_TRE0_EV_CODE_MASK) >> MHI_TRE0_EV_CODE_SHFT;
37154bba8532Spatrick 	chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
37164bba8532Spatrick 
37174bba8532Spatrick 	switch (code) {
37184bba8532Spatrick 	case MHI_EV_CC_EOT:
37194bba8532Spatrick 		for (i = 0; i < nitems(psc->xfer_rings); i++) {
37204bba8532Spatrick 			ring = &psc->xfer_rings[i];
37214bba8532Spatrick 			if (ring->mhi_chan_id == chid)
37224bba8532Spatrick 				break;
37234bba8532Spatrick 		}
37244bba8532Spatrick 		if (i == nitems(psc->xfer_rings)) {
37254bba8532Spatrick 			printf("%s: unhandled channel 0x%x\n",
37264bba8532Spatrick 			    __func__, chid);
37274bba8532Spatrick 			break;
37284bba8532Spatrick 		}
37294bba8532Spatrick 		base = QWZ_DMA_DVA(ring->dmamem);
37304bba8532Spatrick 		/* PTR contains the entry that was last written */
37314bba8532Spatrick 		evrp = letoh64(e->ptr);
37324bba8532Spatrick 		rp = evrp;
37334bba8532Spatrick 		if (rp < base || rp >= base + ring->size) {
37344bba8532Spatrick 			printf("%s: invalid ptr 0x%llx\n",
37354bba8532Spatrick 			    __func__, rp);
37364bba8532Spatrick 			break;
37374bba8532Spatrick 		}
37384bba8532Spatrick 		/* Point rp to next empty slot */
37394bba8532Spatrick 		if (rp + sizeof(*e) >= base + ring->size)
37404bba8532Spatrick 			rp = base;
37414bba8532Spatrick 		else
37424bba8532Spatrick 			rp += sizeof(*e);
37434bba8532Spatrick 		/* Parse until next empty slot */
37444bba8532Spatrick 		while (ring->rp != rp) {
37454bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s:%d: ring->rp 0x%llx "
37464bba8532Spatrick 			    "ring->wp 0x%llx rp 0x%llx\n", __func__,
37474bba8532Spatrick 			    __LINE__, ring->rp, ring->wp, rp);
37484bba8532Spatrick 			e = qwz_pci_xfer_ring_get_elem(ring, ring->rp);
37494bba8532Spatrick 			xfer = qwz_pci_xfer_ring_get_data(ring, ring->rp);
37504bba8532Spatrick 
37514bba8532Spatrick 			if (ring->rp == evrp)
37524bba8532Spatrick 				len = evlen;
37534bba8532Spatrick 			else
37544bba8532Spatrick 				len = xfer->m->m_pkthdr.len;
37554bba8532Spatrick 
37564bba8532Spatrick 			bus_dmamap_sync(sc->sc_dmat, xfer->map, 0,
37574bba8532Spatrick 			    xfer->m->m_pkthdr.len, BUS_DMASYNC_POSTREAD);
37584bba8532Spatrick #ifdef QWZ_DEBUG
37594bba8532Spatrick 			{
37604bba8532Spatrick 			int i;
37614bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "%s: chan %u data (len %u): ",
37624bba8532Spatrick 			    __func__,
37634bba8532Spatrick 			    ring->mhi_chan_id, len);
37644bba8532Spatrick 			for (i = 0; i < MIN(32, len); i++) {
37654bba8532Spatrick 				DNPRINTF(QWZ_D_MHI, "%02x ",
37664bba8532Spatrick 				    (unsigned char)mtod(xfer->m, caddr_t)[i]);
37674bba8532Spatrick 			}
37684bba8532Spatrick 			if (i < len)
37694bba8532Spatrick 				DNPRINTF(QWZ_D_MHI, "...");
37704bba8532Spatrick 			DNPRINTF(QWZ_D_MHI, "\n");
37714bba8532Spatrick 			}
37724bba8532Spatrick #endif
37734bba8532Spatrick 			if (ring->mhi_chan_direction == MHI_CHAN_TYPE_INBOUND) {
37744bba8532Spatrick 				/* Save m_data as upper layers use m_adj(9) */
37754bba8532Spatrick 				void *o_data = xfer->m->m_data;
37764bba8532Spatrick 
37774bba8532Spatrick 				/* Pass mbuf to upper layers */
37784bba8532Spatrick 				qwz_qrtr_recv_msg(sc, xfer->m);
37794bba8532Spatrick 
37804bba8532Spatrick 				/* Reset RX mbuf instead of free/alloc */
37814bba8532Spatrick 				KASSERT(xfer->m->m_next == NULL);
37824bba8532Spatrick 				xfer->m->m_data = o_data;
37834bba8532Spatrick 				xfer->m->m_len = xfer->m->m_pkthdr.len =
37844bba8532Spatrick 				    QWZ_PCI_XFER_MAX_DATA_SIZE;
37854bba8532Spatrick 
37864bba8532Spatrick 				paddr = xfer->map->dm_segs[0].ds_addr;
37874bba8532Spatrick 
37884bba8532Spatrick 				e->ptr = htole64(paddr);
37894bba8532Spatrick 				e->dword[0] = htole32((
37904bba8532Spatrick 				    QWZ_PCI_XFER_MAX_DATA_SIZE <<
37914bba8532Spatrick 				    MHI_TRE0_DATA_LEN_SHFT) &
37924bba8532Spatrick 				    MHI_TRE0_DATA_LEN_MASK);
37934bba8532Spatrick 				e->dword[1] = htole32(MHI_TRE1_DATA_IEOT |
37944bba8532Spatrick 				    MHI_TRE1_DATA_BEI |
37954bba8532Spatrick 				    MHI_TRE1_DATA_TYPE_TRANSFER <<
37964bba8532Spatrick 				    MHI_TRE1_DATA_TYPE_SHIFT);
37974bba8532Spatrick 
37984bba8532Spatrick 				if (ring->wp + sizeof(*e) >= base + ring->size)
37994bba8532Spatrick 					ring->wp = base;
38004bba8532Spatrick 				else
38014bba8532Spatrick 					ring->wp += sizeof(*e);
38024bba8532Spatrick 			} else {
38034bba8532Spatrick 				/* Unload and free TX mbuf */
38044bba8532Spatrick 				bus_dmamap_unload(sc->sc_dmat, xfer->map);
38054bba8532Spatrick 				m_freem(xfer->m);
38064bba8532Spatrick 				xfer->m = NULL;
38074bba8532Spatrick 				ring->queued--;
38084bba8532Spatrick 			}
38094bba8532Spatrick 
38104bba8532Spatrick 			if (ring->rp + sizeof(*e) >= base + ring->size)
38114bba8532Spatrick 				ring->rp = base;
38124bba8532Spatrick 			else
38134bba8532Spatrick 				ring->rp += sizeof(*e);
38144bba8532Spatrick 		}
38154bba8532Spatrick 
38164bba8532Spatrick 		if (ring->mhi_chan_direction == MHI_CHAN_TYPE_INBOUND) {
38174bba8532Spatrick 			ring->chan_ctxt->wp = htole64(ring->wp);
38184bba8532Spatrick 
38194bba8532Spatrick 			bus_dmamap_sync(sc->sc_dmat,
38204bba8532Spatrick 			    QWZ_DMA_MAP(psc->chan_ctxt), 0,
38214bba8532Spatrick 			    QWZ_DMA_LEN(psc->chan_ctxt),
38224bba8532Spatrick 			    BUS_DMASYNC_PREWRITE);
38234bba8532Spatrick 
38244bba8532Spatrick 			qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
38254bba8532Spatrick 		}
38264bba8532Spatrick 		break;
38274bba8532Spatrick 	default:
38284bba8532Spatrick 		printf("%s: unhandled event code 0x%x\n",
38294bba8532Spatrick 		    __func__, code);
38304bba8532Spatrick 	}
38314bba8532Spatrick }
38324bba8532Spatrick 
38334bba8532Spatrick int
qwz_pci_intr_data_event(struct qwz_pci_softc * psc,struct qwz_pci_event_ring * ring)38344bba8532Spatrick qwz_pci_intr_data_event(struct qwz_pci_softc *psc, struct qwz_pci_event_ring *ring)
38354bba8532Spatrick {
38364bba8532Spatrick 	struct qwz_softc *sc = &psc->sc_sc;
38374bba8532Spatrick 	struct qwz_mhi_event_ctxt *c;
38384bba8532Spatrick 	uint64_t rp, wp, base;
38394bba8532Spatrick 	struct qwz_mhi_ring_element *e;
38404bba8532Spatrick 	uint32_t tre0, tre1, type, code, chid, len;
38414bba8532Spatrick 
38424bba8532Spatrick 	c = ring->event_ctxt;
38434bba8532Spatrick 	if (c == NULL) {
38444bba8532Spatrick 		/*
38454bba8532Spatrick 		 * Interrupts can trigger before mhi_init_event_rings()
38464bba8532Spatrick 		 * if the device is still active after a warm reboot.
38474bba8532Spatrick 		 */
38484bba8532Spatrick 		return 0;
38494bba8532Spatrick 	}
38504bba8532Spatrick 
38514bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->event_ctxt), 0,
38524bba8532Spatrick 	    QWZ_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_POSTREAD);
38534bba8532Spatrick 
38544bba8532Spatrick 	rp = le64toh(c->rp);
38554bba8532Spatrick 	wp = le64toh(c->wp);
38564bba8532Spatrick 
38574bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: kernel rp=0x%llx\n", __func__, ring->rp);
38584bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: device rp=0x%llx\n", __func__, rp);
38594bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: kernel wp=0x%llx\n", __func__, ring->wp);
38604bba8532Spatrick 	DNPRINTF(QWZ_D_MHI, "%s: device wp=0x%llx\n", __func__, wp);
38614bba8532Spatrick 
38624bba8532Spatrick 	base = QWZ_DMA_DVA(ring->dmamem);
38634bba8532Spatrick 	if (ring->rp == rp || rp < base || rp >= base + ring->size)
38644bba8532Spatrick 		return 0;
38654bba8532Spatrick 
38664bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(ring->dmamem),
38674bba8532Spatrick 	    0, QWZ_DMA_LEN(ring->dmamem), BUS_DMASYNC_POSTREAD);
38684bba8532Spatrick 
38694bba8532Spatrick 	while (ring->rp != rp) {
38704bba8532Spatrick 		e = qwz_pci_event_ring_get_elem(ring, ring->rp);
38714bba8532Spatrick 		if (e == NULL)
38724bba8532Spatrick 			return 0;
38734bba8532Spatrick 
38744bba8532Spatrick 		tre0 = le32toh(e->dword[0]);
38754bba8532Spatrick 		tre1 = le32toh(e->dword[1]);
38764bba8532Spatrick 
38774bba8532Spatrick 		len = (tre0 & MHI_TRE0_EV_LEN_MASK) >> MHI_TRE0_EV_LEN_SHFT;
38784bba8532Spatrick 		code = (tre0 & MHI_TRE0_EV_CODE_MASK) >> MHI_TRE0_EV_CODE_SHFT;
38794bba8532Spatrick 		type = (tre1 & MHI_TRE1_EV_TYPE_MASK) >> MHI_TRE1_EV_TYPE_SHFT;
38804bba8532Spatrick 		chid = (tre1 & MHI_TRE1_EV_CHID_MASK) >> MHI_TRE1_EV_CHID_SHFT;
38814bba8532Spatrick 		DNPRINTF(QWZ_D_MHI, "%s: len=%u code=0x%x type=0x%x chid=%d\n",
38824bba8532Spatrick 		    __func__, len, code, type, chid);
38834bba8532Spatrick 
38844bba8532Spatrick 		switch (type) {
38854bba8532Spatrick 		case MHI_PKT_TYPE_TX_EVENT:
38864bba8532Spatrick 			qwz_pci_intr_data_event_tx(psc, e);
38874bba8532Spatrick 			break;
38884bba8532Spatrick 		default:
38894bba8532Spatrick 			printf("%s: unhandled event type 0x%x\n",
38904bba8532Spatrick 			    __func__, type);
38914bba8532Spatrick 			break;
38924bba8532Spatrick 		}
38934bba8532Spatrick 
38944bba8532Spatrick 		if (ring->rp + sizeof(*e) >= base + ring->size)
38954bba8532Spatrick 			ring->rp = base;
38964bba8532Spatrick 		else
38974bba8532Spatrick 			ring->rp += sizeof(*e);
38984bba8532Spatrick 
38994bba8532Spatrick 		if (ring->wp + sizeof(*e) >= base + ring->size)
39004bba8532Spatrick 			ring->wp = base;
39014bba8532Spatrick 		else
39024bba8532Spatrick 			ring->wp += sizeof(*e);
39034bba8532Spatrick 	}
39044bba8532Spatrick 
39054bba8532Spatrick 	c->wp = htole64(ring->wp);
39064bba8532Spatrick 
39074bba8532Spatrick 	bus_dmamap_sync(sc->sc_dmat, QWZ_DMA_MAP(psc->event_ctxt), 0,
39084bba8532Spatrick 	    QWZ_DMA_LEN(psc->event_ctxt), BUS_DMASYNC_PREWRITE);
39094bba8532Spatrick 
39104bba8532Spatrick 	qwz_mhi_ring_doorbell(sc, ring->db_addr, ring->wp);
39114bba8532Spatrick 	return 1;
39124bba8532Spatrick }
39134bba8532Spatrick 
39144bba8532Spatrick int
qwz_pci_intr_mhi_ctrl(void * arg)39154bba8532Spatrick qwz_pci_intr_mhi_ctrl(void *arg)
39164bba8532Spatrick {
39174bba8532Spatrick 	struct qwz_pci_softc *psc = arg;
39184bba8532Spatrick 
39194bba8532Spatrick 	if (qwz_pci_intr_ctrl_event(psc, &psc->event_rings[0]))
39204bba8532Spatrick 		return 1;
39214bba8532Spatrick 
39224bba8532Spatrick 	return 0;
39234bba8532Spatrick }
39244bba8532Spatrick 
39254bba8532Spatrick int
qwz_pci_intr_mhi_data(void * arg)39264bba8532Spatrick qwz_pci_intr_mhi_data(void *arg)
39274bba8532Spatrick {
39284bba8532Spatrick 	struct qwz_pci_softc *psc = arg;
39294bba8532Spatrick 
39304bba8532Spatrick 	if (qwz_pci_intr_data_event(psc, &psc->event_rings[1]))
39314bba8532Spatrick 		return 1;
39324bba8532Spatrick 
39334bba8532Spatrick 	return 0;
39344bba8532Spatrick }
39354bba8532Spatrick 
39364bba8532Spatrick int
qwz_pci_intr(void * arg)39374bba8532Spatrick qwz_pci_intr(void *arg)
39384bba8532Spatrick {
39394bba8532Spatrick 	struct qwz_pci_softc *psc = arg;
39404bba8532Spatrick 	struct qwz_softc *sc = (void *)psc;
39414bba8532Spatrick 	uint32_t ee, state;
39424bba8532Spatrick 	int ret = 0;
39434bba8532Spatrick 
39444bba8532Spatrick 	/*
39454bba8532Spatrick 	 * Interrupts can trigger before mhi_start() during boot if the device
39464bba8532Spatrick 	 * is still active after a warm reboot.
39474bba8532Spatrick 	 */
39484bba8532Spatrick 	if (psc->bhi_off == 0)
39494bba8532Spatrick 		psc->bhi_off = qwz_pci_read(sc, MHI_BHI_OFFSET);
39504bba8532Spatrick 
39514bba8532Spatrick 	ee = qwz_pci_read(sc, psc->bhi_off + MHI_BHI_EXECENV);
39524bba8532Spatrick 	state = qwz_pci_read(sc, MHI_STATUS);
39534bba8532Spatrick 	state = (state & MHI_STATUS_MHISTATE_MASK) >>
39544bba8532Spatrick 	    MHI_STATUS_MHISTATE_SHFT;
39554bba8532Spatrick 
39564bba8532Spatrick 	DNPRINTF(QWZ_D_MHI,
39574bba8532Spatrick 	    "%s: BHI interrupt with EE: 0x%x -> 0x%x state: 0x%x -> 0x%x\n",
39584bba8532Spatrick 	     sc->sc_dev.dv_xname, psc->bhi_ee, ee, psc->mhi_state, state);
39594bba8532Spatrick 
39604bba8532Spatrick 	if (ee == MHI_EE_RDDM) {
39614bba8532Spatrick 		/* Firmware crash, e.g. due to invalid DMA memory access. */
39624bba8532Spatrick 		psc->bhi_ee = ee;
39634bba8532Spatrick #ifdef QWZ_DEBUG
39644bba8532Spatrick 		if (!psc->rddm_triggered) {
39654bba8532Spatrick 			/* Write fw memory dump to root's home directory. */
39664bba8532Spatrick 			task_add(systq, &psc->rddm_task);
39674bba8532Spatrick 			psc->rddm_triggered = 1;
39684bba8532Spatrick 		}
39694bba8532Spatrick #else
39704bba8532Spatrick 		printf("%s: fatal firmware error\n",
39714bba8532Spatrick 		   sc->sc_dev.dv_xname);
39724bba8532Spatrick 		if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, sc->sc_flags) &&
39734bba8532Spatrick 		    (sc->sc_ic.ic_if.if_flags & (IFF_UP | IFF_RUNNING)) ==
39744bba8532Spatrick 		    (IFF_UP | IFF_RUNNING)) {
39754bba8532Spatrick 			/* Try to reset the device. */
39764bba8532Spatrick 			set_bit(ATH12K_FLAG_CRASH_FLUSH, sc->sc_flags);
39774bba8532Spatrick 			task_add(systq, &sc->init_task);
39784bba8532Spatrick 		}
39794bba8532Spatrick #endif
39804bba8532Spatrick 		return 1;
39814bba8532Spatrick 	} else if (psc->bhi_ee == MHI_EE_PBL || psc->bhi_ee == MHI_EE_SBL) {
39824bba8532Spatrick 		int new_ee = -1, new_mhi_state = -1;
39834bba8532Spatrick 
39844bba8532Spatrick 		if (psc->bhi_ee != ee)
39854bba8532Spatrick 			new_ee = ee;
39864bba8532Spatrick 
39874bba8532Spatrick 		if (psc->mhi_state != state)
39884bba8532Spatrick 			new_mhi_state = state;
39894bba8532Spatrick 
39904bba8532Spatrick 		if (new_ee != -1 || new_mhi_state != -1)
39914bba8532Spatrick 			qwz_mhi_state_change(psc, new_ee, new_mhi_state);
39924bba8532Spatrick 
39934bba8532Spatrick 		ret = 1;
39944bba8532Spatrick 	}
39954bba8532Spatrick 
39964bba8532Spatrick 	if (!test_bit(ATH12K_FLAG_MULTI_MSI_VECTORS, sc->sc_flags)) {
39974bba8532Spatrick 		int i;
39984bba8532Spatrick 
39994bba8532Spatrick 		if (qwz_pci_intr_ctrl_event(psc, &psc->event_rings[0]))
40004bba8532Spatrick 			ret = 1;
40014bba8532Spatrick 		if (qwz_pci_intr_data_event(psc, &psc->event_rings[1]))
40024bba8532Spatrick 			ret = 1;
40034bba8532Spatrick 
40044bba8532Spatrick 		for (i = 0; i < sc->hw_params.ce_count; i++) {
40054bba8532Spatrick 			struct qwz_ce_pipe *ce_pipe = &sc->ce.ce_pipe[i];
40064bba8532Spatrick 
40074bba8532Spatrick 			if (qwz_ce_intr(ce_pipe))
40084bba8532Spatrick 				ret = 1;
40094bba8532Spatrick 		}
40104bba8532Spatrick 
40114bba8532Spatrick 		if (test_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, sc->sc_flags)) {
40124bba8532Spatrick 			for (i = 0; i < nitems(sc->ext_irq_grp); i++) {
40134bba8532Spatrick 				if (qwz_dp_service_srng(sc, i))
40144bba8532Spatrick 					ret = 1;
40154bba8532Spatrick 			}
40164bba8532Spatrick 		}
40174bba8532Spatrick 	}
40184bba8532Spatrick 
40194bba8532Spatrick 	return ret;
40204bba8532Spatrick }
4021