xref: /freebsd/sys/dev/malo/if_malo.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
13c7e78d3SWeongyo Jeong /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro F. Giffuni  *
43c7e78d3SWeongyo Jeong  * Copyright (c) 2008 Weongyo Jeong <weongyo@freebsd.org>
53c7e78d3SWeongyo Jeong  * Copyright (c) 2007 Marvell Semiconductor, Inc.
63c7e78d3SWeongyo Jeong  * Copyright (c) 2007 Sam Leffler, Errno Consulting
73c7e78d3SWeongyo Jeong  * All rights reserved.
83c7e78d3SWeongyo Jeong  *
93c7e78d3SWeongyo Jeong  * Redistribution and use in source and binary forms, with or without
103c7e78d3SWeongyo Jeong  * modification, are permitted provided that the following conditions
113c7e78d3SWeongyo Jeong  * are met:
123c7e78d3SWeongyo Jeong  * 1. Redistributions of source code must retain the above copyright
133c7e78d3SWeongyo Jeong  *    notice, this list of conditions and the following disclaimer,
143c7e78d3SWeongyo Jeong  *    without modification.
153c7e78d3SWeongyo Jeong  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
163c7e78d3SWeongyo Jeong  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
173c7e78d3SWeongyo Jeong  *    redistribution must be conditioned upon including a substantially
183c7e78d3SWeongyo Jeong  *    similar Disclaimer requirement for further binary redistribution.
193c7e78d3SWeongyo Jeong  *
203c7e78d3SWeongyo Jeong  * NO WARRANTY
213c7e78d3SWeongyo Jeong  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
223c7e78d3SWeongyo Jeong  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
233c7e78d3SWeongyo Jeong  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
243c7e78d3SWeongyo Jeong  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
253c7e78d3SWeongyo Jeong  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
263c7e78d3SWeongyo Jeong  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
273c7e78d3SWeongyo Jeong  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
283c7e78d3SWeongyo Jeong  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
293c7e78d3SWeongyo Jeong  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
303c7e78d3SWeongyo Jeong  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
313c7e78d3SWeongyo Jeong  * THE POSSIBILITY OF SUCH DAMAGES.
323c7e78d3SWeongyo Jeong  */
333c7e78d3SWeongyo Jeong 
343c7e78d3SWeongyo Jeong #include <sys/cdefs.h>
353c7e78d3SWeongyo Jeong #ifdef __FreeBSD__
363c7e78d3SWeongyo Jeong __FBSDID("$FreeBSD$");
373c7e78d3SWeongyo Jeong #endif
383c7e78d3SWeongyo Jeong 
39b032f27cSSam Leffler #include "opt_malo.h"
40b032f27cSSam Leffler 
413c7e78d3SWeongyo Jeong #include <sys/param.h>
423c7e78d3SWeongyo Jeong #include <sys/endian.h>
433c7e78d3SWeongyo Jeong #include <sys/kernel.h>
448ec07310SGleb Smirnoff #include <sys/malloc.h>
453c7e78d3SWeongyo Jeong #include <sys/socket.h>
463c7e78d3SWeongyo Jeong #include <sys/sockio.h>
473c7e78d3SWeongyo Jeong #include <sys/sysctl.h>
483c7e78d3SWeongyo Jeong #include <sys/taskqueue.h>
493c7e78d3SWeongyo Jeong 
503c7e78d3SWeongyo Jeong #include <machine/bus.h>
513c7e78d3SWeongyo Jeong #include <sys/bus.h>
523c7e78d3SWeongyo Jeong 
533c7e78d3SWeongyo Jeong #include <net/if.h>
5476039bc8SGleb Smirnoff #include <net/if_var.h>
553c7e78d3SWeongyo Jeong #include <net/if_dl.h>
563c7e78d3SWeongyo Jeong #include <net/if_media.h>
573c7e78d3SWeongyo Jeong #include <net/if_types.h>
583c7e78d3SWeongyo Jeong #include <net/ethernet.h>
593c7e78d3SWeongyo Jeong 
603c7e78d3SWeongyo Jeong #include <net80211/ieee80211_var.h>
613c7e78d3SWeongyo Jeong #include <net80211/ieee80211_regdomain.h>
623c7e78d3SWeongyo Jeong 
633c7e78d3SWeongyo Jeong #include <net/bpf.h>
643c7e78d3SWeongyo Jeong 
653c7e78d3SWeongyo Jeong #include <dev/malo/if_malo.h>
663c7e78d3SWeongyo Jeong 
673c7e78d3SWeongyo Jeong SYSCTL_NODE(_hw, OID_AUTO, malo, CTLFLAG_RD, 0,
683c7e78d3SWeongyo Jeong     "Marvell 88w8335 driver parameters");
693c7e78d3SWeongyo Jeong 
703c7e78d3SWeongyo Jeong static	int malo_txcoalesce = 8;	/* # tx pkts to q before poking f/w*/
71af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, txcoalesce, CTLFLAG_RWTUN, &malo_txcoalesce,
723c7e78d3SWeongyo Jeong 	    0, "tx buffers to send at once");
733c7e78d3SWeongyo Jeong static	int malo_rxbuf = MALO_RXBUF;		/* # rx buffers to allocate */
74af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &malo_rxbuf,
753c7e78d3SWeongyo Jeong 	    0, "rx buffers allocated");
763c7e78d3SWeongyo Jeong static	int malo_rxquota = MALO_RXBUF;		/* # max buffers to process */
77af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, rxquota, CTLFLAG_RWTUN, &malo_rxquota,
783c7e78d3SWeongyo Jeong 	    0, "max rx buffers to process per interrupt");
793c7e78d3SWeongyo Jeong static	int malo_txbuf = MALO_TXBUF;		/* # tx buffers to allocate */
80af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, txbuf, CTLFLAG_RWTUN, &malo_txbuf,
813c7e78d3SWeongyo Jeong 	    0, "tx buffers allocated");
823c7e78d3SWeongyo Jeong 
833c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
843c7e78d3SWeongyo Jeong static	int malo_debug = 0;
85af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, debug, CTLFLAG_RWTUN, &malo_debug,
863c7e78d3SWeongyo Jeong 	    0, "control debugging printfs");
873c7e78d3SWeongyo Jeong enum {
883c7e78d3SWeongyo Jeong 	MALO_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
893c7e78d3SWeongyo Jeong 	MALO_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
903c7e78d3SWeongyo Jeong 	MALO_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
913c7e78d3SWeongyo Jeong 	MALO_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
923c7e78d3SWeongyo Jeong 	MALO_DEBUG_RESET	= 0x00000010,	/* reset processing */
933c7e78d3SWeongyo Jeong 	MALO_DEBUG_INTR		= 0x00000040,	/* ISR */
943c7e78d3SWeongyo Jeong 	MALO_DEBUG_TX_PROC	= 0x00000080,	/* tx ISR proc */
953c7e78d3SWeongyo Jeong 	MALO_DEBUG_RX_PROC	= 0x00000100,	/* rx ISR proc */
963c7e78d3SWeongyo Jeong 	MALO_DEBUG_STATE	= 0x00000400,	/* 802.11 state transitions */
973c7e78d3SWeongyo Jeong 	MALO_DEBUG_NODE		= 0x00000800,	/* node management */
983c7e78d3SWeongyo Jeong 	MALO_DEBUG_RECV_ALL	= 0x00001000,	/* trace all frames (beacons) */
993c7e78d3SWeongyo Jeong 	MALO_DEBUG_FW		= 0x00008000,	/* firmware */
1003c7e78d3SWeongyo Jeong 	MALO_DEBUG_ANY		= 0xffffffff
1013c7e78d3SWeongyo Jeong };
1023c7e78d3SWeongyo Jeong #define	IS_BEACON(wh)							\
1033c7e78d3SWeongyo Jeong 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK |			\
1043c7e78d3SWeongyo Jeong 		IEEE80211_FC0_SUBTYPE_MASK)) ==				\
1053c7e78d3SWeongyo Jeong 	 (IEEE80211_FC0_TYPE_MGT|IEEE80211_FC0_SUBTYPE_BEACON))
1063c7e78d3SWeongyo Jeong #define	IFF_DUMPPKTS_RECV(sc, wh)					\
1073c7e78d3SWeongyo Jeong 	(((sc->malo_debug & MALO_DEBUG_RECV) &&				\
1087a79cebfSGleb Smirnoff 	  ((sc->malo_debug & MALO_DEBUG_RECV_ALL) || !IS_BEACON(wh))))
1093c7e78d3SWeongyo Jeong #define	IFF_DUMPPKTS_XMIT(sc)						\
1107a79cebfSGleb Smirnoff 	(sc->malo_debug & MALO_DEBUG_XMIT)
1113c7e78d3SWeongyo Jeong #define	DPRINTF(sc, m, fmt, ...) do {				\
1123c7e78d3SWeongyo Jeong 	if (sc->malo_debug & (m))				\
1133c7e78d3SWeongyo Jeong 		printf(fmt, __VA_ARGS__);			\
1143c7e78d3SWeongyo Jeong } while (0)
1153c7e78d3SWeongyo Jeong #else
1163c7e78d3SWeongyo Jeong #define	DPRINTF(sc, m, fmt, ...) do {				\
1173c7e78d3SWeongyo Jeong 	(void) sc;						\
1183c7e78d3SWeongyo Jeong } while (0)
1193c7e78d3SWeongyo Jeong #endif
1203c7e78d3SWeongyo Jeong 
121d745c852SEd Schouten static MALLOC_DEFINE(M_MALODEV, "malodev", "malo driver dma buffers");
1223c7e78d3SWeongyo Jeong 
123fcd9500fSBernhard Schmidt static struct ieee80211vap *malo_vap_create(struct ieee80211com *,
124fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
125fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
126fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
127b032f27cSSam Leffler static  void	malo_vap_delete(struct ieee80211vap *);
1283c7e78d3SWeongyo Jeong static	int	malo_dma_setup(struct malo_softc *);
1293c7e78d3SWeongyo Jeong static	int	malo_setup_hwdma(struct malo_softc *);
1303c7e78d3SWeongyo Jeong static	void	malo_txq_init(struct malo_softc *, struct malo_txq *, int);
1313c7e78d3SWeongyo Jeong static	void	malo_tx_cleanupq(struct malo_softc *, struct malo_txq *);
1327a79cebfSGleb Smirnoff static	void	malo_parent(struct ieee80211com *);
1337a79cebfSGleb Smirnoff static	int	malo_transmit(struct ieee80211com *, struct mbuf *);
1347a79cebfSGleb Smirnoff static	void	malo_start(struct malo_softc *);
1357cf545d0SJohn Baldwin static	void	malo_watchdog(void *);
136272f6adeSGleb Smirnoff static	void	malo_updateslot(struct ieee80211com *);
137b032f27cSSam Leffler static	int	malo_newstate(struct ieee80211vap *, enum ieee80211_state, int);
1383c7e78d3SWeongyo Jeong static	void	malo_scan_start(struct ieee80211com *);
1393c7e78d3SWeongyo Jeong static	void	malo_scan_end(struct ieee80211com *);
1403c7e78d3SWeongyo Jeong static	void	malo_set_channel(struct ieee80211com *);
1413c7e78d3SWeongyo Jeong static	int	malo_raw_xmit(struct ieee80211_node *, struct mbuf *,
1423c7e78d3SWeongyo Jeong 		    const struct ieee80211_bpf_params *);
1433c7e78d3SWeongyo Jeong static	void	malo_sysctlattach(struct malo_softc *);
1443c7e78d3SWeongyo Jeong static	void	malo_announce(struct malo_softc *);
1453c7e78d3SWeongyo Jeong static	void	malo_dma_cleanup(struct malo_softc *);
1467a79cebfSGleb Smirnoff static	void	malo_stop(struct malo_softc *);
1473c7e78d3SWeongyo Jeong static	int	malo_chan_set(struct malo_softc *, struct ieee80211_channel *);
1483c7e78d3SWeongyo Jeong static	int	malo_mode_init(struct malo_softc *);
1493c7e78d3SWeongyo Jeong static	void	malo_tx_proc(void *, int);
1503c7e78d3SWeongyo Jeong static	void	malo_rx_proc(void *, int);
1513c7e78d3SWeongyo Jeong static	void	malo_init(void *);
1523c7e78d3SWeongyo Jeong 
1533c7e78d3SWeongyo Jeong /*
1543c7e78d3SWeongyo Jeong  * Read/Write shorthands for accesses to BAR 0.  Note that all BAR 1
1553c7e78d3SWeongyo Jeong  * operations are done in the "hal" except getting H/W MAC address at
1563c7e78d3SWeongyo Jeong  * malo_attach and there should be no reference to them here.
1573c7e78d3SWeongyo Jeong  */
1583c7e78d3SWeongyo Jeong static uint32_t
1593c7e78d3SWeongyo Jeong malo_bar0_read4(struct malo_softc *sc, bus_size_t off)
1603c7e78d3SWeongyo Jeong {
1613c7e78d3SWeongyo Jeong 	return bus_space_read_4(sc->malo_io0t, sc->malo_io0h, off);
1623c7e78d3SWeongyo Jeong }
1633c7e78d3SWeongyo Jeong 
1643c7e78d3SWeongyo Jeong static void
1653c7e78d3SWeongyo Jeong malo_bar0_write4(struct malo_softc *sc, bus_size_t off, uint32_t val)
1663c7e78d3SWeongyo Jeong {
16768d9da91SWarner Losh 	DPRINTF(sc, MALO_DEBUG_FW, "%s: off 0x%jx val 0x%x\n",
1682706e872SMarius Strobl 	    __func__, (uintmax_t)off, val);
1693c7e78d3SWeongyo Jeong 
1703c7e78d3SWeongyo Jeong 	bus_space_write_4(sc->malo_io0t, sc->malo_io0h, off, val);
1713c7e78d3SWeongyo Jeong }
1723c7e78d3SWeongyo Jeong 
1733c7e78d3SWeongyo Jeong int
1743c7e78d3SWeongyo Jeong malo_attach(uint16_t devid, struct malo_softc *sc)
1753c7e78d3SWeongyo Jeong {
1767a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
177ba2c1fbcSAdrian Chadd 	struct malo_hal *mh;
1787a79cebfSGleb Smirnoff 	int error;
179a061fea6SAndriy Voskoboinyk 	uint8_t bands[IEEE80211_MODE_BYTES];
1803c7e78d3SWeongyo Jeong 
1813c7e78d3SWeongyo Jeong 	MALO_LOCK_INIT(sc);
1827cf545d0SJohn Baldwin 	callout_init_mtx(&sc->malo_watchdog_timer, &sc->malo_mtx, 0);
1837a79cebfSGleb Smirnoff 	mbufq_init(&sc->malo_snd, ifqmaxlen);
1843c7e78d3SWeongyo Jeong 
1853c7e78d3SWeongyo Jeong 	mh = malo_hal_attach(sc->malo_dev, devid,
1863c7e78d3SWeongyo Jeong 	    sc->malo_io1h, sc->malo_io1t, sc->malo_dmat);
1873c7e78d3SWeongyo Jeong 	if (mh == NULL) {
1887a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "unable to attach HAL\n");
1893c7e78d3SWeongyo Jeong 		error = EIO;
1903c7e78d3SWeongyo Jeong 		goto bad;
1913c7e78d3SWeongyo Jeong 	}
1923c7e78d3SWeongyo Jeong 	sc->malo_mh = mh;
1933c7e78d3SWeongyo Jeong 
194b032f27cSSam Leffler 	/*
195b032f27cSSam Leffler 	 * Load firmware so we can get setup.  We arbitrarily pick station
196b032f27cSSam Leffler 	 * firmware; we'll re-load firmware as needed so setting up
197b032f27cSSam Leffler 	 * the wrong mode isn't a big deal.
198b032f27cSSam Leffler 	 */
199b032f27cSSam Leffler 	error = malo_hal_fwload(mh, "malo8335-h", "malo8335-m");
200b032f27cSSam Leffler 	if (error != 0) {
2017a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "unable to setup firmware\n");
202b032f27cSSam Leffler 		goto bad1;
203b032f27cSSam Leffler 	}
204b032f27cSSam Leffler 	/* XXX gethwspecs() extracts correct informations?  not maybe!  */
205b032f27cSSam Leffler 	error = malo_hal_gethwspecs(mh, &sc->malo_hwspecs);
206b032f27cSSam Leffler 	if (error != 0) {
2077a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "unable to fetch h/w specs\n");
208b032f27cSSam Leffler 		goto bad1;
209b032f27cSSam Leffler 	}
210b032f27cSSam Leffler 
211b032f27cSSam Leffler 	DPRINTF(sc, MALO_DEBUG_FW,
212b032f27cSSam Leffler 	    "malo_hal_gethwspecs: hwversion 0x%x hostif 0x%x"
213b032f27cSSam Leffler 	    "maxnum_wcb 0x%x maxnum_mcaddr 0x%x maxnum_tx_wcb 0x%x"
214b032f27cSSam Leffler 	    "regioncode 0x%x num_antenna 0x%x fw_releasenum 0x%x"
215b032f27cSSam Leffler 	    "wcbbase0 0x%x rxdesc_read 0x%x rxdesc_write 0x%x"
216b032f27cSSam Leffler 	    "ul_fw_awakecookie 0x%x w[4] = %x %x %x %x",
217b032f27cSSam Leffler 	    sc->malo_hwspecs.hwversion,
218b032f27cSSam Leffler 	    sc->malo_hwspecs.hostinterface, sc->malo_hwspecs.maxnum_wcb,
219b032f27cSSam Leffler 	    sc->malo_hwspecs.maxnum_mcaddr, sc->malo_hwspecs.maxnum_tx_wcb,
220b032f27cSSam Leffler 	    sc->malo_hwspecs.regioncode, sc->malo_hwspecs.num_antenna,
221b032f27cSSam Leffler 	    sc->malo_hwspecs.fw_releasenum, sc->malo_hwspecs.wcbbase0,
222b032f27cSSam Leffler 	    sc->malo_hwspecs.rxdesc_read, sc->malo_hwspecs.rxdesc_write,
223b032f27cSSam Leffler 	    sc->malo_hwspecs.ul_fw_awakecookie,
224b032f27cSSam Leffler 	    sc->malo_hwspecs.wcbbase[0], sc->malo_hwspecs.wcbbase[1],
225b032f27cSSam Leffler 	    sc->malo_hwspecs.wcbbase[2], sc->malo_hwspecs.wcbbase[3]);
226b032f27cSSam Leffler 
227b032f27cSSam Leffler 	/* NB: firmware looks that it does not export regdomain info API.  */
2280046e186SAndriy Voskoboinyk 	memset(bands, 0, sizeof(bands));
2290046e186SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11B);
2300046e186SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11G);
2310046e186SAndriy Voskoboinyk 	ieee80211_init_channels(ic, NULL, bands);
232b032f27cSSam Leffler 
2333c7e78d3SWeongyo Jeong 	sc->malo_txantenna = 0x2;	/* h/w default */
2343c7e78d3SWeongyo Jeong 	sc->malo_rxantenna = 0xffff;	/* h/w default */
2353c7e78d3SWeongyo Jeong 
2363c7e78d3SWeongyo Jeong 	/*
2373c7e78d3SWeongyo Jeong 	 * Allocate tx + rx descriptors and populate the lists.
2383c7e78d3SWeongyo Jeong 	 * We immediately push the information to the firmware
2393c7e78d3SWeongyo Jeong 	 * as otherwise it gets upset.
2403c7e78d3SWeongyo Jeong 	 */
2413c7e78d3SWeongyo Jeong 	error = malo_dma_setup(sc);
2423c7e78d3SWeongyo Jeong 	if (error != 0) {
2437a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
2447a79cebfSGleb Smirnoff 		    "failed to setup descriptors: %d\n", error);
2453c7e78d3SWeongyo Jeong 		goto bad1;
2463c7e78d3SWeongyo Jeong 	}
247b032f27cSSam Leffler 	error = malo_setup_hwdma(sc);	/* push to firmware */
248b032f27cSSam Leffler 	if (error != 0)			/* NB: malo_setupdma prints msg */
249136694a4SWeongyo Jeong 		goto bad2;
2503c7e78d3SWeongyo Jeong 
2513c7e78d3SWeongyo Jeong 	sc->malo_tq = taskqueue_create_fast("malo_taskq", M_NOWAIT,
2523c7e78d3SWeongyo Jeong 		taskqueue_thread_enqueue, &sc->malo_tq);
2533c7e78d3SWeongyo Jeong 	taskqueue_start_threads(&sc->malo_tq, 1, PI_NET,
2547a79cebfSGleb Smirnoff 		"%s taskq", device_get_nameunit(sc->malo_dev));
2553c7e78d3SWeongyo Jeong 
2563c7e78d3SWeongyo Jeong 	TASK_INIT(&sc->malo_rxtask, 0, malo_rx_proc, sc);
2573c7e78d3SWeongyo Jeong 	TASK_INIT(&sc->malo_txtask, 0, malo_tx_proc, sc);
2583c7e78d3SWeongyo Jeong 
25959686fe9SGleb Smirnoff 	ic->ic_softc = sc;
260c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(sc->malo_dev);
2613c7e78d3SWeongyo Jeong 	/* XXX not right but it's not used anywhere important */
2623c7e78d3SWeongyo Jeong 	ic->ic_phytype = IEEE80211_T_OFDM;
2633c7e78d3SWeongyo Jeong 	ic->ic_opmode = IEEE80211_M_STA;
2643c7e78d3SWeongyo Jeong 	ic->ic_caps =
265c43feedeSSam Leffler 	      IEEE80211_C_STA			/* station mode supported */
266c43feedeSSam Leffler 	    | IEEE80211_C_BGSCAN		/* capable of bg scanning */
2673c7e78d3SWeongyo Jeong 	    | IEEE80211_C_MONITOR		/* monitor mode */
2683c7e78d3SWeongyo Jeong 	    | IEEE80211_C_SHPREAMBLE		/* short preamble supported */
2693c7e78d3SWeongyo Jeong 	    | IEEE80211_C_SHSLOT		/* short slot time supported */
2703c7e78d3SWeongyo Jeong 	    | IEEE80211_C_TXPMGT		/* capable of txpow mgt */
2713c7e78d3SWeongyo Jeong 	    | IEEE80211_C_WPA			/* capable of WPA1+WPA2 */
2723c7e78d3SWeongyo Jeong 	    ;
2737a79cebfSGleb Smirnoff 	IEEE80211_ADDR_COPY(ic->ic_macaddr, sc->malo_hwspecs.macaddr);
2743c7e78d3SWeongyo Jeong 
2753c7e78d3SWeongyo Jeong 	/*
2763c7e78d3SWeongyo Jeong 	 * Transmit requires space in the packet for a special format transmit
2773c7e78d3SWeongyo Jeong 	 * record and optional padding between this record and the payload.
2783c7e78d3SWeongyo Jeong 	 * Ask the net80211 layer to arrange this when encapsulating
2793c7e78d3SWeongyo Jeong 	 * packets so we can add it efficiently.
2803c7e78d3SWeongyo Jeong 	 */
2813c7e78d3SWeongyo Jeong 	ic->ic_headroom = sizeof(struct malo_txrec) -
2823c7e78d3SWeongyo Jeong 		sizeof(struct ieee80211_frame);
2833c7e78d3SWeongyo Jeong 
2843c7e78d3SWeongyo Jeong 	/* call MI attach routine. */
2857a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
2863c7e78d3SWeongyo Jeong 	/* override default methods */
287b032f27cSSam Leffler 	ic->ic_vap_create = malo_vap_create;
288b032f27cSSam Leffler 	ic->ic_vap_delete = malo_vap_delete;
2893c7e78d3SWeongyo Jeong 	ic->ic_raw_xmit = malo_raw_xmit;
290b032f27cSSam Leffler 	ic->ic_updateslot = malo_updateslot;
2913c7e78d3SWeongyo Jeong 	ic->ic_scan_start = malo_scan_start;
2923c7e78d3SWeongyo Jeong 	ic->ic_scan_end = malo_scan_end;
2933c7e78d3SWeongyo Jeong 	ic->ic_set_channel = malo_set_channel;
2947a79cebfSGleb Smirnoff 	ic->ic_parent = malo_parent;
2957a79cebfSGleb Smirnoff 	ic->ic_transmit = malo_transmit;
2963c7e78d3SWeongyo Jeong 
2973c7e78d3SWeongyo Jeong 	sc->malo_invalid = 0;		/* ready to go, enable int handling */
2983c7e78d3SWeongyo Jeong 
2995463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
3005463c4a4SSam Leffler 	    &sc->malo_tx_th.wt_ihdr, sizeof(sc->malo_tx_th),
3015463c4a4SSam Leffler 		MALO_TX_RADIOTAP_PRESENT,
3025463c4a4SSam Leffler 	    &sc->malo_rx_th.wr_ihdr, sizeof(sc->malo_rx_th),
3035463c4a4SSam Leffler 		MALO_RX_RADIOTAP_PRESENT);
3043c7e78d3SWeongyo Jeong 
3053c7e78d3SWeongyo Jeong 	/*
3063c7e78d3SWeongyo Jeong 	 * Setup dynamic sysctl's.
3073c7e78d3SWeongyo Jeong 	 */
3083c7e78d3SWeongyo Jeong 	malo_sysctlattach(sc);
3093c7e78d3SWeongyo Jeong 
3103c7e78d3SWeongyo Jeong 	if (bootverbose)
3113c7e78d3SWeongyo Jeong 		ieee80211_announce(ic);
312b032f27cSSam Leffler 	malo_announce(sc);
3133c7e78d3SWeongyo Jeong 
3143c7e78d3SWeongyo Jeong 	return 0;
315136694a4SWeongyo Jeong bad2:
316136694a4SWeongyo Jeong 	malo_dma_cleanup(sc);
3173c7e78d3SWeongyo Jeong bad1:
3183c7e78d3SWeongyo Jeong 	malo_hal_detach(mh);
3193c7e78d3SWeongyo Jeong bad:
3203c7e78d3SWeongyo Jeong 	sc->malo_invalid = 1;
3213c7e78d3SWeongyo Jeong 
3223c7e78d3SWeongyo Jeong 	return error;
3233c7e78d3SWeongyo Jeong }
3243c7e78d3SWeongyo Jeong 
325b032f27cSSam Leffler static struct ieee80211vap *
326fcd9500fSBernhard Schmidt malo_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
327fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
328b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
329b032f27cSSam Leffler     const uint8_t mac[IEEE80211_ADDR_LEN])
330b032f27cSSam Leffler {
3317a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
332b032f27cSSam Leffler 	struct malo_vap *mvp;
333b032f27cSSam Leffler 	struct ieee80211vap *vap;
334b032f27cSSam Leffler 
335b032f27cSSam Leffler 	if (!TAILQ_EMPTY(&ic->ic_vaps)) {
3367a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "multiple vaps not supported\n");
337b032f27cSSam Leffler 		return NULL;
338b032f27cSSam Leffler 	}
339b032f27cSSam Leffler 	switch (opmode) {
340b032f27cSSam Leffler 	case IEEE80211_M_STA:
341b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
342b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
343b032f27cSSam Leffler 		/* fall thru... */
344b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
345b032f27cSSam Leffler 		break;
346b032f27cSSam Leffler 	default:
3477a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "%s mode not supported\n",
348b032f27cSSam Leffler 		    ieee80211_opmode_name[opmode]);
349b032f27cSSam Leffler 		return NULL;		/* unsupported */
350b032f27cSSam Leffler 	}
3517a79cebfSGleb Smirnoff 	mvp = malloc(sizeof(struct malo_vap), M_80211_VAP, M_WAITOK | M_ZERO);
352b032f27cSSam Leffler 	vap = &mvp->malo_vap;
3537a79cebfSGleb Smirnoff 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
354b032f27cSSam Leffler 
355b032f27cSSam Leffler 	/* override state transition machine */
356b032f27cSSam Leffler 	mvp->malo_newstate = vap->iv_newstate;
357b032f27cSSam Leffler 	vap->iv_newstate = malo_newstate;
358b032f27cSSam Leffler 
359b032f27cSSam Leffler 	/* complete setup */
360b032f27cSSam Leffler 	ieee80211_vap_attach(vap,
3617a79cebfSGleb Smirnoff 	    ieee80211_media_change, ieee80211_media_status, mac);
362b032f27cSSam Leffler 	ic->ic_opmode = opmode;
363b032f27cSSam Leffler 	return vap;
364b032f27cSSam Leffler }
365b032f27cSSam Leffler 
366b032f27cSSam Leffler static void
367b032f27cSSam Leffler malo_vap_delete(struct ieee80211vap *vap)
368b032f27cSSam Leffler {
369b032f27cSSam Leffler 	struct malo_vap *mvp = MALO_VAP(vap);
370b032f27cSSam Leffler 
371b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
372b032f27cSSam Leffler 	free(mvp, M_80211_VAP);
373b032f27cSSam Leffler }
374b032f27cSSam Leffler 
3753c7e78d3SWeongyo Jeong int
3763c7e78d3SWeongyo Jeong malo_intr(void *arg)
3773c7e78d3SWeongyo Jeong {
3783c7e78d3SWeongyo Jeong 	struct malo_softc *sc = arg;
3793c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
3803c7e78d3SWeongyo Jeong 	uint32_t status;
3813c7e78d3SWeongyo Jeong 
3823c7e78d3SWeongyo Jeong 	if (sc->malo_invalid) {
3833c7e78d3SWeongyo Jeong 		/*
3843c7e78d3SWeongyo Jeong 		 * The hardware is not ready/present, don't touch anything.
3853c7e78d3SWeongyo Jeong 		 * Note this can happen early on if the IRQ is shared.
3863c7e78d3SWeongyo Jeong 		 */
3873c7e78d3SWeongyo Jeong 		DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
3883c7e78d3SWeongyo Jeong 		return (FILTER_STRAY);
3893c7e78d3SWeongyo Jeong 	}
3903c7e78d3SWeongyo Jeong 
3913c7e78d3SWeongyo Jeong 	/*
3923c7e78d3SWeongyo Jeong 	 * Figure out the reason(s) for the interrupt.
3933c7e78d3SWeongyo Jeong 	 */
3943c7e78d3SWeongyo Jeong 	malo_hal_getisr(mh, &status);		/* NB: clears ISR too */
3953c7e78d3SWeongyo Jeong 	if (status == 0)			/* must be a shared irq */
3963c7e78d3SWeongyo Jeong 		return (FILTER_STRAY);
3973c7e78d3SWeongyo Jeong 
3983c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_INTR, "%s: status 0x%x imask 0x%x\n",
3993c7e78d3SWeongyo Jeong 	    __func__, status, sc->malo_imask);
4003c7e78d3SWeongyo Jeong 
4013c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_RX_RDY)
402cbc4d2dbSJohn Baldwin 		taskqueue_enqueue(sc->malo_tq, &sc->malo_rxtask);
4033c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_TX_DONE)
404cbc4d2dbSJohn Baldwin 		taskqueue_enqueue(sc->malo_tq, &sc->malo_txtask);
4053c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_OPC_DONE)
4063c7e78d3SWeongyo Jeong 		malo_hal_cmddone(mh);
4073c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_MAC_EVENT)
4083c7e78d3SWeongyo Jeong 		;
4093c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_RX_PROBLEM)
4103c7e78d3SWeongyo Jeong 		;
4113c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_ICV_ERROR) {
4123c7e78d3SWeongyo Jeong 		/* TKIP ICV error */
4133c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_rx_badtkipicv++;
4143c7e78d3SWeongyo Jeong 	}
4153c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
4163c7e78d3SWeongyo Jeong 	if (((status | sc->malo_imask) ^ sc->malo_imask) != 0)
4173c7e78d3SWeongyo Jeong 		DPRINTF(sc, MALO_DEBUG_INTR,
4183c7e78d3SWeongyo Jeong 		    "%s: can't handle interrupt status 0x%x\n",
4193c7e78d3SWeongyo Jeong 		    __func__, status);
4203c7e78d3SWeongyo Jeong #endif
4213c7e78d3SWeongyo Jeong 	return (FILTER_HANDLED);
4223c7e78d3SWeongyo Jeong }
4233c7e78d3SWeongyo Jeong 
4243c7e78d3SWeongyo Jeong static void
4253c7e78d3SWeongyo Jeong malo_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
4263c7e78d3SWeongyo Jeong {
4273c7e78d3SWeongyo Jeong 	bus_addr_t *paddr = (bus_addr_t*) arg;
4283c7e78d3SWeongyo Jeong 
4293c7e78d3SWeongyo Jeong 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
4303c7e78d3SWeongyo Jeong 
4313c7e78d3SWeongyo Jeong 	*paddr = segs->ds_addr;
4323c7e78d3SWeongyo Jeong }
4333c7e78d3SWeongyo Jeong 
4343c7e78d3SWeongyo Jeong static int
4353c7e78d3SWeongyo Jeong malo_desc_setup(struct malo_softc *sc, const char *name,
4363c7e78d3SWeongyo Jeong     struct malo_descdma *dd,
4373c7e78d3SWeongyo Jeong     int nbuf, size_t bufsize, int ndesc, size_t descsize)
4383c7e78d3SWeongyo Jeong {
4393c7e78d3SWeongyo Jeong 	int error;
4403c7e78d3SWeongyo Jeong 	uint8_t *ds;
4413c7e78d3SWeongyo Jeong 
4423c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RESET,
4433c7e78d3SWeongyo Jeong 	    "%s: %s DMA: %u bufs (%ju) %u desc/buf (%ju)\n",
4443c7e78d3SWeongyo Jeong 	    __func__, name, nbuf, (uintmax_t) bufsize,
4453c7e78d3SWeongyo Jeong 	    ndesc, (uintmax_t) descsize);
4463c7e78d3SWeongyo Jeong 
4473c7e78d3SWeongyo Jeong 	dd->dd_name = name;
4483c7e78d3SWeongyo Jeong 	dd->dd_desc_len = nbuf * ndesc * descsize;
4493c7e78d3SWeongyo Jeong 
4503c7e78d3SWeongyo Jeong 	/*
4513c7e78d3SWeongyo Jeong 	 * Setup DMA descriptor area.
4523c7e78d3SWeongyo Jeong 	 */
4533c7e78d3SWeongyo Jeong 	error = bus_dma_tag_create(bus_get_dma_tag(sc->malo_dev),/* parent */
4543c7e78d3SWeongyo Jeong 		       PAGE_SIZE, 0,		/* alignment, bounds */
4553c7e78d3SWeongyo Jeong 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
4563c7e78d3SWeongyo Jeong 		       BUS_SPACE_MAXADDR,	/* highaddr */
4573c7e78d3SWeongyo Jeong 		       NULL, NULL,		/* filter, filterarg */
4583c7e78d3SWeongyo Jeong 		       dd->dd_desc_len,		/* maxsize */
4593c7e78d3SWeongyo Jeong 		       1,			/* nsegments */
4603c7e78d3SWeongyo Jeong 		       dd->dd_desc_len,		/* maxsegsize */
4613c7e78d3SWeongyo Jeong 		       BUS_DMA_ALLOCNOW,	/* flags */
4623c7e78d3SWeongyo Jeong 		       NULL,			/* lockfunc */
4633c7e78d3SWeongyo Jeong 		       NULL,			/* lockarg */
4643c7e78d3SWeongyo Jeong 		       &dd->dd_dmat);
4653c7e78d3SWeongyo Jeong 	if (error != 0) {
4667a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "cannot allocate %s DMA tag\n",
4677a79cebfSGleb Smirnoff 		    dd->dd_name);
4683c7e78d3SWeongyo Jeong 		return error;
4693c7e78d3SWeongyo Jeong 	}
4703c7e78d3SWeongyo Jeong 
4713c7e78d3SWeongyo Jeong 	/* allocate descriptors */
4723c7e78d3SWeongyo Jeong 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
4733c7e78d3SWeongyo Jeong 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dd->dd_dmamap);
4743c7e78d3SWeongyo Jeong 	if (error != 0) {
4757a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
4767a79cebfSGleb Smirnoff 		    "unable to alloc memory for %u %s descriptors, "
4773c7e78d3SWeongyo Jeong 		    "error %u\n", nbuf * ndesc, dd->dd_name, error);
4783c7e78d3SWeongyo Jeong 		goto fail1;
4793c7e78d3SWeongyo Jeong 	}
4803c7e78d3SWeongyo Jeong 
4813c7e78d3SWeongyo Jeong 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
4823c7e78d3SWeongyo Jeong 	    dd->dd_desc, dd->dd_desc_len,
4833c7e78d3SWeongyo Jeong 	    malo_load_cb, &dd->dd_desc_paddr, BUS_DMA_NOWAIT);
4843c7e78d3SWeongyo Jeong 	if (error != 0) {
4857a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
4867a79cebfSGleb Smirnoff 		    "unable to map %s descriptors, error %u\n",
4873c7e78d3SWeongyo Jeong 		    dd->dd_name, error);
4883c7e78d3SWeongyo Jeong 		goto fail2;
4893c7e78d3SWeongyo Jeong 	}
4903c7e78d3SWeongyo Jeong 
4913c7e78d3SWeongyo Jeong 	ds = dd->dd_desc;
4923c7e78d3SWeongyo Jeong 	memset(ds, 0, dd->dd_desc_len);
4932706e872SMarius Strobl 	DPRINTF(sc, MALO_DEBUG_RESET,
4942706e872SMarius Strobl 	    "%s: %s DMA map: %p (%lu) -> 0x%jx (%lu)\n",
4953c7e78d3SWeongyo Jeong 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
4962706e872SMarius Strobl 	    (uintmax_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
4973c7e78d3SWeongyo Jeong 
4983c7e78d3SWeongyo Jeong 	return 0;
4993c7e78d3SWeongyo Jeong fail2:
5003c7e78d3SWeongyo Jeong 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
5013c7e78d3SWeongyo Jeong fail1:
5023c7e78d3SWeongyo Jeong 	bus_dma_tag_destroy(dd->dd_dmat);
5033c7e78d3SWeongyo Jeong 	memset(dd, 0, sizeof(*dd));
5043c7e78d3SWeongyo Jeong 	return error;
5053c7e78d3SWeongyo Jeong }
5063c7e78d3SWeongyo Jeong 
5073c7e78d3SWeongyo Jeong #define	DS2PHYS(_dd, _ds) \
5083c7e78d3SWeongyo Jeong 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
5093c7e78d3SWeongyo Jeong 
5103c7e78d3SWeongyo Jeong static int
5113c7e78d3SWeongyo Jeong malo_rxdma_setup(struct malo_softc *sc)
5123c7e78d3SWeongyo Jeong {
5133c7e78d3SWeongyo Jeong 	int error, bsize, i;
5143c7e78d3SWeongyo Jeong 	struct malo_rxbuf *bf;
5153c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
5163c7e78d3SWeongyo Jeong 
5173c7e78d3SWeongyo Jeong 	error = malo_desc_setup(sc, "rx", &sc->malo_rxdma,
5183c7e78d3SWeongyo Jeong 	    malo_rxbuf, sizeof(struct malo_rxbuf),
5193c7e78d3SWeongyo Jeong 	    1, sizeof(struct malo_rxdesc));
5203c7e78d3SWeongyo Jeong 	if (error != 0)
5213c7e78d3SWeongyo Jeong 		return error;
5223c7e78d3SWeongyo Jeong 
5233c7e78d3SWeongyo Jeong 	/*
5243c7e78d3SWeongyo Jeong 	 * Allocate rx buffers and set them up.
5253c7e78d3SWeongyo Jeong 	 */
5263c7e78d3SWeongyo Jeong 	bsize = malo_rxbuf * sizeof(struct malo_rxbuf);
5273c7e78d3SWeongyo Jeong 	bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
5283c7e78d3SWeongyo Jeong 	if (bf == NULL) {
5297a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
5307a79cebfSGleb Smirnoff 		    "malloc of %u rx buffers failed\n", bsize);
5313c7e78d3SWeongyo Jeong 		return error;
5323c7e78d3SWeongyo Jeong 	}
5333c7e78d3SWeongyo Jeong 	sc->malo_rxdma.dd_bufptr = bf;
5343c7e78d3SWeongyo Jeong 
5353c7e78d3SWeongyo Jeong 	STAILQ_INIT(&sc->malo_rxbuf);
5363c7e78d3SWeongyo Jeong 	ds = sc->malo_rxdma.dd_desc;
5373c7e78d3SWeongyo Jeong 	for (i = 0; i < malo_rxbuf; i++, bf++, ds++) {
5383c7e78d3SWeongyo Jeong 		bf->bf_desc = ds;
5393c7e78d3SWeongyo Jeong 		bf->bf_daddr = DS2PHYS(&sc->malo_rxdma, ds);
5403c7e78d3SWeongyo Jeong 		error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
5413c7e78d3SWeongyo Jeong 		    &bf->bf_dmamap);
5423c7e78d3SWeongyo Jeong 		if (error != 0) {
5437a79cebfSGleb Smirnoff 			device_printf(sc->malo_dev,
5447a79cebfSGleb Smirnoff 			    "%s: unable to dmamap for rx buffer, error %d\n",
5457a79cebfSGleb Smirnoff 			    __func__, error);
5463c7e78d3SWeongyo Jeong 			return error;
5473c7e78d3SWeongyo Jeong 		}
5483c7e78d3SWeongyo Jeong 		/* NB: tail is intentional to preserve descriptor order */
5493c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&sc->malo_rxbuf, bf, bf_list);
5503c7e78d3SWeongyo Jeong 	}
5513c7e78d3SWeongyo Jeong 	return 0;
5523c7e78d3SWeongyo Jeong }
5533c7e78d3SWeongyo Jeong 
5543c7e78d3SWeongyo Jeong static int
5553c7e78d3SWeongyo Jeong malo_txdma_setup(struct malo_softc *sc, struct malo_txq *txq)
5563c7e78d3SWeongyo Jeong {
5573c7e78d3SWeongyo Jeong 	int error, bsize, i;
5583c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
5593c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
5603c7e78d3SWeongyo Jeong 
5613c7e78d3SWeongyo Jeong 	error = malo_desc_setup(sc, "tx", &txq->dma,
5623c7e78d3SWeongyo Jeong 	    malo_txbuf, sizeof(struct malo_txbuf),
5633c7e78d3SWeongyo Jeong 	    MALO_TXDESC, sizeof(struct malo_txdesc));
5643c7e78d3SWeongyo Jeong 	if (error != 0)
5653c7e78d3SWeongyo Jeong 		return error;
5663c7e78d3SWeongyo Jeong 
5673c7e78d3SWeongyo Jeong 	/* allocate and setup tx buffers */
5683c7e78d3SWeongyo Jeong 	bsize = malo_txbuf * sizeof(struct malo_txbuf);
5693c7e78d3SWeongyo Jeong 	bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
5703c7e78d3SWeongyo Jeong 	if (bf == NULL) {
5717a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "malloc of %u tx buffers failed\n",
5723c7e78d3SWeongyo Jeong 		    malo_txbuf);
5733c7e78d3SWeongyo Jeong 		return ENOMEM;
5743c7e78d3SWeongyo Jeong 	}
5753c7e78d3SWeongyo Jeong 	txq->dma.dd_bufptr = bf;
5763c7e78d3SWeongyo Jeong 
5773c7e78d3SWeongyo Jeong 	STAILQ_INIT(&txq->free);
5783c7e78d3SWeongyo Jeong 	txq->nfree = 0;
5793c7e78d3SWeongyo Jeong 	ds = txq->dma.dd_desc;
5803c7e78d3SWeongyo Jeong 	for (i = 0; i < malo_txbuf; i++, bf++, ds += MALO_TXDESC) {
5813c7e78d3SWeongyo Jeong 		bf->bf_desc = ds;
5823c7e78d3SWeongyo Jeong 		bf->bf_daddr = DS2PHYS(&txq->dma, ds);
5833c7e78d3SWeongyo Jeong 		error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
5843c7e78d3SWeongyo Jeong 		    &bf->bf_dmamap);
5853c7e78d3SWeongyo Jeong 		if (error != 0) {
5867a79cebfSGleb Smirnoff 			device_printf(sc->malo_dev,
5877a79cebfSGleb Smirnoff 			    "unable to create dmamap for tx "
5883c7e78d3SWeongyo Jeong 			    "buffer %u, error %u\n", i, error);
5893c7e78d3SWeongyo Jeong 			return error;
5903c7e78d3SWeongyo Jeong 		}
5913c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
5923c7e78d3SWeongyo Jeong 		txq->nfree++;
5933c7e78d3SWeongyo Jeong 	}
5943c7e78d3SWeongyo Jeong 
5953c7e78d3SWeongyo Jeong 	return 0;
5963c7e78d3SWeongyo Jeong }
5973c7e78d3SWeongyo Jeong 
5983c7e78d3SWeongyo Jeong static void
5993c7e78d3SWeongyo Jeong malo_desc_cleanup(struct malo_softc *sc, struct malo_descdma *dd)
6003c7e78d3SWeongyo Jeong {
6013c7e78d3SWeongyo Jeong 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
6023c7e78d3SWeongyo Jeong 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
6033c7e78d3SWeongyo Jeong 	bus_dma_tag_destroy(dd->dd_dmat);
6043c7e78d3SWeongyo Jeong 
6053c7e78d3SWeongyo Jeong 	memset(dd, 0, sizeof(*dd));
6063c7e78d3SWeongyo Jeong }
6073c7e78d3SWeongyo Jeong 
6083c7e78d3SWeongyo Jeong static void
6093c7e78d3SWeongyo Jeong malo_rxdma_cleanup(struct malo_softc *sc)
6103c7e78d3SWeongyo Jeong {
6113c7e78d3SWeongyo Jeong 	struct malo_rxbuf *bf;
6123c7e78d3SWeongyo Jeong 
6133c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
6143c7e78d3SWeongyo Jeong 		if (bf->bf_m != NULL) {
6153c7e78d3SWeongyo Jeong 			m_freem(bf->bf_m);
6163c7e78d3SWeongyo Jeong 			bf->bf_m = NULL;
6173c7e78d3SWeongyo Jeong 		}
6183c7e78d3SWeongyo Jeong 		if (bf->bf_dmamap != NULL) {
6193c7e78d3SWeongyo Jeong 			bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
6203c7e78d3SWeongyo Jeong 			bf->bf_dmamap = NULL;
6213c7e78d3SWeongyo Jeong 		}
6223c7e78d3SWeongyo Jeong 	}
6233c7e78d3SWeongyo Jeong 	STAILQ_INIT(&sc->malo_rxbuf);
6243c7e78d3SWeongyo Jeong 	if (sc->malo_rxdma.dd_bufptr != NULL) {
6253c7e78d3SWeongyo Jeong 		free(sc->malo_rxdma.dd_bufptr, M_MALODEV);
6263c7e78d3SWeongyo Jeong 		sc->malo_rxdma.dd_bufptr = NULL;
6273c7e78d3SWeongyo Jeong 	}
6283c7e78d3SWeongyo Jeong 	if (sc->malo_rxdma.dd_desc_len != 0)
6293c7e78d3SWeongyo Jeong 		malo_desc_cleanup(sc, &sc->malo_rxdma);
6303c7e78d3SWeongyo Jeong }
6313c7e78d3SWeongyo Jeong 
6323c7e78d3SWeongyo Jeong static void
6333c7e78d3SWeongyo Jeong malo_txdma_cleanup(struct malo_softc *sc, struct malo_txq *txq)
6343c7e78d3SWeongyo Jeong {
6353c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
6363c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
6373c7e78d3SWeongyo Jeong 
6383c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &txq->free, bf_list) {
6393c7e78d3SWeongyo Jeong 		if (bf->bf_m != NULL) {
6403c7e78d3SWeongyo Jeong 			m_freem(bf->bf_m);
6413c7e78d3SWeongyo Jeong 			bf->bf_m = NULL;
6423c7e78d3SWeongyo Jeong 		}
6433c7e78d3SWeongyo Jeong 		ni = bf->bf_node;
6443c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
6453c7e78d3SWeongyo Jeong 		if (ni != NULL) {
6463c7e78d3SWeongyo Jeong 			/*
6473c7e78d3SWeongyo Jeong 			 * Reclaim node reference.
6483c7e78d3SWeongyo Jeong 			 */
6493c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
6503c7e78d3SWeongyo Jeong 		}
6513c7e78d3SWeongyo Jeong 		if (bf->bf_dmamap != NULL) {
6523c7e78d3SWeongyo Jeong 			bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
6533c7e78d3SWeongyo Jeong 			bf->bf_dmamap = NULL;
6543c7e78d3SWeongyo Jeong 		}
6553c7e78d3SWeongyo Jeong 	}
6563c7e78d3SWeongyo Jeong 	STAILQ_INIT(&txq->free);
6573c7e78d3SWeongyo Jeong 	txq->nfree = 0;
6583c7e78d3SWeongyo Jeong 	if (txq->dma.dd_bufptr != NULL) {
6593c7e78d3SWeongyo Jeong 		free(txq->dma.dd_bufptr, M_MALODEV);
6603c7e78d3SWeongyo Jeong 		txq->dma.dd_bufptr = NULL;
6613c7e78d3SWeongyo Jeong 	}
6623c7e78d3SWeongyo Jeong 	if (txq->dma.dd_desc_len != 0)
6633c7e78d3SWeongyo Jeong 		malo_desc_cleanup(sc, &txq->dma);
6643c7e78d3SWeongyo Jeong }
6653c7e78d3SWeongyo Jeong 
6663c7e78d3SWeongyo Jeong static void
6673c7e78d3SWeongyo Jeong malo_dma_cleanup(struct malo_softc *sc)
6683c7e78d3SWeongyo Jeong {
6693c7e78d3SWeongyo Jeong 	int i;
6703c7e78d3SWeongyo Jeong 
6713c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
6723c7e78d3SWeongyo Jeong 		malo_txdma_cleanup(sc, &sc->malo_txq[i]);
6733c7e78d3SWeongyo Jeong 
6743c7e78d3SWeongyo Jeong 	malo_rxdma_cleanup(sc);
6753c7e78d3SWeongyo Jeong }
6763c7e78d3SWeongyo Jeong 
6773c7e78d3SWeongyo Jeong static int
6783c7e78d3SWeongyo Jeong malo_dma_setup(struct malo_softc *sc)
6793c7e78d3SWeongyo Jeong {
6803c7e78d3SWeongyo Jeong 	int error, i;
6813c7e78d3SWeongyo Jeong 
6823c7e78d3SWeongyo Jeong 	/* rxdma initializing.  */
6833c7e78d3SWeongyo Jeong 	error = malo_rxdma_setup(sc);
6843c7e78d3SWeongyo Jeong 	if (error != 0)
6853c7e78d3SWeongyo Jeong 		return error;
6863c7e78d3SWeongyo Jeong 
6873c7e78d3SWeongyo Jeong 	/* NB: we just have 1 tx queue now.  */
6883c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
6893c7e78d3SWeongyo Jeong 		error = malo_txdma_setup(sc, &sc->malo_txq[i]);
6903c7e78d3SWeongyo Jeong 		if (error != 0) {
6913c7e78d3SWeongyo Jeong 			malo_dma_cleanup(sc);
6923c7e78d3SWeongyo Jeong 
6933c7e78d3SWeongyo Jeong 			return error;
6943c7e78d3SWeongyo Jeong 		}
6953c7e78d3SWeongyo Jeong 
6963c7e78d3SWeongyo Jeong 		malo_txq_init(sc, &sc->malo_txq[i], i);
6973c7e78d3SWeongyo Jeong 	}
6983c7e78d3SWeongyo Jeong 
6993c7e78d3SWeongyo Jeong 	return 0;
7003c7e78d3SWeongyo Jeong }
7013c7e78d3SWeongyo Jeong 
7023c7e78d3SWeongyo Jeong static void
7033c7e78d3SWeongyo Jeong malo_hal_set_rxtxdma(struct malo_softc *sc)
7043c7e78d3SWeongyo Jeong {
7053c7e78d3SWeongyo Jeong 	int i;
7063c7e78d3SWeongyo Jeong 
7073c7e78d3SWeongyo Jeong 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read,
7083c7e78d3SWeongyo Jeong 	    sc->malo_hwdma.rxdesc_read);
7093c7e78d3SWeongyo Jeong 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_write,
7103c7e78d3SWeongyo Jeong 	    sc->malo_hwdma.rxdesc_read);
7113c7e78d3SWeongyo Jeong 
7123c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
7133c7e78d3SWeongyo Jeong 		malo_bar0_write4(sc,
7143c7e78d3SWeongyo Jeong 		    sc->malo_hwspecs.wcbbase[i], sc->malo_hwdma.wcbbase[i]);
7153c7e78d3SWeongyo Jeong 	}
7163c7e78d3SWeongyo Jeong }
7173c7e78d3SWeongyo Jeong 
7183c7e78d3SWeongyo Jeong /*
7193c7e78d3SWeongyo Jeong  * Inform firmware of our tx/rx dma setup.  The BAR 0 writes below are
7203c7e78d3SWeongyo Jeong  * for compatibility with older firmware.  For current firmware we send
7213c7e78d3SWeongyo Jeong  * this information with a cmd block via malo_hal_sethwdma.
7223c7e78d3SWeongyo Jeong  */
7233c7e78d3SWeongyo Jeong static int
7243c7e78d3SWeongyo Jeong malo_setup_hwdma(struct malo_softc *sc)
7253c7e78d3SWeongyo Jeong {
7263c7e78d3SWeongyo Jeong 	int i;
7273c7e78d3SWeongyo Jeong 	struct malo_txq *txq;
7283c7e78d3SWeongyo Jeong 
7293c7e78d3SWeongyo Jeong 	sc->malo_hwdma.rxdesc_read = sc->malo_rxdma.dd_desc_paddr;
7303c7e78d3SWeongyo Jeong 
7313c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
7323c7e78d3SWeongyo Jeong 		txq = &sc->malo_txq[i];
7333c7e78d3SWeongyo Jeong 		sc->malo_hwdma.wcbbase[i] = txq->dma.dd_desc_paddr;
7343c7e78d3SWeongyo Jeong 	}
7353c7e78d3SWeongyo Jeong 	sc->malo_hwdma.maxnum_txwcb = malo_txbuf;
7363c7e78d3SWeongyo Jeong 	sc->malo_hwdma.maxnum_wcb = MALO_NUM_TX_QUEUES;
7373c7e78d3SWeongyo Jeong 
7383c7e78d3SWeongyo Jeong 	malo_hal_set_rxtxdma(sc);
7393c7e78d3SWeongyo Jeong 
7403c7e78d3SWeongyo Jeong 	return 0;
7413c7e78d3SWeongyo Jeong }
7423c7e78d3SWeongyo Jeong 
7433c7e78d3SWeongyo Jeong static void
7443c7e78d3SWeongyo Jeong malo_txq_init(struct malo_softc *sc, struct malo_txq *txq, int qnum)
7453c7e78d3SWeongyo Jeong {
7463c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf, *bn;
7473c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
7483c7e78d3SWeongyo Jeong 
7493c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK_INIT(sc, txq);
7503c7e78d3SWeongyo Jeong 	txq->qnum = qnum;
7513c7e78d3SWeongyo Jeong 	txq->txpri = 0;	/* XXX */
7523c7e78d3SWeongyo Jeong 
7533c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &txq->free, bf_list) {
7543c7e78d3SWeongyo Jeong 		bf->bf_txq = txq;
7553c7e78d3SWeongyo Jeong 
7563c7e78d3SWeongyo Jeong 		ds = bf->bf_desc;
7573c7e78d3SWeongyo Jeong 		bn = STAILQ_NEXT(bf, bf_list);
7583c7e78d3SWeongyo Jeong 		if (bn == NULL)
7593c7e78d3SWeongyo Jeong 			bn = STAILQ_FIRST(&txq->free);
7603c7e78d3SWeongyo Jeong 		ds->physnext = htole32(bn->bf_daddr);
7613c7e78d3SWeongyo Jeong 	}
7623c7e78d3SWeongyo Jeong 	STAILQ_INIT(&txq->active);
7633c7e78d3SWeongyo Jeong }
7643c7e78d3SWeongyo Jeong 
7653c7e78d3SWeongyo Jeong /*
7663c7e78d3SWeongyo Jeong  * Reclaim resources for a setup queue.
7673c7e78d3SWeongyo Jeong  */
7683c7e78d3SWeongyo Jeong static void
7693c7e78d3SWeongyo Jeong malo_tx_cleanupq(struct malo_softc *sc, struct malo_txq *txq)
7703c7e78d3SWeongyo Jeong {
7713c7e78d3SWeongyo Jeong 	/* XXX hal work? */
7723c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK_DESTROY(txq);
7733c7e78d3SWeongyo Jeong }
7743c7e78d3SWeongyo Jeong 
7753c7e78d3SWeongyo Jeong /*
7763c7e78d3SWeongyo Jeong  * Allocate a tx buffer for sending a frame.
7773c7e78d3SWeongyo Jeong  */
7783c7e78d3SWeongyo Jeong static struct malo_txbuf *
7793c7e78d3SWeongyo Jeong malo_getbuf(struct malo_softc *sc, struct malo_txq *txq)
7803c7e78d3SWeongyo Jeong {
7813c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
7823c7e78d3SWeongyo Jeong 
7833c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK(txq);
7843c7e78d3SWeongyo Jeong 	bf = STAILQ_FIRST(&txq->free);
7853c7e78d3SWeongyo Jeong 	if (bf != NULL) {
7863c7e78d3SWeongyo Jeong 		STAILQ_REMOVE_HEAD(&txq->free, bf_list);
7873c7e78d3SWeongyo Jeong 		txq->nfree--;
7883c7e78d3SWeongyo Jeong 	}
7893c7e78d3SWeongyo Jeong 	MALO_TXQ_UNLOCK(txq);
7903c7e78d3SWeongyo Jeong 	if (bf == NULL) {
7913c7e78d3SWeongyo Jeong 		DPRINTF(sc, MALO_DEBUG_XMIT,
7923c7e78d3SWeongyo Jeong 		    "%s: out of xmit buffers on q %d\n", __func__, txq->qnum);
7933c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_qstop++;
7943c7e78d3SWeongyo Jeong 	}
7953c7e78d3SWeongyo Jeong 	return bf;
7963c7e78d3SWeongyo Jeong }
7973c7e78d3SWeongyo Jeong 
7983c7e78d3SWeongyo Jeong static int
7993c7e78d3SWeongyo Jeong malo_tx_dmasetup(struct malo_softc *sc, struct malo_txbuf *bf, struct mbuf *m0)
8003c7e78d3SWeongyo Jeong {
8013c7e78d3SWeongyo Jeong 	struct mbuf *m;
8023c7e78d3SWeongyo Jeong 	int error;
8033c7e78d3SWeongyo Jeong 
8043c7e78d3SWeongyo Jeong 	/*
8053c7e78d3SWeongyo Jeong 	 * Load the DMA map so any coalescing is done.  This also calculates
8063c7e78d3SWeongyo Jeong 	 * the number of descriptors we need.
8073c7e78d3SWeongyo Jeong 	 */
8083c7e78d3SWeongyo Jeong 	error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
8093c7e78d3SWeongyo Jeong 				     bf->bf_segs, &bf->bf_nseg,
8103c7e78d3SWeongyo Jeong 				     BUS_DMA_NOWAIT);
8113c7e78d3SWeongyo Jeong 	if (error == EFBIG) {
8123c7e78d3SWeongyo Jeong 		/* XXX packet requires too many descriptors */
8133c7e78d3SWeongyo Jeong 		bf->bf_nseg = MALO_TXDESC + 1;
8143c7e78d3SWeongyo Jeong 	} else if (error != 0) {
8153c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_busdma++;
8163c7e78d3SWeongyo Jeong 		m_freem(m0);
8173c7e78d3SWeongyo Jeong 		return error;
8183c7e78d3SWeongyo Jeong 	}
8193c7e78d3SWeongyo Jeong 	/*
8203c7e78d3SWeongyo Jeong 	 * Discard null packets and check for packets that require too many
8213c7e78d3SWeongyo Jeong 	 * TX descriptors.  We try to convert the latter to a cluster.
8223c7e78d3SWeongyo Jeong 	 */
8233c7e78d3SWeongyo Jeong 	if (error == EFBIG) {		/* too many desc's, linearize */
8243c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_linear++;
825c6499eccSGleb Smirnoff 		m = m_defrag(m0, M_NOWAIT);
8263c7e78d3SWeongyo Jeong 		if (m == NULL) {
8273c7e78d3SWeongyo Jeong 			m_freem(m0);
8283c7e78d3SWeongyo Jeong 			sc->malo_stats.mst_tx_nombuf++;
8293c7e78d3SWeongyo Jeong 			return ENOMEM;
8303c7e78d3SWeongyo Jeong 		}
8313c7e78d3SWeongyo Jeong 		m0 = m;
8323c7e78d3SWeongyo Jeong 		error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
8333c7e78d3SWeongyo Jeong 					     bf->bf_segs, &bf->bf_nseg,
8343c7e78d3SWeongyo Jeong 					     BUS_DMA_NOWAIT);
8353c7e78d3SWeongyo Jeong 		if (error != 0) {
8363c7e78d3SWeongyo Jeong 			sc->malo_stats.mst_tx_busdma++;
8373c7e78d3SWeongyo Jeong 			m_freem(m0);
8383c7e78d3SWeongyo Jeong 			return error;
8393c7e78d3SWeongyo Jeong 		}
8403c7e78d3SWeongyo Jeong 		KASSERT(bf->bf_nseg <= MALO_TXDESC,
8413c7e78d3SWeongyo Jeong 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
8423c7e78d3SWeongyo Jeong 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
8433c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_nodata++;
8443c7e78d3SWeongyo Jeong 		m_freem(m0);
8453c7e78d3SWeongyo Jeong 		return EIO;
8463c7e78d3SWeongyo Jeong 	}
8473c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_XMIT, "%s: m %p len %u\n",
8483c7e78d3SWeongyo Jeong 		__func__, m0, m0->m_pkthdr.len);
8493c7e78d3SWeongyo Jeong 	bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
8503c7e78d3SWeongyo Jeong 	bf->bf_m = m0;
8513c7e78d3SWeongyo Jeong 
8523c7e78d3SWeongyo Jeong 	return 0;
8533c7e78d3SWeongyo Jeong }
8543c7e78d3SWeongyo Jeong 
8553c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
8563c7e78d3SWeongyo Jeong static void
8573c7e78d3SWeongyo Jeong malo_printrxbuf(const struct malo_rxbuf *bf, u_int ix)
8583c7e78d3SWeongyo Jeong {
8593c7e78d3SWeongyo Jeong 	const struct malo_rxdesc *ds = bf->bf_desc;
8603c7e78d3SWeongyo Jeong 	uint32_t status = le32toh(ds->status);
8613c7e78d3SWeongyo Jeong 
8622706e872SMarius Strobl 	printf("R[%2u] (DS.V:%p DS.P:0x%jx) NEXT:%08x DATA:%08x RC:%02x%s\n"
8633c7e78d3SWeongyo Jeong 	    "      STAT:%02x LEN:%04x SNR:%02x NF:%02x CHAN:%02x"
8642706e872SMarius Strobl 	    " RATE:%02x QOS:%04x\n", ix, ds, (uintmax_t)bf->bf_daddr,
8653c7e78d3SWeongyo Jeong 	    le32toh(ds->physnext), le32toh(ds->physbuffdata),
8663c7e78d3SWeongyo Jeong 	    ds->rxcontrol,
8673c7e78d3SWeongyo Jeong 	    ds->rxcontrol != MALO_RXD_CTRL_DRIVER_OWN ?
8683c7e78d3SWeongyo Jeong 	        "" : (status & MALO_RXD_STATUS_OK) ? " *" : " !",
8693c7e78d3SWeongyo Jeong 	    ds->status, le16toh(ds->pktlen), ds->snr, ds->nf, ds->channel,
8703c7e78d3SWeongyo Jeong 	    ds->rate, le16toh(ds->qosctrl));
8713c7e78d3SWeongyo Jeong }
8723c7e78d3SWeongyo Jeong 
8733c7e78d3SWeongyo Jeong static void
8743c7e78d3SWeongyo Jeong malo_printtxbuf(const struct malo_txbuf *bf, u_int qnum, u_int ix)
8753c7e78d3SWeongyo Jeong {
8763c7e78d3SWeongyo Jeong 	const struct malo_txdesc *ds = bf->bf_desc;
8773c7e78d3SWeongyo Jeong 	uint32_t status = le32toh(ds->status);
8783c7e78d3SWeongyo Jeong 
8793c7e78d3SWeongyo Jeong 	printf("Q%u[%3u]", qnum, ix);
8802706e872SMarius Strobl 	printf(" (DS.V:%p DS.P:0x%jx)\n", ds, (uintmax_t)bf->bf_daddr);
8813c7e78d3SWeongyo Jeong 	printf("    NEXT:%08x DATA:%08x LEN:%04x STAT:%08x%s\n",
8823c7e78d3SWeongyo Jeong 	    le32toh(ds->physnext),
8833c7e78d3SWeongyo Jeong 	    le32toh(ds->pktptr), le16toh(ds->pktlen), status,
8843c7e78d3SWeongyo Jeong 	    status & MALO_TXD_STATUS_USED ?
8853c7e78d3SWeongyo Jeong 	    "" : (status & 3) != 0 ? " *" : " !");
8863c7e78d3SWeongyo Jeong 	printf("    RATE:%02x PRI:%x QOS:%04x SAP:%08x FORMAT:%04x\n",
8873c7e78d3SWeongyo Jeong 	    ds->datarate, ds->txpriority, le16toh(ds->qosctrl),
8883c7e78d3SWeongyo Jeong 	    le32toh(ds->sap_pktinfo), le16toh(ds->format));
8893c7e78d3SWeongyo Jeong #if 0
8903c7e78d3SWeongyo Jeong 	{
8913c7e78d3SWeongyo Jeong 		const uint8_t *cp = (const uint8_t *) ds;
8923c7e78d3SWeongyo Jeong 		int i;
8933c7e78d3SWeongyo Jeong 		for (i = 0; i < sizeof(struct malo_txdesc); i++) {
8943c7e78d3SWeongyo Jeong 			printf("%02x ", cp[i]);
8953c7e78d3SWeongyo Jeong 			if (((i+1) % 16) == 0)
8963c7e78d3SWeongyo Jeong 				printf("\n");
8973c7e78d3SWeongyo Jeong 		}
8983c7e78d3SWeongyo Jeong 		printf("\n");
8993c7e78d3SWeongyo Jeong 	}
9003c7e78d3SWeongyo Jeong #endif
9013c7e78d3SWeongyo Jeong }
9023c7e78d3SWeongyo Jeong #endif /* MALO_DEBUG */
9033c7e78d3SWeongyo Jeong 
9043c7e78d3SWeongyo Jeong static __inline void
9053c7e78d3SWeongyo Jeong malo_updatetxrate(struct ieee80211_node *ni, int rix)
9063c7e78d3SWeongyo Jeong {
9073c7e78d3SWeongyo Jeong 	static const int ieeerates[] =
9083c7e78d3SWeongyo Jeong 	    { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 96, 108 };
909d6166defSAdrian Chadd 	if (rix < nitems(ieeerates))
9103c7e78d3SWeongyo Jeong 		ni->ni_txrate = ieeerates[rix];
9113c7e78d3SWeongyo Jeong }
9123c7e78d3SWeongyo Jeong 
9133c7e78d3SWeongyo Jeong static int
9143c7e78d3SWeongyo Jeong malo_fix2rate(int fix_rate)
9153c7e78d3SWeongyo Jeong {
9163c7e78d3SWeongyo Jeong 	static const int rates[] =
9173c7e78d3SWeongyo Jeong 	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 96, 108 };
918d6166defSAdrian Chadd 	return (fix_rate < nitems(rates) ? rates[fix_rate] : 0);
9193c7e78d3SWeongyo Jeong }
9203c7e78d3SWeongyo Jeong 
9213c7e78d3SWeongyo Jeong /* idiomatic shorthands: MS = mask+shift, SM = shift+mask */
9223c7e78d3SWeongyo Jeong #define	MS(v,x)			(((v) & x) >> x##_S)
9233c7e78d3SWeongyo Jeong #define	SM(v,x)			(((v) << x##_S) & x)
9243c7e78d3SWeongyo Jeong 
9253c7e78d3SWeongyo Jeong /*
9263c7e78d3SWeongyo Jeong  * Process completed xmit descriptors from the specified queue.
9273c7e78d3SWeongyo Jeong  */
9283c7e78d3SWeongyo Jeong static int
9293c7e78d3SWeongyo Jeong malo_tx_processq(struct malo_softc *sc, struct malo_txq *txq)
9303c7e78d3SWeongyo Jeong {
9313c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
9323c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
9333c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
9343c7e78d3SWeongyo Jeong 	int nreaped;
9353c7e78d3SWeongyo Jeong 	uint32_t status;
9363c7e78d3SWeongyo Jeong 
9373c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_TX_PROC, "%s: tx queue %u\n",
9383c7e78d3SWeongyo Jeong 	    __func__, txq->qnum);
9393c7e78d3SWeongyo Jeong 	for (nreaped = 0;; nreaped++) {
9403c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
9413c7e78d3SWeongyo Jeong 		bf = STAILQ_FIRST(&txq->active);
9423c7e78d3SWeongyo Jeong 		if (bf == NULL) {
9433c7e78d3SWeongyo Jeong 			MALO_TXQ_UNLOCK(txq);
9443c7e78d3SWeongyo Jeong 			break;
9453c7e78d3SWeongyo Jeong 		}
9463c7e78d3SWeongyo Jeong 		ds = bf->bf_desc;
9473c7e78d3SWeongyo Jeong 		MALO_TXDESC_SYNC(txq, ds,
9483c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9493c7e78d3SWeongyo Jeong 		if (ds->status & htole32(MALO_TXD_STATUS_FW_OWNED)) {
9503c7e78d3SWeongyo Jeong 			MALO_TXQ_UNLOCK(txq);
9513c7e78d3SWeongyo Jeong 			break;
9523c7e78d3SWeongyo Jeong 		}
9533c7e78d3SWeongyo Jeong 		STAILQ_REMOVE_HEAD(&txq->active, bf_list);
9543c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
9553c7e78d3SWeongyo Jeong 
9563c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
9573c7e78d3SWeongyo Jeong 		if (sc->malo_debug & MALO_DEBUG_XMIT_DESC)
9583c7e78d3SWeongyo Jeong 			malo_printtxbuf(bf, txq->qnum, nreaped);
9593c7e78d3SWeongyo Jeong #endif
9603c7e78d3SWeongyo Jeong 		ni = bf->bf_node;
9613c7e78d3SWeongyo Jeong 		if (ni != NULL) {
9623c7e78d3SWeongyo Jeong 			status = le32toh(ds->status);
9633c7e78d3SWeongyo Jeong 			if (status & MALO_TXD_STATUS_OK) {
9643c7e78d3SWeongyo Jeong 				uint16_t format = le16toh(ds->format);
9653c7e78d3SWeongyo Jeong 				uint8_t txant = MS(format, MALO_TXD_ANTENNA);
9663c7e78d3SWeongyo Jeong 
9673c7e78d3SWeongyo Jeong 				sc->malo_stats.mst_ant_tx[txant]++;
9683c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_OK_RETRY)
9693c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_retries++;
9703c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_OK_MORE_RETRY)
9713c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_mretries++;
9723c7e78d3SWeongyo Jeong 				malo_updatetxrate(ni, ds->datarate);
9733c7e78d3SWeongyo Jeong 				sc->malo_stats.mst_tx_rate = ds->datarate;
9743c7e78d3SWeongyo Jeong 			} else {
9753c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_FAILED_LINK_ERROR)
9763c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_linkerror++;
9773c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_FAILED_XRETRY)
9783c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_xretries++;
9793c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_FAILED_AGING)
9803c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_aging++;
9813c7e78d3SWeongyo Jeong 			}
9823c7e78d3SWeongyo Jeong 			/* XXX strip fw len in case header inspected */
9833c7e78d3SWeongyo Jeong 			m_adj(bf->bf_m, sizeof(uint16_t));
9847a79cebfSGleb Smirnoff 			ieee80211_tx_complete(ni, bf->bf_m,
9853c7e78d3SWeongyo Jeong 			    (status & MALO_TXD_STATUS_OK) == 0);
9867a79cebfSGleb Smirnoff 		} else
9877a79cebfSGleb Smirnoff 			m_freem(bf->bf_m);
9887a79cebfSGleb Smirnoff 
9893c7e78d3SWeongyo Jeong 		ds->status = htole32(MALO_TXD_STATUS_IDLE);
9903c7e78d3SWeongyo Jeong 		ds->pktlen = htole32(0);
9913c7e78d3SWeongyo Jeong 
9923c7e78d3SWeongyo Jeong 		bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
9933c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTWRITE);
9943c7e78d3SWeongyo Jeong 		bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
9953c7e78d3SWeongyo Jeong 		bf->bf_m = NULL;
9963c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
9973c7e78d3SWeongyo Jeong 
9983c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
9993c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
10003c7e78d3SWeongyo Jeong 		txq->nfree++;
10013c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
10023c7e78d3SWeongyo Jeong 	}
10033c7e78d3SWeongyo Jeong 	return nreaped;
10043c7e78d3SWeongyo Jeong }
10053c7e78d3SWeongyo Jeong 
10063c7e78d3SWeongyo Jeong /*
10073c7e78d3SWeongyo Jeong  * Deferred processing of transmit interrupt.
10083c7e78d3SWeongyo Jeong  */
10093c7e78d3SWeongyo Jeong static void
10103c7e78d3SWeongyo Jeong malo_tx_proc(void *arg, int npending)
10113c7e78d3SWeongyo Jeong {
10123c7e78d3SWeongyo Jeong 	struct malo_softc *sc = arg;
10133c7e78d3SWeongyo Jeong 	int i, nreaped;
10143c7e78d3SWeongyo Jeong 
10153c7e78d3SWeongyo Jeong 	/*
10163c7e78d3SWeongyo Jeong 	 * Process each active queue.
10173c7e78d3SWeongyo Jeong 	 */
10183c7e78d3SWeongyo Jeong 	nreaped = 0;
10197a79cebfSGleb Smirnoff 	MALO_LOCK(sc);
10203c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
10213c7e78d3SWeongyo Jeong 		if (!STAILQ_EMPTY(&sc->malo_txq[i].active))
10223c7e78d3SWeongyo Jeong 			nreaped += malo_tx_processq(sc, &sc->malo_txq[i]);
10233c7e78d3SWeongyo Jeong 	}
10243c7e78d3SWeongyo Jeong 
10253c7e78d3SWeongyo Jeong 	if (nreaped != 0) {
10267cf545d0SJohn Baldwin 		sc->malo_timer = 0;
10277a79cebfSGleb Smirnoff 		malo_start(sc);
10283c7e78d3SWeongyo Jeong 	}
10297a79cebfSGleb Smirnoff 	MALO_UNLOCK(sc);
10303c7e78d3SWeongyo Jeong }
10313c7e78d3SWeongyo Jeong 
10323c7e78d3SWeongyo Jeong static int
10333c7e78d3SWeongyo Jeong malo_tx_start(struct malo_softc *sc, struct ieee80211_node *ni,
10343c7e78d3SWeongyo Jeong     struct malo_txbuf *bf, struct mbuf *m0)
10353c7e78d3SWeongyo Jeong {
10363c7e78d3SWeongyo Jeong #define	IS_DATA_FRAME(wh)						\
10373c7e78d3SWeongyo Jeong 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK)) == IEEE80211_FC0_TYPE_DATA)
10383c7e78d3SWeongyo Jeong 	int error, ismcast, iswep;
10393c7e78d3SWeongyo Jeong 	int copyhdrlen, hdrlen, pktlen;
10403c7e78d3SWeongyo Jeong 	struct ieee80211_frame *wh;
10417a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
10425463c4a4SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
10433c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
10443c7e78d3SWeongyo Jeong 	struct malo_txrec *tr;
10453c7e78d3SWeongyo Jeong 	struct malo_txq *txq;
10463c7e78d3SWeongyo Jeong 	uint16_t qos;
10473c7e78d3SWeongyo Jeong 
10483c7e78d3SWeongyo Jeong 	wh = mtod(m0, struct ieee80211_frame *);
10495945b5f5SKevin Lo 	iswep = wh->i_fc[1] & IEEE80211_FC1_PROTECTED;
10503c7e78d3SWeongyo Jeong 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
10513c7e78d3SWeongyo Jeong 	copyhdrlen = hdrlen = ieee80211_anyhdrsize(wh);
10523c7e78d3SWeongyo Jeong 	pktlen = m0->m_pkthdr.len;
10533c7e78d3SWeongyo Jeong 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
1054d6166defSAdrian Chadd 		if (IEEE80211_IS_DSTODS(wh)) {
10553c7e78d3SWeongyo Jeong 			qos = *(uint16_t *)
10563c7e78d3SWeongyo Jeong 			    (((struct ieee80211_qosframe_addr4 *) wh)->i_qos);
10573c7e78d3SWeongyo Jeong 			copyhdrlen -= sizeof(qos);
10583c7e78d3SWeongyo Jeong 		} else
10593c7e78d3SWeongyo Jeong 			qos = *(uint16_t *)
10603c7e78d3SWeongyo Jeong 			    (((struct ieee80211_qosframe *) wh)->i_qos);
10613c7e78d3SWeongyo Jeong 	} else
10623c7e78d3SWeongyo Jeong 		qos = 0;
10633c7e78d3SWeongyo Jeong 
10643c7e78d3SWeongyo Jeong 	if (iswep) {
10653c7e78d3SWeongyo Jeong 		struct ieee80211_key *k;
10663c7e78d3SWeongyo Jeong 
10673c7e78d3SWeongyo Jeong 		/*
10683c7e78d3SWeongyo Jeong 		 * Construct the 802.11 header+trailer for an encrypted
10693c7e78d3SWeongyo Jeong 		 * frame. The only reason this can fail is because of an
10703c7e78d3SWeongyo Jeong 		 * unknown or unsupported cipher/key type.
10713c7e78d3SWeongyo Jeong 		 *
10723c7e78d3SWeongyo Jeong 		 * NB: we do this even though the firmware will ignore
10733c7e78d3SWeongyo Jeong 		 *     what we've done for WEP and TKIP as we need the
10743c7e78d3SWeongyo Jeong 		 *     ExtIV filled in for CCMP and this also adjusts
10753c7e78d3SWeongyo Jeong 		 *     the headers which simplifies our work below.
10763c7e78d3SWeongyo Jeong 		 */
1077b032f27cSSam Leffler 		k = ieee80211_crypto_encap(ni, m0);
10783c7e78d3SWeongyo Jeong 		if (k == NULL) {
10793c7e78d3SWeongyo Jeong 			/*
10803c7e78d3SWeongyo Jeong 			 * This can happen when the key is yanked after the
10813c7e78d3SWeongyo Jeong 			 * frame was queued.  Just discard the frame; the
10823c7e78d3SWeongyo Jeong 			 * 802.11 layer counts failures and provides
10833c7e78d3SWeongyo Jeong 			 * debugging/diagnostics.
10843c7e78d3SWeongyo Jeong 			 */
10853c7e78d3SWeongyo Jeong 			m_freem(m0);
10863c7e78d3SWeongyo Jeong 			return EIO;
10873c7e78d3SWeongyo Jeong 		}
10883c7e78d3SWeongyo Jeong 
10893c7e78d3SWeongyo Jeong 		/*
10903c7e78d3SWeongyo Jeong 		 * Adjust the packet length for the crypto additions
10913c7e78d3SWeongyo Jeong 		 * done during encap and any other bits that the f/w
10923c7e78d3SWeongyo Jeong 		 * will add later on.
10933c7e78d3SWeongyo Jeong 		 */
10943c7e78d3SWeongyo Jeong 		pktlen = m0->m_pkthdr.len;
10953c7e78d3SWeongyo Jeong 
10963c7e78d3SWeongyo Jeong 		/* packet header may have moved, reset our local pointer */
10973c7e78d3SWeongyo Jeong 		wh = mtod(m0, struct ieee80211_frame *);
10983c7e78d3SWeongyo Jeong 	}
10993c7e78d3SWeongyo Jeong 
11005463c4a4SSam Leffler 	if (ieee80211_radiotap_active_vap(vap)) {
11013c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_flags = 0;	/* XXX */
11023c7e78d3SWeongyo Jeong 		if (iswep)
11033c7e78d3SWeongyo Jeong 			sc->malo_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
11043c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_txpower = ni->ni_txpower;
11053c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_antenna = sc->malo_txantenna;
11063c7e78d3SWeongyo Jeong 
11075463c4a4SSam Leffler 		ieee80211_radiotap_tx(vap, m0);
11083c7e78d3SWeongyo Jeong 	}
11093c7e78d3SWeongyo Jeong 
11103c7e78d3SWeongyo Jeong 	/*
11113c7e78d3SWeongyo Jeong 	 * Copy up/down the 802.11 header; the firmware requires
11123c7e78d3SWeongyo Jeong 	 * we present a 2-byte payload length followed by a
11133c7e78d3SWeongyo Jeong 	 * 4-address header (w/o QoS), followed (optionally) by
11143c7e78d3SWeongyo Jeong 	 * any WEP/ExtIV header (but only filled in for CCMP).
11153c7e78d3SWeongyo Jeong 	 * We are assured the mbuf has sufficient headroom to
11163c7e78d3SWeongyo Jeong 	 * prepend in-place by the setup of ic_headroom in
11173c7e78d3SWeongyo Jeong 	 * malo_attach.
11183c7e78d3SWeongyo Jeong 	 */
11193c7e78d3SWeongyo Jeong 	if (hdrlen < sizeof(struct malo_txrec)) {
11203c7e78d3SWeongyo Jeong 		const int space = sizeof(struct malo_txrec) - hdrlen;
11213c7e78d3SWeongyo Jeong 		if (M_LEADINGSPACE(m0) < space) {
11223c7e78d3SWeongyo Jeong 			/* NB: should never happen */
11233c7e78d3SWeongyo Jeong 			device_printf(sc->malo_dev,
11243c7e78d3SWeongyo Jeong 			    "not enough headroom, need %d found %zd, "
11253c7e78d3SWeongyo Jeong 			    "m_flags 0x%x m_len %d\n",
11263c7e78d3SWeongyo Jeong 			    space, M_LEADINGSPACE(m0), m0->m_flags, m0->m_len);
11273c7e78d3SWeongyo Jeong 			ieee80211_dump_pkt(ic,
11283c7e78d3SWeongyo Jeong 			    mtod(m0, const uint8_t *), m0->m_len, 0, -1);
11293c7e78d3SWeongyo Jeong 			m_freem(m0);
11303c7e78d3SWeongyo Jeong 			/* XXX stat */
11313c7e78d3SWeongyo Jeong 			return EIO;
11323c7e78d3SWeongyo Jeong 		}
11333c7e78d3SWeongyo Jeong 		M_PREPEND(m0, space, M_NOWAIT);
11343c7e78d3SWeongyo Jeong 	}
11353c7e78d3SWeongyo Jeong 	tr = mtod(m0, struct malo_txrec *);
11363c7e78d3SWeongyo Jeong 	if (wh != (struct ieee80211_frame *) &tr->wh)
11373c7e78d3SWeongyo Jeong 		ovbcopy(wh, &tr->wh, hdrlen);
11383c7e78d3SWeongyo Jeong 	/*
11393c7e78d3SWeongyo Jeong 	 * Note: the "firmware length" is actually the length of the fully
11403c7e78d3SWeongyo Jeong 	 * formed "802.11 payload".  That is, it's everything except for
11413c7e78d3SWeongyo Jeong 	 * the 802.11 header.  In particular this includes all crypto
11423c7e78d3SWeongyo Jeong 	 * material including the MIC!
11433c7e78d3SWeongyo Jeong 	 */
11443c7e78d3SWeongyo Jeong 	tr->fwlen = htole16(pktlen - hdrlen);
11453c7e78d3SWeongyo Jeong 
11463c7e78d3SWeongyo Jeong 	/*
11473c7e78d3SWeongyo Jeong 	 * Load the DMA map so any coalescing is done.  This
11483c7e78d3SWeongyo Jeong 	 * also calculates the number of descriptors we need.
11493c7e78d3SWeongyo Jeong 	 */
11503c7e78d3SWeongyo Jeong 	error = malo_tx_dmasetup(sc, bf, m0);
11513c7e78d3SWeongyo Jeong 	if (error != 0)
11523c7e78d3SWeongyo Jeong 		return error;
11533c7e78d3SWeongyo Jeong 	bf->bf_node = ni;			/* NB: held reference */
11543c7e78d3SWeongyo Jeong 	m0 = bf->bf_m;				/* NB: may have changed */
11553c7e78d3SWeongyo Jeong 	tr = mtod(m0, struct malo_txrec *);
11563c7e78d3SWeongyo Jeong 	wh = (struct ieee80211_frame *)&tr->wh;
11573c7e78d3SWeongyo Jeong 
11583c7e78d3SWeongyo Jeong 	/*
11593c7e78d3SWeongyo Jeong 	 * Formulate tx descriptor.
11603c7e78d3SWeongyo Jeong 	 */
11613c7e78d3SWeongyo Jeong 	ds = bf->bf_desc;
11623c7e78d3SWeongyo Jeong 	txq = bf->bf_txq;
11633c7e78d3SWeongyo Jeong 
11643c7e78d3SWeongyo Jeong 	ds->qosctrl = qos;			/* NB: already little-endian */
11653c7e78d3SWeongyo Jeong 	ds->pktptr = htole32(bf->bf_segs[0].ds_addr);
11663c7e78d3SWeongyo Jeong 	ds->pktlen = htole16(bf->bf_segs[0].ds_len);
11673c7e78d3SWeongyo Jeong 	/* NB: pPhysNext setup once, don't touch */
11683c7e78d3SWeongyo Jeong 	ds->datarate = IS_DATA_FRAME(wh) ? 1 : 0;
11693c7e78d3SWeongyo Jeong 	ds->sap_pktinfo = 0;
11703c7e78d3SWeongyo Jeong 	ds->format = 0;
11713c7e78d3SWeongyo Jeong 
11723c7e78d3SWeongyo Jeong 	/*
11733c7e78d3SWeongyo Jeong 	 * Select transmit rate.
11743c7e78d3SWeongyo Jeong 	 */
11753c7e78d3SWeongyo Jeong 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
11763c7e78d3SWeongyo Jeong 	case IEEE80211_FC0_TYPE_MGT:
11773c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_mgmt++;
11783c7e78d3SWeongyo Jeong 		/* fall thru... */
11793c7e78d3SWeongyo Jeong 	case IEEE80211_FC0_TYPE_CTL:
11803c7e78d3SWeongyo Jeong 		ds->txpriority = 1;
11813c7e78d3SWeongyo Jeong 		break;
11823c7e78d3SWeongyo Jeong 	case IEEE80211_FC0_TYPE_DATA:
11833c7e78d3SWeongyo Jeong 		ds->txpriority = txq->qnum;
11843c7e78d3SWeongyo Jeong 		break;
11853c7e78d3SWeongyo Jeong 	default:
11867a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "bogus frame type 0x%x (%s)\n",
11873c7e78d3SWeongyo Jeong 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
11883c7e78d3SWeongyo Jeong 		/* XXX statistic */
11893c7e78d3SWeongyo Jeong 		m_freem(m0);
11903c7e78d3SWeongyo Jeong 		return EIO;
11913c7e78d3SWeongyo Jeong 	}
11923c7e78d3SWeongyo Jeong 
11933c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
11943c7e78d3SWeongyo Jeong 	if (IFF_DUMPPKTS_XMIT(sc))
11953c7e78d3SWeongyo Jeong 		ieee80211_dump_pkt(ic,
11963c7e78d3SWeongyo Jeong 		    mtod(m0, const uint8_t *)+sizeof(uint16_t),
11973c7e78d3SWeongyo Jeong 		    m0->m_len - sizeof(uint16_t), ds->datarate, -1);
11983c7e78d3SWeongyo Jeong #endif
11993c7e78d3SWeongyo Jeong 
12003c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK(txq);
12013c7e78d3SWeongyo Jeong 	if (!IS_DATA_FRAME(wh))
12023c7e78d3SWeongyo Jeong 		ds->status |= htole32(1);
12033c7e78d3SWeongyo Jeong 	ds->status |= htole32(MALO_TXD_STATUS_FW_OWNED);
12043c7e78d3SWeongyo Jeong 	STAILQ_INSERT_TAIL(&txq->active, bf, bf_list);
12053c7e78d3SWeongyo Jeong 	MALO_TXDESC_SYNC(txq, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
12063c7e78d3SWeongyo Jeong 
12077cf545d0SJohn Baldwin 	sc->malo_timer = 5;
12083c7e78d3SWeongyo Jeong 	MALO_TXQ_UNLOCK(txq);
12093c7e78d3SWeongyo Jeong 	return 0;
12103c7e78d3SWeongyo Jeong }
12113c7e78d3SWeongyo Jeong 
12127a79cebfSGleb Smirnoff static int
12137a79cebfSGleb Smirnoff malo_transmit(struct ieee80211com *ic, struct mbuf *m)
121479d2c5e8SGleb Smirnoff {
12157a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
12167a79cebfSGleb Smirnoff 	int error;
12177a79cebfSGleb Smirnoff 
12187a79cebfSGleb Smirnoff 	MALO_LOCK(sc);
12197a79cebfSGleb Smirnoff 	if (!sc->malo_running) {
12207a79cebfSGleb Smirnoff 		MALO_UNLOCK(sc);
12217a79cebfSGleb Smirnoff 		return (ENXIO);
12227a79cebfSGleb Smirnoff 	}
12237a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->malo_snd, m);
12247a79cebfSGleb Smirnoff 	if (error) {
12257a79cebfSGleb Smirnoff 		MALO_UNLOCK(sc);
12267a79cebfSGleb Smirnoff 		return (error);
12277a79cebfSGleb Smirnoff 	}
12287a79cebfSGleb Smirnoff 	malo_start(sc);
12297a79cebfSGleb Smirnoff 	MALO_UNLOCK(sc);
12307a79cebfSGleb Smirnoff 	return (0);
12317a79cebfSGleb Smirnoff }
12327a79cebfSGleb Smirnoff 
12337a79cebfSGleb Smirnoff static void
12347a79cebfSGleb Smirnoff malo_start(struct malo_softc *sc)
12357a79cebfSGleb Smirnoff {
12363c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
1237b032f27cSSam Leffler 	struct malo_txq *txq = &sc->malo_txq[0];
12383c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf = NULL;
12393c7e78d3SWeongyo Jeong 	struct mbuf *m;
1240b032f27cSSam Leffler 	int nqueued = 0;
12413c7e78d3SWeongyo Jeong 
12427a79cebfSGleb Smirnoff 	MALO_LOCK_ASSERT(sc);
12437a79cebfSGleb Smirnoff 
12447a79cebfSGleb Smirnoff 	if (!sc->malo_running || sc->malo_invalid)
12453c7e78d3SWeongyo Jeong 		return;
12463c7e78d3SWeongyo Jeong 
12477a79cebfSGleb Smirnoff 	while ((m = mbufq_dequeue(&sc->malo_snd)) != NULL) {
1248b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
12493c7e78d3SWeongyo Jeong 		bf = malo_getbuf(sc, txq);
12503c7e78d3SWeongyo Jeong 		if (bf == NULL) {
12517a79cebfSGleb Smirnoff 			mbufq_prepend(&sc->malo_snd, m);
12523c7e78d3SWeongyo Jeong 			sc->malo_stats.mst_tx_qstop++;
12533c7e78d3SWeongyo Jeong 			break;
12543c7e78d3SWeongyo Jeong 		}
12553c7e78d3SWeongyo Jeong 		/*
12563c7e78d3SWeongyo Jeong 		 * Pass the frame to the h/w for transmission.
12573c7e78d3SWeongyo Jeong 		 */
12583c7e78d3SWeongyo Jeong 		if (malo_tx_start(sc, ni, bf, m)) {
12597a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
12607a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
12613c7e78d3SWeongyo Jeong 			if (bf != NULL) {
12623c7e78d3SWeongyo Jeong 				bf->bf_m = NULL;
12633c7e78d3SWeongyo Jeong 				bf->bf_node = NULL;
12643c7e78d3SWeongyo Jeong 				MALO_TXQ_LOCK(txq);
12653c7e78d3SWeongyo Jeong 				STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
12663c7e78d3SWeongyo Jeong 				MALO_TXQ_UNLOCK(txq);
12673c7e78d3SWeongyo Jeong 			}
12683c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
12693c7e78d3SWeongyo Jeong 			continue;
12703c7e78d3SWeongyo Jeong 		}
12713c7e78d3SWeongyo Jeong 		nqueued++;
12723c7e78d3SWeongyo Jeong 
12733c7e78d3SWeongyo Jeong 		if (nqueued >= malo_txcoalesce) {
12743c7e78d3SWeongyo Jeong 			/*
12753c7e78d3SWeongyo Jeong 			 * Poke the firmware to process queued frames;
12763c7e78d3SWeongyo Jeong 			 * see below about (lack of) locking.
12773c7e78d3SWeongyo Jeong 			 */
12783c7e78d3SWeongyo Jeong 			nqueued = 0;
12793c7e78d3SWeongyo Jeong 			malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
12803c7e78d3SWeongyo Jeong 		}
12813c7e78d3SWeongyo Jeong 	}
12823c7e78d3SWeongyo Jeong 
12833c7e78d3SWeongyo Jeong 	if (nqueued) {
12843c7e78d3SWeongyo Jeong 		/*
12853c7e78d3SWeongyo Jeong 		 * NB: We don't need to lock against tx done because
12863c7e78d3SWeongyo Jeong 		 * this just prods the firmware to check the transmit
12873c7e78d3SWeongyo Jeong 		 * descriptors.  The firmware will also start fetching
12883c7e78d3SWeongyo Jeong 		 * descriptors by itself if it notices new ones are
12893c7e78d3SWeongyo Jeong 		 * present when it goes to deliver a tx done interrupt
12903c7e78d3SWeongyo Jeong 		 * to the host. So if we race with tx done processing
12913c7e78d3SWeongyo Jeong 		 * it's ok.  Delivering the kick here rather than in
12923c7e78d3SWeongyo Jeong 		 * malo_tx_start is an optimization to avoid poking the
12933c7e78d3SWeongyo Jeong 		 * firmware for each packet.
12943c7e78d3SWeongyo Jeong 		 *
12953c7e78d3SWeongyo Jeong 		 * NB: the queue id isn't used so 0 is ok.
12963c7e78d3SWeongyo Jeong 		 */
12973c7e78d3SWeongyo Jeong 		malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
12983c7e78d3SWeongyo Jeong 	}
12993c7e78d3SWeongyo Jeong }
13003c7e78d3SWeongyo Jeong 
13013c7e78d3SWeongyo Jeong static void
13027cf545d0SJohn Baldwin malo_watchdog(void *arg)
13033c7e78d3SWeongyo Jeong {
13047a79cebfSGleb Smirnoff 	struct malo_softc *sc = arg;
13053c7e78d3SWeongyo Jeong 
13067cf545d0SJohn Baldwin 	callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
13077cf545d0SJohn Baldwin 	if (sc->malo_timer == 0 || --sc->malo_timer > 0)
13087cf545d0SJohn Baldwin 		return;
13097cf545d0SJohn Baldwin 
13107a79cebfSGleb Smirnoff 	if (sc->malo_running && !sc->malo_invalid) {
13117a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "watchdog timeout\n");
13123c7e78d3SWeongyo Jeong 
13133c7e78d3SWeongyo Jeong 		/* XXX no way to reset h/w. now  */
13143c7e78d3SWeongyo Jeong 
13157a79cebfSGleb Smirnoff 		counter_u64_add(sc->malo_ic.ic_oerrors, 1);
13163c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_watchdog++;
13173c7e78d3SWeongyo Jeong 	}
13183c7e78d3SWeongyo Jeong }
13193c7e78d3SWeongyo Jeong 
13203c7e78d3SWeongyo Jeong static int
13213c7e78d3SWeongyo Jeong malo_hal_reset(struct malo_softc *sc)
13223c7e78d3SWeongyo Jeong {
13233c7e78d3SWeongyo Jeong 	static int first = 0;
13247a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
13253c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
13263c7e78d3SWeongyo Jeong 
13273c7e78d3SWeongyo Jeong 	if (first == 0) {
13283c7e78d3SWeongyo Jeong 		/*
13293c7e78d3SWeongyo Jeong 		 * NB: when the device firstly is initialized, sometimes
13303c7e78d3SWeongyo Jeong 		 * firmware could override rx/tx dma registers so we re-set
13313c7e78d3SWeongyo Jeong 		 * these values once.
13323c7e78d3SWeongyo Jeong 		 */
13333c7e78d3SWeongyo Jeong 		malo_hal_set_rxtxdma(sc);
13343c7e78d3SWeongyo Jeong 		first = 1;
13353c7e78d3SWeongyo Jeong 	}
13363c7e78d3SWeongyo Jeong 
13373c7e78d3SWeongyo Jeong 	malo_hal_setantenna(mh, MHA_ANTENNATYPE_RX, sc->malo_rxantenna);
13383c7e78d3SWeongyo Jeong 	malo_hal_setantenna(mh, MHA_ANTENNATYPE_TX, sc->malo_txantenna);
13393c7e78d3SWeongyo Jeong 	malo_hal_setradio(mh, 1, MHP_AUTO_PREAMBLE);
13403c7e78d3SWeongyo Jeong 	malo_chan_set(sc, ic->ic_curchan);
13413c7e78d3SWeongyo Jeong 
13423c7e78d3SWeongyo Jeong 	/* XXX needs other stuffs?  */
13433c7e78d3SWeongyo Jeong 
13443c7e78d3SWeongyo Jeong 	return 1;
13453c7e78d3SWeongyo Jeong }
13463c7e78d3SWeongyo Jeong 
13473c7e78d3SWeongyo Jeong static __inline struct mbuf *
13483c7e78d3SWeongyo Jeong malo_getrxmbuf(struct malo_softc *sc, struct malo_rxbuf *bf)
13493c7e78d3SWeongyo Jeong {
13503c7e78d3SWeongyo Jeong 	struct mbuf *m;
13513c7e78d3SWeongyo Jeong 	bus_addr_t paddr;
13523c7e78d3SWeongyo Jeong 	int error;
13533c7e78d3SWeongyo Jeong 
13543c7e78d3SWeongyo Jeong 	/* XXX don't need mbuf, just dma buffer */
1355c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
13563c7e78d3SWeongyo Jeong 	if (m == NULL) {
13573c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_rx_nombuf++;	/* XXX */
13583c7e78d3SWeongyo Jeong 		return NULL;
13593c7e78d3SWeongyo Jeong 	}
13603c7e78d3SWeongyo Jeong 	error = bus_dmamap_load(sc->malo_dmat, bf->bf_dmamap,
13613c7e78d3SWeongyo Jeong 	    mtod(m, caddr_t), MJUMPAGESIZE,
13623c7e78d3SWeongyo Jeong 	    malo_load_cb, &paddr, BUS_DMA_NOWAIT);
13633c7e78d3SWeongyo Jeong 	if (error != 0) {
13647a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
13653c7e78d3SWeongyo Jeong 		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
13663c7e78d3SWeongyo Jeong 		m_freem(m);
13673c7e78d3SWeongyo Jeong 		return NULL;
13683c7e78d3SWeongyo Jeong 	}
13693c7e78d3SWeongyo Jeong 	bf->bf_data = paddr;
13703c7e78d3SWeongyo Jeong 	bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
13713c7e78d3SWeongyo Jeong 
13723c7e78d3SWeongyo Jeong 	return m;
13733c7e78d3SWeongyo Jeong }
13743c7e78d3SWeongyo Jeong 
13753c7e78d3SWeongyo Jeong static int
13763c7e78d3SWeongyo Jeong malo_rxbuf_init(struct malo_softc *sc, struct malo_rxbuf *bf)
13773c7e78d3SWeongyo Jeong {
13783c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
13793c7e78d3SWeongyo Jeong 
13803c7e78d3SWeongyo Jeong 	ds = bf->bf_desc;
13813c7e78d3SWeongyo Jeong 	if (bf->bf_m == NULL) {
13823c7e78d3SWeongyo Jeong 		bf->bf_m = malo_getrxmbuf(sc, bf);
13833c7e78d3SWeongyo Jeong 		if (bf->bf_m == NULL) {
13843c7e78d3SWeongyo Jeong 			/* mark descriptor to be skipped */
13853c7e78d3SWeongyo Jeong 			ds->rxcontrol = MALO_RXD_CTRL_OS_OWN;
13863c7e78d3SWeongyo Jeong 			/* NB: don't need PREREAD */
13873c7e78d3SWeongyo Jeong 			MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREWRITE);
13883c7e78d3SWeongyo Jeong 			return ENOMEM;
13893c7e78d3SWeongyo Jeong 		}
13903c7e78d3SWeongyo Jeong 	}
13913c7e78d3SWeongyo Jeong 
13923c7e78d3SWeongyo Jeong 	/*
13933c7e78d3SWeongyo Jeong 	 * Setup descriptor.
13943c7e78d3SWeongyo Jeong 	 */
13953c7e78d3SWeongyo Jeong 	ds->qosctrl = 0;
13963c7e78d3SWeongyo Jeong 	ds->snr = 0;
13973c7e78d3SWeongyo Jeong 	ds->status = MALO_RXD_STATUS_IDLE;
13983c7e78d3SWeongyo Jeong 	ds->channel = 0;
13993c7e78d3SWeongyo Jeong 	ds->pktlen = htole16(MALO_RXSIZE);
14003c7e78d3SWeongyo Jeong 	ds->nf = 0;
14013c7e78d3SWeongyo Jeong 	ds->physbuffdata = htole32(bf->bf_data);
14023c7e78d3SWeongyo Jeong 	/* NB: don't touch pPhysNext, set once */
14033c7e78d3SWeongyo Jeong 	ds->rxcontrol = MALO_RXD_CTRL_DRIVER_OWN;
14043c7e78d3SWeongyo Jeong 	MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
14053c7e78d3SWeongyo Jeong 
14063c7e78d3SWeongyo Jeong 	return 0;
14073c7e78d3SWeongyo Jeong }
14083c7e78d3SWeongyo Jeong 
14093c7e78d3SWeongyo Jeong /*
14103c7e78d3SWeongyo Jeong  * Setup the rx data structures.  This should only be done once or we may get
14113c7e78d3SWeongyo Jeong  * out of sync with the firmware.
14123c7e78d3SWeongyo Jeong  */
14133c7e78d3SWeongyo Jeong static int
14143c7e78d3SWeongyo Jeong malo_startrecv(struct malo_softc *sc)
14153c7e78d3SWeongyo Jeong {
14163c7e78d3SWeongyo Jeong 	struct malo_rxbuf *bf, *prev;
14173c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
14183c7e78d3SWeongyo Jeong 
14193c7e78d3SWeongyo Jeong 	if (sc->malo_recvsetup == 1) {
14203c7e78d3SWeongyo Jeong 		malo_mode_init(sc);		/* set filters, etc. */
14213c7e78d3SWeongyo Jeong 		return 0;
14223c7e78d3SWeongyo Jeong 	}
14233c7e78d3SWeongyo Jeong 
14243c7e78d3SWeongyo Jeong 	prev = NULL;
14253c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
14263c7e78d3SWeongyo Jeong 		int error = malo_rxbuf_init(sc, bf);
14273c7e78d3SWeongyo Jeong 		if (error != 0) {
14283c7e78d3SWeongyo Jeong 			DPRINTF(sc, MALO_DEBUG_RECV,
14293c7e78d3SWeongyo Jeong 			    "%s: malo_rxbuf_init failed %d\n",
14303c7e78d3SWeongyo Jeong 			    __func__, error);
14313c7e78d3SWeongyo Jeong 			return error;
14323c7e78d3SWeongyo Jeong 		}
14333c7e78d3SWeongyo Jeong 		if (prev != NULL) {
14343c7e78d3SWeongyo Jeong 			ds = prev->bf_desc;
14353c7e78d3SWeongyo Jeong 			ds->physnext = htole32(bf->bf_daddr);
14363c7e78d3SWeongyo Jeong 		}
14373c7e78d3SWeongyo Jeong 		prev = bf;
14383c7e78d3SWeongyo Jeong 	}
14393c7e78d3SWeongyo Jeong 	if (prev != NULL) {
14403c7e78d3SWeongyo Jeong 		ds = prev->bf_desc;
14413c7e78d3SWeongyo Jeong 		ds->physnext =
14423c7e78d3SWeongyo Jeong 		    htole32(STAILQ_FIRST(&sc->malo_rxbuf)->bf_daddr);
14433c7e78d3SWeongyo Jeong 	}
14443c7e78d3SWeongyo Jeong 
14453c7e78d3SWeongyo Jeong 	sc->malo_recvsetup = 1;
14463c7e78d3SWeongyo Jeong 
14473c7e78d3SWeongyo Jeong 	malo_mode_init(sc);		/* set filters, etc. */
14483c7e78d3SWeongyo Jeong 
14493c7e78d3SWeongyo Jeong 	return 0;
14503c7e78d3SWeongyo Jeong }
14513c7e78d3SWeongyo Jeong 
14523c7e78d3SWeongyo Jeong static void
1453b032f27cSSam Leffler malo_init_locked(struct malo_softc *sc)
14543c7e78d3SWeongyo Jeong {
14553c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
14563c7e78d3SWeongyo Jeong 	int error;
14573c7e78d3SWeongyo Jeong 
1458b032f27cSSam Leffler 	MALO_LOCK_ASSERT(sc);
14593c7e78d3SWeongyo Jeong 
14603c7e78d3SWeongyo Jeong 	/*
14613c7e78d3SWeongyo Jeong 	 * Stop anything previously setup.  This is safe whether this is
14623c7e78d3SWeongyo Jeong 	 * the first time through or not.
14633c7e78d3SWeongyo Jeong 	 */
14647a79cebfSGleb Smirnoff 	malo_stop(sc);
14653c7e78d3SWeongyo Jeong 
14663c7e78d3SWeongyo Jeong 	/*
14673c7e78d3SWeongyo Jeong 	 * Push state to the firmware.
14683c7e78d3SWeongyo Jeong 	 */
14693c7e78d3SWeongyo Jeong 	if (!malo_hal_reset(sc)) {
14707a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
14717a79cebfSGleb Smirnoff 		    "%s: unable to reset hardware\n", __func__);
1472b032f27cSSam Leffler 		return;
14733c7e78d3SWeongyo Jeong 	}
14743c7e78d3SWeongyo Jeong 
14753c7e78d3SWeongyo Jeong 	/*
14763c7e78d3SWeongyo Jeong 	 * Setup recv (once); transmit is already good to go.
14773c7e78d3SWeongyo Jeong 	 */
14783c7e78d3SWeongyo Jeong 	error = malo_startrecv(sc);
14793c7e78d3SWeongyo Jeong 	if (error != 0) {
14807a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
14817a79cebfSGleb Smirnoff 		    "%s: unable to start recv logic, error %d\n",
14823c7e78d3SWeongyo Jeong 		    __func__, error);
1483b032f27cSSam Leffler 		return;
14843c7e78d3SWeongyo Jeong 	}
14853c7e78d3SWeongyo Jeong 
14863c7e78d3SWeongyo Jeong 	/*
14873c7e78d3SWeongyo Jeong 	 * Enable interrupts.
14883c7e78d3SWeongyo Jeong 	 */
14893c7e78d3SWeongyo Jeong 	sc->malo_imask = MALO_A2HRIC_BIT_RX_RDY
14903c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_TX_DONE
14913c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_OPC_DONE
14923c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_MAC_EVENT
14933c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_RX_PROBLEM
14943c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_ICV_ERROR
14953c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_RADAR_DETECT
14963c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_CHAN_SWITCH;
14973c7e78d3SWeongyo Jeong 
14987a79cebfSGleb Smirnoff 	sc->malo_running = 1;
14993c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, sc->malo_imask);
15007cf545d0SJohn Baldwin 	callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
1501b032f27cSSam Leffler }
15023c7e78d3SWeongyo Jeong 
1503b032f27cSSam Leffler static void
1504b032f27cSSam Leffler malo_init(void *arg)
1505b032f27cSSam Leffler {
1506b032f27cSSam Leffler 	struct malo_softc *sc = (struct malo_softc *) arg;
15077a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
1508b032f27cSSam Leffler 
1509b032f27cSSam Leffler 	MALO_LOCK(sc);
1510b032f27cSSam Leffler 	malo_init_locked(sc);
15113c7e78d3SWeongyo Jeong 	MALO_UNLOCK(sc);
15123c7e78d3SWeongyo Jeong 
15137a79cebfSGleb Smirnoff 	if (sc->malo_running)
1514b032f27cSSam Leffler 		ieee80211_start_all(ic);	/* start all vap's */
15153c7e78d3SWeongyo Jeong }
15163c7e78d3SWeongyo Jeong 
15173c7e78d3SWeongyo Jeong /*
15183c7e78d3SWeongyo Jeong  * Set the multicast filter contents into the hardware.
15193c7e78d3SWeongyo Jeong  */
15203c7e78d3SWeongyo Jeong static void
15213c7e78d3SWeongyo Jeong malo_setmcastfilter(struct malo_softc *sc)
15223c7e78d3SWeongyo Jeong {
15237a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
15247a79cebfSGleb Smirnoff 	struct ieee80211vap *vap;
15253c7e78d3SWeongyo Jeong 	uint8_t macs[IEEE80211_ADDR_LEN * MALO_HAL_MCAST_MAX];
15263c7e78d3SWeongyo Jeong 	uint8_t *mp;
15273c7e78d3SWeongyo Jeong 	int nmc;
15283c7e78d3SWeongyo Jeong 
15293c7e78d3SWeongyo Jeong 	mp = macs;
15303c7e78d3SWeongyo Jeong 	nmc = 0;
15313c7e78d3SWeongyo Jeong 
15327a79cebfSGleb Smirnoff 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
15337a79cebfSGleb Smirnoff 	    ic->ic_promisc > 0)
15343c7e78d3SWeongyo Jeong 		goto all;
15353c7e78d3SWeongyo Jeong 
15367a79cebfSGleb Smirnoff 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
15377a79cebfSGleb Smirnoff 		struct ifnet *ifp;
15387a79cebfSGleb Smirnoff 		struct ifmultiaddr *ifma;
15397a79cebfSGleb Smirnoff 
15407a79cebfSGleb Smirnoff 		ifp = vap->iv_ifp;
1541eb956cd0SRobert Watson 		if_maddr_rlock(ifp);
15423c7e78d3SWeongyo Jeong 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
15433c7e78d3SWeongyo Jeong 			if (ifma->ifma_addr->sa_family != AF_LINK)
15443c7e78d3SWeongyo Jeong 				continue;
15453c7e78d3SWeongyo Jeong 
15463c7e78d3SWeongyo Jeong 			if (nmc == MALO_HAL_MCAST_MAX) {
15473c7e78d3SWeongyo Jeong 				ifp->if_flags |= IFF_ALLMULTI;
1548eb956cd0SRobert Watson 				if_maddr_runlock(ifp);
15493c7e78d3SWeongyo Jeong 				goto all;
15503c7e78d3SWeongyo Jeong 			}
15513c7e78d3SWeongyo Jeong 			IEEE80211_ADDR_COPY(mp,
15523c7e78d3SWeongyo Jeong 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
15533c7e78d3SWeongyo Jeong 
15543c7e78d3SWeongyo Jeong 			mp += IEEE80211_ADDR_LEN, nmc++;
15553c7e78d3SWeongyo Jeong 		}
1556eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
15577a79cebfSGleb Smirnoff 	}
15583c7e78d3SWeongyo Jeong 
15593c7e78d3SWeongyo Jeong 	malo_hal_setmcast(sc->malo_mh, nmc, macs);
15603c7e78d3SWeongyo Jeong 
15613c7e78d3SWeongyo Jeong all:
15623c7e78d3SWeongyo Jeong 	/*
15633c7e78d3SWeongyo Jeong 	 * XXX we don't know how to set the f/w for supporting
15643c7e78d3SWeongyo Jeong 	 * IFF_ALLMULTI | IFF_PROMISC cases
15653c7e78d3SWeongyo Jeong 	 */
15663c7e78d3SWeongyo Jeong 	return;
15673c7e78d3SWeongyo Jeong }
15683c7e78d3SWeongyo Jeong 
15693c7e78d3SWeongyo Jeong static int
15703c7e78d3SWeongyo Jeong malo_mode_init(struct malo_softc *sc)
15713c7e78d3SWeongyo Jeong {
15727a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
15733c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
15743c7e78d3SWeongyo Jeong 
15756459bd28SAndriy Voskoboinyk 	malo_hal_setpromisc(mh, ic->ic_promisc > 0);
15763c7e78d3SWeongyo Jeong 	malo_setmcastfilter(sc);
15773c7e78d3SWeongyo Jeong 
15783c7e78d3SWeongyo Jeong 	return ENXIO;
15793c7e78d3SWeongyo Jeong }
15803c7e78d3SWeongyo Jeong 
15813c7e78d3SWeongyo Jeong static void
15823c7e78d3SWeongyo Jeong malo_tx_draintxq(struct malo_softc *sc, struct malo_txq *txq)
15833c7e78d3SWeongyo Jeong {
15843c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
15853c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
15863c7e78d3SWeongyo Jeong 	u_int ix;
15873c7e78d3SWeongyo Jeong 
15883c7e78d3SWeongyo Jeong 	/*
15893c7e78d3SWeongyo Jeong 	 * NB: this assumes output has been stopped and
15903c7e78d3SWeongyo Jeong 	 *     we do not need to block malo_tx_tasklet
15913c7e78d3SWeongyo Jeong 	 */
15923c7e78d3SWeongyo Jeong 	for (ix = 0;; ix++) {
15933c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
15943c7e78d3SWeongyo Jeong 		bf = STAILQ_FIRST(&txq->active);
15953c7e78d3SWeongyo Jeong 		if (bf == NULL) {
15963c7e78d3SWeongyo Jeong 			MALO_TXQ_UNLOCK(txq);
15973c7e78d3SWeongyo Jeong 			break;
15983c7e78d3SWeongyo Jeong 		}
15993c7e78d3SWeongyo Jeong 		STAILQ_REMOVE_HEAD(&txq->active, bf_list);
16003c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
16013c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
16023c7e78d3SWeongyo Jeong 		if (sc->malo_debug & MALO_DEBUG_RESET) {
16037a79cebfSGleb Smirnoff 			struct ieee80211com *ic = &sc->malo_ic;
16043c7e78d3SWeongyo Jeong 			const struct malo_txrec *tr =
16053c7e78d3SWeongyo Jeong 			    mtod(bf->bf_m, const struct malo_txrec *);
16063c7e78d3SWeongyo Jeong 			malo_printtxbuf(bf, txq->qnum, ix);
1607b032f27cSSam Leffler 			ieee80211_dump_pkt(ic, (const uint8_t *)&tr->wh,
16083c7e78d3SWeongyo Jeong 			    bf->bf_m->m_len - sizeof(tr->fwlen), 0, -1);
16093c7e78d3SWeongyo Jeong 		}
16103c7e78d3SWeongyo Jeong #endif /* MALO_DEBUG */
16113c7e78d3SWeongyo Jeong 		bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
16123c7e78d3SWeongyo Jeong 		ni = bf->bf_node;
16133c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
16143c7e78d3SWeongyo Jeong 		if (ni != NULL) {
16153c7e78d3SWeongyo Jeong 			/*
16163c7e78d3SWeongyo Jeong 			 * Reclaim node reference.
16173c7e78d3SWeongyo Jeong 			 */
16183c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
16193c7e78d3SWeongyo Jeong 		}
16203c7e78d3SWeongyo Jeong 		m_freem(bf->bf_m);
16213c7e78d3SWeongyo Jeong 		bf->bf_m = NULL;
16223c7e78d3SWeongyo Jeong 
16233c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
16243c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
16253c7e78d3SWeongyo Jeong 		txq->nfree++;
16263c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
16273c7e78d3SWeongyo Jeong 	}
16283c7e78d3SWeongyo Jeong }
16293c7e78d3SWeongyo Jeong 
16303c7e78d3SWeongyo Jeong static void
16317a79cebfSGleb Smirnoff malo_stop(struct malo_softc *sc)
16323c7e78d3SWeongyo Jeong {
16333c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
1634b032f27cSSam Leffler 	int i;
16353c7e78d3SWeongyo Jeong 
16367a79cebfSGleb Smirnoff 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid %u running %u\n",
16377a79cebfSGleb Smirnoff 	    __func__, sc->malo_invalid, sc->malo_running);
16383c7e78d3SWeongyo Jeong 
16393c7e78d3SWeongyo Jeong 	MALO_LOCK_ASSERT(sc);
16403c7e78d3SWeongyo Jeong 
16417a79cebfSGleb Smirnoff 	if (!sc->malo_running)
16423c7e78d3SWeongyo Jeong 		return;
16433c7e78d3SWeongyo Jeong 
16443c7e78d3SWeongyo Jeong 	/*
16453c7e78d3SWeongyo Jeong 	 * Shutdown the hardware and driver:
16463c7e78d3SWeongyo Jeong 	 *    disable interrupts
16473c7e78d3SWeongyo Jeong 	 *    turn off the radio
16483c7e78d3SWeongyo Jeong 	 *    drain and release tx queues
16493c7e78d3SWeongyo Jeong 	 *
16503c7e78d3SWeongyo Jeong 	 * Note that some of this work is not possible if the hardware
16513c7e78d3SWeongyo Jeong 	 * is gone (invalid).
16523c7e78d3SWeongyo Jeong 	 */
16537a79cebfSGleb Smirnoff 	sc->malo_running = 0;
16547cf545d0SJohn Baldwin 	callout_stop(&sc->malo_watchdog_timer);
16557cf545d0SJohn Baldwin 	sc->malo_timer = 0;
16567a79cebfSGleb Smirnoff 	/* disable interrupt.  */
16573c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, 0);
16583c7e78d3SWeongyo Jeong 	/* turn off the radio.  */
16593c7e78d3SWeongyo Jeong 	malo_hal_setradio(mh, 0, MHP_AUTO_PREAMBLE);
16603c7e78d3SWeongyo Jeong 
16613c7e78d3SWeongyo Jeong 	/* drain and release tx queues.  */
16623c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
16633c7e78d3SWeongyo Jeong 		malo_tx_draintxq(sc, &sc->malo_txq[i]);
16643c7e78d3SWeongyo Jeong }
16653c7e78d3SWeongyo Jeong 
16667a79cebfSGleb Smirnoff static void
16677a79cebfSGleb Smirnoff malo_parent(struct ieee80211com *ic)
16683c7e78d3SWeongyo Jeong {
16697a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
16707a79cebfSGleb Smirnoff 	int startall = 0;
16713c7e78d3SWeongyo Jeong 
16723c7e78d3SWeongyo Jeong 	MALO_LOCK(sc);
16737a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
16743c7e78d3SWeongyo Jeong 		/*
16753c7e78d3SWeongyo Jeong 		 * Beware of being called during attach/detach
16763c7e78d3SWeongyo Jeong 		 * to reset promiscuous mode.  In that case we
16773c7e78d3SWeongyo Jeong 		 * will still be marked UP but not RUNNING.
16783c7e78d3SWeongyo Jeong 		 * However trying to re-init the interface
16793c7e78d3SWeongyo Jeong 		 * is the wrong thing to do as we've already
16803c7e78d3SWeongyo Jeong 		 * torn down much of our state.  There's
16813c7e78d3SWeongyo Jeong 		 * probably a better way to deal with this.
16823c7e78d3SWeongyo Jeong 		 */
16837a79cebfSGleb Smirnoff 		if (!sc->malo_running && !sc->malo_invalid) {
16847a79cebfSGleb Smirnoff 			malo_init(sc);
1685b032f27cSSam Leffler 			startall = 1;
1686b032f27cSSam Leffler 		}
16877a79cebfSGleb Smirnoff 		/*
16887a79cebfSGleb Smirnoff 		 * To avoid rescanning another access point,
16897a79cebfSGleb Smirnoff 		 * do not call malo_init() here.  Instead,
16907a79cebfSGleb Smirnoff 		 * only reflect promisc mode settings.
16917a79cebfSGleb Smirnoff 		 */
16927a79cebfSGleb Smirnoff 		malo_mode_init(sc);
16937a79cebfSGleb Smirnoff 	} else if (sc->malo_running)
16947a79cebfSGleb Smirnoff 		malo_stop(sc);
16953c7e78d3SWeongyo Jeong 	MALO_UNLOCK(sc);
1696b032f27cSSam Leffler 	if (startall)
1697b032f27cSSam Leffler 		ieee80211_start_all(ic);
16983c7e78d3SWeongyo Jeong }
16993c7e78d3SWeongyo Jeong 
17003c7e78d3SWeongyo Jeong /*
17013c7e78d3SWeongyo Jeong  * Callback from the 802.11 layer to update the slot time
17023c7e78d3SWeongyo Jeong  * based on the current setting.  We use it to notify the
17033c7e78d3SWeongyo Jeong  * firmware of ERP changes and the f/w takes care of things
17043c7e78d3SWeongyo Jeong  * like slot time and preamble.
17053c7e78d3SWeongyo Jeong  */
17063c7e78d3SWeongyo Jeong static void
1707272f6adeSGleb Smirnoff malo_updateslot(struct ieee80211com *ic)
17083c7e78d3SWeongyo Jeong {
1709272f6adeSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
17103c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
17113c7e78d3SWeongyo Jeong 	int error;
17123c7e78d3SWeongyo Jeong 
17133c7e78d3SWeongyo Jeong 	/* NB: can be called early; suppress needless cmds */
17147a79cebfSGleb Smirnoff 	if (!sc->malo_running)
17153c7e78d3SWeongyo Jeong 		return;
17163c7e78d3SWeongyo Jeong 
17173c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RESET,
17183c7e78d3SWeongyo Jeong 	    "%s: chan %u MHz/flags 0x%x %s slot, (ic_flags 0x%x)\n",
17193c7e78d3SWeongyo Jeong 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
17203c7e78d3SWeongyo Jeong 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", ic->ic_flags);
17213c7e78d3SWeongyo Jeong 
17223c7e78d3SWeongyo Jeong 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
17233c7e78d3SWeongyo Jeong 		error = malo_hal_set_slot(mh, 1);
17243c7e78d3SWeongyo Jeong 	else
17253c7e78d3SWeongyo Jeong 		error = malo_hal_set_slot(mh, 0);
17263c7e78d3SWeongyo Jeong 
17273c7e78d3SWeongyo Jeong 	if (error != 0)
17283c7e78d3SWeongyo Jeong 		device_printf(sc->malo_dev, "setting %s slot failed\n",
17293c7e78d3SWeongyo Jeong 			ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long");
17303c7e78d3SWeongyo Jeong }
17313c7e78d3SWeongyo Jeong 
17323c7e78d3SWeongyo Jeong static int
1733b032f27cSSam Leffler malo_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17343c7e78d3SWeongyo Jeong {
1735b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17367a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
17373c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
17383c7e78d3SWeongyo Jeong 	int error;
17393c7e78d3SWeongyo Jeong 
17403c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1741b032f27cSSam Leffler 	    ieee80211_state_name[vap->iv_state],
17423c7e78d3SWeongyo Jeong 	    ieee80211_state_name[nstate]);
17433c7e78d3SWeongyo Jeong 
17443c7e78d3SWeongyo Jeong 	/*
1745b032f27cSSam Leffler 	 * Invoke the net80211 layer first so iv_bss is setup.
17463c7e78d3SWeongyo Jeong 	 */
1747b032f27cSSam Leffler 	error = MALO_VAP(vap)->malo_newstate(vap, nstate, arg);
1748b032f27cSSam Leffler 	if (error != 0)
1749b032f27cSSam Leffler 		return error;
1750b032f27cSSam Leffler 
1751b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1752b032f27cSSam Leffler 		struct ieee80211_node *ni = vap->iv_bss;
1753b032f27cSSam Leffler 		enum ieee80211_phymode mode = ieee80211_chan2mode(ni->ni_chan);
1754b032f27cSSam Leffler 		const struct ieee80211_txparam *tp = &vap->iv_txparms[mode];
1755b032f27cSSam Leffler 
1756b032f27cSSam Leffler 		DPRINTF(sc, MALO_DEBUG_STATE,
1757b032f27cSSam Leffler 		    "%s: %s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
1758b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d associd 0x%x mode %d rate %d\n",
1759b032f27cSSam Leffler 		    vap->iv_ifp->if_xname, __func__, vap->iv_flags,
1760b032f27cSSam Leffler 		    ni->ni_intval, ether_sprintf(ni->ni_bssid), ni->ni_capinfo,
1761b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan),
1762b032f27cSSam Leffler 		    ni->ni_associd, mode, tp->ucastrate);
1763b032f27cSSam Leffler 
17643c7e78d3SWeongyo Jeong 		malo_hal_setradio(mh, 1,
17653c7e78d3SWeongyo Jeong 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
17663c7e78d3SWeongyo Jeong 			MHP_SHORT_PREAMBLE : MHP_LONG_PREAMBLE);
1767b032f27cSSam Leffler 		malo_hal_setassocid(sc->malo_mh, ni->ni_bssid, ni->ni_associd);
1768b032f27cSSam Leffler 		malo_hal_set_rate(mh, mode,
1769b032f27cSSam Leffler 		   tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1770b032f27cSSam Leffler 		       0 : malo_fix2rate(tp->ucastrate));
17713c7e78d3SWeongyo Jeong 	}
1772b032f27cSSam Leffler 	return 0;
17733c7e78d3SWeongyo Jeong }
17743c7e78d3SWeongyo Jeong 
17753c7e78d3SWeongyo Jeong static int
17763c7e78d3SWeongyo Jeong malo_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
17773c7e78d3SWeongyo Jeong 	const struct ieee80211_bpf_params *params)
17783c7e78d3SWeongyo Jeong {
17793c7e78d3SWeongyo Jeong 	struct ieee80211com *ic = ni->ni_ic;
17807a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
17813c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
17823c7e78d3SWeongyo Jeong 	struct malo_txq *txq;
17833c7e78d3SWeongyo Jeong 
17847a79cebfSGleb Smirnoff 	if (!sc->malo_running || sc->malo_invalid) {
17853c7e78d3SWeongyo Jeong 		m_freem(m);
17863c7e78d3SWeongyo Jeong 		return ENETDOWN;
17873c7e78d3SWeongyo Jeong 	}
17883c7e78d3SWeongyo Jeong 
17893c7e78d3SWeongyo Jeong 	/*
17903c7e78d3SWeongyo Jeong 	 * Grab a TX buffer and associated resources.  Note that we depend
17913c7e78d3SWeongyo Jeong 	 * on the classification by the 802.11 layer to get to the right h/w
17923c7e78d3SWeongyo Jeong 	 * queue.  Management frames must ALWAYS go on queue 1 but we
17933c7e78d3SWeongyo Jeong 	 * cannot just force that here because we may receive non-mgt frames.
17943c7e78d3SWeongyo Jeong 	 */
17953c7e78d3SWeongyo Jeong 	txq = &sc->malo_txq[0];
17963c7e78d3SWeongyo Jeong 	bf = malo_getbuf(sc, txq);
17973c7e78d3SWeongyo Jeong 	if (bf == NULL) {
17983c7e78d3SWeongyo Jeong 		m_freem(m);
17993c7e78d3SWeongyo Jeong 		return ENOBUFS;
18003c7e78d3SWeongyo Jeong 	}
18013c7e78d3SWeongyo Jeong 
18023c7e78d3SWeongyo Jeong 	/*
18033c7e78d3SWeongyo Jeong 	 * Pass the frame to the h/w for transmission.
18043c7e78d3SWeongyo Jeong 	 */
18053c7e78d3SWeongyo Jeong 	if (malo_tx_start(sc, ni, bf, m) != 0) {
18063c7e78d3SWeongyo Jeong 		bf->bf_m = NULL;
18073c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
18083c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
18093c7e78d3SWeongyo Jeong 		STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
18103c7e78d3SWeongyo Jeong 		txq->nfree++;
18113c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
18123c7e78d3SWeongyo Jeong 
18133c7e78d3SWeongyo Jeong 		return EIO;		/* XXX */
18143c7e78d3SWeongyo Jeong 	}
18153c7e78d3SWeongyo Jeong 
18163c7e78d3SWeongyo Jeong 	/*
18173c7e78d3SWeongyo Jeong 	 * NB: We don't need to lock against tx done because this just
18183c7e78d3SWeongyo Jeong 	 * prods the firmware to check the transmit descriptors.  The firmware
18193c7e78d3SWeongyo Jeong 	 * will also start fetching descriptors by itself if it notices
18203c7e78d3SWeongyo Jeong 	 * new ones are present when it goes to deliver a tx done interrupt
18213c7e78d3SWeongyo Jeong 	 * to the host. So if we race with tx done processing it's ok.
18223c7e78d3SWeongyo Jeong 	 * Delivering the kick here rather than in malo_tx_start is
18233c7e78d3SWeongyo Jeong 	 * an optimization to avoid poking the firmware for each packet.
18243c7e78d3SWeongyo Jeong 	 *
18253c7e78d3SWeongyo Jeong 	 * NB: the queue id isn't used so 0 is ok.
18263c7e78d3SWeongyo Jeong 	 */
18273c7e78d3SWeongyo Jeong 	malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
18283c7e78d3SWeongyo Jeong 
18293c7e78d3SWeongyo Jeong 	return 0;
18303c7e78d3SWeongyo Jeong }
18313c7e78d3SWeongyo Jeong 
18323c7e78d3SWeongyo Jeong static void
18333c7e78d3SWeongyo Jeong malo_sysctlattach(struct malo_softc *sc)
18343c7e78d3SWeongyo Jeong {
18353c7e78d3SWeongyo Jeong #ifdef	MALO_DEBUG
18363c7e78d3SWeongyo Jeong 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->malo_dev);
18373c7e78d3SWeongyo Jeong 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->malo_dev);
18383c7e78d3SWeongyo Jeong 
18393c7e78d3SWeongyo Jeong 	sc->malo_debug = malo_debug;
18403c7e78d3SWeongyo Jeong 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
18413c7e78d3SWeongyo Jeong 		"debug", CTLFLAG_RW, &sc->malo_debug, 0,
18423c7e78d3SWeongyo Jeong 		"control debugging printfs");
18433c7e78d3SWeongyo Jeong #endif
18443c7e78d3SWeongyo Jeong }
18453c7e78d3SWeongyo Jeong 
18463c7e78d3SWeongyo Jeong static void
18473c7e78d3SWeongyo Jeong malo_announce(struct malo_softc *sc)
18483c7e78d3SWeongyo Jeong {
18493c7e78d3SWeongyo Jeong 
18507a79cebfSGleb Smirnoff 	device_printf(sc->malo_dev,
18517a79cebfSGleb Smirnoff 		"versions [hw %d fw %d.%d.%d.%d] (regioncode %d)\n",
18523c7e78d3SWeongyo Jeong 		sc->malo_hwspecs.hwversion,
18533c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 24) & 0xff,
18543c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 16) & 0xff,
18553c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 8) & 0xff,
18563c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 0) & 0xff,
18573c7e78d3SWeongyo Jeong 		sc->malo_hwspecs.regioncode);
18583c7e78d3SWeongyo Jeong 
18593c7e78d3SWeongyo Jeong 	if (bootverbose || malo_rxbuf != MALO_RXBUF)
18607a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
18617a79cebfSGleb Smirnoff 		    "using %u rx buffers\n", malo_rxbuf);
18623c7e78d3SWeongyo Jeong 	if (bootverbose || malo_txbuf != MALO_TXBUF)
18637a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
18647a79cebfSGleb Smirnoff 		    "using %u tx buffers\n", malo_txbuf);
18653c7e78d3SWeongyo Jeong }
18663c7e78d3SWeongyo Jeong 
18673c7e78d3SWeongyo Jeong /*
18683c7e78d3SWeongyo Jeong  * Convert net80211 channel to a HAL channel.
18693c7e78d3SWeongyo Jeong  */
18703c7e78d3SWeongyo Jeong static void
18713c7e78d3SWeongyo Jeong malo_mapchan(struct malo_hal_channel *hc, const struct ieee80211_channel *chan)
18723c7e78d3SWeongyo Jeong {
18733c7e78d3SWeongyo Jeong 	hc->channel = chan->ic_ieee;
18743c7e78d3SWeongyo Jeong 
18753c7e78d3SWeongyo Jeong 	*(uint32_t *)&hc->flags = 0;
18763c7e78d3SWeongyo Jeong 	if (IEEE80211_IS_CHAN_2GHZ(chan))
18773c7e78d3SWeongyo Jeong 		hc->flags.freqband = MALO_FREQ_BAND_2DOT4GHZ;
18783c7e78d3SWeongyo Jeong }
18793c7e78d3SWeongyo Jeong 
18803c7e78d3SWeongyo Jeong /*
18813c7e78d3SWeongyo Jeong  * Set/change channels.  If the channel is really being changed,
18823c7e78d3SWeongyo Jeong  * it's done by reseting the chip.  To accomplish this we must
18833c7e78d3SWeongyo Jeong  * first cleanup any pending DMA, then restart stuff after a la
18843c7e78d3SWeongyo Jeong  * malo_init.
18853c7e78d3SWeongyo Jeong  */
18863c7e78d3SWeongyo Jeong static int
18873c7e78d3SWeongyo Jeong malo_chan_set(struct malo_softc *sc, struct ieee80211_channel *chan)
18883c7e78d3SWeongyo Jeong {
18893c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
18903c7e78d3SWeongyo Jeong 	struct malo_hal_channel hchan;
18913c7e78d3SWeongyo Jeong 
18923c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RESET, "%s: chan %u MHz/flags 0x%x\n",
18933c7e78d3SWeongyo Jeong 	    __func__, chan->ic_freq, chan->ic_flags);
18943c7e78d3SWeongyo Jeong 
18953c7e78d3SWeongyo Jeong 	/*
18963c7e78d3SWeongyo Jeong 	 * Convert to a HAL channel description with the flags constrained
18973c7e78d3SWeongyo Jeong 	 * to reflect the current operating mode.
18983c7e78d3SWeongyo Jeong 	 */
18993c7e78d3SWeongyo Jeong 	malo_mapchan(&hchan, chan);
19003c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, 0);		/* disable interrupts */
19013c7e78d3SWeongyo Jeong 	malo_hal_setchannel(mh, &hchan);
19023c7e78d3SWeongyo Jeong 	malo_hal_settxpower(mh, &hchan);
19033c7e78d3SWeongyo Jeong 
19043c7e78d3SWeongyo Jeong 	/*
19053c7e78d3SWeongyo Jeong 	 * Update internal state.
19063c7e78d3SWeongyo Jeong 	 */
19073c7e78d3SWeongyo Jeong 	sc->malo_tx_th.wt_chan_freq = htole16(chan->ic_freq);
19083c7e78d3SWeongyo Jeong 	sc->malo_rx_th.wr_chan_freq = htole16(chan->ic_freq);
19093c7e78d3SWeongyo Jeong 	if (IEEE80211_IS_CHAN_ANYG(chan)) {
19103c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_G);
19113c7e78d3SWeongyo Jeong 		sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_G);
19123c7e78d3SWeongyo Jeong 	} else {
19133c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_B);
19143c7e78d3SWeongyo Jeong 		sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_B);
19153c7e78d3SWeongyo Jeong 	}
19163c7e78d3SWeongyo Jeong 	sc->malo_curchan = hchan;
19173c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, sc->malo_imask);
19183c7e78d3SWeongyo Jeong 
19193c7e78d3SWeongyo Jeong 	return 0;
19203c7e78d3SWeongyo Jeong }
19213c7e78d3SWeongyo Jeong 
19223c7e78d3SWeongyo Jeong static void
19233c7e78d3SWeongyo Jeong malo_scan_start(struct ieee80211com *ic)
19243c7e78d3SWeongyo Jeong {
19257a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
19263c7e78d3SWeongyo Jeong 
19273c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
19283c7e78d3SWeongyo Jeong }
19293c7e78d3SWeongyo Jeong 
19303c7e78d3SWeongyo Jeong static void
19313c7e78d3SWeongyo Jeong malo_scan_end(struct ieee80211com *ic)
19323c7e78d3SWeongyo Jeong {
19337a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
19343c7e78d3SWeongyo Jeong 
19353c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
19363c7e78d3SWeongyo Jeong }
19373c7e78d3SWeongyo Jeong 
19383c7e78d3SWeongyo Jeong static void
19393c7e78d3SWeongyo Jeong malo_set_channel(struct ieee80211com *ic)
19403c7e78d3SWeongyo Jeong {
19417a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
19423c7e78d3SWeongyo Jeong 
19433c7e78d3SWeongyo Jeong 	(void) malo_chan_set(sc, ic->ic_curchan);
19443c7e78d3SWeongyo Jeong }
19453c7e78d3SWeongyo Jeong 
19463c7e78d3SWeongyo Jeong static void
19473c7e78d3SWeongyo Jeong malo_rx_proc(void *arg, int npending)
19483c7e78d3SWeongyo Jeong {
19493c7e78d3SWeongyo Jeong 	struct malo_softc *sc = arg;
19507a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
1951b032f27cSSam Leffler 	struct malo_rxbuf *bf;
19523c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
19533c7e78d3SWeongyo Jeong 	struct mbuf *m, *mnew;
19543c7e78d3SWeongyo Jeong 	struct ieee80211_qosframe *wh;
19553c7e78d3SWeongyo Jeong 	struct ieee80211_qosframe_addr4 *wh4;
19563c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
19573c7e78d3SWeongyo Jeong 	int off, len, hdrlen, pktlen, rssi, ntodo;
19583c7e78d3SWeongyo Jeong 	uint8_t *data, status;
19593c7e78d3SWeongyo Jeong 	uint32_t readptr, writeptr;
19603c7e78d3SWeongyo Jeong 
19613c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RX_PROC,
19623c7e78d3SWeongyo Jeong 	    "%s: pending %u rdptr(0x%x) 0x%x wrptr(0x%x) 0x%x\n",
19633c7e78d3SWeongyo Jeong 	    __func__, npending,
19643c7e78d3SWeongyo Jeong 	    sc->malo_hwspecs.rxdesc_read,
19653c7e78d3SWeongyo Jeong 	    malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read),
19663c7e78d3SWeongyo Jeong 	    sc->malo_hwspecs.rxdesc_write,
19673c7e78d3SWeongyo Jeong 	    malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write));
19683c7e78d3SWeongyo Jeong 
19693c7e78d3SWeongyo Jeong 	readptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read);
19703c7e78d3SWeongyo Jeong 	writeptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write);
19713c7e78d3SWeongyo Jeong 	if (readptr == writeptr)
19723c7e78d3SWeongyo Jeong 		return;
19733c7e78d3SWeongyo Jeong 
19743c7e78d3SWeongyo Jeong 	bf = sc->malo_rxnext;
1975b032f27cSSam Leffler 	for (ntodo = malo_rxquota; ntodo > 0 && readptr != writeptr; ntodo--) {
19763c7e78d3SWeongyo Jeong 		if (bf == NULL) {
19773c7e78d3SWeongyo Jeong 			bf = STAILQ_FIRST(&sc->malo_rxbuf);
19783c7e78d3SWeongyo Jeong 			break;
19793c7e78d3SWeongyo Jeong 		}
19803c7e78d3SWeongyo Jeong 		ds = bf->bf_desc;
19813c7e78d3SWeongyo Jeong 		if (bf->bf_m == NULL) {
19823c7e78d3SWeongyo Jeong 			/*
19833c7e78d3SWeongyo Jeong 			 * If data allocation failed previously there
19843c7e78d3SWeongyo Jeong 			 * will be no buffer; try again to re-populate it.
19853c7e78d3SWeongyo Jeong 			 * Note the firmware will not advance to the next
19863c7e78d3SWeongyo Jeong 			 * descriptor with a dma buffer so we must mimic
19873c7e78d3SWeongyo Jeong 			 * this or we'll get out of sync.
19883c7e78d3SWeongyo Jeong 			 */
19893c7e78d3SWeongyo Jeong 			DPRINTF(sc, MALO_DEBUG_ANY,
19903c7e78d3SWeongyo Jeong 			    "%s: rx buf w/o dma memory\n", __func__);
19913c7e78d3SWeongyo Jeong 			(void)malo_rxbuf_init(sc, bf);
19923c7e78d3SWeongyo Jeong 			break;
19933c7e78d3SWeongyo Jeong 		}
19943c7e78d3SWeongyo Jeong 		MALO_RXDESC_SYNC(sc, ds,
19953c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
19963c7e78d3SWeongyo Jeong 		if (ds->rxcontrol != MALO_RXD_CTRL_DMA_OWN)
19973c7e78d3SWeongyo Jeong 			break;
19983c7e78d3SWeongyo Jeong 
19993c7e78d3SWeongyo Jeong 		readptr = le32toh(ds->physnext);
20003c7e78d3SWeongyo Jeong 
20013c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
20023c7e78d3SWeongyo Jeong 		if (sc->malo_debug & MALO_DEBUG_RECV_DESC)
20033c7e78d3SWeongyo Jeong 			malo_printrxbuf(bf, 0);
20043c7e78d3SWeongyo Jeong #endif
20053c7e78d3SWeongyo Jeong 		status = ds->status;
20063c7e78d3SWeongyo Jeong 		if (status & MALO_RXD_STATUS_DECRYPT_ERR_MASK) {
20077a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
20083c7e78d3SWeongyo Jeong 			goto rx_next;
20093c7e78d3SWeongyo Jeong 		}
20103c7e78d3SWeongyo Jeong 		/*
20113c7e78d3SWeongyo Jeong 		 * Sync the data buffer.
20123c7e78d3SWeongyo Jeong 		 */
20133c7e78d3SWeongyo Jeong 		len = le16toh(ds->pktlen);
20143c7e78d3SWeongyo Jeong 		bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
20153c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTREAD);
20163c7e78d3SWeongyo Jeong 		/*
20173c7e78d3SWeongyo Jeong 		 * The 802.11 header is provided all or in part at the front;
20183c7e78d3SWeongyo Jeong 		 * use it to calculate the true size of the header that we'll
20193c7e78d3SWeongyo Jeong 		 * construct below.  We use this to figure out where to copy
20203c7e78d3SWeongyo Jeong 		 * payload prior to constructing the header.
20213c7e78d3SWeongyo Jeong 		 */
20223c7e78d3SWeongyo Jeong 		m = bf->bf_m;
2023c2ede4b3SMartin Blapp 		data = mtod(m, uint8_t *);
20243c7e78d3SWeongyo Jeong 		hdrlen = ieee80211_anyhdrsize(data + sizeof(uint16_t));
20253c7e78d3SWeongyo Jeong 		off = sizeof(uint16_t) + sizeof(struct ieee80211_frame_addr4);
20263c7e78d3SWeongyo Jeong 
20273c7e78d3SWeongyo Jeong 		/*
20283c7e78d3SWeongyo Jeong 		 * Calculate RSSI. XXX wrong
20293c7e78d3SWeongyo Jeong 		 */
20303c7e78d3SWeongyo Jeong 		rssi = 2 * ((int) ds->snr - ds->nf);	/* NB: .5 dBm  */
20313c7e78d3SWeongyo Jeong 		if (rssi > 100)
20323c7e78d3SWeongyo Jeong 			rssi = 100;
20333c7e78d3SWeongyo Jeong 
20343c7e78d3SWeongyo Jeong 		pktlen = hdrlen + (len - off);
20353c7e78d3SWeongyo Jeong 		/*
20363c7e78d3SWeongyo Jeong 		 * NB: we know our frame is at least as large as
20373c7e78d3SWeongyo Jeong 		 * IEEE80211_MIN_LEN because there is a 4-address frame at
20383c7e78d3SWeongyo Jeong 		 * the front.  Hence there's no need to vet the packet length.
20393c7e78d3SWeongyo Jeong 		 * If the frame in fact is too small it should be discarded
20403c7e78d3SWeongyo Jeong 		 * at the net80211 layer.
20413c7e78d3SWeongyo Jeong 		 */
20423c7e78d3SWeongyo Jeong 
20433c7e78d3SWeongyo Jeong 		/* XXX don't need mbuf, just dma buffer */
20443c7e78d3SWeongyo Jeong 		mnew = malo_getrxmbuf(sc, bf);
20453c7e78d3SWeongyo Jeong 		if (mnew == NULL) {
20467a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
20473c7e78d3SWeongyo Jeong 			goto rx_next;
20483c7e78d3SWeongyo Jeong 		}
20493c7e78d3SWeongyo Jeong 		/*
20503c7e78d3SWeongyo Jeong 		 * Attach the dma buffer to the mbuf; malo_rxbuf_init will
20513c7e78d3SWeongyo Jeong 		 * re-setup the rx descriptor using the replacement dma
20523c7e78d3SWeongyo Jeong 		 * buffer we just installed above.
20533c7e78d3SWeongyo Jeong 		 */
20543c7e78d3SWeongyo Jeong 		bf->bf_m = mnew;
20553c7e78d3SWeongyo Jeong 		m->m_data += off - hdrlen;
20563c7e78d3SWeongyo Jeong 		m->m_pkthdr.len = m->m_len = pktlen;
20573c7e78d3SWeongyo Jeong 
20583c7e78d3SWeongyo Jeong 		/*
20593c7e78d3SWeongyo Jeong 		 * Piece 802.11 header together.
20603c7e78d3SWeongyo Jeong 		 */
20613c7e78d3SWeongyo Jeong 		wh = mtod(m, struct ieee80211_qosframe *);
20623c7e78d3SWeongyo Jeong 		/* NB: don't need to do this sometimes but ... */
20633c7e78d3SWeongyo Jeong 		/* XXX special case so we can memcpy after m_devget? */
20643c7e78d3SWeongyo Jeong 		ovbcopy(data + sizeof(uint16_t), wh, hdrlen);
20653c7e78d3SWeongyo Jeong 		if (IEEE80211_QOS_HAS_SEQ(wh)) {
2066d6166defSAdrian Chadd 			if (IEEE80211_IS_DSTODS(wh)) {
20673c7e78d3SWeongyo Jeong 				wh4 = mtod(m,
20683c7e78d3SWeongyo Jeong 				    struct ieee80211_qosframe_addr4*);
20693c7e78d3SWeongyo Jeong 				*(uint16_t *)wh4->i_qos = ds->qosctrl;
20703c7e78d3SWeongyo Jeong 			} else {
20713c7e78d3SWeongyo Jeong 				*(uint16_t *)wh->i_qos = ds->qosctrl;
20723c7e78d3SWeongyo Jeong 			}
20733c7e78d3SWeongyo Jeong 		}
20745463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic)) {
20753c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_flags = 0;
20763c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_rate = ds->rate;
20773c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_antsignal = rssi;
20783c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_antnoise = ds->nf;
20793c7e78d3SWeongyo Jeong 		}
20803c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
20813c7e78d3SWeongyo Jeong 		if (IFF_DUMPPKTS_RECV(sc, wh)) {
20823c7e78d3SWeongyo Jeong 			ieee80211_dump_pkt(ic, mtod(m, caddr_t),
20833c7e78d3SWeongyo Jeong 			    len, ds->rate, rssi);
20843c7e78d3SWeongyo Jeong 		}
20853c7e78d3SWeongyo Jeong #endif
20863c7e78d3SWeongyo Jeong 		/* dispatch */
20873c7e78d3SWeongyo Jeong 		ni = ieee80211_find_rxnode(ic,
2088b032f27cSSam Leffler 		    (struct ieee80211_frame_min *)wh);
2089b032f27cSSam Leffler 		if (ni != NULL) {
20905463c4a4SSam Leffler 			(void) ieee80211_input(ni, m, rssi, ds->nf);
20913c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
2092b032f27cSSam Leffler 		} else
20935463c4a4SSam Leffler 			(void) ieee80211_input_all(ic, m, rssi, ds->nf);
20943c7e78d3SWeongyo Jeong rx_next:
20953c7e78d3SWeongyo Jeong 		/* NB: ignore ENOMEM so we process more descriptors */
20963c7e78d3SWeongyo Jeong 		(void) malo_rxbuf_init(sc, bf);
20973c7e78d3SWeongyo Jeong 		bf = STAILQ_NEXT(bf, bf_list);
20983c7e78d3SWeongyo Jeong 	}
20993c7e78d3SWeongyo Jeong 
21003c7e78d3SWeongyo Jeong 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read, readptr);
21013c7e78d3SWeongyo Jeong 	sc->malo_rxnext = bf;
21023c7e78d3SWeongyo Jeong 
21037a79cebfSGleb Smirnoff 	if (mbufq_first(&sc->malo_snd) != NULL)
21047a79cebfSGleb Smirnoff 		malo_start(sc);
21053c7e78d3SWeongyo Jeong }
21063c7e78d3SWeongyo Jeong 
21073c7e78d3SWeongyo Jeong /*
21083c7e78d3SWeongyo Jeong  * Reclaim all tx queue resources.
21093c7e78d3SWeongyo Jeong  */
21103c7e78d3SWeongyo Jeong static void
21113c7e78d3SWeongyo Jeong malo_tx_cleanup(struct malo_softc *sc)
21123c7e78d3SWeongyo Jeong {
21133c7e78d3SWeongyo Jeong 	int i;
21143c7e78d3SWeongyo Jeong 
21153c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
21163c7e78d3SWeongyo Jeong 		malo_tx_cleanupq(sc, &sc->malo_txq[i]);
21173c7e78d3SWeongyo Jeong }
21183c7e78d3SWeongyo Jeong 
21193c7e78d3SWeongyo Jeong int
21203c7e78d3SWeongyo Jeong malo_detach(struct malo_softc *sc)
21213c7e78d3SWeongyo Jeong {
21227a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
21233c7e78d3SWeongyo Jeong 
21247a79cebfSGleb Smirnoff 	malo_stop(sc);
21253c7e78d3SWeongyo Jeong 
21263c7e78d3SWeongyo Jeong 	if (sc->malo_tq != NULL) {
21273c7e78d3SWeongyo Jeong 		taskqueue_drain(sc->malo_tq, &sc->malo_rxtask);
21283c7e78d3SWeongyo Jeong 		taskqueue_drain(sc->malo_tq, &sc->malo_txtask);
21293c7e78d3SWeongyo Jeong 		taskqueue_free(sc->malo_tq);
21303c7e78d3SWeongyo Jeong 		sc->malo_tq = NULL;
21313c7e78d3SWeongyo Jeong 	}
21323c7e78d3SWeongyo Jeong 
21333c7e78d3SWeongyo Jeong 	/*
21343c7e78d3SWeongyo Jeong 	 * NB: the order of these is important:
21353c7e78d3SWeongyo Jeong 	 * o call the 802.11 layer before detaching the hal to
21363c7e78d3SWeongyo Jeong 	 *   insure callbacks into the driver to delete global
21373c7e78d3SWeongyo Jeong 	 *   key cache entries can be handled
21383c7e78d3SWeongyo Jeong 	 * o reclaim the tx queue data structures after calling
21393c7e78d3SWeongyo Jeong 	 *   the 802.11 layer as we'll get called back to reclaim
21403c7e78d3SWeongyo Jeong 	 *   node state and potentially want to use them
21413c7e78d3SWeongyo Jeong 	 * o to cleanup the tx queues the hal is called, so detach
21423c7e78d3SWeongyo Jeong 	 *   it last
21433c7e78d3SWeongyo Jeong 	 * Other than that, it's straightforward...
21443c7e78d3SWeongyo Jeong 	 */
2145b032f27cSSam Leffler 	ieee80211_ifdetach(ic);
21467cf545d0SJohn Baldwin 	callout_drain(&sc->malo_watchdog_timer);
21473c7e78d3SWeongyo Jeong 	malo_dma_cleanup(sc);
21483c7e78d3SWeongyo Jeong 	malo_tx_cleanup(sc);
21493c7e78d3SWeongyo Jeong 	malo_hal_detach(sc->malo_mh);
21507a79cebfSGleb Smirnoff 	mbufq_drain(&sc->malo_snd);
21513c7e78d3SWeongyo Jeong 	MALO_LOCK_DESTROY(sc);
21523c7e78d3SWeongyo Jeong 
21533c7e78d3SWeongyo Jeong 	return 0;
21543c7e78d3SWeongyo Jeong }
21553c7e78d3SWeongyo Jeong 
21563c7e78d3SWeongyo Jeong void
21573c7e78d3SWeongyo Jeong malo_shutdown(struct malo_softc *sc)
21583c7e78d3SWeongyo Jeong {
21597a79cebfSGleb Smirnoff 
21607a79cebfSGleb Smirnoff 	malo_stop(sc);
21613c7e78d3SWeongyo Jeong }
21623c7e78d3SWeongyo Jeong 
21633c7e78d3SWeongyo Jeong void
21643c7e78d3SWeongyo Jeong malo_suspend(struct malo_softc *sc)
21653c7e78d3SWeongyo Jeong {
21663c7e78d3SWeongyo Jeong 
21677a79cebfSGleb Smirnoff 	malo_stop(sc);
21683c7e78d3SWeongyo Jeong }
21693c7e78d3SWeongyo Jeong 
21703c7e78d3SWeongyo Jeong void
21713c7e78d3SWeongyo Jeong malo_resume(struct malo_softc *sc)
21723c7e78d3SWeongyo Jeong {
21733c7e78d3SWeongyo Jeong 
21747a79cebfSGleb Smirnoff 	if (sc->malo_ic.ic_nrunning > 0)
21753c7e78d3SWeongyo Jeong 		malo_init(sc);
21763c7e78d3SWeongyo Jeong }
2177