xref: /openbsd-src/sys/dev/pci/if_rtwn.c (revision 0f9891f1fafd8f53a63c41edb56ce51e2589b910)
1*0f9891f1Sjsg /*	$OpenBSD: if_rtwn.c,v 1.42 2024/05/24 06:02:56 jsg Exp $	*/
260772a97Sstsp 
360772a97Sstsp /*-
460772a97Sstsp  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
560772a97Sstsp  * Copyright (c) 2015 Stefan Sperling <stsp@openbsd.org>
61f929dc7Sstsp  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
760772a97Sstsp  *
860772a97Sstsp  * Permission to use, copy, modify, and distribute this software for any
960772a97Sstsp  * purpose with or without fee is hereby granted, provided that the above
1060772a97Sstsp  * copyright notice and this permission notice appear in all copies.
1160772a97Sstsp  *
1260772a97Sstsp  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1360772a97Sstsp  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1460772a97Sstsp  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1560772a97Sstsp  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1660772a97Sstsp  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1760772a97Sstsp  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1860772a97Sstsp  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1960772a97Sstsp  */
2060772a97Sstsp 
2160772a97Sstsp /*
22600882e8Sjmatthew  * PCI front-end for Realtek RTL8188CE/RTL8188EE/RTL8192CE/RTL8723AE driver.
2360772a97Sstsp  */
2460772a97Sstsp 
2560772a97Sstsp #include "bpfilter.h"
2660772a97Sstsp 
2760772a97Sstsp #include <sys/param.h>
2860772a97Sstsp #include <sys/mbuf.h>
2960772a97Sstsp #include <sys/socket.h>
3060772a97Sstsp #include <sys/systm.h>
3160772a97Sstsp #include <sys/timeout.h>
3260772a97Sstsp #include <sys/device.h>
3360772a97Sstsp #include <sys/endian.h>
3460772a97Sstsp 
3560772a97Sstsp #include <machine/bus.h>
3660772a97Sstsp #include <machine/intr.h>
3760772a97Sstsp 
3860772a97Sstsp #if NBPFILTER > 0
3960772a97Sstsp #include <net/bpf.h>
4060772a97Sstsp #endif
4160772a97Sstsp #include <net/if.h>
4260772a97Sstsp #include <net/if_media.h>
4360772a97Sstsp 
4460772a97Sstsp #include <netinet/in.h>
4560772a97Sstsp #include <netinet/if_ether.h>
4660772a97Sstsp 
4760772a97Sstsp #include <net80211/ieee80211_var.h>
481f929dc7Sstsp #include <net80211/ieee80211_amrr.h>
4960772a97Sstsp #include <net80211/ieee80211_radiotap.h>
5060772a97Sstsp 
5160772a97Sstsp #include <dev/pci/pcireg.h>
5260772a97Sstsp #include <dev/pci/pcivar.h>
5360772a97Sstsp #include <dev/pci/pcidevs.h>
5460772a97Sstsp 
5553c3c4d0Sstsp #include <dev/ic/r92creg.h>
568b9da0f5Sstsp #include <dev/ic/rtwnvar.h>
572a915f22Sstsp 
582a915f22Sstsp /*
592a915f22Sstsp  * Driver definitions.
602a915f22Sstsp  */
61b81d0ffeSstsp 
62d8e41173Sjmatthew #define R92C_NPQ_NPAGES		0
63b81d0ffeSstsp #define R92C_PUBQ_NPAGES	176
64b81d0ffeSstsp #define R92C_HPQ_NPAGES		41
65b81d0ffeSstsp #define R92C_LPQ_NPAGES		28
66b81d0ffeSstsp #define R92C_TXPKTBUF_COUNT	256
67b81d0ffeSstsp #define R92C_TX_PAGE_COUNT	\
68b81d0ffeSstsp 	(R92C_PUBQ_NPAGES + R92C_HPQ_NPAGES + R92C_LPQ_NPAGES)
69b81d0ffeSstsp #define R92C_TX_PAGE_BOUNDARY	(R92C_TX_PAGE_COUNT + 1)
70d8e41173Sjmatthew #define R92C_MAX_RX_DMA_SIZE	0x2800
71d8e41173Sjmatthew 
72d8e41173Sjmatthew #define R88E_NPQ_NPAGES		0
73d8e41173Sjmatthew #define R88E_PUBQ_NPAGES	116
74d8e41173Sjmatthew #define R88E_HPQ_NPAGES		41
75d8e41173Sjmatthew #define R88E_LPQ_NPAGES		13
76d8e41173Sjmatthew #define R88E_TXPKTBUF_COUNT	176
77d8e41173Sjmatthew #define R88E_TX_PAGE_COUNT	\
78d8e41173Sjmatthew 	(R88E_PUBQ_NPAGES + R88E_HPQ_NPAGES + R88E_LPQ_NPAGES)
79d8e41173Sjmatthew #define R88E_TX_PAGE_BOUNDARY	(R88E_TX_PAGE_COUNT + 1)
80d8e41173Sjmatthew #define R88E_MAX_RX_DMA_SIZE	0x2600
81b81d0ffeSstsp 
82600882e8Sjmatthew #define R23A_NPQ_NPAGES		0
83600882e8Sjmatthew #define R23A_PUBQ_NPAGES	189
84600882e8Sjmatthew #define R23A_HPQ_NPAGES		28
85600882e8Sjmatthew #define R23A_LPQ_NPAGES		28
86600882e8Sjmatthew #define R23A_TXPKTBUF_COUNT	256
87600882e8Sjmatthew #define R23A_TX_PAGE_COUNT	\
88600882e8Sjmatthew 	(R23A_PUBQ_NPAGES + R23A_HPQ_NPAGES + R23A_LPQ_NPAGES)
89600882e8Sjmatthew #define R23A_TX_PAGE_BOUNDARY	(R23A_TX_PAGE_COUNT + 1)
90600882e8Sjmatthew #define R23A_MAX_RX_DMA_SIZE	0x2800
91600882e8Sjmatthew 
922a915f22Sstsp #define RTWN_NTXQUEUES			9
932a915f22Sstsp #define RTWN_RX_LIST_COUNT		256
942a915f22Sstsp #define RTWN_TX_LIST_COUNT		256
952a915f22Sstsp 
962a915f22Sstsp /* TX queue indices. */
972a915f22Sstsp #define RTWN_BK_QUEUE			0
982a915f22Sstsp #define RTWN_BE_QUEUE			1
992a915f22Sstsp #define RTWN_VI_QUEUE			2
1002a915f22Sstsp #define RTWN_VO_QUEUE			3
1012a915f22Sstsp #define RTWN_BEACON_QUEUE		4
1022a915f22Sstsp #define RTWN_TXCMD_QUEUE		5
1032a915f22Sstsp #define RTWN_MGNT_QUEUE			6
1042a915f22Sstsp #define RTWN_HIGH_QUEUE			7
1052a915f22Sstsp #define RTWN_HCCA_QUEUE			8
1062a915f22Sstsp 
1072a915f22Sstsp struct rtwn_rx_radiotap_header {
1082a915f22Sstsp 	struct ieee80211_radiotap_header wr_ihdr;
1092a915f22Sstsp 	uint8_t		wr_flags;
1102a915f22Sstsp 	uint8_t		wr_rate;
1112a915f22Sstsp 	uint16_t	wr_chan_freq;
1122a915f22Sstsp 	uint16_t	wr_chan_flags;
1132a915f22Sstsp 	uint8_t		wr_dbm_antsignal;
1142a915f22Sstsp } __packed;
1152a915f22Sstsp 
1162a915f22Sstsp #define RTWN_RX_RADIOTAP_PRESENT			\
1172a915f22Sstsp 	(1 << IEEE80211_RADIOTAP_FLAGS |		\
1182a915f22Sstsp 	 1 << IEEE80211_RADIOTAP_RATE |			\
1192a915f22Sstsp 	 1 << IEEE80211_RADIOTAP_CHANNEL |		\
1202a915f22Sstsp 	 1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL)
1212a915f22Sstsp 
1222a915f22Sstsp struct rtwn_tx_radiotap_header {
1232a915f22Sstsp 	struct ieee80211_radiotap_header wt_ihdr;
1242a915f22Sstsp 	uint8_t		wt_flags;
1252a915f22Sstsp 	uint16_t	wt_chan_freq;
1262a915f22Sstsp 	uint16_t	wt_chan_flags;
1272a915f22Sstsp } __packed;
1282a915f22Sstsp 
1292a915f22Sstsp #define RTWN_TX_RADIOTAP_PRESENT			\
1302a915f22Sstsp 	(1 << IEEE80211_RADIOTAP_FLAGS |		\
1312a915f22Sstsp 	 1 << IEEE80211_RADIOTAP_CHANNEL)
1322a915f22Sstsp 
1332a915f22Sstsp struct rtwn_rx_data {
1342a915f22Sstsp 	bus_dmamap_t		map;
1352a915f22Sstsp 	struct mbuf		*m;
1362a915f22Sstsp };
1372a915f22Sstsp 
1382a915f22Sstsp struct rtwn_rx_ring {
1392a915f22Sstsp 	struct r92c_rx_desc_pci	*desc;
1402a915f22Sstsp 	bus_dmamap_t		map;
1412a915f22Sstsp 	bus_dma_segment_t	seg;
1422a915f22Sstsp 	int			nsegs;
1432a915f22Sstsp 	struct rtwn_rx_data	rx_data[RTWN_RX_LIST_COUNT];
1442a915f22Sstsp 
1452a915f22Sstsp };
1462a915f22Sstsp struct rtwn_tx_data {
1472a915f22Sstsp 	bus_dmamap_t			map;
1482a915f22Sstsp 	struct mbuf			*m;
1492a915f22Sstsp 	struct ieee80211_node		*ni;
1502a915f22Sstsp };
1512a915f22Sstsp 
1522a915f22Sstsp struct rtwn_tx_ring {
1532a915f22Sstsp 	bus_dmamap_t		map;
1542a915f22Sstsp 	bus_dma_segment_t	seg;
1552a915f22Sstsp 	int			nsegs;
1562a915f22Sstsp 	struct r92c_tx_desc_pci	*desc;
1572a915f22Sstsp 	struct rtwn_tx_data	tx_data[RTWN_TX_LIST_COUNT];
1582a915f22Sstsp 	int			queued;
1592a915f22Sstsp 	int			cur;
1602a915f22Sstsp };
1612a915f22Sstsp 
1628b9da0f5Sstsp struct rtwn_pci_softc {
1632a915f22Sstsp 	struct device		sc_dev;
1648b9da0f5Sstsp 	struct rtwn_softc	sc_sc;
1658b9da0f5Sstsp 
1668b9da0f5Sstsp 	struct rtwn_rx_ring	rx_ring;
1678b9da0f5Sstsp 	struct rtwn_tx_ring	tx_ring[RTWN_NTXQUEUES];
1688b9da0f5Sstsp 	uint32_t		qfullmsk;
1692a915f22Sstsp 
170e9f037d1Sstsp 	struct timeout		calib_to;
171e9f037d1Sstsp 	struct timeout		scan_to;
172e9f037d1Sstsp 
1732a915f22Sstsp 	/* PCI specific goo. */
1742a915f22Sstsp 	bus_dma_tag_t 		sc_dmat;
1752a915f22Sstsp 	pci_chipset_tag_t	sc_pc;
1762a915f22Sstsp 	pcitag_t		sc_tag;
1772a915f22Sstsp 	void			*sc_ih;
1782a915f22Sstsp 	bus_space_tag_t		sc_st;
1792a915f22Sstsp 	bus_space_handle_t	sc_sh;
1802a915f22Sstsp 	bus_size_t		sc_mapsize;
1812a915f22Sstsp 	int			sc_cap_off;
1822a915f22Sstsp 
1831f929dc7Sstsp 	struct ieee80211_amrr		amrr;
1841f929dc7Sstsp 	struct ieee80211_amrr_node	amn;
1851f929dc7Sstsp 
1862a915f22Sstsp #if NBPFILTER > 0
1872a915f22Sstsp 	caddr_t				sc_drvbpf;
1882a915f22Sstsp 
1892a915f22Sstsp 	union {
1902a915f22Sstsp 		struct rtwn_rx_radiotap_header th;
1912a915f22Sstsp 		uint8_t	pad[64];
1922a915f22Sstsp 	}				sc_rxtapu;
1932a915f22Sstsp #define sc_rxtap	sc_rxtapu.th
1942a915f22Sstsp 	int				sc_rxtap_len;
1952a915f22Sstsp 
1962a915f22Sstsp 	union {
1972a915f22Sstsp 		struct rtwn_tx_radiotap_header th;
1982a915f22Sstsp 		uint8_t	pad[64];
1992a915f22Sstsp 	}				sc_txtapu;
2002a915f22Sstsp #define sc_txtap	sc_txtapu.th
2012a915f22Sstsp 	int				sc_txtap_len;
2022a915f22Sstsp #endif
2032a915f22Sstsp };
2042a915f22Sstsp 
20560772a97Sstsp #ifdef RTWN_DEBUG
20660772a97Sstsp #define DPRINTF(x)	do { if (rtwn_debug) printf x; } while (0)
20760772a97Sstsp #define DPRINTFN(n, x)	do { if (rtwn_debug >= (n)) printf x; } while (0)
2088b9da0f5Sstsp extern int rtwn_debug;
20960772a97Sstsp #else
21060772a97Sstsp #define DPRINTF(x)
21160772a97Sstsp #define DPRINTFN(n, x)
21260772a97Sstsp #endif
21360772a97Sstsp 
21460772a97Sstsp /*
21560772a97Sstsp  * PCI configuration space registers.
21660772a97Sstsp  */
21760772a97Sstsp #define	RTWN_PCI_IOBA		0x10	/* i/o mapped base */
21860772a97Sstsp #define	RTWN_PCI_MMBA		0x18	/* memory mapped base */
21960772a97Sstsp 
22060772a97Sstsp static const struct pci_matchid rtwn_pci_devices[] = {
22168f52d7eSkevlo 	{ PCI_VENDOR_REALTEK,	PCI_PRODUCT_REALTEK_RTL8188CE },
222d8e41173Sjmatthew 	{ PCI_VENDOR_REALTEK,	PCI_PRODUCT_REALTEK_RTL8188EE },
223600882e8Sjmatthew 	{ PCI_VENDOR_REALTEK,	PCI_PRODUCT_REALTEK_RTL8192CE },
224600882e8Sjmatthew 	{ PCI_VENDOR_REALTEK,	PCI_PRODUCT_REALTEK_RTL8723AE }
22560772a97Sstsp };
22660772a97Sstsp 
2278b9da0f5Sstsp int		rtwn_pci_match(struct device *, void *, void *);
2288b9da0f5Sstsp void		rtwn_pci_attach(struct device *, struct device *, void *);
2298b9da0f5Sstsp int		rtwn_pci_detach(struct device *, int);
2308b9da0f5Sstsp int		rtwn_pci_activate(struct device *, int);
2318b9da0f5Sstsp int		rtwn_alloc_rx_list(struct rtwn_pci_softc *);
2328b9da0f5Sstsp void		rtwn_reset_rx_list(struct rtwn_pci_softc *);
2338b9da0f5Sstsp void		rtwn_free_rx_list(struct rtwn_pci_softc *);
2348b9da0f5Sstsp void		rtwn_setup_rx_desc(struct rtwn_pci_softc *,
2358caabc14Sstsp 		    struct r92c_rx_desc_pci *, bus_addr_t, size_t, int);
2368b9da0f5Sstsp int		rtwn_alloc_tx_list(struct rtwn_pci_softc *, int);
2378b9da0f5Sstsp void		rtwn_reset_tx_list(struct rtwn_pci_softc *, int);
2388b9da0f5Sstsp void		rtwn_free_tx_list(struct rtwn_pci_softc *, int);
2398b9da0f5Sstsp void		rtwn_pci_write_1(void *, uint16_t, uint8_t);
2408b9da0f5Sstsp void		rtwn_pci_write_2(void *, uint16_t, uint16_t);
2418b9da0f5Sstsp void		rtwn_pci_write_4(void *, uint16_t, uint32_t);
2428b9da0f5Sstsp uint8_t		rtwn_pci_read_1(void *, uint16_t);
2438b9da0f5Sstsp uint16_t	rtwn_pci_read_2(void *, uint16_t);
2448b9da0f5Sstsp uint32_t	rtwn_pci_read_4(void *, uint16_t);
2458b9da0f5Sstsp void		rtwn_rx_frame(struct rtwn_pci_softc *,
2468fbaf8a2Sstsp 		    struct r92c_rx_desc_pci *, struct rtwn_rx_data *, int,
2478fbaf8a2Sstsp 		    struct mbuf_list *);
2488b9da0f5Sstsp int		rtwn_tx(void *, struct mbuf *, struct ieee80211_node *);
2498b9da0f5Sstsp void		rtwn_tx_done(struct rtwn_pci_softc *, int);
25023340becSstsp int		rtwn_alloc_buffers(void *);
25123340becSstsp int		rtwn_pci_init(void *);
252d8e41173Sjmatthew void		rtwn_pci_88e_stop(struct rtwn_pci_softc *);
2538b9da0f5Sstsp void		rtwn_pci_stop(void *);
25460772a97Sstsp int		rtwn_intr(void *);
2558b9da0f5Sstsp int		rtwn_is_oactive(void *);
256d8e41173Sjmatthew int		rtwn_92c_power_on(struct rtwn_pci_softc *);
257d8e41173Sjmatthew int		rtwn_88e_power_on(struct rtwn_pci_softc *);
258600882e8Sjmatthew int		rtwn_23a_power_on(struct rtwn_pci_softc *);
2595fda900fSstsp int		rtwn_power_on(void *);
260b81d0ffeSstsp int		rtwn_llt_write(struct rtwn_pci_softc *, uint32_t, uint32_t);
261d8e41173Sjmatthew int		rtwn_llt_init(struct rtwn_pci_softc *, int);
262b81d0ffeSstsp int		rtwn_dma_init(void *);
26323340becSstsp int		rtwn_fw_loadpage(void *, int, uint8_t *, int);
2645fda900fSstsp int		rtwn_pci_load_firmware(void *, u_char **, size_t *);
2655fda900fSstsp void		rtwn_mac_init(void *);
2665fda900fSstsp void		rtwn_bb_init(void *);
267e9f037d1Sstsp void		rtwn_calib_to(void *);
268e9f037d1Sstsp void		rtwn_next_calib(void *);
269e9f037d1Sstsp void		rtwn_cancel_calib(void *);
270e9f037d1Sstsp void		rtwn_scan_to(void *);
271e9f037d1Sstsp void		rtwn_pci_next_scan(void *);
272e9f037d1Sstsp void		rtwn_cancel_scan(void *);
27323340becSstsp void		rtwn_wait_async(void *);
2741f929dc7Sstsp void		rtwn_poll_c2h_events(struct rtwn_pci_softc *);
2751f929dc7Sstsp void		rtwn_tx_report(struct rtwn_pci_softc *, uint8_t *, int);
27660772a97Sstsp 
2775fda900fSstsp /* Aliases. */
2785fda900fSstsp #define	rtwn_bb_write	rtwn_pci_write_4
2795fda900fSstsp #define rtwn_bb_read	rtwn_pci_read_4
2805fda900fSstsp 
28160772a97Sstsp struct cfdriver rtwn_cd = {
28260772a97Sstsp 	NULL, "rtwn", DV_IFNET
28360772a97Sstsp };
28460772a97Sstsp 
2858b9da0f5Sstsp const struct cfattach rtwn_pci_ca = {
2868b9da0f5Sstsp 	sizeof(struct rtwn_pci_softc),
2878b9da0f5Sstsp 	rtwn_pci_match,
2888b9da0f5Sstsp 	rtwn_pci_attach,
2898b9da0f5Sstsp 	rtwn_pci_detach,
2908b9da0f5Sstsp 	rtwn_pci_activate
29160772a97Sstsp };
29260772a97Sstsp 
29360772a97Sstsp int
rtwn_pci_match(struct device * parent,void * match,void * aux)2948b9da0f5Sstsp rtwn_pci_match(struct device *parent, void *match, void *aux)
29560772a97Sstsp {
29660772a97Sstsp 	return (pci_matchbyid(aux, rtwn_pci_devices,
29760772a97Sstsp 	    nitems(rtwn_pci_devices)));
29860772a97Sstsp }
29960772a97Sstsp 
30060772a97Sstsp void
rtwn_pci_attach(struct device * parent,struct device * self,void * aux)3018b9da0f5Sstsp rtwn_pci_attach(struct device *parent, struct device *self, void *aux)
30260772a97Sstsp {
3038b9da0f5Sstsp 	struct rtwn_pci_softc *sc = (struct rtwn_pci_softc*)self;
30460772a97Sstsp 	struct pci_attach_args *pa = aux;
3058b9da0f5Sstsp 	struct ifnet *ifp;
30660772a97Sstsp 	int i, error;
30760772a97Sstsp 	pcireg_t memtype;
30860772a97Sstsp 	pci_intr_handle_t ih;
30960772a97Sstsp 	const char *intrstr;
31060772a97Sstsp 
31160772a97Sstsp 	sc->sc_dmat = pa->pa_dmat;
31260772a97Sstsp 	sc->sc_pc = pa->pa_pc;
31360772a97Sstsp 	sc->sc_tag = pa->pa_tag;
31460772a97Sstsp 
315e9f037d1Sstsp 	timeout_set(&sc->calib_to, rtwn_calib_to, sc);
316e9f037d1Sstsp 	timeout_set(&sc->scan_to, rtwn_scan_to, sc);
317e9f037d1Sstsp 
31860772a97Sstsp 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
31960772a97Sstsp 
32060772a97Sstsp 	/* Map control/status registers. */
32160772a97Sstsp 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTWN_PCI_MMBA);
32260772a97Sstsp 	error = pci_mapreg_map(pa, RTWN_PCI_MMBA, memtype, 0, &sc->sc_st,
32360772a97Sstsp 	    &sc->sc_sh, NULL, &sc->sc_mapsize, 0);
32460772a97Sstsp 	if (error != 0) {
32560772a97Sstsp 		printf(": can't map mem space\n");
32660772a97Sstsp 		return;
32760772a97Sstsp 	}
32860772a97Sstsp 
32960772a97Sstsp 	if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
33060772a97Sstsp 		printf(": can't map interrupt\n");
33160772a97Sstsp 		return;
33260772a97Sstsp 	}
33360772a97Sstsp 	intrstr = pci_intr_string(sc->sc_pc, ih);
33460772a97Sstsp 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET,
33560772a97Sstsp 	    rtwn_intr, sc, sc->sc_dev.dv_xname);
33660772a97Sstsp 	if (sc->sc_ih == NULL) {
33760772a97Sstsp 		printf(": can't establish interrupt");
33860772a97Sstsp 		if (intrstr != NULL)
33960772a97Sstsp 			printf(" at %s", intrstr);
34060772a97Sstsp 		printf("\n");
34160772a97Sstsp 		return;
34260772a97Sstsp 	}
34360772a97Sstsp 	printf(": %s\n", intrstr);
34460772a97Sstsp 
34560772a97Sstsp 	/* Disable PCIe Active State Power Management (ASPM). */
34660772a97Sstsp 	if (pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_PCIEXPRESS,
34760772a97Sstsp 	    &sc->sc_cap_off, NULL)) {
34860772a97Sstsp 		uint32_t lcsr = pci_conf_read(sc->sc_pc, sc->sc_tag,
34960772a97Sstsp 		    sc->sc_cap_off + PCI_PCIE_LCSR);
35060772a97Sstsp 		lcsr &= ~(PCI_PCIE_LCSR_ASPM_L0S | PCI_PCIE_LCSR_ASPM_L1);
35160772a97Sstsp 		pci_conf_write(sc->sc_pc, sc->sc_tag,
35260772a97Sstsp 		    sc->sc_cap_off + PCI_PCIE_LCSR, lcsr);
35360772a97Sstsp 	}
35460772a97Sstsp 
35560772a97Sstsp 	/* Allocate Tx/Rx buffers. */
35660772a97Sstsp 	error = rtwn_alloc_rx_list(sc);
35760772a97Sstsp 	if (error != 0) {
35860772a97Sstsp 		printf("%s: could not allocate Rx buffers\n",
35960772a97Sstsp 		    sc->sc_dev.dv_xname);
36060772a97Sstsp 		return;
36160772a97Sstsp 	}
36260772a97Sstsp 	for (i = 0; i < RTWN_NTXQUEUES; i++) {
36360772a97Sstsp 		error = rtwn_alloc_tx_list(sc, i);
36460772a97Sstsp 		if (error != 0) {
36560772a97Sstsp 			printf("%s: could not allocate Tx buffers\n",
36660772a97Sstsp 			    sc->sc_dev.dv_xname);
367fde5a185Sstsp 			rtwn_free_rx_list(sc);
36860772a97Sstsp 			return;
36960772a97Sstsp 		}
37060772a97Sstsp 	}
37160772a97Sstsp 
3721f929dc7Sstsp 	sc->amrr.amrr_min_success_threshold = 1;
3731f929dc7Sstsp 	sc->amrr.amrr_max_success_threshold = 15;
3741f929dc7Sstsp 
3758b9da0f5Sstsp 	/* Attach the bus-agnostic driver. */
3768b9da0f5Sstsp 	sc->sc_sc.sc_ops.cookie = sc;
3778b9da0f5Sstsp 	sc->sc_sc.sc_ops.write_1 = rtwn_pci_write_1;
3788b9da0f5Sstsp 	sc->sc_sc.sc_ops.write_2 = rtwn_pci_write_2;
3798b9da0f5Sstsp 	sc->sc_sc.sc_ops.write_4 = rtwn_pci_write_4;
3808b9da0f5Sstsp 	sc->sc_sc.sc_ops.read_1 = rtwn_pci_read_1;
3818b9da0f5Sstsp 	sc->sc_sc.sc_ops.read_2 = rtwn_pci_read_2;
3828b9da0f5Sstsp 	sc->sc_sc.sc_ops.read_4 = rtwn_pci_read_4;
3838b9da0f5Sstsp 	sc->sc_sc.sc_ops.tx = rtwn_tx;
3845fda900fSstsp 	sc->sc_sc.sc_ops.power_on = rtwn_power_on;
385b81d0ffeSstsp 	sc->sc_sc.sc_ops.dma_init = rtwn_dma_init;
3865fda900fSstsp 	sc->sc_sc.sc_ops.load_firmware = rtwn_pci_load_firmware;
38723340becSstsp 	sc->sc_sc.sc_ops.fw_loadpage = rtwn_fw_loadpage;
3885fda900fSstsp 	sc->sc_sc.sc_ops.mac_init = rtwn_mac_init;
3895fda900fSstsp 	sc->sc_sc.sc_ops.bb_init = rtwn_bb_init;
39023340becSstsp 	sc->sc_sc.sc_ops.alloc_buffers = rtwn_alloc_buffers;
39123340becSstsp 	sc->sc_sc.sc_ops.init = rtwn_pci_init;
3928b9da0f5Sstsp 	sc->sc_sc.sc_ops.stop = rtwn_pci_stop;
3938b9da0f5Sstsp 	sc->sc_sc.sc_ops.is_oactive = rtwn_is_oactive;
394e9f037d1Sstsp 	sc->sc_sc.sc_ops.next_calib = rtwn_next_calib;
395e9f037d1Sstsp 	sc->sc_sc.sc_ops.cancel_calib = rtwn_cancel_calib;
396e9f037d1Sstsp 	sc->sc_sc.sc_ops.next_scan = rtwn_pci_next_scan;
397e9f037d1Sstsp 	sc->sc_sc.sc_ops.cancel_scan = rtwn_cancel_scan;
39823340becSstsp 	sc->sc_sc.sc_ops.wait_async = rtwn_wait_async;
39934a2cadeSkevlo 
400d8e41173Sjmatthew 	sc->sc_sc.chip = RTWN_CHIP_PCI;
401d8e41173Sjmatthew 	switch (PCI_PRODUCT(pa->pa_id)) {
402d8e41173Sjmatthew 	case PCI_PRODUCT_REALTEK_RTL8188CE:
403d8e41173Sjmatthew 	case PCI_PRODUCT_REALTEK_RTL8192CE:
404d8e41173Sjmatthew 		sc->sc_sc.chip |= RTWN_CHIP_88C | RTWN_CHIP_92C;
405d8e41173Sjmatthew 		break;
406d8e41173Sjmatthew 	case PCI_PRODUCT_REALTEK_RTL8188EE:
407d8e41173Sjmatthew 		sc->sc_sc.chip |= RTWN_CHIP_88E;
408d8e41173Sjmatthew 		break;
409600882e8Sjmatthew 	case PCI_PRODUCT_REALTEK_RTL8723AE:
410600882e8Sjmatthew 		sc->sc_sc.chip |= RTWN_CHIP_23A;
411600882e8Sjmatthew 		break;
412d8e41173Sjmatthew 	}
41334a2cadeSkevlo 
41434a2cadeSkevlo 	error = rtwn_attach(&sc->sc_dev, &sc->sc_sc);
415fde5a185Sstsp 	if (error != 0) {
416fde5a185Sstsp 		rtwn_free_rx_list(sc);
417fde5a185Sstsp 		for (i = 0; i < RTWN_NTXQUEUES; i++)
418fde5a185Sstsp 			rtwn_free_tx_list(sc, i);
419fde5a185Sstsp 		return;
420fde5a185Sstsp 	}
42160772a97Sstsp 
4228b9da0f5Sstsp 	/* ifp is now valid */
4238b9da0f5Sstsp 	ifp = &sc->sc_sc.sc_ic.ic_if;
42460772a97Sstsp #if NBPFILTER > 0
42560772a97Sstsp 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
42660772a97Sstsp 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
42760772a97Sstsp 
42860772a97Sstsp 	sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
42960772a97Sstsp 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
43060772a97Sstsp 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RTWN_RX_RADIOTAP_PRESENT);
43160772a97Sstsp 
43260772a97Sstsp 	sc->sc_txtap_len = sizeof(sc->sc_txtapu);
43360772a97Sstsp 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
43460772a97Sstsp 	sc->sc_txtap.wt_ihdr.it_present = htole32(RTWN_TX_RADIOTAP_PRESENT);
43560772a97Sstsp #endif
43660772a97Sstsp }
43760772a97Sstsp 
43860772a97Sstsp int
rtwn_pci_detach(struct device * self,int flags)4398b9da0f5Sstsp rtwn_pci_detach(struct device *self, int flags)
44060772a97Sstsp {
4418b9da0f5Sstsp 	struct rtwn_pci_softc *sc = (struct rtwn_pci_softc *)self;
44260772a97Sstsp 	int s, i;
44360772a97Sstsp 
444e9f037d1Sstsp 	s = splnet();
445e9f037d1Sstsp 
446e9f037d1Sstsp 	if (timeout_initialized(&sc->calib_to))
447e9f037d1Sstsp 		timeout_del(&sc->calib_to);
448e9f037d1Sstsp 	if (timeout_initialized(&sc->scan_to))
449e9f037d1Sstsp 		timeout_del(&sc->scan_to);
450e9f037d1Sstsp 
4518b9da0f5Sstsp 	rtwn_detach(&sc->sc_sc, flags);
4528b9da0f5Sstsp 
45360772a97Sstsp 	/* Free Tx/Rx buffers. */
45460772a97Sstsp 	for (i = 0; i < RTWN_NTXQUEUES; i++)
45560772a97Sstsp 		rtwn_free_tx_list(sc, i);
45660772a97Sstsp 	rtwn_free_rx_list(sc);
45760772a97Sstsp 	splx(s);
45860772a97Sstsp 
45960772a97Sstsp 	return (0);
46060772a97Sstsp }
46160772a97Sstsp 
46260772a97Sstsp int
rtwn_pci_activate(struct device * self,int act)4638b9da0f5Sstsp rtwn_pci_activate(struct device *self, int act)
46460772a97Sstsp {
4658b9da0f5Sstsp 	struct rtwn_pci_softc *sc = (struct rtwn_pci_softc *)self;
46660772a97Sstsp 
4678b9da0f5Sstsp 	return rtwn_activate(&sc->sc_sc, act);
46860772a97Sstsp }
46960772a97Sstsp 
47060772a97Sstsp void
rtwn_setup_rx_desc(struct rtwn_pci_softc * sc,struct r92c_rx_desc_pci * desc,bus_addr_t addr,size_t len,int idx)4718b9da0f5Sstsp rtwn_setup_rx_desc(struct rtwn_pci_softc *sc, struct r92c_rx_desc_pci *desc,
47260772a97Sstsp     bus_addr_t addr, size_t len, int idx)
47360772a97Sstsp {
47460772a97Sstsp 	memset(desc, 0, sizeof(*desc));
47560772a97Sstsp 	desc->rxdw0 = htole32(SM(R92C_RXDW0_PKTLEN, len) |
47660772a97Sstsp 		((idx == RTWN_RX_LIST_COUNT - 1) ? R92C_RXDW0_EOR : 0));
47760772a97Sstsp 	desc->rxbufaddr = htole32(addr);
47860772a97Sstsp 	bus_space_barrier(sc->sc_st, sc->sc_sh, 0, sc->sc_mapsize,
47960772a97Sstsp 	    BUS_SPACE_BARRIER_WRITE);
48060772a97Sstsp 	desc->rxdw0 |= htole32(R92C_RXDW0_OWN);
48160772a97Sstsp }
48260772a97Sstsp 
48360772a97Sstsp int
rtwn_alloc_rx_list(struct rtwn_pci_softc * sc)4848b9da0f5Sstsp rtwn_alloc_rx_list(struct rtwn_pci_softc *sc)
48560772a97Sstsp {
48660772a97Sstsp 	struct rtwn_rx_ring *rx_ring = &sc->rx_ring;
48760772a97Sstsp 	struct rtwn_rx_data *rx_data;
48860772a97Sstsp 	size_t size;
48960772a97Sstsp 	int i, error = 0;
49060772a97Sstsp 
49160772a97Sstsp 	/* Allocate Rx descriptors. */
4928caabc14Sstsp 	size = sizeof(struct r92c_rx_desc_pci) * RTWN_RX_LIST_COUNT;
49360772a97Sstsp 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, BUS_DMA_NOWAIT,
49460772a97Sstsp 		&rx_ring->map);
49560772a97Sstsp 	if (error != 0) {
49660772a97Sstsp 		printf("%s: could not create rx desc DMA map\n",
49760772a97Sstsp 		    sc->sc_dev.dv_xname);
49860772a97Sstsp 		rx_ring->map = NULL;
49960772a97Sstsp 		goto fail;
50060772a97Sstsp 	}
50160772a97Sstsp 
50260772a97Sstsp 	error = bus_dmamem_alloc(sc->sc_dmat, size, 0, 0, &rx_ring->seg, 1,
50360772a97Sstsp 	    &rx_ring->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
50460772a97Sstsp 	if (error != 0) {
50560772a97Sstsp 		printf("%s: could not allocate rx desc\n",
50660772a97Sstsp 		    sc->sc_dev.dv_xname);
50760772a97Sstsp 		goto fail;
50860772a97Sstsp 	}
50960772a97Sstsp 
51060772a97Sstsp 	error = bus_dmamem_map(sc->sc_dmat, &rx_ring->seg, rx_ring->nsegs,
51160772a97Sstsp 	    size, (caddr_t *)&rx_ring->desc,
51260772a97Sstsp 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
51360772a97Sstsp 	if (error != 0) {
51460772a97Sstsp 		bus_dmamem_free(sc->sc_dmat, &rx_ring->seg, rx_ring->nsegs);
51560772a97Sstsp 		rx_ring->desc = NULL;
51660772a97Sstsp 		printf("%s: could not map rx desc\n", sc->sc_dev.dv_xname);
51760772a97Sstsp 		goto fail;
51860772a97Sstsp 	}
51960772a97Sstsp 
52060772a97Sstsp 	error = bus_dmamap_load_raw(sc->sc_dmat, rx_ring->map, &rx_ring->seg,
52160772a97Sstsp 	    1, size, BUS_DMA_NOWAIT);
52260772a97Sstsp 	if (error != 0) {
52360772a97Sstsp 		printf("%s: could not load rx desc\n",
52460772a97Sstsp 		    sc->sc_dev.dv_xname);
52560772a97Sstsp 		goto fail;
52660772a97Sstsp 	}
52760772a97Sstsp 
52860772a97Sstsp 	bus_dmamap_sync(sc->sc_dmat, rx_ring->map, 0, size,
52960772a97Sstsp 	    BUS_DMASYNC_PREWRITE);
53060772a97Sstsp 
53160772a97Sstsp 	/* Allocate Rx buffers. */
53260772a97Sstsp 	for (i = 0; i < RTWN_RX_LIST_COUNT; i++) {
53360772a97Sstsp 		rx_data = &rx_ring->rx_data[i];
53460772a97Sstsp 
53560772a97Sstsp 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
53660772a97Sstsp 		    0, BUS_DMA_NOWAIT, &rx_data->map);
53760772a97Sstsp 		if (error != 0) {
53860772a97Sstsp 			printf("%s: could not create rx buf DMA map\n",
53960772a97Sstsp 			    sc->sc_dev.dv_xname);
54060772a97Sstsp 			goto fail;
54160772a97Sstsp 		}
54260772a97Sstsp 
543471f2571Sjan 		rx_data->m = MCLGETL(NULL, M_DONTWAIT, MCLBYTES);
54460772a97Sstsp 		if (rx_data->m == NULL) {
54560772a97Sstsp 			printf("%s: could not allocate rx mbuf\n",
54660772a97Sstsp 			    sc->sc_dev.dv_xname);
54760772a97Sstsp 			error = ENOMEM;
54860772a97Sstsp 			goto fail;
54960772a97Sstsp 		}
55060772a97Sstsp 
55160772a97Sstsp 		error = bus_dmamap_load(sc->sc_dmat, rx_data->map,
55260772a97Sstsp 		    mtod(rx_data->m, void *), MCLBYTES, NULL,
55360772a97Sstsp 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
55460772a97Sstsp 		if (error != 0) {
55560772a97Sstsp 			printf("%s: could not load rx buf DMA map\n",
55660772a97Sstsp 			    sc->sc_dev.dv_xname);
55760772a97Sstsp 			goto fail;
55860772a97Sstsp 		}
55960772a97Sstsp 
56060772a97Sstsp 		rtwn_setup_rx_desc(sc, &rx_ring->desc[i],
56160772a97Sstsp 		    rx_data->map->dm_segs[0].ds_addr, MCLBYTES, i);
56260772a97Sstsp 	}
56360772a97Sstsp fail:	if (error != 0)
56460772a97Sstsp 		rtwn_free_rx_list(sc);
56560772a97Sstsp 	return (error);
56660772a97Sstsp }
56760772a97Sstsp 
56860772a97Sstsp void
rtwn_reset_rx_list(struct rtwn_pci_softc * sc)5698b9da0f5Sstsp rtwn_reset_rx_list(struct rtwn_pci_softc *sc)
57060772a97Sstsp {
57160772a97Sstsp 	struct rtwn_rx_ring *rx_ring = &sc->rx_ring;
57260772a97Sstsp 	struct rtwn_rx_data *rx_data;
57360772a97Sstsp 	int i;
57460772a97Sstsp 
57560772a97Sstsp 	for (i = 0; i < RTWN_RX_LIST_COUNT; i++) {
57660772a97Sstsp 		rx_data = &rx_ring->rx_data[i];
57760772a97Sstsp 		rtwn_setup_rx_desc(sc, &rx_ring->desc[i],
57860772a97Sstsp 		    rx_data->map->dm_segs[0].ds_addr, MCLBYTES, i);
57960772a97Sstsp 	}
58060772a97Sstsp }
58160772a97Sstsp 
58260772a97Sstsp void
rtwn_free_rx_list(struct rtwn_pci_softc * sc)5838b9da0f5Sstsp rtwn_free_rx_list(struct rtwn_pci_softc *sc)
58460772a97Sstsp {
58560772a97Sstsp 	struct rtwn_rx_ring *rx_ring = &sc->rx_ring;
58660772a97Sstsp 	struct rtwn_rx_data *rx_data;
58760772a97Sstsp 	int i, s;
58860772a97Sstsp 
58960772a97Sstsp 	s = splnet();
59060772a97Sstsp 
59160772a97Sstsp 	if (rx_ring->map) {
59260772a97Sstsp 		if (rx_ring->desc) {
59360772a97Sstsp 			bus_dmamap_unload(sc->sc_dmat, rx_ring->map);
59460772a97Sstsp 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)rx_ring->desc,
5958caabc14Sstsp 			    sizeof (struct r92c_rx_desc_pci) *
5968caabc14Sstsp 			    RTWN_RX_LIST_COUNT);
59760772a97Sstsp 			bus_dmamem_free(sc->sc_dmat, &rx_ring->seg,
59860772a97Sstsp 			    rx_ring->nsegs);
59960772a97Sstsp 			rx_ring->desc = NULL;
60060772a97Sstsp 		}
60160772a97Sstsp 		bus_dmamap_destroy(sc->sc_dmat, rx_ring->map);
60260772a97Sstsp 		rx_ring->map = NULL;
60360772a97Sstsp 	}
60460772a97Sstsp 
60560772a97Sstsp 	for (i = 0; i < RTWN_RX_LIST_COUNT; i++) {
60660772a97Sstsp 		rx_data = &rx_ring->rx_data[i];
60760772a97Sstsp 
60860772a97Sstsp 		if (rx_data->m != NULL) {
60960772a97Sstsp 			bus_dmamap_unload(sc->sc_dmat, rx_data->map);
61060772a97Sstsp 			m_freem(rx_data->m);
61160772a97Sstsp 			rx_data->m = NULL;
61260772a97Sstsp 		}
61360772a97Sstsp 		bus_dmamap_destroy(sc->sc_dmat, rx_data->map);
61460772a97Sstsp 		rx_data->map = NULL;
61560772a97Sstsp 	}
61660772a97Sstsp 
61760772a97Sstsp 	splx(s);
61860772a97Sstsp }
61960772a97Sstsp 
62060772a97Sstsp int
rtwn_alloc_tx_list(struct rtwn_pci_softc * sc,int qid)6218b9da0f5Sstsp rtwn_alloc_tx_list(struct rtwn_pci_softc *sc, int qid)
62260772a97Sstsp {
62360772a97Sstsp 	struct rtwn_tx_ring *tx_ring = &sc->tx_ring[qid];
62460772a97Sstsp 	struct rtwn_tx_data *tx_data;
62560772a97Sstsp 	int i = 0, error = 0;
62660772a97Sstsp 
62760772a97Sstsp 	error = bus_dmamap_create(sc->sc_dmat,
6288caabc14Sstsp 	    sizeof (struct r92c_tx_desc_pci) * RTWN_TX_LIST_COUNT, 1,
6298caabc14Sstsp 	    sizeof (struct r92c_tx_desc_pci) * RTWN_TX_LIST_COUNT, 0,
6308caabc14Sstsp 	    BUS_DMA_NOWAIT, &tx_ring->map);
63160772a97Sstsp 	if (error != 0) {
63260772a97Sstsp 		printf("%s: could not create tx ring DMA map\n",
63360772a97Sstsp 		    sc->sc_dev.dv_xname);
63460772a97Sstsp 		goto fail;
63560772a97Sstsp 	}
63660772a97Sstsp 
63760772a97Sstsp 	error = bus_dmamem_alloc(sc->sc_dmat,
6388caabc14Sstsp 	    sizeof (struct r92c_tx_desc_pci) * RTWN_TX_LIST_COUNT, PAGE_SIZE, 0,
63960772a97Sstsp 	    &tx_ring->seg, 1, &tx_ring->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
64060772a97Sstsp 	if (error != 0) {
64160772a97Sstsp 		printf("%s: could not allocate tx ring DMA memory\n",
64260772a97Sstsp 		    sc->sc_dev.dv_xname);
64360772a97Sstsp 		goto fail;
64460772a97Sstsp 	}
64560772a97Sstsp 
64660772a97Sstsp 	error = bus_dmamem_map(sc->sc_dmat, &tx_ring->seg, tx_ring->nsegs,
6478caabc14Sstsp 	    sizeof (struct r92c_tx_desc_pci) * RTWN_TX_LIST_COUNT,
64860772a97Sstsp 	    (caddr_t *)&tx_ring->desc, BUS_DMA_NOWAIT);
64960772a97Sstsp 	if (error != 0) {
65060772a97Sstsp 		bus_dmamem_free(sc->sc_dmat, &tx_ring->seg, tx_ring->nsegs);
65160772a97Sstsp 		printf("%s: can't map tx ring DMA memory\n",
65260772a97Sstsp 		    sc->sc_dev.dv_xname);
65360772a97Sstsp 		goto fail;
65460772a97Sstsp 	}
65560772a97Sstsp 
65660772a97Sstsp 	error = bus_dmamap_load(sc->sc_dmat, tx_ring->map, tx_ring->desc,
6578caabc14Sstsp 	    sizeof (struct r92c_tx_desc_pci) * RTWN_TX_LIST_COUNT, NULL,
65860772a97Sstsp 	    BUS_DMA_NOWAIT);
65960772a97Sstsp 	if (error != 0) {
66060772a97Sstsp 		printf("%s: could not load tx ring DMA map\n",
66160772a97Sstsp 		    sc->sc_dev.dv_xname);
66260772a97Sstsp 		goto fail;
66360772a97Sstsp 	}
66460772a97Sstsp 
66560772a97Sstsp 	for (i = 0; i < RTWN_TX_LIST_COUNT; i++) {
6668caabc14Sstsp 		struct r92c_tx_desc_pci *desc = &tx_ring->desc[i];
66760772a97Sstsp 
66860772a97Sstsp 		/* setup tx desc */
66960772a97Sstsp 		desc->nextdescaddr = htole32(tx_ring->map->dm_segs[0].ds_addr
6708caabc14Sstsp 		  + sizeof(struct r92c_tx_desc_pci)
67160772a97Sstsp 		  * ((i + 1) % RTWN_TX_LIST_COUNT));
67260772a97Sstsp 
67360772a97Sstsp 		tx_data = &tx_ring->tx_data[i];
67460772a97Sstsp 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
67560772a97Sstsp 		    0, BUS_DMA_NOWAIT, &tx_data->map);
67660772a97Sstsp 		if (error != 0) {
67760772a97Sstsp 			printf("%s: could not create tx buf DMA map\n",
67860772a97Sstsp 			    sc->sc_dev.dv_xname);
67960772a97Sstsp 			goto fail;
68060772a97Sstsp 		}
68160772a97Sstsp 		tx_data->m = NULL;
68260772a97Sstsp 		tx_data->ni = NULL;
68360772a97Sstsp 	}
68460772a97Sstsp fail:
68560772a97Sstsp 	if (error != 0)
68660772a97Sstsp 		rtwn_free_tx_list(sc, qid);
68760772a97Sstsp 	return (error);
68860772a97Sstsp }
68960772a97Sstsp 
69060772a97Sstsp void
rtwn_reset_tx_list(struct rtwn_pci_softc * sc,int qid)6918b9da0f5Sstsp rtwn_reset_tx_list(struct rtwn_pci_softc *sc, int qid)
69260772a97Sstsp {
6938b9da0f5Sstsp 	struct ieee80211com *ic = &sc->sc_sc.sc_ic;
69460772a97Sstsp 	struct rtwn_tx_ring *tx_ring = &sc->tx_ring[qid];
69560772a97Sstsp 	int i;
69660772a97Sstsp 
69760772a97Sstsp 	for (i = 0; i < RTWN_TX_LIST_COUNT; i++) {
6988caabc14Sstsp 		struct r92c_tx_desc_pci *desc = &tx_ring->desc[i];
69960772a97Sstsp 		struct rtwn_tx_data *tx_data = &tx_ring->tx_data[i];
70060772a97Sstsp 
70160772a97Sstsp 		memset(desc, 0, sizeof(*desc) -
70260772a97Sstsp 		    (sizeof(desc->reserved) + sizeof(desc->nextdescaddr64) +
70360772a97Sstsp 		    sizeof(desc->nextdescaddr)));
70460772a97Sstsp 
70560772a97Sstsp 		if (tx_data->m != NULL) {
70660772a97Sstsp 			bus_dmamap_unload(sc->sc_dmat, tx_data->map);
70760772a97Sstsp 			m_freem(tx_data->m);
70860772a97Sstsp 			tx_data->m = NULL;
70960772a97Sstsp 			ieee80211_release_node(ic, tx_data->ni);
71060772a97Sstsp 			tx_data->ni = NULL;
71160772a97Sstsp 		}
71260772a97Sstsp 	}
71360772a97Sstsp 
71460772a97Sstsp 	bus_dmamap_sync(sc->sc_dmat, tx_ring->map, 0, MCLBYTES,
71560772a97Sstsp 	    BUS_DMASYNC_POSTWRITE);
71660772a97Sstsp 
71760772a97Sstsp 	sc->qfullmsk &= ~(1 << qid);
71860772a97Sstsp 	tx_ring->queued = 0;
71960772a97Sstsp 	tx_ring->cur = 0;
72060772a97Sstsp }
72160772a97Sstsp 
72260772a97Sstsp void
rtwn_free_tx_list(struct rtwn_pci_softc * sc,int qid)7238b9da0f5Sstsp rtwn_free_tx_list(struct rtwn_pci_softc *sc, int qid)
72460772a97Sstsp {
72560772a97Sstsp 	struct rtwn_tx_ring *tx_ring = &sc->tx_ring[qid];
72660772a97Sstsp 	struct rtwn_tx_data *tx_data;
72760772a97Sstsp 	int i;
72860772a97Sstsp 
72960772a97Sstsp 	if (tx_ring->map != NULL) {
73060772a97Sstsp 		if (tx_ring->desc != NULL) {
73160772a97Sstsp 			bus_dmamap_unload(sc->sc_dmat, tx_ring->map);
73260772a97Sstsp 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)tx_ring->desc,
7338caabc14Sstsp 			    sizeof (struct r92c_tx_desc_pci) *
7348caabc14Sstsp 			    RTWN_TX_LIST_COUNT);
73560772a97Sstsp 			bus_dmamem_free(sc->sc_dmat, &tx_ring->seg, tx_ring->nsegs);
73660772a97Sstsp 		}
73760772a97Sstsp 		bus_dmamap_destroy(sc->sc_dmat, tx_ring->map);
73860772a97Sstsp 	}
73960772a97Sstsp 
74060772a97Sstsp 	for (i = 0; i < RTWN_TX_LIST_COUNT; i++) {
74160772a97Sstsp 		tx_data = &tx_ring->tx_data[i];
74260772a97Sstsp 
74360772a97Sstsp 		if (tx_data->m != NULL) {
74460772a97Sstsp 			bus_dmamap_unload(sc->sc_dmat, tx_data->map);
74560772a97Sstsp 			m_freem(tx_data->m);
74660772a97Sstsp 			tx_data->m = NULL;
74760772a97Sstsp 		}
74860772a97Sstsp 		bus_dmamap_destroy(sc->sc_dmat, tx_data->map);
74960772a97Sstsp 	}
75060772a97Sstsp 
75160772a97Sstsp 	sc->qfullmsk &= ~(1 << qid);
75260772a97Sstsp 	tx_ring->queued = 0;
75360772a97Sstsp 	tx_ring->cur = 0;
75460772a97Sstsp }
75560772a97Sstsp 
75660772a97Sstsp void
rtwn_pci_write_1(void * cookie,uint16_t addr,uint8_t val)7578b9da0f5Sstsp rtwn_pci_write_1(void *cookie, uint16_t addr, uint8_t val)
75860772a97Sstsp {
7598b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
7608b9da0f5Sstsp 
76160772a97Sstsp 	bus_space_write_1(sc->sc_st, sc->sc_sh, addr, val);
76260772a97Sstsp }
76360772a97Sstsp 
76460772a97Sstsp void
rtwn_pci_write_2(void * cookie,uint16_t addr,uint16_t val)7658b9da0f5Sstsp rtwn_pci_write_2(void *cookie, uint16_t addr, uint16_t val)
76660772a97Sstsp {
7678b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
7688b9da0f5Sstsp 
76960772a97Sstsp 	val = htole16(val);
77060772a97Sstsp 	bus_space_write_2(sc->sc_st, sc->sc_sh, addr, val);
77160772a97Sstsp }
77260772a97Sstsp 
77360772a97Sstsp void
rtwn_pci_write_4(void * cookie,uint16_t addr,uint32_t val)7748b9da0f5Sstsp rtwn_pci_write_4(void *cookie, uint16_t addr, uint32_t val)
77560772a97Sstsp {
7768b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
7778b9da0f5Sstsp 
77860772a97Sstsp 	val = htole32(val);
77960772a97Sstsp 	bus_space_write_4(sc->sc_st, sc->sc_sh, addr, val);
78060772a97Sstsp }
78160772a97Sstsp 
78260772a97Sstsp uint8_t
rtwn_pci_read_1(void * cookie,uint16_t addr)7838b9da0f5Sstsp rtwn_pci_read_1(void *cookie, uint16_t addr)
78460772a97Sstsp {
7858b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
7868b9da0f5Sstsp 
78760772a97Sstsp 	return bus_space_read_1(sc->sc_st, sc->sc_sh, addr);
78860772a97Sstsp }
78960772a97Sstsp 
79060772a97Sstsp uint16_t
rtwn_pci_read_2(void * cookie,uint16_t addr)7918b9da0f5Sstsp rtwn_pci_read_2(void *cookie, uint16_t addr)
79260772a97Sstsp {
7938b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
794eef50e66Sstsp 	uint16_t val;
7958b9da0f5Sstsp 
796eef50e66Sstsp 	val = bus_space_read_2(sc->sc_st, sc->sc_sh, addr);
797eef50e66Sstsp 	return le16toh(val);
79860772a97Sstsp }
79960772a97Sstsp 
80060772a97Sstsp uint32_t
rtwn_pci_read_4(void * cookie,uint16_t addr)8018b9da0f5Sstsp rtwn_pci_read_4(void *cookie, uint16_t addr)
80260772a97Sstsp {
8038b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
804eef50e66Sstsp 	uint32_t val;
8058b9da0f5Sstsp 
806eef50e66Sstsp 	val = bus_space_read_4(sc->sc_st, sc->sc_sh, addr);
807eef50e66Sstsp 	return le32toh(val);
80860772a97Sstsp }
80960772a97Sstsp 
81060772a97Sstsp void
rtwn_rx_frame(struct rtwn_pci_softc * sc,struct r92c_rx_desc_pci * rx_desc,struct rtwn_rx_data * rx_data,int desc_idx,struct mbuf_list * ml)8118b9da0f5Sstsp rtwn_rx_frame(struct rtwn_pci_softc *sc, struct r92c_rx_desc_pci *rx_desc,
8128fbaf8a2Sstsp     struct rtwn_rx_data *rx_data, int desc_idx, struct mbuf_list *ml)
81360772a97Sstsp {
8148b9da0f5Sstsp 	struct ieee80211com *ic = &sc->sc_sc.sc_ic;
81560772a97Sstsp 	struct ifnet *ifp = &ic->ic_if;
81660772a97Sstsp 	struct ieee80211_rxinfo rxi;
81760772a97Sstsp 	struct ieee80211_frame *wh;
81860772a97Sstsp 	struct ieee80211_node *ni;
81960772a97Sstsp 	struct r92c_rx_phystat *phy = NULL;
82060772a97Sstsp 	uint32_t rxdw0, rxdw3;
82160772a97Sstsp 	struct mbuf *m, *m1;
82260772a97Sstsp 	uint8_t rate;
82360772a97Sstsp 	int8_t rssi = 0;
82460772a97Sstsp 	int infosz, pktlen, shift, error;
82560772a97Sstsp 
82660772a97Sstsp 	rxdw0 = letoh32(rx_desc->rxdw0);
82760772a97Sstsp 	rxdw3 = letoh32(rx_desc->rxdw3);
82860772a97Sstsp 
829d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E) {
830d8e41173Sjmatthew 		int ntries, type;
831d8e41173Sjmatthew 		struct r88e_tx_rpt_ccx *rxstat;
832d8e41173Sjmatthew 
833d8e41173Sjmatthew 		type = MS(rxdw3, R88E_RXDW3_RPT);
834d8e41173Sjmatthew 		if (type == R88E_RXDW3_RPT_TX1) {
835d8e41173Sjmatthew 			uint32_t rptb1, rptb2;
836d8e41173Sjmatthew 
837d8e41173Sjmatthew 			rxstat = mtod(rx_data->m, struct r88e_tx_rpt_ccx *);
838d8e41173Sjmatthew 			rptb1 = letoh32(rxstat->rptb1);
839d8e41173Sjmatthew 			rptb2 = letoh32(rxstat->rptb2);
840d8e41173Sjmatthew 			ntries = MS(rptb2, R88E_RPTB2_RETRY_CNT);
841d8e41173Sjmatthew 			if (rptb1 & R88E_RPTB1_PKT_OK)
842d8e41173Sjmatthew 				sc->amn.amn_txcnt++;
843d8e41173Sjmatthew 			if (ntries > 0)
844d8e41173Sjmatthew 				sc->amn.amn_retrycnt++;
845d8e41173Sjmatthew 
846d8e41173Sjmatthew 			rtwn_setup_rx_desc(sc, rx_desc,
847d8e41173Sjmatthew 			    rx_data->map->dm_segs[0].ds_addr, MCLBYTES,
848d8e41173Sjmatthew 			    desc_idx);
849d8e41173Sjmatthew 			return;
850d8e41173Sjmatthew 		}
851d8e41173Sjmatthew 	}
852d8e41173Sjmatthew 
85360772a97Sstsp 	if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) {
85460772a97Sstsp 		/*
85560772a97Sstsp 		 * This should not happen since we setup our Rx filter
85660772a97Sstsp 		 * to not receive these frames.
85760772a97Sstsp 		 */
85860772a97Sstsp 		ifp->if_ierrors++;
85960772a97Sstsp 		return;
86060772a97Sstsp 	}
86160772a97Sstsp 
86260772a97Sstsp 	pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN);
86360772a97Sstsp 	if (__predict_false(pktlen < sizeof(*wh) || pktlen > MCLBYTES)) {
86460772a97Sstsp 		ifp->if_ierrors++;
86560772a97Sstsp 		return;
86660772a97Sstsp 	}
86760772a97Sstsp 
86860772a97Sstsp 	rate = MS(rxdw3, R92C_RXDW3_RATE);
86960772a97Sstsp 	infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
87060772a97Sstsp 	if (infosz > sizeof(struct r92c_rx_phystat))
87160772a97Sstsp 		infosz = sizeof(struct r92c_rx_phystat);
87260772a97Sstsp 	shift = MS(rxdw0, R92C_RXDW0_SHIFT);
87360772a97Sstsp 
87460772a97Sstsp 	/* Get RSSI from PHY status descriptor if present. */
87560772a97Sstsp 	if (infosz != 0 && (rxdw0 & R92C_RXDW0_PHYST)) {
87660772a97Sstsp 		phy = mtod(rx_data->m, struct r92c_rx_phystat *);
8778b9da0f5Sstsp 		rssi = rtwn_get_rssi(&sc->sc_sc, rate, phy);
87860772a97Sstsp 		/* Update our average RSSI. */
8798b9da0f5Sstsp 		rtwn_update_avgrssi(&sc->sc_sc, rate, rssi);
88060772a97Sstsp 	}
88160772a97Sstsp 
88260772a97Sstsp 	DPRINTFN(5, ("Rx frame len=%d rate=%d infosz=%d shift=%d rssi=%d\n",
88360772a97Sstsp 	    pktlen, rate, infosz, shift, rssi));
88460772a97Sstsp 
885471f2571Sjan 	m1 = MCLGETL(NULL, M_DONTWAIT, MCLBYTES);
88660772a97Sstsp 	if (m1 == NULL) {
88760772a97Sstsp 		ifp->if_ierrors++;
88860772a97Sstsp 		return;
88960772a97Sstsp 	}
89060772a97Sstsp 	bus_dmamap_unload(sc->sc_dmat, rx_data->map);
89160772a97Sstsp 	error = bus_dmamap_load(sc->sc_dmat, rx_data->map,
89260772a97Sstsp 	    mtod(m1, void *), MCLBYTES, NULL,
89360772a97Sstsp 	    BUS_DMA_NOWAIT | BUS_DMA_READ);
89460772a97Sstsp 	if (error != 0) {
89560772a97Sstsp 		m_freem(m1);
89660772a97Sstsp 
89760772a97Sstsp 		if (bus_dmamap_load_mbuf(sc->sc_dmat, rx_data->map,
89860772a97Sstsp 		    rx_data->m, BUS_DMA_NOWAIT))
89960772a97Sstsp 			panic("%s: could not load old RX mbuf",
90060772a97Sstsp 			    sc->sc_dev.dv_xname);
90160772a97Sstsp 
90260772a97Sstsp 		/* Physical address may have changed. */
90360772a97Sstsp 		rtwn_setup_rx_desc(sc, rx_desc,
90460772a97Sstsp 		    rx_data->map->dm_segs[0].ds_addr, MCLBYTES, desc_idx);
90560772a97Sstsp 
90660772a97Sstsp 		ifp->if_ierrors++;
90760772a97Sstsp 		return;
90860772a97Sstsp 	}
90960772a97Sstsp 
91060772a97Sstsp 	/* Finalize mbuf. */
91160772a97Sstsp 	m = rx_data->m;
91260772a97Sstsp 	rx_data->m = m1;
91360772a97Sstsp 	m->m_pkthdr.len = m->m_len = pktlen + infosz + shift;
91460772a97Sstsp 
91560772a97Sstsp 	/* Update RX descriptor. */
91660772a97Sstsp 	rtwn_setup_rx_desc(sc, rx_desc, rx_data->map->dm_segs[0].ds_addr,
91760772a97Sstsp 	    MCLBYTES, desc_idx);
91860772a97Sstsp 
91960772a97Sstsp 	/* Get ieee80211 frame header. */
92060772a97Sstsp 	if (rxdw0 & R92C_RXDW0_PHYST)
92160772a97Sstsp 		m_adj(m, infosz + shift);
92260772a97Sstsp 	else
92360772a97Sstsp 		m_adj(m, shift);
92460772a97Sstsp 	wh = mtod(m, struct ieee80211_frame *);
92560772a97Sstsp 
92660772a97Sstsp #if NBPFILTER > 0
92760772a97Sstsp 	if (__predict_false(sc->sc_drvbpf != NULL)) {
92860772a97Sstsp 		struct rtwn_rx_radiotap_header *tap = &sc->sc_rxtap;
92960772a97Sstsp 		struct mbuf mb;
93060772a97Sstsp 
93160772a97Sstsp 		tap->wr_flags = 0;
93260772a97Sstsp 		/* Map HW rate index to 802.11 rate. */
93360772a97Sstsp 		tap->wr_flags = 2;
93460772a97Sstsp 		if (!(rxdw3 & R92C_RXDW3_HT)) {
93560772a97Sstsp 			switch (rate) {
93660772a97Sstsp 			/* CCK. */
93760772a97Sstsp 			case  0: tap->wr_rate =   2; break;
93860772a97Sstsp 			case  1: tap->wr_rate =   4; break;
93960772a97Sstsp 			case  2: tap->wr_rate =  11; break;
94060772a97Sstsp 			case  3: tap->wr_rate =  22; break;
94160772a97Sstsp 			/* OFDM. */
94260772a97Sstsp 			case  4: tap->wr_rate =  12; break;
94360772a97Sstsp 			case  5: tap->wr_rate =  18; break;
94460772a97Sstsp 			case  6: tap->wr_rate =  24; break;
94560772a97Sstsp 			case  7: tap->wr_rate =  36; break;
94660772a97Sstsp 			case  8: tap->wr_rate =  48; break;
94760772a97Sstsp 			case  9: tap->wr_rate =  72; break;
94860772a97Sstsp 			case 10: tap->wr_rate =  96; break;
94960772a97Sstsp 			case 11: tap->wr_rate = 108; break;
95060772a97Sstsp 			}
95160772a97Sstsp 		} else if (rate >= 12) {	/* MCS0~15. */
95260772a97Sstsp 			/* Bit 7 set means HT MCS instead of rate. */
95360772a97Sstsp 			tap->wr_rate = 0x80 | (rate - 12);
95460772a97Sstsp 		}
95560772a97Sstsp 		tap->wr_dbm_antsignal = rssi;
95660772a97Sstsp 		tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
95760772a97Sstsp 		tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
95860772a97Sstsp 
95960772a97Sstsp 		mb.m_data = (caddr_t)tap;
96060772a97Sstsp 		mb.m_len = sc->sc_rxtap_len;
96160772a97Sstsp 		mb.m_next = m;
96260772a97Sstsp 		mb.m_nextpkt = NULL;
96360772a97Sstsp 		mb.m_type = 0;
96460772a97Sstsp 		mb.m_flags = 0;
96560772a97Sstsp 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
96660772a97Sstsp 	}
96760772a97Sstsp #endif
96860772a97Sstsp 
96960772a97Sstsp 	ni = ieee80211_find_rxnode(ic, wh);
97052a13037Sstsp 	memset(&rxi, 0, sizeof(rxi));
97160772a97Sstsp 	rxi.rxi_rssi = rssi;
9728fbaf8a2Sstsp 	ieee80211_inputm(ifp, m, ni, &rxi, ml);
97360772a97Sstsp 	/* Node is no longer needed. */
97460772a97Sstsp 	ieee80211_release_node(ic, ni);
97560772a97Sstsp }
97660772a97Sstsp 
97760772a97Sstsp int
rtwn_tx(void * cookie,struct mbuf * m,struct ieee80211_node * ni)9788b9da0f5Sstsp rtwn_tx(void *cookie, struct mbuf *m, struct ieee80211_node *ni)
97960772a97Sstsp {
9808b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
9818b9da0f5Sstsp 	struct ieee80211com *ic = &sc->sc_sc.sc_ic;
98260772a97Sstsp 	struct ieee80211_frame *wh;
98360772a97Sstsp 	struct ieee80211_key *k = NULL;
98460772a97Sstsp 	struct rtwn_tx_ring *tx_ring;
98560772a97Sstsp 	struct rtwn_tx_data *data;
9868caabc14Sstsp 	struct r92c_tx_desc_pci *txd;
98760772a97Sstsp 	uint16_t qos;
98860772a97Sstsp 	uint8_t raid, type, tid, qid;
98960772a97Sstsp 	int hasqos, error;
99060772a97Sstsp 
99160772a97Sstsp 	wh = mtod(m, struct ieee80211_frame *);
99260772a97Sstsp 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
99360772a97Sstsp 
99460772a97Sstsp 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
99560772a97Sstsp 		k = ieee80211_get_txkey(ic, wh, ni);
99660772a97Sstsp 		if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
99760772a97Sstsp 			return (ENOBUFS);
99860772a97Sstsp 		wh = mtod(m, struct ieee80211_frame *);
99960772a97Sstsp 	}
100060772a97Sstsp 
100160772a97Sstsp 	if ((hasqos = ieee80211_has_qos(wh))) {
100260772a97Sstsp 		qos = ieee80211_get_qos(wh);
100360772a97Sstsp 		tid = qos & IEEE80211_QOS_TID;
100460772a97Sstsp 		qid = ieee80211_up_to_ac(ic, tid);
100560772a97Sstsp 	} else if (type != IEEE80211_FC0_TYPE_DATA) {
100660772a97Sstsp 		qid = RTWN_VO_QUEUE;
100760772a97Sstsp 	} else
100860772a97Sstsp 		qid = RTWN_BE_QUEUE;
100960772a97Sstsp 
101060772a97Sstsp 	/* Grab a Tx buffer from the ring. */
101160772a97Sstsp 	tx_ring = &sc->tx_ring[qid];
101260772a97Sstsp 	data = &tx_ring->tx_data[tx_ring->cur];
101360772a97Sstsp 	if (data->m != NULL) {
101460772a97Sstsp 		m_freem(m);
101560772a97Sstsp 		return (ENOBUFS);
101660772a97Sstsp 	}
101760772a97Sstsp 
101860772a97Sstsp 	/* Fill Tx descriptor. */
101960772a97Sstsp 	txd = &tx_ring->desc[tx_ring->cur];
10205144f945Skevlo 	if (htole32(txd->txdw0) & R92C_TXDW0_OWN) {
102160772a97Sstsp 		m_freem(m);
102260772a97Sstsp 		return (ENOBUFS);
102360772a97Sstsp 	}
102460772a97Sstsp 	txd->txdw0 = htole32(
102560772a97Sstsp 	    SM(R92C_TXDW0_PKTLEN, m->m_pkthdr.len) |
102660772a97Sstsp 	    SM(R92C_TXDW0_OFFSET, sizeof(*txd)) |
102760772a97Sstsp 	    R92C_TXDW0_FSG | R92C_TXDW0_LSG);
102860772a97Sstsp 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
102960772a97Sstsp 		txd->txdw0 |= htole32(R92C_TXDW0_BMCAST);
103060772a97Sstsp 
103160772a97Sstsp 	txd->txdw1 = 0;
103260772a97Sstsp #ifdef notyet
103360772a97Sstsp 	if (k != NULL) {
103460772a97Sstsp 		switch (k->k_cipher) {
103560772a97Sstsp 		case IEEE80211_CIPHER_WEP40:
103660772a97Sstsp 		case IEEE80211_CIPHER_WEP104:
103760772a97Sstsp 		case IEEE80211_CIPHER_TKIP:
103860772a97Sstsp 			cipher = R92C_TXDW1_CIPHER_RC4;
103960772a97Sstsp 			break;
104060772a97Sstsp 		case IEEE80211_CIPHER_CCMP:
104160772a97Sstsp 			cipher = R92C_TXDW1_CIPHER_AES;
104260772a97Sstsp 			break;
104360772a97Sstsp 		default:
104460772a97Sstsp 			cipher = R92C_TXDW1_CIPHER_NONE;
104560772a97Sstsp 		}
104660772a97Sstsp 		txd->txdw1 |= htole32(SM(R92C_TXDW1_CIPHER, cipher));
104760772a97Sstsp 	}
104860772a97Sstsp #endif
104960772a97Sstsp 	txd->txdw4 = 0;
105060772a97Sstsp 	txd->txdw5 = 0;
105160772a97Sstsp 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
105260772a97Sstsp 	    type == IEEE80211_FC0_TYPE_DATA) {
10535fda900fSstsp 		if (ic->ic_curmode == IEEE80211_MODE_11B ||
10545fda900fSstsp 		    (sc->sc_sc.sc_flags & RTWN_FLAG_FORCE_RAID_11B))
105560772a97Sstsp 			raid = R92C_RAID_11B;
105660772a97Sstsp 		else
105760772a97Sstsp 			raid = R92C_RAID_11BG;
1058d8e41173Sjmatthew 
1059d8e41173Sjmatthew 		if (sc->sc_sc.chip & RTWN_CHIP_88E) {
1060d8e41173Sjmatthew 			txd->txdw1 |= htole32(
1061d8e41173Sjmatthew 			    SM(R88E_TXDW1_MACID, R92C_MACID_BSS) |
1062d8e41173Sjmatthew 			    SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) |
1063d8e41173Sjmatthew 			    SM(R92C_TXDW1_RAID, raid));
1064d8e41173Sjmatthew 			txd->txdw2 |= htole32(R88E_TXDW2_AGGBK);
1065d8e41173Sjmatthew 		} else {
106660772a97Sstsp 			txd->txdw1 |= htole32(
106753c3c4d0Sstsp 			    SM(R92C_TXDW1_MACID, R92C_MACID_BSS) |
106860772a97Sstsp 			    SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) |
106960772a97Sstsp 			    SM(R92C_TXDW1_RAID, raid) |
107060772a97Sstsp 			    R92C_TXDW1_AGGBK);
1071d8e41173Sjmatthew 		}
107260772a97Sstsp 
10731f929dc7Sstsp 		/* Request TX status report for AMRR. */
10741f929dc7Sstsp 		txd->txdw2 |= htole32(R92C_TXDW2_CCX_RPT);
10751f929dc7Sstsp 
10765098e9a8Sstsp 		if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) {
10775098e9a8Sstsp 			txd->txdw4 |= htole32(R92C_TXDW4_RTSEN |
10785098e9a8Sstsp 			    R92C_TXDW4_HWRTSEN);
10795098e9a8Sstsp 		} else if (ic->ic_flags & IEEE80211_F_USEPROT) {
108060772a97Sstsp 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
108160772a97Sstsp 				txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF |
108260772a97Sstsp 				    R92C_TXDW4_HWRTSEN);
108360772a97Sstsp 			} else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
108460772a97Sstsp 				txd->txdw4 |= htole32(R92C_TXDW4_RTSEN |
108560772a97Sstsp 				    R92C_TXDW4_HWRTSEN);
108660772a97Sstsp 			}
108760772a97Sstsp 		}
108860772a97Sstsp 
10891f929dc7Sstsp 		if (ic->ic_curmode == IEEE80211_MODE_11B)
10901f929dc7Sstsp 			txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, 0));
10911f929dc7Sstsp 		else
10921342a1caSstsp 			txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, 8));
10931f929dc7Sstsp 		txd->txdw5 |= htole32(SM(R92C_TXDW5_RTSRATE_FBLIMIT, 0xf));
10941f929dc7Sstsp 
10951f929dc7Sstsp 		/* Use AMMR rate for data. */
10961f929dc7Sstsp 		txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
1097a255ae8aSstsp 		if (ic->ic_fixed_rate != -1)
1098a255ae8aSstsp 			txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE,
1099a255ae8aSstsp 			    ic->ic_fixed_rate));
1100a255ae8aSstsp 		else
1101a255ae8aSstsp 			txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE,
1102a255ae8aSstsp 			    ni->ni_txrate));
11031f929dc7Sstsp 		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FBLIMIT, 0x1f));
110460772a97Sstsp 	} else {
110560772a97Sstsp 		txd->txdw1 |= htole32(
110660772a97Sstsp 		    SM(R92C_TXDW1_MACID, 0) |
110760772a97Sstsp 		    SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT) |
110860772a97Sstsp 		    SM(R92C_TXDW1_RAID, R92C_RAID_11B));
110960772a97Sstsp 
111060772a97Sstsp 		/* Force CCK1. */
111160772a97Sstsp 		txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
111260772a97Sstsp 		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 0));
111360772a97Sstsp 	}
111460772a97Sstsp 	/* Set sequence number (already little endian). */
11159befa0edSjmatthew 	txd->txdseq = (*(uint16_t *)wh->i_seq) >> IEEE80211_SEQ_SEQ_SHIFT;
1116600882e8Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_23A)
1117600882e8Sjmatthew 		txd->txdseq |= htole16(R23A_TXDW3_TXRPTEN);
111860772a97Sstsp 
111960772a97Sstsp 	if (!hasqos) {
112060772a97Sstsp 		/* Use HW sequence numbering for non-QoS frames. */
1121600882e8Sjmatthew 		if (!(sc->sc_sc.chip & RTWN_CHIP_23A))
112260772a97Sstsp 			txd->txdw4 |= htole32(R92C_TXDW4_HWSEQ);
11239befa0edSjmatthew 		txd->txdseq |= htole16(R92C_TXDW3_HWSEQEN);
112460772a97Sstsp 	} else
112560772a97Sstsp 		txd->txdw4 |= htole32(R92C_TXDW4_QOS);
112660772a97Sstsp 
112760772a97Sstsp 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
112860772a97Sstsp 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
112960772a97Sstsp 	if (error && error != EFBIG) {
113060772a97Sstsp 		printf("%s: can't map mbuf (error %d)\n",
113160772a97Sstsp 		    sc->sc_dev.dv_xname, error);
113260772a97Sstsp 		m_freem(m);
113360772a97Sstsp 		return error;
113460772a97Sstsp 	}
113560772a97Sstsp 	if (error != 0) {
113660772a97Sstsp 		/* Too many DMA segments, linearize mbuf. */
113760772a97Sstsp 		if (m_defrag(m, M_DONTWAIT)) {
113860772a97Sstsp 			m_freem(m);
113960772a97Sstsp 			return ENOBUFS;
114060772a97Sstsp 		}
114160772a97Sstsp 
114260772a97Sstsp 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
114360772a97Sstsp 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
114460772a97Sstsp 		if (error != 0) {
114560772a97Sstsp 			printf("%s: can't map mbuf (error %d)\n",
114660772a97Sstsp 			    sc->sc_dev.dv_xname, error);
114760772a97Sstsp 			m_freem(m);
114860772a97Sstsp 			return error;
114960772a97Sstsp 		}
115060772a97Sstsp 	}
115160772a97Sstsp 
115260772a97Sstsp 	txd->txbufaddr = htole32(data->map->dm_segs[0].ds_addr);
115360772a97Sstsp 	txd->txbufsize = htole16(m->m_pkthdr.len);
115460772a97Sstsp 	bus_space_barrier(sc->sc_st, sc->sc_sh, 0, sc->sc_mapsize,
115560772a97Sstsp 	    BUS_SPACE_BARRIER_WRITE);
115660772a97Sstsp 	txd->txdw0 |= htole32(R92C_TXDW0_OWN);
115760772a97Sstsp 
115860772a97Sstsp 	bus_dmamap_sync(sc->sc_dmat, tx_ring->map, 0, MCLBYTES,
115960772a97Sstsp 	    BUS_DMASYNC_POSTWRITE);
116060772a97Sstsp 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES,
116160772a97Sstsp 	    BUS_DMASYNC_POSTWRITE);
116260772a97Sstsp 
116360772a97Sstsp 	data->m = m;
116460772a97Sstsp 	data->ni = ni;
116560772a97Sstsp 
116660772a97Sstsp #if NBPFILTER > 0
116760772a97Sstsp 	if (__predict_false(sc->sc_drvbpf != NULL)) {
116860772a97Sstsp 		struct rtwn_tx_radiotap_header *tap = &sc->sc_txtap;
116960772a97Sstsp 		struct mbuf mb;
117060772a97Sstsp 
117160772a97Sstsp 		tap->wt_flags = 0;
117260772a97Sstsp 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
117360772a97Sstsp 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
117460772a97Sstsp 
117560772a97Sstsp 		mb.m_data = (caddr_t)tap;
117660772a97Sstsp 		mb.m_len = sc->sc_txtap_len;
117760772a97Sstsp 		mb.m_next = m;
117860772a97Sstsp 		mb.m_nextpkt = NULL;
117960772a97Sstsp 		mb.m_type = 0;
118060772a97Sstsp 		mb.m_flags = 0;
118160772a97Sstsp 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
118260772a97Sstsp 	}
118360772a97Sstsp #endif
118460772a97Sstsp 
118560772a97Sstsp 	tx_ring->cur = (tx_ring->cur + 1) % RTWN_TX_LIST_COUNT;
118660772a97Sstsp 	tx_ring->queued++;
118760772a97Sstsp 
118860772a97Sstsp 	if (tx_ring->queued >= (RTWN_TX_LIST_COUNT - 1))
118960772a97Sstsp 		sc->qfullmsk |= (1 << qid);
119060772a97Sstsp 
119160772a97Sstsp 	/* Kick TX. */
11928b9da0f5Sstsp 	rtwn_pci_write_2(sc, R92C_PCIE_CTRL_REG, (1 << qid));
119360772a97Sstsp 
119460772a97Sstsp 	return (0);
119560772a97Sstsp }
119660772a97Sstsp 
119760772a97Sstsp void
rtwn_tx_done(struct rtwn_pci_softc * sc,int qid)11988b9da0f5Sstsp rtwn_tx_done(struct rtwn_pci_softc *sc, int qid)
119960772a97Sstsp {
12008b9da0f5Sstsp 	struct ieee80211com *ic = &sc->sc_sc.sc_ic;
120160772a97Sstsp 	struct ifnet *ifp = &ic->ic_if;
120260772a97Sstsp 	struct rtwn_tx_ring *tx_ring = &sc->tx_ring[qid];
120360772a97Sstsp 	struct rtwn_tx_data *tx_data;
12048caabc14Sstsp 	struct r92c_tx_desc_pci *tx_desc;
120560772a97Sstsp 	int i;
120660772a97Sstsp 
120760772a97Sstsp 	bus_dmamap_sync(sc->sc_dmat, tx_ring->map, 0, MCLBYTES,
120860772a97Sstsp 	    BUS_DMASYNC_POSTREAD);
120960772a97Sstsp 
121060772a97Sstsp 	for (i = 0; i < RTWN_TX_LIST_COUNT; i++) {
121160772a97Sstsp 		tx_data = &tx_ring->tx_data[i];
121260772a97Sstsp 		if (tx_data->m == NULL)
121360772a97Sstsp 			continue;
121460772a97Sstsp 
121560772a97Sstsp 		tx_desc = &tx_ring->desc[i];
121660772a97Sstsp 		if (letoh32(tx_desc->txdw0) & R92C_TXDW0_OWN)
121760772a97Sstsp 			continue;
121860772a97Sstsp 
121960772a97Sstsp 		bus_dmamap_unload(sc->sc_dmat, tx_data->map);
122060772a97Sstsp 		m_freem(tx_data->m);
122160772a97Sstsp 		tx_data->m = NULL;
122260772a97Sstsp 		ieee80211_release_node(ic, tx_data->ni);
122360772a97Sstsp 		tx_data->ni = NULL;
122460772a97Sstsp 
12258b9da0f5Sstsp 		sc->sc_sc.sc_tx_timer = 0;
122660772a97Sstsp 		tx_ring->queued--;
12271f929dc7Sstsp 
1228600882e8Sjmatthew 		if (!(sc->sc_sc.chip & RTWN_CHIP_23A))
12291f929dc7Sstsp 			rtwn_poll_c2h_events(sc);
123060772a97Sstsp 	}
123160772a97Sstsp 
123260772a97Sstsp 	if (tx_ring->queued < (RTWN_TX_LIST_COUNT - 1))
123360772a97Sstsp 		sc->qfullmsk &= ~(1 << qid);
123460772a97Sstsp 
123560772a97Sstsp 	if (sc->qfullmsk == 0) {
1236de6cd8fbSdlg 		ifq_clr_oactive(&ifp->if_snd);
123760772a97Sstsp 		(*ifp->if_start)(ifp);
123860772a97Sstsp 	}
123960772a97Sstsp }
124060772a97Sstsp 
124123340becSstsp int
rtwn_alloc_buffers(void * cookie)124223340becSstsp rtwn_alloc_buffers(void *cookie)
124323340becSstsp {
124423340becSstsp 	/* Tx/Rx buffers were already allocated in rtwn_pci_attach() */
124523340becSstsp 	return (0);
124623340becSstsp }
124723340becSstsp 
124823340becSstsp int
rtwn_pci_init(void * cookie)124923340becSstsp rtwn_pci_init(void *cookie)
125023340becSstsp {
12511f929dc7Sstsp 	struct rtwn_pci_softc *sc = cookie;
12521f929dc7Sstsp 	ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
1253d8e41173Sjmatthew 
1254d8e41173Sjmatthew 	/* Enable TX reports for AMRR */
1255d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E) {
1256d8e41173Sjmatthew 		rtwn_pci_write_1(sc, R88E_TX_RPT_CTRL,
1257d8e41173Sjmatthew 		    (rtwn_pci_read_1(sc, R88E_TX_RPT_CTRL) & ~0) |
1258d8e41173Sjmatthew 		    R88E_TX_RPT_CTRL_EN);
1259d8e41173Sjmatthew 		rtwn_pci_write_1(sc, R88E_TX_RPT_CTRL + 1, 0x02);
1260d8e41173Sjmatthew 
1261d8e41173Sjmatthew 		rtwn_pci_write_2(sc, R88E_TX_RPT_TIME, 0xcdf0);
1262d8e41173Sjmatthew 	}
1263d8e41173Sjmatthew 
126423340becSstsp 	return (0);
126523340becSstsp }
126623340becSstsp 
126760772a97Sstsp void
rtwn_pci_92c_stop(struct rtwn_pci_softc * sc)1268600882e8Sjmatthew rtwn_pci_92c_stop(struct rtwn_pci_softc *sc)
1269d8e41173Sjmatthew {
1270d8e41173Sjmatthew 	uint16_t reg;
1271d8e41173Sjmatthew 
1272600882e8Sjmatthew 	/* Disable interrupts. */
1273600882e8Sjmatthew 	rtwn_pci_write_4(sc, R92C_HIMR, 0x00000000);
1274600882e8Sjmatthew 
1275600882e8Sjmatthew 	/* Stop hardware. */
1276600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_TXPAUSE, R92C_TXPAUSE_ALL);
1277600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RF_CTRL, 0x00);
1278600882e8Sjmatthew 	reg = rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN);
1279600882e8Sjmatthew 	reg |= R92C_SYS_FUNC_EN_BB_GLB_RST;
1280600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN, reg);
1281600882e8Sjmatthew 	reg &= ~R92C_SYS_FUNC_EN_BB_GLB_RST;
1282600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN, reg);
1283600882e8Sjmatthew 	reg = rtwn_pci_read_2(sc, R92C_CR);
1284600882e8Sjmatthew 	reg &= ~(R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
1285600882e8Sjmatthew 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
1286600882e8Sjmatthew 	    R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
1287600882e8Sjmatthew 	    R92C_CR_ENSEC);
1288600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR, reg);
1289600882e8Sjmatthew 	if (rtwn_pci_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL)
1290600882e8Sjmatthew 		rtwn_fw_reset(&sc->sc_sc);
1291600882e8Sjmatthew 	/* TODO: linux does additional btcoex stuff here */
1292600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_AFE_PLL_CTRL, 0x80); /* linux magic number */
1293600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_SPS0_CTRL, 0x23); /* ditto */
1294600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_AFE_XTAL_CTRL, 0x0e); /* differs in btcoex */
1295600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL, R92C_RSV_CTRL_WLOCK_00 |
1296600882e8Sjmatthew 	    R92C_RSV_CTRL_WLOCK_04 | R92C_RSV_CTRL_WLOCK_08);
1297600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_APS_FSMCO, R92C_APS_FSMCO_PDN_EN);
1298600882e8Sjmatthew }
1299600882e8Sjmatthew 
1300600882e8Sjmatthew void
rtwn_pci_88e_stop(struct rtwn_pci_softc * sc)1301600882e8Sjmatthew rtwn_pci_88e_stop(struct rtwn_pci_softc *sc)
1302600882e8Sjmatthew {
1303600882e8Sjmatthew 	int i;
1304600882e8Sjmatthew 	uint16_t reg;
1305d8e41173Sjmatthew 
1306d8e41173Sjmatthew 	/* Disable interrupts. */
1307d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HIMR, 0x00000000);
1308d8e41173Sjmatthew 
1309d8e41173Sjmatthew 	/* Stop hardware. */
1310d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R88E_TX_RPT_CTRL,
1311d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R88E_TX_RPT_CTRL) &
1312d8e41173Sjmatthew 	    ~(R88E_TX_RPT_CTRL_EN));
1313d8e41173Sjmatthew 
1314d8e41173Sjmatthew 	for (i = 0; i < 100; i++) {
1315d8e41173Sjmatthew 		if (rtwn_pci_read_1(sc, R88E_RXDMA_CTRL) & 0x02)
1316d8e41173Sjmatthew 			break;
1317d8e41173Sjmatthew 		DELAY(10);
1318d8e41173Sjmatthew 	}
1319d8e41173Sjmatthew 	if (i == 100)
1320d8e41173Sjmatthew 		DPRINTF(("rxdma ctrl didn't go off, %x\n", rtwn_pci_read_1(sc, R88E_RXDMA_CTRL)));
1321d8e41173Sjmatthew 
1322d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 1, 0xff);
1323d8e41173Sjmatthew 
1324d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_TXPAUSE, R92C_TXPAUSE_ALL);
1325d8e41173Sjmatthew 
1326d8e41173Sjmatthew 	/* ensure transmission has stopped */
1327d8e41173Sjmatthew 	for (i = 0; i < 100; i++) {
1328d8e41173Sjmatthew 		if (rtwn_pci_read_4(sc, 0x5f8) == 0)
1329d8e41173Sjmatthew 			break;
1330d8e41173Sjmatthew 		DELAY(10);
1331d8e41173Sjmatthew 	}
1332d8e41173Sjmatthew 	if (i == 100)
1333d8e41173Sjmatthew 		DPRINTF(("tx didn't stop\n"));
1334d8e41173Sjmatthew 
1335d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN,
1336d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN) &
1337d8e41173Sjmatthew 	    ~(R92C_SYS_FUNC_EN_BBRSTB));
1338d8e41173Sjmatthew 	DELAY(1);
1339d8e41173Sjmatthew 	reg = rtwn_pci_read_2(sc, R92C_CR);
1340d8e41173Sjmatthew 	reg &= ~(R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
1341d8e41173Sjmatthew 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
1342d8e41173Sjmatthew 	    R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
1343d8e41173Sjmatthew 	    R92C_CR_ENSEC);
1344d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR, reg);
1345d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_DUAL_TSF_RST,
1346d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_DUAL_TSF_RST) | 0x20);
1347d8e41173Sjmatthew 
1348d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_RF_CTRL, 0x00);
1349d8e41173Sjmatthew 	if (rtwn_pci_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL)
1350d8e41173Sjmatthew 		rtwn_fw_reset(&sc->sc_sc);
1351d8e41173Sjmatthew 
1352d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN + 1,
1353d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN + 1) & ~0x02);
1354d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_MCUFWDL, 0);
1355d8e41173Sjmatthew 
1356d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R88E_32K_CTRL,
1357d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R88E_32K_CTRL) & ~(0x01));
1358d8e41173Sjmatthew 
1359d8e41173Sjmatthew 	/* transition to cardemu state */
1360d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_RF_CTRL, 0);
1361d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_LPLDO_CTRL,
1362d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_LPLDO_CTRL) | 0x10);
1363d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1364d8e41173Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_OFF);
1365d8e41173Sjmatthew 	for (i = 0; i < 100; i++) {
1366d8e41173Sjmatthew 		if ((rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1367d8e41173Sjmatthew 		    R92C_APS_FSMCO_APFM_OFF) == 0)
1368d8e41173Sjmatthew 			break;
1369d8e41173Sjmatthew 		DELAY(10);
1370d8e41173Sjmatthew 	}
1371d8e41173Sjmatthew 	if (i == 100)
1372d8e41173Sjmatthew 		DPRINTF(("apfm off didn't go off\n"));
1373d8e41173Sjmatthew 
1374d8e41173Sjmatthew 	/* transition to card disabled state */
1375d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_AFE_XTAL_CTRL + 2,
1376d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_AFE_XTAL_CTRL + 2) | 0x80);
1377d8e41173Sjmatthew 
1378d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL + 1,
1379600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_RSV_CTRL + 1) & ~R92C_RSV_CTRL_WLOCK_08);
1380d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL + 1,
1381600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_RSV_CTRL + 1) | R92C_RSV_CTRL_WLOCK_08);
1382d8e41173Sjmatthew 
13839befa0edSjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL, R92C_RSV_CTRL_WLOCK_00 |
13849befa0edSjmatthew 	    R92C_RSV_CTRL_WLOCK_04 | R92C_RSV_CTRL_WLOCK_08);
1385600882e8Sjmatthew }
1386d8e41173Sjmatthew 
1387600882e8Sjmatthew void
rtwn_pci_23a_stop(struct rtwn_pci_softc * sc)1388600882e8Sjmatthew rtwn_pci_23a_stop(struct rtwn_pci_softc *sc)
1389600882e8Sjmatthew {
1390600882e8Sjmatthew 	int i;
1391d8e41173Sjmatthew 
1392600882e8Sjmatthew 	/* Disable interrupts. */
1393600882e8Sjmatthew 	rtwn_pci_write_4(sc, R92C_HIMR, 0x00000000);
1394600882e8Sjmatthew 
1395600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 1, 0xff);
1396600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_TXPAUSE, R92C_TXPAUSE_ALL);
1397600882e8Sjmatthew 
1398600882e8Sjmatthew 	/* ensure transmission has stopped */
1399600882e8Sjmatthew 	for (i = 0; i < 100; i++) {
1400600882e8Sjmatthew 		if (rtwn_pci_read_4(sc, 0x5f8) == 0)
1401600882e8Sjmatthew 			break;
1402600882e8Sjmatthew 		DELAY(10);
1403600882e8Sjmatthew 	}
1404600882e8Sjmatthew 	if (i == 100)
1405600882e8Sjmatthew 		DPRINTF(("tx didn't stop\n"));
1406600882e8Sjmatthew 
1407600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN,
1408600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN) &
1409600882e8Sjmatthew 	    ~(R92C_SYS_FUNC_EN_BBRSTB));
1410600882e8Sjmatthew 	DELAY(1);
1411600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN,
1412600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN) &
1413600882e8Sjmatthew 	    ~(R92C_SYS_FUNC_EN_BB_GLB_RST));
1414600882e8Sjmatthew 
1415600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR,
1416600882e8Sjmatthew 	    rtwn_pci_read_2(sc, R92C_CR) &
1417600882e8Sjmatthew 	    ~(R92C_CR_MACTXEN | R92C_CR_MACRXEN | R92C_CR_ENSWBCN));
1418600882e8Sjmatthew 
1419600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_DUAL_TSF_RST,
1420600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_DUAL_TSF_RST) | 0x20);
1421600882e8Sjmatthew 
1422600882e8Sjmatthew 	/* Turn off RF */
1423600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RF_CTRL, 0x00);
1424600882e8Sjmatthew 	if (rtwn_pci_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL)
1425600882e8Sjmatthew 		rtwn_fw_reset(&sc->sc_sc);
1426600882e8Sjmatthew 
1427600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN + 1,
1428600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN + 1) & ~R92C_SYS_FUNC_EN_DIOE);
1429600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_MCUFWDL, 0);
1430600882e8Sjmatthew 
1431600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RF_CTRL, 0x00);
1432600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_LEDCFG2, rtwn_pci_read_1(sc, R92C_LEDCFG2) & ~(0x80));
1433600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO, rtwn_pci_read_2(sc, R92C_APS_FSMCO) |
1434600882e8Sjmatthew 	    R92C_APS_FSMCO_APFM_OFF);
1435600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO, rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1436600882e8Sjmatthew 	    ~(R92C_APS_FSMCO_APFM_OFF));
1437600882e8Sjmatthew 
1438600882e8Sjmatthew 	rtwn_pci_write_4(sc, R92C_APS_FSMCO,
1439600882e8Sjmatthew 	    rtwn_pci_read_4(sc, R92C_APS_FSMCO) & ~R92C_APS_FSMCO_RDY_MACON);
1440600882e8Sjmatthew 	rtwn_pci_write_4(sc, R92C_APS_FSMCO,
1441600882e8Sjmatthew 	    rtwn_pci_read_4(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APDM_HPDN);
1442600882e8Sjmatthew 
1443600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL + 1,
1444600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_RSV_CTRL + 1) & ~R92C_RSV_CTRL_WLOCK_08);
1445600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL + 1,
1446600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_RSV_CTRL + 1) | R92C_RSV_CTRL_WLOCK_08);
1447600882e8Sjmatthew 
1448600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL, R92C_RSV_CTRL_WLOCK_00 |
1449600882e8Sjmatthew 	    R92C_RSV_CTRL_WLOCK_04 | R92C_RSV_CTRL_WLOCK_08);
1450d8e41173Sjmatthew }
1451d8e41173Sjmatthew 
1452d8e41173Sjmatthew void
rtwn_pci_stop(void * cookie)14538b9da0f5Sstsp rtwn_pci_stop(void *cookie)
145460772a97Sstsp {
14558b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
145623340becSstsp 	int i, s;
145723340becSstsp 
145823340becSstsp 	s = splnet();
145923340becSstsp 
1460600882e8Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E) {
1461600882e8Sjmatthew 		rtwn_pci_88e_stop(sc);
1462600882e8Sjmatthew 	} else if (sc->sc_sc.chip & RTWN_CHIP_23A) {
1463600882e8Sjmatthew 		rtwn_pci_23a_stop(sc);
1464600882e8Sjmatthew 	} else {
1465600882e8Sjmatthew 		rtwn_pci_92c_stop(sc);
1466600882e8Sjmatthew 	}
146760772a97Sstsp 
146860772a97Sstsp 	for (i = 0; i < RTWN_NTXQUEUES; i++)
146960772a97Sstsp 		rtwn_reset_tx_list(sc, i);
147060772a97Sstsp 	rtwn_reset_rx_list(sc);
147123340becSstsp 
147223340becSstsp 	splx(s);
147360772a97Sstsp }
147460772a97Sstsp 
147560772a97Sstsp int
rtwn_88e_intr(struct rtwn_pci_softc * sc)1476d8e41173Sjmatthew rtwn_88e_intr(struct rtwn_pci_softc *sc)
1477d8e41173Sjmatthew {
14788fbaf8a2Sstsp 	struct mbuf_list ml = MBUF_LIST_INITIALIZER();
1479d8e41173Sjmatthew 	u_int32_t status, estatus;
1480d8e41173Sjmatthew 	int i;
1481d8e41173Sjmatthew 
1482d8e41173Sjmatthew 	status = rtwn_pci_read_4(sc, R88E_HISR);
1483d8e41173Sjmatthew 	if (status == 0 || status == 0xffffffff)
1484d8e41173Sjmatthew 		return (0);
1485d8e41173Sjmatthew 
1486d8e41173Sjmatthew 	estatus = rtwn_pci_read_4(sc, R88E_HISRE);
1487d8e41173Sjmatthew 
1488d8e41173Sjmatthew 	status &= RTWN_88E_INT_ENABLE;
1489d8e41173Sjmatthew 	estatus &= R88E_HIMRE_RXFOVW;
1490d8e41173Sjmatthew 
1491d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HIMR, 0);
1492d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HIMRE, 0);
1493d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HISR, status);
1494d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HISRE, estatus);
1495d8e41173Sjmatthew 
1496d8e41173Sjmatthew 	if (status & R88E_HIMR_HIGHDOK)
1497d8e41173Sjmatthew 		rtwn_tx_done(sc, RTWN_HIGH_QUEUE);
1498d8e41173Sjmatthew 	if (status & R88E_HIMR_MGNTDOK)
1499d8e41173Sjmatthew 		rtwn_tx_done(sc, RTWN_MGNT_QUEUE);
1500d8e41173Sjmatthew 	if (status & R88E_HIMR_BKDOK)
1501d8e41173Sjmatthew 		rtwn_tx_done(sc, RTWN_BK_QUEUE);
1502d8e41173Sjmatthew 	if (status & R88E_HIMR_BEDOK)
1503d8e41173Sjmatthew 		rtwn_tx_done(sc, RTWN_BE_QUEUE);
1504d8e41173Sjmatthew 	if (status & R88E_HIMR_VIDOK)
1505d8e41173Sjmatthew 		rtwn_tx_done(sc, RTWN_VI_QUEUE);
1506d8e41173Sjmatthew 	if (status & R88E_HIMR_VODOK)
1507d8e41173Sjmatthew 		rtwn_tx_done(sc, RTWN_VO_QUEUE);
1508d8e41173Sjmatthew 	if ((status & (R88E_HIMR_ROK | R88E_HIMR_RDU)) ||
1509d8e41173Sjmatthew 	    (estatus & R88E_HIMRE_RXFOVW)) {
15108fbaf8a2Sstsp 		struct ieee80211com *ic = &sc->sc_sc.sc_ic;
15118fbaf8a2Sstsp 
1512d8e41173Sjmatthew 		bus_dmamap_sync(sc->sc_dmat, sc->rx_ring.map, 0,
1513d8e41173Sjmatthew 		    sizeof(struct r92c_rx_desc_pci) * RTWN_RX_LIST_COUNT,
1514d8e41173Sjmatthew 		    BUS_DMASYNC_POSTREAD);
1515d8e41173Sjmatthew 
1516d8e41173Sjmatthew 		for (i = 0; i < RTWN_RX_LIST_COUNT; i++) {
1517d8e41173Sjmatthew 			struct r92c_rx_desc_pci *rx_desc = &sc->rx_ring.desc[i];
1518d8e41173Sjmatthew 			struct rtwn_rx_data *rx_data = &sc->rx_ring.rx_data[i];
1519d8e41173Sjmatthew 
1520d8e41173Sjmatthew 			if (letoh32(rx_desc->rxdw0) & R92C_RXDW0_OWN)
1521d8e41173Sjmatthew 				continue;
1522d8e41173Sjmatthew 
15238fbaf8a2Sstsp 			rtwn_rx_frame(sc, rx_desc, rx_data, i, &ml);
1524d8e41173Sjmatthew 		}
15258fbaf8a2Sstsp 		if_input(&ic->ic_if, &ml);
1526d8e41173Sjmatthew 	}
1527d8e41173Sjmatthew 
1528d8e41173Sjmatthew 	if (status & R88E_HIMR_HSISR_IND_ON_INT) {
1529d8e41173Sjmatthew 		rtwn_pci_write_1(sc, R92C_HSISR,
1530d8e41173Sjmatthew 		    rtwn_pci_read_1(sc, R92C_HSISR) |
1531d8e41173Sjmatthew 		    R88E_HSIMR_PDN_INT_EN | R88E_HSIMR_RON_INT_EN);
1532d8e41173Sjmatthew 	}
1533d8e41173Sjmatthew 
1534d8e41173Sjmatthew 	/* Enable interrupts. */
1535d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HIMR, RTWN_88E_INT_ENABLE);
1536d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R88E_HIMRE, R88E_HIMRE_RXFOVW);
1537d8e41173Sjmatthew 
1538d8e41173Sjmatthew 	return (1);
1539d8e41173Sjmatthew }
1540d8e41173Sjmatthew 
1541d8e41173Sjmatthew int
rtwn_intr(void * xsc)154260772a97Sstsp rtwn_intr(void *xsc)
154360772a97Sstsp {
15448b9da0f5Sstsp 	struct rtwn_pci_softc *sc = xsc;
15458fbaf8a2Sstsp 	struct mbuf_list ml = MBUF_LIST_INITIALIZER();
154660772a97Sstsp 	u_int32_t status;
154760772a97Sstsp 	int i;
154860772a97Sstsp 
1549d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E)
1550d8e41173Sjmatthew 		return (rtwn_88e_intr(sc));
1551d8e41173Sjmatthew 
15528b9da0f5Sstsp 	status = rtwn_pci_read_4(sc, R92C_HISR);
155360772a97Sstsp 	if (status == 0 || status == 0xffffffff)
155460772a97Sstsp 		return (0);
155560772a97Sstsp 
155660772a97Sstsp 	/* Disable interrupts. */
15578b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_HIMR, 0x00000000);
155860772a97Sstsp 
155960772a97Sstsp 	/* Ack interrupts. */
15608b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_HISR, status);
156160772a97Sstsp 
156260772a97Sstsp 	/* Vendor driver treats RX errors like ROK... */
156360772a97Sstsp 	if (status & (R92C_IMR_ROK | R92C_IMR_RXFOVW | R92C_IMR_RDU)) {
15648fbaf8a2Sstsp 		struct ieee80211com *ic = &sc->sc_sc.sc_ic;
15658fbaf8a2Sstsp 
156660772a97Sstsp 		bus_dmamap_sync(sc->sc_dmat, sc->rx_ring.map, 0,
15678caabc14Sstsp 		    sizeof(struct r92c_rx_desc_pci) * RTWN_RX_LIST_COUNT,
156860772a97Sstsp 		    BUS_DMASYNC_POSTREAD);
156960772a97Sstsp 
157060772a97Sstsp 		for (i = 0; i < RTWN_RX_LIST_COUNT; i++) {
15718caabc14Sstsp 			struct r92c_rx_desc_pci *rx_desc = &sc->rx_ring.desc[i];
157260772a97Sstsp 			struct rtwn_rx_data *rx_data = &sc->rx_ring.rx_data[i];
157360772a97Sstsp 
157460772a97Sstsp 			if (letoh32(rx_desc->rxdw0) & R92C_RXDW0_OWN)
157560772a97Sstsp 				continue;
157660772a97Sstsp 
15778fbaf8a2Sstsp 			rtwn_rx_frame(sc, rx_desc, rx_data, i, &ml);
157860772a97Sstsp 		}
15798fbaf8a2Sstsp 		if_input(&ic->ic_if, &ml);
158060772a97Sstsp 	}
158160772a97Sstsp 
158260772a97Sstsp 	if (status & R92C_IMR_BDOK)
158360772a97Sstsp 		rtwn_tx_done(sc, RTWN_BEACON_QUEUE);
158460772a97Sstsp 	if (status & R92C_IMR_HIGHDOK)
158560772a97Sstsp 		rtwn_tx_done(sc, RTWN_HIGH_QUEUE);
158660772a97Sstsp 	if (status & R92C_IMR_MGNTDOK)
158760772a97Sstsp 		rtwn_tx_done(sc, RTWN_MGNT_QUEUE);
158860772a97Sstsp 	if (status & R92C_IMR_BKDOK)
158960772a97Sstsp 		rtwn_tx_done(sc, RTWN_BK_QUEUE);
159060772a97Sstsp 	if (status & R92C_IMR_BEDOK)
159160772a97Sstsp 		rtwn_tx_done(sc, RTWN_BE_QUEUE);
159260772a97Sstsp 	if (status & R92C_IMR_VIDOK)
159360772a97Sstsp 		rtwn_tx_done(sc, RTWN_VI_QUEUE);
159460772a97Sstsp 	if (status & R92C_IMR_VODOK)
159560772a97Sstsp 		rtwn_tx_done(sc, RTWN_VO_QUEUE);
159660772a97Sstsp 
1597600882e8Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_23A) {
1598600882e8Sjmatthew 		if (status & R92C_IMR_ATIMEND)
1599600882e8Sjmatthew 			rtwn_poll_c2h_events(sc);
1600600882e8Sjmatthew 	}
1601600882e8Sjmatthew 
160260772a97Sstsp 	/* Enable interrupts. */
1603d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R92C_HIMR, RTWN_92C_INT_ENABLE);
160460772a97Sstsp 
160560772a97Sstsp 	return (1);
160660772a97Sstsp }
16078b9da0f5Sstsp 
16088b9da0f5Sstsp int
rtwn_is_oactive(void * cookie)16098b9da0f5Sstsp rtwn_is_oactive(void *cookie)
16108b9da0f5Sstsp {
16118b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
16128b9da0f5Sstsp 
16138b9da0f5Sstsp 	return (sc->qfullmsk != 0);
16148b9da0f5Sstsp }
16158b9da0f5Sstsp 
16168b9da0f5Sstsp int
rtwn_llt_write(struct rtwn_pci_softc * sc,uint32_t addr,uint32_t data)1617b81d0ffeSstsp rtwn_llt_write(struct rtwn_pci_softc *sc, uint32_t addr, uint32_t data)
1618b81d0ffeSstsp {
1619b81d0ffeSstsp 	int ntries;
1620b81d0ffeSstsp 
1621b81d0ffeSstsp 	rtwn_pci_write_4(sc, R92C_LLT_INIT,
1622b81d0ffeSstsp 	    SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) |
1623b81d0ffeSstsp 	    SM(R92C_LLT_INIT_ADDR, addr) |
1624b81d0ffeSstsp 	    SM(R92C_LLT_INIT_DATA, data));
1625b81d0ffeSstsp 	/* Wait for write operation to complete. */
1626b81d0ffeSstsp 	for (ntries = 0; ntries < 20; ntries++) {
1627b81d0ffeSstsp 		if (MS(rtwn_pci_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) ==
1628b81d0ffeSstsp 		    R92C_LLT_INIT_OP_NO_ACTIVE)
1629b81d0ffeSstsp 			return (0);
1630b81d0ffeSstsp 		DELAY(5);
1631b81d0ffeSstsp 	}
1632b81d0ffeSstsp 	return (ETIMEDOUT);
1633b81d0ffeSstsp }
1634b81d0ffeSstsp 
1635b81d0ffeSstsp int
rtwn_llt_init(struct rtwn_pci_softc * sc,int page_count)1636d8e41173Sjmatthew rtwn_llt_init(struct rtwn_pci_softc *sc, int page_count)
1637b81d0ffeSstsp {
1638d8e41173Sjmatthew 	int i, error, pktbuf_count;
1639b81d0ffeSstsp 
1640600882e8Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E)
1641600882e8Sjmatthew 		pktbuf_count = R88E_TXPKTBUF_COUNT;
1642600882e8Sjmatthew 	else if (sc->sc_sc.chip & RTWN_CHIP_23A)
1643600882e8Sjmatthew 		pktbuf_count = R23A_TXPKTBUF_COUNT;
1644600882e8Sjmatthew 	else
1645600882e8Sjmatthew 		pktbuf_count = R92C_TXPKTBUF_COUNT;
1646d8e41173Sjmatthew 
1647d8e41173Sjmatthew 	/* Reserve pages [0; page_count]. */
1648d8e41173Sjmatthew 	for (i = 0; i < page_count; i++) {
1649b81d0ffeSstsp 		if ((error = rtwn_llt_write(sc, i, i + 1)) != 0)
1650b81d0ffeSstsp 			return (error);
1651b81d0ffeSstsp 	}
1652b81d0ffeSstsp 	/* NB: 0xff indicates end-of-list. */
1653b81d0ffeSstsp 	if ((error = rtwn_llt_write(sc, i, 0xff)) != 0)
1654b81d0ffeSstsp 		return (error);
1655b81d0ffeSstsp 	/*
1656d8e41173Sjmatthew 	 * Use pages [page_count + 1; pktbuf_count - 1]
1657b81d0ffeSstsp 	 * as ring buffer.
1658b81d0ffeSstsp 	 */
1659d8e41173Sjmatthew 	for (++i; i < pktbuf_count - 1; i++) {
1660b81d0ffeSstsp 		if ((error = rtwn_llt_write(sc, i, i + 1)) != 0)
1661b81d0ffeSstsp 			return (error);
1662b81d0ffeSstsp 	}
1663b81d0ffeSstsp 	/* Make the last page point to the beginning of the ring buffer. */
1664d8e41173Sjmatthew 	error = rtwn_llt_write(sc, i, pktbuf_count + 1);
1665b81d0ffeSstsp 	return (error);
1666b81d0ffeSstsp }
1667b81d0ffeSstsp 
1668b81d0ffeSstsp int
rtwn_92c_power_on(struct rtwn_pci_softc * sc)1669d8e41173Sjmatthew rtwn_92c_power_on(struct rtwn_pci_softc *sc)
16705fda900fSstsp {
16715fda900fSstsp 	uint32_t reg;
16725fda900fSstsp 	int ntries;
16735fda900fSstsp 
16745fda900fSstsp 	/* Wait for autoload done bit. */
16755fda900fSstsp 	for (ntries = 0; ntries < 1000; ntries++) {
16765fda900fSstsp 		if (rtwn_pci_read_1(sc, R92C_APS_FSMCO) &
16775fda900fSstsp 		    R92C_APS_FSMCO_PFM_ALDN)
16785fda900fSstsp 			break;
16795fda900fSstsp 		DELAY(5);
16805fda900fSstsp 	}
16815fda900fSstsp 	if (ntries == 1000) {
16825fda900fSstsp 		printf("%s: timeout waiting for chip autoload\n",
16835fda900fSstsp 		    sc->sc_dev.dv_xname);
16845fda900fSstsp 		return (ETIMEDOUT);
16855fda900fSstsp 	}
16865fda900fSstsp 
16875fda900fSstsp 	/* Unlock ISO/CLK/Power control register. */
16885fda900fSstsp 	rtwn_pci_write_1(sc, R92C_RSV_CTRL, 0);
16895fda900fSstsp 
16905fda900fSstsp 	/* TODO: check if we need this for 8188CE */
16915fda900fSstsp 	if (sc->sc_sc.board_type != R92C_BOARD_TYPE_DONGLE) {
16925fda900fSstsp 		/* bt coex */
16935fda900fSstsp 		reg = rtwn_pci_read_4(sc, R92C_APS_FSMCO);
16945fda900fSstsp 		reg |= (R92C_APS_FSMCO_SOP_ABG |
16955fda900fSstsp 			R92C_APS_FSMCO_SOP_AMB |
16965fda900fSstsp 			R92C_APS_FSMCO_XOP_BTCK);
16975fda900fSstsp 		rtwn_pci_write_4(sc, R92C_APS_FSMCO, reg);
16985fda900fSstsp 	}
16995fda900fSstsp 
17005fda900fSstsp 	/* Move SPS into PWM mode. */
17015fda900fSstsp 	rtwn_pci_write_1(sc, R92C_SPS0_CTRL, 0x2b);
17025fda900fSstsp 	DELAY(100);
17035fda900fSstsp 
17045fda900fSstsp 	/* Set low byte to 0x0f, leave others unchanged. */
17055fda900fSstsp 	rtwn_pci_write_4(sc, R92C_AFE_XTAL_CTRL,
17065fda900fSstsp 	    (rtwn_pci_read_4(sc, R92C_AFE_XTAL_CTRL) & 0xffffff00) | 0x0f);
17075fda900fSstsp 
17085fda900fSstsp 	/* TODO: check if we need this for 8188CE */
17095fda900fSstsp 	if (sc->sc_sc.board_type != R92C_BOARD_TYPE_DONGLE) {
17105fda900fSstsp 		/* bt coex */
17115fda900fSstsp 		reg = rtwn_pci_read_4(sc, R92C_AFE_XTAL_CTRL);
17125fda900fSstsp 		reg &= (~0x00024800); /* XXX magic from linux */
17135fda900fSstsp 		rtwn_pci_write_4(sc, R92C_AFE_XTAL_CTRL, reg);
17145fda900fSstsp 	}
17155fda900fSstsp 
17165fda900fSstsp 	rtwn_pci_write_2(sc, R92C_SYS_ISO_CTRL,
17175fda900fSstsp 	  (rtwn_pci_read_2(sc, R92C_SYS_ISO_CTRL) & 0xff) |
17185fda900fSstsp 	  R92C_SYS_ISO_CTRL_PWC_EV12V | R92C_SYS_ISO_CTRL_DIOR);
17195fda900fSstsp 	DELAY(200);
17205fda900fSstsp 
17215fda900fSstsp 	/* TODO: linux does additional btcoex stuff here */
17225fda900fSstsp 
17235fda900fSstsp 	/* Auto enable WLAN. */
17245fda900fSstsp 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
17255fda900fSstsp 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC);
17265fda900fSstsp 	for (ntries = 0; ntries < 1000; ntries++) {
17275fda900fSstsp 		if (!(rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
17285fda900fSstsp 		    R92C_APS_FSMCO_APFM_ONMAC))
17295fda900fSstsp 			break;
17305fda900fSstsp 		DELAY(5);
17315fda900fSstsp 	}
17325fda900fSstsp 	if (ntries == 1000) {
17335fda900fSstsp 		printf("%s: timeout waiting for MAC auto ON\n",
17345fda900fSstsp 		    sc->sc_dev.dv_xname);
17355fda900fSstsp 		return (ETIMEDOUT);
17365fda900fSstsp 	}
17375fda900fSstsp 
17385fda900fSstsp 	/* Enable radio, GPIO and LED functions. */
17395fda900fSstsp 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
17405fda900fSstsp 	    R92C_APS_FSMCO_AFSM_PCIE |
17415fda900fSstsp 	    R92C_APS_FSMCO_PDN_EN |
17425fda900fSstsp 	    R92C_APS_FSMCO_PFM_ALDN);
17435fda900fSstsp 	/* Release RF digital isolation. */
17445fda900fSstsp 	rtwn_pci_write_2(sc, R92C_SYS_ISO_CTRL,
17455fda900fSstsp 	    rtwn_pci_read_2(sc, R92C_SYS_ISO_CTRL) & ~R92C_SYS_ISO_CTRL_DIOR);
17465fda900fSstsp 
17475fda900fSstsp 	if (sc->sc_sc.chip & RTWN_CHIP_92C)
17485fda900fSstsp 		rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 3, 0x77);
17495fda900fSstsp 	else
17505fda900fSstsp 		rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 3, 0x22);
17515fda900fSstsp 
17525fda900fSstsp 	rtwn_pci_write_4(sc, R92C_INT_MIG, 0);
17535fda900fSstsp 
17545fda900fSstsp 	if (sc->sc_sc.board_type != R92C_BOARD_TYPE_DONGLE) {
17555fda900fSstsp 		/* bt coex */
17565fda900fSstsp 		reg = rtwn_pci_read_4(sc, R92C_AFE_XTAL_CTRL + 2);
17575fda900fSstsp 		reg &= 0xfd; /* XXX magic from linux */
17585fda900fSstsp 		rtwn_pci_write_4(sc, R92C_AFE_XTAL_CTRL + 2, reg);
17595fda900fSstsp 	}
17605fda900fSstsp 
17615fda900fSstsp 	rtwn_pci_write_1(sc, R92C_GPIO_MUXCFG,
17625fda900fSstsp 	    rtwn_pci_read_1(sc, R92C_GPIO_MUXCFG) & ~R92C_GPIO_MUXCFG_RFKILL);
17635fda900fSstsp 
17645fda900fSstsp 	reg = rtwn_pci_read_1(sc, R92C_GPIO_IO_SEL);
17655fda900fSstsp 	if (!(reg & R92C_GPIO_IO_SEL_RFKILL)) {
17665fda900fSstsp 		printf("%s: radio is disabled by hardware switch\n",
17675fda900fSstsp 		    sc->sc_dev.dv_xname);
17685fda900fSstsp 		return (EPERM);	/* :-) */
17695fda900fSstsp 	}
17705fda900fSstsp 
17715fda900fSstsp 	/* Initialize MAC. */
17725fda900fSstsp 	rtwn_pci_write_1(sc, R92C_APSD_CTRL,
17735fda900fSstsp 	    rtwn_pci_read_1(sc, R92C_APSD_CTRL) & ~R92C_APSD_CTRL_OFF);
17745fda900fSstsp 	for (ntries = 0; ntries < 200; ntries++) {
17755fda900fSstsp 		if (!(rtwn_pci_read_1(sc, R92C_APSD_CTRL) &
17765fda900fSstsp 		    R92C_APSD_CTRL_OFF_STATUS))
17775fda900fSstsp 			break;
17785fda900fSstsp 		DELAY(500);
17795fda900fSstsp 	}
17805fda900fSstsp 	if (ntries == 200) {
17815fda900fSstsp 		printf("%s: timeout waiting for MAC initialization\n",
17825fda900fSstsp 		    sc->sc_dev.dv_xname);
17835fda900fSstsp 		return (ETIMEDOUT);
17845fda900fSstsp 	}
17855fda900fSstsp 
17865fda900fSstsp 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
17875fda900fSstsp 	reg = rtwn_pci_read_2(sc, R92C_CR);
17885fda900fSstsp 	reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
17895fda900fSstsp 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
17905fda900fSstsp 	    R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
17915fda900fSstsp 	    R92C_CR_ENSEC;
17925fda900fSstsp 	rtwn_pci_write_2(sc, R92C_CR, reg);
17935fda900fSstsp 
17945fda900fSstsp 	rtwn_pci_write_1(sc, 0xfe10, 0x19);
17955fda900fSstsp 
17965fda900fSstsp 	return (0);
17975fda900fSstsp }
17985fda900fSstsp 
17995fda900fSstsp int
rtwn_88e_power_on(struct rtwn_pci_softc * sc)1800d8e41173Sjmatthew rtwn_88e_power_on(struct rtwn_pci_softc *sc)
1801d8e41173Sjmatthew {
1802d8e41173Sjmatthew 	uint32_t reg;
1803d8e41173Sjmatthew 	int ntries;
1804d8e41173Sjmatthew 
1805d8e41173Sjmatthew 	/* Disable XTAL output for power saving. */
1806d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R88E_XCK_OUT_CTRL,
1807d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R88E_XCK_OUT_CTRL) & ~R88E_XCK_OUT_CTRL_EN);
1808d8e41173Sjmatthew 
1809d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1810d8e41173Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) & (~R92C_APS_FSMCO_APDM_HPDN));
1811d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL, 0);
1812d8e41173Sjmatthew 
1813d8e41173Sjmatthew 	/* Wait for power ready bit. */
1814d8e41173Sjmatthew 	for (ntries = 0; ntries < 5000; ntries++) {
1815d8e41173Sjmatthew 		if (rtwn_pci_read_4(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_SUS_HOST)
1816d8e41173Sjmatthew 			break;
1817d8e41173Sjmatthew 		DELAY(10);
1818d8e41173Sjmatthew 	}
1819d8e41173Sjmatthew 	if (ntries == 5000) {
1820d8e41173Sjmatthew 		printf("%s: timeout waiting for chip power up\n",
1821d8e41173Sjmatthew 		    sc->sc_dev.dv_xname);
1822d8e41173Sjmatthew 		return (ETIMEDOUT);
1823d8e41173Sjmatthew 	}
1824d8e41173Sjmatthew 
1825d8e41173Sjmatthew 	/* Reset BB. */
1826d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN,
1827d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_FUNC_EN) & ~(R92C_SYS_FUNC_EN_BBRSTB |
1828d8e41173Sjmatthew 	    R92C_SYS_FUNC_EN_BB_GLB_RST));
1829d8e41173Sjmatthew 
1830d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_AFE_XTAL_CTRL + 2,
1831d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_AFE_XTAL_CTRL + 2) | 0x80);
1832d8e41173Sjmatthew 
1833d8e41173Sjmatthew 	/* Disable HWPDN. */
1834d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1835d8e41173Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) & ~R92C_APS_FSMCO_APDM_HPDN);
1836d8e41173Sjmatthew 	/* Disable WL suspend. */
1837d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1838d8e41173Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1839d8e41173Sjmatthew 	    ~(R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_AFSM_PCIE));
1840d8e41173Sjmatthew 
1841d8e41173Sjmatthew 	/* Auto enable WLAN. */
1842d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1843d8e41173Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC);
1844d8e41173Sjmatthew 	for (ntries = 0; ntries < 5000; ntries++) {
1845d8e41173Sjmatthew 		if (!(rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1846d8e41173Sjmatthew 		    R92C_APS_FSMCO_APFM_ONMAC))
1847d8e41173Sjmatthew 			break;
1848d8e41173Sjmatthew 		DELAY(10);
1849d8e41173Sjmatthew 	}
1850d8e41173Sjmatthew 	if (ntries == 5000) {
1851d8e41173Sjmatthew 		printf("%s: timeout waiting for MAC auto ON\n",
1852d8e41173Sjmatthew 		    sc->sc_dev.dv_xname);
1853d8e41173Sjmatthew 		return (ETIMEDOUT);
1854d8e41173Sjmatthew 	}
1855d8e41173Sjmatthew 
1856d8e41173Sjmatthew 	/* Enable LDO normal mode. */
1857d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_LPLDO_CTRL,
1858d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_LPLDO_CTRL) & ~0x10);
1859d8e41173Sjmatthew 
1860d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_APS_FSMCO,
1861d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_PDN_EN);
1862d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 2,
1863d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_PCIE_CTRL_REG + 2) | 0x04);
1864d8e41173Sjmatthew 
1865d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_AFE_XTAL_CTRL_EXT + 1,
1866d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_AFE_XTAL_CTRL_EXT + 1) | 0x02);
1867d8e41173Sjmatthew 
1868d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_SYS_CLKR,
1869d8e41173Sjmatthew 	    rtwn_pci_read_1(sc, R92C_SYS_CLKR) | 0x08);
1870d8e41173Sjmatthew 
1871d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_GPIO_MUXCFG,
1872d8e41173Sjmatthew 	    rtwn_pci_read_2(sc, R92C_GPIO_MUXCFG) & ~R92C_GPIO_MUXCFG_ENSIC);
1873d8e41173Sjmatthew 
1874d8e41173Sjmatthew 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
1875d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR, 0);
1876d8e41173Sjmatthew 	reg = rtwn_pci_read_2(sc, R92C_CR);
1877d8e41173Sjmatthew 	reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
1878d8e41173Sjmatthew 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
1879d8e41173Sjmatthew 	    R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
1880d8e41173Sjmatthew 	    R92C_CR_ENSEC | R92C_CR_CALTMR_EN;
1881d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR, reg);
1882d8e41173Sjmatthew 
1883d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_MSR, 0);
1884d8e41173Sjmatthew 	return (0);
1885d8e41173Sjmatthew }
1886d8e41173Sjmatthew 
1887d8e41173Sjmatthew int
rtwn_23a_power_on(struct rtwn_pci_softc * sc)1888600882e8Sjmatthew rtwn_23a_power_on(struct rtwn_pci_softc *sc)
1889600882e8Sjmatthew {
1890600882e8Sjmatthew 	uint32_t reg;
1891600882e8Sjmatthew 	int ntries;
1892600882e8Sjmatthew 
1893600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_RSV_CTRL, 0x00);
1894600882e8Sjmatthew 
1895600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1896600882e8Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1897600882e8Sjmatthew 	    ~(R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_AFSM_PCIE));
1898600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 1, 0x00);
1899600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1900600882e8Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) & ~R92C_APS_FSMCO_APFM_RSM);
1901600882e8Sjmatthew 
1902600882e8Sjmatthew 	/* Wait for power ready bit. */
1903600882e8Sjmatthew 	for (ntries = 0; ntries < 5000; ntries++) {
1904600882e8Sjmatthew 		if (rtwn_pci_read_4(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_SUS_HOST)
1905600882e8Sjmatthew 			break;
1906600882e8Sjmatthew 		DELAY(10);
1907600882e8Sjmatthew 	}
1908600882e8Sjmatthew 	if (ntries == 5000) {
1909600882e8Sjmatthew 		printf("%s: timeout waiting for chip power up\n",
1910600882e8Sjmatthew 		    sc->sc_dev.dv_xname);
1911600882e8Sjmatthew 		return (ETIMEDOUT);
1912600882e8Sjmatthew 	}
1913600882e8Sjmatthew 
1914600882e8Sjmatthew 	/* Release WLON reset */
1915600882e8Sjmatthew 	rtwn_pci_write_4(sc, R92C_APS_FSMCO, rtwn_pci_read_4(sc, R92C_APS_FSMCO) |
1916600882e8Sjmatthew 	    R92C_APS_FSMCO_RDY_MACON);
1917600882e8Sjmatthew 	/* Disable HWPDN. */
1918600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1919600882e8Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) & ~R92C_APS_FSMCO_APDM_HPDN);
1920600882e8Sjmatthew 	/* Disable WL suspend. */
1921600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1922600882e8Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1923600882e8Sjmatthew 	    ~(R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_AFSM_PCIE));
1924600882e8Sjmatthew 
1925600882e8Sjmatthew 	/* Auto enable WLAN. */
1926600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_APS_FSMCO,
1927600882e8Sjmatthew 	    rtwn_pci_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC);
1928600882e8Sjmatthew 	for (ntries = 0; ntries < 5000; ntries++) {
1929600882e8Sjmatthew 		if (!(rtwn_pci_read_2(sc, R92C_APS_FSMCO) &
1930600882e8Sjmatthew 		    R92C_APS_FSMCO_APFM_ONMAC))
1931600882e8Sjmatthew 			break;
1932600882e8Sjmatthew 		DELAY(10);
1933600882e8Sjmatthew 	}
1934600882e8Sjmatthew 	if (ntries == 5000) {
1935600882e8Sjmatthew 		printf("%s: timeout waiting for MAC auto ON (%x)\n",
1936600882e8Sjmatthew 		    sc->sc_dev.dv_xname, rtwn_pci_read_2(sc, R92C_APS_FSMCO));
1937600882e8Sjmatthew 		return (ETIMEDOUT);
1938600882e8Sjmatthew 	}
1939600882e8Sjmatthew 
1940600882e8Sjmatthew 	rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG + 2,
1941600882e8Sjmatthew 	    rtwn_pci_read_1(sc, R92C_PCIE_CTRL_REG + 2) | 0x04);
1942600882e8Sjmatthew 
1943600882e8Sjmatthew 	/* emac time out */
1944600882e8Sjmatthew 	rtwn_pci_write_1(sc, 0x369, rtwn_pci_read_1(sc, 0x369) | 0x80);
1945600882e8Sjmatthew 
1946600882e8Sjmatthew 	for (ntries = 0; ntries < 100; ntries++) {
1947600882e8Sjmatthew 		rtwn_pci_write_2(sc, R92C_MDIO + 4, 0x5e);
1948600882e8Sjmatthew 		DELAY(100);
1949600882e8Sjmatthew 		rtwn_pci_write_2(sc, R92C_MDIO + 2, 0xc280);
1950600882e8Sjmatthew 		rtwn_pci_write_2(sc, R92C_MDIO, 0xc290);
1951600882e8Sjmatthew 		rtwn_pci_write_2(sc, R92C_MDIO + 4, 0x3e);
1952600882e8Sjmatthew 		DELAY(100);
1953600882e8Sjmatthew 		rtwn_pci_write_2(sc, R92C_MDIO + 4, 0x5e);
1954600882e8Sjmatthew 		DELAY(100);
1955600882e8Sjmatthew 		if (rtwn_pci_read_2(sc, R92C_MDIO + 2) == 0xc290)
1956600882e8Sjmatthew 			break;
1957600882e8Sjmatthew 	}
1958600882e8Sjmatthew 	if (ntries == 100) {
1959600882e8Sjmatthew 		printf("%s: timeout configuring ePHY\n", sc->sc_dev.dv_xname);
1960600882e8Sjmatthew 		return (ETIMEDOUT);
1961600882e8Sjmatthew 	}
1962600882e8Sjmatthew 
1963600882e8Sjmatthew 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
1964600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR, 0);
1965600882e8Sjmatthew 	reg = rtwn_pci_read_2(sc, R92C_CR);
1966600882e8Sjmatthew 	reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
1967600882e8Sjmatthew 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
1968600882e8Sjmatthew 	    R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
1969600882e8Sjmatthew 	    R92C_CR_ENSEC | R92C_CR_CALTMR_EN;
1970600882e8Sjmatthew 	rtwn_pci_write_2(sc, R92C_CR, reg);
1971600882e8Sjmatthew 
1972600882e8Sjmatthew 	return (0);
1973600882e8Sjmatthew }
1974600882e8Sjmatthew 
1975600882e8Sjmatthew int
rtwn_power_on(void * cookie)1976d8e41173Sjmatthew rtwn_power_on(void *cookie)
1977d8e41173Sjmatthew {
1978d8e41173Sjmatthew 	struct rtwn_pci_softc *sc = cookie;
1979d8e41173Sjmatthew 
1980d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E)
1981d8e41173Sjmatthew 		return (rtwn_88e_power_on(sc));
1982600882e8Sjmatthew 	else if (sc->sc_sc.chip & RTWN_CHIP_23A)
1983600882e8Sjmatthew 		return (rtwn_23a_power_on(sc));
1984d8e41173Sjmatthew 	else
1985d8e41173Sjmatthew 		return (rtwn_92c_power_on(sc));
1986d8e41173Sjmatthew }
1987d8e41173Sjmatthew 
1988d8e41173Sjmatthew int
rtwn_dma_init(void * cookie)1989b81d0ffeSstsp rtwn_dma_init(void *cookie)
19908b9da0f5Sstsp {
19918b9da0f5Sstsp 	struct rtwn_pci_softc *sc = cookie;
1992b81d0ffeSstsp 	uint32_t reg;
1993d8e41173Sjmatthew 	uint16_t dmasize;
1994d8e41173Sjmatthew 	int hqpages, lqpages, nqpages, pagecnt, boundary, trxdma, tcr;
1995b81d0ffeSstsp 	int error;
1996b81d0ffeSstsp 
1997d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E) {
1998d8e41173Sjmatthew 		nqpages = R88E_NPQ_NPAGES;
1999d8e41173Sjmatthew 		hqpages = R88E_HPQ_NPAGES;
2000d8e41173Sjmatthew 		lqpages = R88E_LPQ_NPAGES;
2001d8e41173Sjmatthew 		pagecnt = R88E_TX_PAGE_COUNT;
2002d8e41173Sjmatthew 		boundary = R88E_TX_PAGE_BOUNDARY;
2003d8e41173Sjmatthew 		dmasize = R88E_MAX_RX_DMA_SIZE;
2004d8e41173Sjmatthew 		tcr = R92C_TCR_CFENDFORM | R92C_TCR_ERRSTEN3;
2005d8e41173Sjmatthew 		trxdma = 0xe771;
2006600882e8Sjmatthew 	} else if (sc->sc_sc.chip & RTWN_CHIP_23A) {
2007600882e8Sjmatthew 		nqpages = R23A_NPQ_NPAGES;
2008600882e8Sjmatthew 		hqpages = R23A_HPQ_NPAGES;
2009600882e8Sjmatthew 		lqpages = R23A_LPQ_NPAGES;
2010600882e8Sjmatthew 		pagecnt = R23A_TX_PAGE_COUNT;
2011600882e8Sjmatthew 		boundary = R23A_TX_PAGE_BOUNDARY;
2012600882e8Sjmatthew 		dmasize = R23A_MAX_RX_DMA_SIZE;
2013600882e8Sjmatthew 		tcr = R92C_TCR_CFENDFORM | R92C_TCR_ERRSTEN0 |
2014600882e8Sjmatthew 		    R92C_TCR_ERRSTEN1;
2015600882e8Sjmatthew 		trxdma = 0xf771;
2016d8e41173Sjmatthew 	} else {
2017d8e41173Sjmatthew 		nqpages = R92C_NPQ_NPAGES;
2018d8e41173Sjmatthew 		hqpages = R92C_HPQ_NPAGES;
2019d8e41173Sjmatthew 		lqpages = R92C_LPQ_NPAGES;
2020d8e41173Sjmatthew 		pagecnt = R92C_TX_PAGE_COUNT;
2021d8e41173Sjmatthew 		boundary = R92C_TX_PAGE_BOUNDARY;
2022d8e41173Sjmatthew 		dmasize = R92C_MAX_RX_DMA_SIZE;
2023d8e41173Sjmatthew 		tcr = R92C_TCR_CFENDFORM | R92C_TCR_ERRSTEN0 |
2024d8e41173Sjmatthew 		    R92C_TCR_ERRSTEN1;
2025d8e41173Sjmatthew 		trxdma = 0xf771;
2026d8e41173Sjmatthew 	}
2027d8e41173Sjmatthew 
2028b81d0ffeSstsp 	/* Initialize LLT table. */
2029d8e41173Sjmatthew 	error = rtwn_llt_init(sc, pagecnt);
2030b81d0ffeSstsp 	if (error != 0)
2031b81d0ffeSstsp 		return error;
2032b81d0ffeSstsp 
2033b81d0ffeSstsp 	/* Set number of pages for normal priority queue. */
2034d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_RQPN_NPQ, nqpages);
2035b81d0ffeSstsp 	rtwn_pci_write_4(sc, R92C_RQPN,
2036b81d0ffeSstsp 	    /* Set number of pages for public queue. */
2037d8e41173Sjmatthew 	    SM(R92C_RQPN_PUBQ, pagecnt) |
2038b81d0ffeSstsp 	    /* Set number of pages for high priority queue. */
2039d8e41173Sjmatthew 	    SM(R92C_RQPN_HPQ, hqpages) |
2040b81d0ffeSstsp 	    /* Set number of pages for low priority queue. */
2041d8e41173Sjmatthew 	    SM(R92C_RQPN_LPQ, lqpages) |
2042b81d0ffeSstsp 	    /* Load values. */
2043b81d0ffeSstsp 	    R92C_RQPN_LD);
2044b81d0ffeSstsp 
2045d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, boundary);
2046d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, boundary);
2047b81d0ffeSstsp 	rtwn_pci_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD,
2048d8e41173Sjmatthew 	    boundary);
2049d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_TRXFF_BNDY, boundary);
2050d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_TDECTRL + 1, boundary);
2051b81d0ffeSstsp 
2052b81d0ffeSstsp 	reg = rtwn_pci_read_2(sc, R92C_TRXDMA_CTRL);
2053b81d0ffeSstsp 	reg &= ~R92C_TRXDMA_CTRL_QMAP_M;
2054d8e41173Sjmatthew 	reg |= trxdma;
2055b81d0ffeSstsp 	rtwn_pci_write_2(sc, R92C_TRXDMA_CTRL, reg);
2056b81d0ffeSstsp 
2057d8e41173Sjmatthew 	rtwn_pci_write_4(sc, R92C_TCR, tcr);
20588b9da0f5Sstsp 
20598b9da0f5Sstsp 	/* Configure Tx DMA. */
20608b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_BKQ_DESA,
20618b9da0f5Sstsp 		sc->tx_ring[RTWN_BK_QUEUE].map->dm_segs[0].ds_addr);
20628b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_BEQ_DESA,
20638b9da0f5Sstsp 		sc->tx_ring[RTWN_BE_QUEUE].map->dm_segs[0].ds_addr);
20648b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_VIQ_DESA,
20658b9da0f5Sstsp 		sc->tx_ring[RTWN_VI_QUEUE].map->dm_segs[0].ds_addr);
20668b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_VOQ_DESA,
20678b9da0f5Sstsp 		sc->tx_ring[RTWN_VO_QUEUE].map->dm_segs[0].ds_addr);
20688b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_BCNQ_DESA,
20698b9da0f5Sstsp 		sc->tx_ring[RTWN_BEACON_QUEUE].map->dm_segs[0].ds_addr);
20708b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_MGQ_DESA,
20718b9da0f5Sstsp 		sc->tx_ring[RTWN_MGNT_QUEUE].map->dm_segs[0].ds_addr);
20728b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_HQ_DESA,
20738b9da0f5Sstsp 		sc->tx_ring[RTWN_HIGH_QUEUE].map->dm_segs[0].ds_addr);
20748b9da0f5Sstsp 
20758b9da0f5Sstsp 	/* Configure Rx DMA. */
20768b9da0f5Sstsp 	rtwn_pci_write_4(sc, R92C_RX_DESA, sc->rx_ring.map->dm_segs[0].ds_addr);
2077d8e41173Sjmatthew 	rtwn_pci_write_1(sc, R92C_PCIE_CTRL_REG+1, 0);
20788b9da0f5Sstsp 
2079b81d0ffeSstsp 	/* Set Tx/Rx transfer page boundary. */
2080d8e41173Sjmatthew 	rtwn_pci_write_2(sc, R92C_TRXFF_BNDY + 2, dmasize - 1);
2081b81d0ffeSstsp 
2082b81d0ffeSstsp 	/* Set Tx/Rx transfer page size. */
2083b81d0ffeSstsp 	rtwn_pci_write_1(sc, R92C_PBP,
2084b81d0ffeSstsp 	    SM(R92C_PBP_PSRX, R92C_PBP_128) |
2085b81d0ffeSstsp 	    SM(R92C_PBP_PSTX, R92C_PBP_128));
2086b81d0ffeSstsp 
20878b9da0f5Sstsp 	return (0);
20888b9da0f5Sstsp }
20898b9da0f5Sstsp 
20905fda900fSstsp int
rtwn_fw_loadpage(void * cookie,int page,uint8_t * buf,int len)209123340becSstsp rtwn_fw_loadpage(void *cookie, int page, uint8_t *buf, int len)
209223340becSstsp {
209323340becSstsp 	struct rtwn_pci_softc *sc = cookie;
209423340becSstsp 	uint32_t reg;
209523340becSstsp 	int off, mlen, error = 0, i;
209623340becSstsp 
209723340becSstsp 	reg = rtwn_pci_read_4(sc, R92C_MCUFWDL);
209823340becSstsp 	reg = RW(reg, R92C_MCUFWDL_PAGE, page);
209923340becSstsp 	rtwn_pci_write_4(sc, R92C_MCUFWDL, reg);
210023340becSstsp 
210123340becSstsp 	DELAY(5);
210223340becSstsp 
210323340becSstsp 	off = R92C_FW_START_ADDR;
210423340becSstsp 	while (len > 0) {
210523340becSstsp 		if (len > 196)
210623340becSstsp 			mlen = 196;
210723340becSstsp 		else if (len > 4)
210823340becSstsp 			mlen = 4;
210923340becSstsp 		else
211023340becSstsp 			mlen = 1;
211123340becSstsp 		for (i = 0; i < mlen; i++)
211223340becSstsp 			rtwn_pci_write_1(sc, off++, buf[i]);
211323340becSstsp 		buf += mlen;
211423340becSstsp 		len -= mlen;
211523340becSstsp 	}
211623340becSstsp 
211723340becSstsp 	return (error);
211823340becSstsp }
211923340becSstsp 
212023340becSstsp int
rtwn_pci_load_firmware(void * cookie,u_char ** fw,size_t * len)21215fda900fSstsp rtwn_pci_load_firmware(void *cookie, u_char **fw, size_t *len)
21225fda900fSstsp {
21235fda900fSstsp 	struct rtwn_pci_softc *sc = cookie;
21245fda900fSstsp 	const char *name;
21255fda900fSstsp 	int error;
21265fda900fSstsp 
2127d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E)
21281e1b982aSkevlo 		name = "rtwn-rtl8188e";
2129600882e8Sjmatthew 	else if (sc->sc_sc.chip & RTWN_CHIP_23A) {
2130600882e8Sjmatthew 		if (sc->sc_sc.chip & RTWN_CHIP_UMC_A_CUT)
21311e1b982aSkevlo 			name = "rtwn-rtl8723";
2132600882e8Sjmatthew 		else
21331e1b982aSkevlo 			name = "rtwn-rtl8723_B";
2134600882e8Sjmatthew 	} else if ((sc->sc_sc.chip & (RTWN_CHIP_UMC_A_CUT | RTWN_CHIP_92C)) ==
21355fda900fSstsp 	    RTWN_CHIP_UMC_A_CUT)
21361e1b982aSkevlo 		name = "rtwn-rtl8192cU";
21375fda900fSstsp 	else
21381e1b982aSkevlo 		name = "rtwn-rtl8192cU_B";
21395fda900fSstsp 
21405fda900fSstsp 	error = loadfirmware(name, fw, len);
21415fda900fSstsp 	if (error)
21425fda900fSstsp 		printf("%s: could not read firmware %s (error %d)\n",
21435fda900fSstsp 		    sc->sc_dev.dv_xname, name, error);
21445fda900fSstsp 	return (error);
21455fda900fSstsp }
21465fda900fSstsp 
21475fda900fSstsp void
rtwn_mac_init(void * cookie)21485fda900fSstsp rtwn_mac_init(void *cookie)
21495fda900fSstsp {
21505fda900fSstsp 	struct rtwn_pci_softc *sc = cookie;
21515fda900fSstsp 	int i;
21525fda900fSstsp 
21535fda900fSstsp 	/* Write MAC initialization values. */
2154d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E) {
2155d8e41173Sjmatthew 		for (i = 0; i < nitems(rtl8188eu_mac); i++) {
2156d8e41173Sjmatthew 			if (rtl8188eu_mac[i].reg == R92C_GPIO_MUXCFG)
2157d8e41173Sjmatthew 				continue;
2158d8e41173Sjmatthew 			rtwn_pci_write_1(sc, rtl8188eu_mac[i].reg,
2159d8e41173Sjmatthew 			    rtl8188eu_mac[i].val);
2160d8e41173Sjmatthew 		}
2161d8e41173Sjmatthew 		rtwn_pci_write_1(sc, R92C_MAX_AGGR_NUM, 0x07);
2162600882e8Sjmatthew 	} else if (sc->sc_sc.chip & RTWN_CHIP_23A) {
2163600882e8Sjmatthew 		for (i = 0; i < nitems(rtl8192cu_mac); i++) {
2164600882e8Sjmatthew 			rtwn_pci_write_1(sc, rtl8192cu_mac[i].reg,
2165600882e8Sjmatthew 			    rtl8192cu_mac[i].val);
2166600882e8Sjmatthew 		}
2167600882e8Sjmatthew 		rtwn_pci_write_1(sc, R92C_MAX_AGGR_NUM, 0x0a);
2168d8e41173Sjmatthew 	} else {
21695fda900fSstsp 		for (i = 0; i < nitems(rtl8192ce_mac); i++)
21705fda900fSstsp 			rtwn_pci_write_1(sc, rtl8192ce_mac[i].reg,
21715fda900fSstsp 			    rtl8192ce_mac[i].val);
21725fda900fSstsp 	}
2173d8e41173Sjmatthew }
21745fda900fSstsp 
21755fda900fSstsp void
rtwn_bb_init(void * cookie)21765fda900fSstsp rtwn_bb_init(void *cookie)
21775fda900fSstsp {
21785fda900fSstsp 	struct rtwn_pci_softc *sc = cookie;
21795fda900fSstsp 	const struct r92c_bb_prog *prog;
21805fda900fSstsp 	uint32_t reg;
21815fda900fSstsp 	int i;
21825fda900fSstsp 
21835fda900fSstsp 	/* Enable BB and RF. */
21845fda900fSstsp 	rtwn_pci_write_2(sc, R92C_SYS_FUNC_EN,
21855fda900fSstsp 	    rtwn_pci_read_2(sc, R92C_SYS_FUNC_EN) |
21865fda900fSstsp 	    R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST |
21875fda900fSstsp 	    R92C_SYS_FUNC_EN_DIO_RF);
21885fda900fSstsp 
2189d8e41173Sjmatthew 	if (!(sc->sc_sc.chip & RTWN_CHIP_88E))
21905fda900fSstsp 		rtwn_pci_write_2(sc, R92C_AFE_PLL_CTRL, 0xdb83);
21915fda900fSstsp 
21925fda900fSstsp 	rtwn_pci_write_1(sc, R92C_RF_CTRL,
21935fda900fSstsp 	    R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB);
21945fda900fSstsp 
21955fda900fSstsp 	rtwn_pci_write_1(sc, R92C_SYS_FUNC_EN,
21965fda900fSstsp 	    R92C_SYS_FUNC_EN_DIO_PCIE | R92C_SYS_FUNC_EN_PCIEA |
21975fda900fSstsp 	    R92C_SYS_FUNC_EN_PPLL | R92C_SYS_FUNC_EN_BB_GLB_RST |
21985fda900fSstsp 	    R92C_SYS_FUNC_EN_BBRSTB);
21995fda900fSstsp 
2200d8e41173Sjmatthew 	if (!(sc->sc_sc.chip & RTWN_CHIP_88E)) {
22015fda900fSstsp 		rtwn_pci_write_1(sc, R92C_AFE_XTAL_CTRL + 1, 0x80);
2202d8e41173Sjmatthew 	}
22035fda900fSstsp 
22045fda900fSstsp 	rtwn_pci_write_4(sc, R92C_LEDCFG0,
22055fda900fSstsp 	    rtwn_pci_read_4(sc, R92C_LEDCFG0) | 0x00800000);
22065fda900fSstsp 
22075fda900fSstsp 	/* Select BB programming. */
2208d8e41173Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_88E)
2209d8e41173Sjmatthew 		prog = &rtl8188eu_bb_prog;
2210600882e8Sjmatthew 	else if (sc->sc_sc.chip & RTWN_CHIP_23A)
2211600882e8Sjmatthew 		prog = &rtl8723a_bb_prog;
2212d8e41173Sjmatthew 	else if (!(sc->sc_sc.chip & RTWN_CHIP_92C))
2213d8e41173Sjmatthew 		prog = &rtl8192ce_bb_prog_1t;
2214d8e41173Sjmatthew 	else
2215d8e41173Sjmatthew 		prog = &rtl8192ce_bb_prog_2t;
22165fda900fSstsp 
22175fda900fSstsp 	/* Write BB initialization values. */
22185fda900fSstsp 	for (i = 0; i < prog->count; i++) {
22195fda900fSstsp 		rtwn_bb_write(sc, prog->regs[i], prog->vals[i]);
22205fda900fSstsp 		DELAY(1);
22215fda900fSstsp 	}
22225fda900fSstsp 
22235fda900fSstsp 	if (sc->sc_sc.chip & RTWN_CHIP_92C_1T2R) {
22245fda900fSstsp 		/* 8192C 1T only configuration. */
22255fda900fSstsp 		reg = rtwn_bb_read(sc, R92C_FPGA0_TXINFO);
22265fda900fSstsp 		reg = (reg & ~0x00000003) | 0x2;
22275fda900fSstsp 		rtwn_bb_write(sc, R92C_FPGA0_TXINFO, reg);
22285fda900fSstsp 
22295fda900fSstsp 		reg = rtwn_bb_read(sc, R92C_FPGA1_TXINFO);
22305fda900fSstsp 		reg = (reg & ~0x00300033) | 0x00200022;
22315fda900fSstsp 		rtwn_bb_write(sc, R92C_FPGA1_TXINFO, reg);
22325fda900fSstsp 
22335fda900fSstsp 		reg = rtwn_bb_read(sc, R92C_CCK0_AFESETTING);
22345fda900fSstsp 		reg = (reg & ~0xff000000) | 0x45 << 24;
22355fda900fSstsp 		rtwn_bb_write(sc, R92C_CCK0_AFESETTING, reg);
22365fda900fSstsp 
22375fda900fSstsp 		reg = rtwn_bb_read(sc, R92C_OFDM0_TRXPATHENA);
22385fda900fSstsp 		reg = (reg & ~0x000000ff) | 0x23;
22395fda900fSstsp 		rtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, reg);
22405fda900fSstsp 
22415fda900fSstsp 		reg = rtwn_bb_read(sc, R92C_OFDM0_AGCPARAM1);
22425fda900fSstsp 		reg = (reg & ~0x00000030) | 1 << 4;
22435fda900fSstsp 		rtwn_bb_write(sc, R92C_OFDM0_AGCPARAM1, reg);
22445fda900fSstsp 
22455fda900fSstsp 		reg = rtwn_bb_read(sc, 0xe74);
22465fda900fSstsp 		reg = (reg & ~0x0c000000) | 2 << 26;
22475fda900fSstsp 		rtwn_bb_write(sc, 0xe74, reg);
22485fda900fSstsp 		reg = rtwn_bb_read(sc, 0xe78);
22495fda900fSstsp 		reg = (reg & ~0x0c000000) | 2 << 26;
22505fda900fSstsp 		rtwn_bb_write(sc, 0xe78, reg);
22515fda900fSstsp 		reg = rtwn_bb_read(sc, 0xe7c);
22525fda900fSstsp 		reg = (reg & ~0x0c000000) | 2 << 26;
22535fda900fSstsp 		rtwn_bb_write(sc, 0xe7c, reg);
22545fda900fSstsp 		reg = rtwn_bb_read(sc, 0xe80);
22555fda900fSstsp 		reg = (reg & ~0x0c000000) | 2 << 26;
22565fda900fSstsp 		rtwn_bb_write(sc, 0xe80, reg);
22575fda900fSstsp 		reg = rtwn_bb_read(sc, 0xe88);
22585fda900fSstsp 		reg = (reg & ~0x0c000000) | 2 << 26;
22595fda900fSstsp 		rtwn_bb_write(sc, 0xe88, reg);
22605fda900fSstsp 	}
22615fda900fSstsp 
22625fda900fSstsp 	/* Write AGC values. */
22635fda900fSstsp 	for (i = 0; i < prog->agccount; i++) {
22645fda900fSstsp 		rtwn_bb_write(sc, R92C_OFDM0_AGCRSSITABLE,
22655fda900fSstsp 		    prog->agcvals[i]);
22665fda900fSstsp 		DELAY(1);
22675fda900fSstsp 	}
22685fda900fSstsp 
22695cc093eeSkevlo 	if (rtwn_bb_read(sc, R92C_HSSI_PARAM2(0)) & R92C_HSSI_PARAM2_CCK_HIPWR)
22705fda900fSstsp 		sc->sc_sc.sc_flags |= RTWN_FLAG_CCK_HIPWR;
22715fda900fSstsp }
22725fda900fSstsp 
22738b9da0f5Sstsp void
rtwn_calib_to(void * arg)2274e9f037d1Sstsp rtwn_calib_to(void *arg)
2275e9f037d1Sstsp {
2276e9f037d1Sstsp 	struct rtwn_pci_softc *sc = arg;
22771f929dc7Sstsp 	struct ieee80211com *ic = &sc->sc_sc.sc_ic;
22781f929dc7Sstsp 	int s;
22791f929dc7Sstsp 
22801f929dc7Sstsp 	s = splnet();
22811f929dc7Sstsp 	ieee80211_amrr_choose(&sc->amrr, ic->ic_bss, &sc->amn);
22821f929dc7Sstsp 	splx(s);
2283e9f037d1Sstsp 
2284e9f037d1Sstsp 	rtwn_calib(&sc->sc_sc);
2285e9f037d1Sstsp }
2286e9f037d1Sstsp 
2287e9f037d1Sstsp void
rtwn_next_calib(void * cookie)2288e9f037d1Sstsp rtwn_next_calib(void *cookie)
2289e9f037d1Sstsp {
2290e9f037d1Sstsp 	struct rtwn_pci_softc *sc = cookie;
2291e9f037d1Sstsp 
2292e9f037d1Sstsp 	timeout_add_sec(&sc->calib_to, 2);
2293e9f037d1Sstsp }
2294e9f037d1Sstsp 
2295e9f037d1Sstsp void
rtwn_cancel_calib(void * cookie)2296e9f037d1Sstsp rtwn_cancel_calib(void *cookie)
2297e9f037d1Sstsp {
2298e9f037d1Sstsp 	struct rtwn_pci_softc *sc = cookie;
2299e9f037d1Sstsp 
2300e9f037d1Sstsp 	if (timeout_initialized(&sc->calib_to))
2301e9f037d1Sstsp 		timeout_del(&sc->calib_to);
2302e9f037d1Sstsp }
2303e9f037d1Sstsp 
2304e9f037d1Sstsp void
rtwn_scan_to(void * arg)2305e9f037d1Sstsp rtwn_scan_to(void *arg)
2306e9f037d1Sstsp {
2307e9f037d1Sstsp 	struct rtwn_pci_softc *sc = arg;
2308e9f037d1Sstsp 
2309e9f037d1Sstsp 	rtwn_next_scan(&sc->sc_sc);
2310e9f037d1Sstsp }
2311e9f037d1Sstsp 
2312e9f037d1Sstsp void
rtwn_pci_next_scan(void * cookie)2313e9f037d1Sstsp rtwn_pci_next_scan(void *cookie)
2314e9f037d1Sstsp {
2315e9f037d1Sstsp 	struct rtwn_pci_softc *sc = cookie;
2316e9f037d1Sstsp 
2317e9f037d1Sstsp 	timeout_add_msec(&sc->scan_to, 200);
2318e9f037d1Sstsp }
2319e9f037d1Sstsp 
2320e9f037d1Sstsp void
rtwn_cancel_scan(void * cookie)2321e9f037d1Sstsp rtwn_cancel_scan(void *cookie)
2322e9f037d1Sstsp {
2323e9f037d1Sstsp 	struct rtwn_pci_softc *sc = cookie;
2324e9f037d1Sstsp 
2325e9f037d1Sstsp 	if (timeout_initialized(&sc->scan_to))
2326e9f037d1Sstsp 		timeout_del(&sc->scan_to);
2327e9f037d1Sstsp }
232823340becSstsp 
232923340becSstsp void
rtwn_wait_async(void * cookie)233023340becSstsp rtwn_wait_async(void *cookie)
233123340becSstsp {
233223340becSstsp 	/* nothing to do */
233323340becSstsp }
23341f929dc7Sstsp 
23351f929dc7Sstsp void
rtwn_tx_report(struct rtwn_pci_softc * sc,uint8_t * buf,int len)23361f929dc7Sstsp rtwn_tx_report(struct rtwn_pci_softc *sc, uint8_t *buf, int len)
23371f929dc7Sstsp {
23381f929dc7Sstsp 	struct r92c_c2h_tx_rpt *rpt = (struct r92c_c2h_tx_rpt *)buf;
23391f929dc7Sstsp 	int packets, tries, tx_ok, drop, expire, over;
23401f929dc7Sstsp 
23411f929dc7Sstsp 	if (len != sizeof(*rpt))
23421f929dc7Sstsp 		return;
23431f929dc7Sstsp 
2344600882e8Sjmatthew 	if (sc->sc_sc.chip & RTWN_CHIP_23A) {
2345600882e8Sjmatthew 		struct r88e_tx_rpt_ccx *rxstat = (struct r88e_tx_rpt_ccx *)buf;
2346600882e8Sjmatthew 
2347600882e8Sjmatthew 		/*
2348600882e8Sjmatthew 		 * we seem to get some garbage reports, so check macid makes
2349600882e8Sjmatthew 		 * sense.
2350600882e8Sjmatthew 		 */
2351600882e8Sjmatthew 		if (MS(rxstat->rptb1, R88E_RPTB1_MACID) != R92C_MACID_BSS) {
2352600882e8Sjmatthew 			return;
2353600882e8Sjmatthew 		}
2354600882e8Sjmatthew 
2355600882e8Sjmatthew 		packets = 1;
2356600882e8Sjmatthew 		tx_ok = (rxstat->rptb1 & R88E_RPTB1_PKT_OK) ? 1 : 0;
2357600882e8Sjmatthew 		tries = MS(rxstat->rptb2, R88E_RPTB2_RETRY_CNT);
2358600882e8Sjmatthew 		expire = (rxstat->rptb2 & R88E_RPTB2_LIFE_EXPIRE);
2359600882e8Sjmatthew 		over = (rxstat->rptb2 & R88E_RPTB2_RETRY_OVER);
2360600882e8Sjmatthew 		drop = 0;
2361600882e8Sjmatthew 	} else {
23621f929dc7Sstsp 		packets = MS(rpt->rptb6, R92C_RPTB6_RPT_PKT_NUM);
23631f929dc7Sstsp 		tries = MS(rpt->rptb0, R92C_RPTB0_RETRY_CNT);
23641f929dc7Sstsp 		tx_ok = (rpt->rptb7 & R92C_RPTB7_PKT_OK);
23651f929dc7Sstsp 		drop = (rpt->rptb6 & R92C_RPTB6_PKT_DROP);
23661f929dc7Sstsp 		expire = (rpt->rptb6 & R92C_RPTB6_LIFE_EXPIRE);
23671f929dc7Sstsp 		over = (rpt->rptb6 & R92C_RPTB6_RETRY_OVER);
2368600882e8Sjmatthew 	}
23691f929dc7Sstsp 
23701f929dc7Sstsp 	if (packets > 0) {
23711f929dc7Sstsp 		sc->amn.amn_txcnt += packets;
23723396b3adSstsp 		if (!tx_ok || tries > 1 || drop || expire || over)
23731f929dc7Sstsp 			sc->amn.amn_retrycnt++;
23741f929dc7Sstsp 	}
23751f929dc7Sstsp }
23761f929dc7Sstsp 
23771f929dc7Sstsp void
rtwn_poll_c2h_events(struct rtwn_pci_softc * sc)23781f929dc7Sstsp rtwn_poll_c2h_events(struct rtwn_pci_softc *sc)
23791f929dc7Sstsp {
23801f929dc7Sstsp 	const uint16_t off = R92C_C2HEVT_MSG + sizeof(struct r92c_c2h_evt);
23811f929dc7Sstsp 	uint8_t buf[R92C_C2H_MSG_MAX_LEN];
23821f929dc7Sstsp 	uint8_t id, len, status;
23831f929dc7Sstsp 	int i;
23841f929dc7Sstsp 
23851f929dc7Sstsp 	/* Read current status. */
23861f929dc7Sstsp 	status = rtwn_pci_read_1(sc, R92C_C2HEVT_CLEAR);
23871f929dc7Sstsp 	if (status == R92C_C2HEVT_HOST_CLOSE)
23881f929dc7Sstsp 		return;	/* nothing to do */
23891f929dc7Sstsp 
23901f929dc7Sstsp 	if (status == R92C_C2HEVT_FW_CLOSE) {
23911f929dc7Sstsp 		len = rtwn_pci_read_1(sc, R92C_C2HEVT_MSG);
23921f929dc7Sstsp 		id = MS(len, R92C_C2H_EVTB0_ID);
23931f929dc7Sstsp 		len = MS(len, R92C_C2H_EVTB0_LEN);
23941f929dc7Sstsp 
23951f929dc7Sstsp 		if (id == R92C_C2HEVT_TX_REPORT && len <= sizeof(buf)) {
23961f929dc7Sstsp 			memset(buf, 0, sizeof(buf));
23971f929dc7Sstsp 			for (i = 0; i < len; i++)
23981f929dc7Sstsp 				buf[i] = rtwn_pci_read_1(sc, off + i);
23991f929dc7Sstsp 			rtwn_tx_report(sc, buf, len);
24001f929dc7Sstsp 		} else
24011f929dc7Sstsp 			DPRINTF(("unhandled C2H event %d (%d bytes)\n",
24021f929dc7Sstsp 			    id, len));
24031f929dc7Sstsp 	}
24041f929dc7Sstsp 
24051f929dc7Sstsp 	/* Prepare for next event. */
24061f929dc7Sstsp 	rtwn_pci_write_1(sc, R92C_C2HEVT_CLEAR, R92C_C2HEVT_HOST_CLOSE);
24071f929dc7Sstsp }
2408