xref: /freebsd/sys/dev/malo/if_malo.c (revision c249cc3822dc002288700ee206cf28c0c6031449)
13c7e78d3SWeongyo Jeong /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro 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 
34b032f27cSSam Leffler #include "opt_malo.h"
35b032f27cSSam Leffler 
363c7e78d3SWeongyo Jeong #include <sys/param.h>
373c7e78d3SWeongyo Jeong #include <sys/endian.h>
383c7e78d3SWeongyo Jeong #include <sys/kernel.h>
398ec07310SGleb Smirnoff #include <sys/malloc.h>
403c7e78d3SWeongyo Jeong #include <sys/socket.h>
413c7e78d3SWeongyo Jeong #include <sys/sockio.h>
423c7e78d3SWeongyo Jeong #include <sys/sysctl.h>
433c7e78d3SWeongyo Jeong #include <sys/taskqueue.h>
443c7e78d3SWeongyo Jeong 
453c7e78d3SWeongyo Jeong #include <machine/bus.h>
463c7e78d3SWeongyo Jeong #include <sys/bus.h>
473c7e78d3SWeongyo Jeong 
483c7e78d3SWeongyo Jeong #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
503c7e78d3SWeongyo Jeong #include <net/if_dl.h>
513c7e78d3SWeongyo Jeong #include <net/if_media.h>
523c7e78d3SWeongyo Jeong #include <net/if_types.h>
533c7e78d3SWeongyo Jeong #include <net/ethernet.h>
543c7e78d3SWeongyo Jeong 
553c7e78d3SWeongyo Jeong #include <net80211/ieee80211_var.h>
563c7e78d3SWeongyo Jeong #include <net80211/ieee80211_regdomain.h>
573c7e78d3SWeongyo Jeong 
583c7e78d3SWeongyo Jeong #include <net/bpf.h>
593c7e78d3SWeongyo Jeong 
603c7e78d3SWeongyo Jeong #include <dev/malo/if_malo.h>
613c7e78d3SWeongyo Jeong 
627029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, malo, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
633c7e78d3SWeongyo Jeong     "Marvell 88w8335 driver parameters");
643c7e78d3SWeongyo Jeong 
653c7e78d3SWeongyo Jeong static	int malo_txcoalesce = 8;	/* # tx pkts to q before poking f/w*/
66af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, txcoalesce, CTLFLAG_RWTUN, &malo_txcoalesce,
673c7e78d3SWeongyo Jeong 	    0, "tx buffers to send at once");
683c7e78d3SWeongyo Jeong static	int malo_rxbuf = MALO_RXBUF;		/* # rx buffers to allocate */
69af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &malo_rxbuf,
703c7e78d3SWeongyo Jeong 	    0, "rx buffers allocated");
713c7e78d3SWeongyo Jeong static	int malo_rxquota = MALO_RXBUF;		/* # max buffers to process */
72af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, rxquota, CTLFLAG_RWTUN, &malo_rxquota,
733c7e78d3SWeongyo Jeong 	    0, "max rx buffers to process per interrupt");
743c7e78d3SWeongyo Jeong static	int malo_txbuf = MALO_TXBUF;		/* # tx buffers to allocate */
75af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, txbuf, CTLFLAG_RWTUN, &malo_txbuf,
763c7e78d3SWeongyo Jeong 	    0, "tx buffers allocated");
773c7e78d3SWeongyo Jeong 
783c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
793c7e78d3SWeongyo Jeong static	int malo_debug = 0;
80af3b2549SHans Petter Selasky SYSCTL_INT(_hw_malo, OID_AUTO, debug, CTLFLAG_RWTUN, &malo_debug,
813c7e78d3SWeongyo Jeong 	    0, "control debugging printfs");
823c7e78d3SWeongyo Jeong enum {
833c7e78d3SWeongyo Jeong 	MALO_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
843c7e78d3SWeongyo Jeong 	MALO_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
853c7e78d3SWeongyo Jeong 	MALO_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
863c7e78d3SWeongyo Jeong 	MALO_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
873c7e78d3SWeongyo Jeong 	MALO_DEBUG_RESET	= 0x00000010,	/* reset processing */
883c7e78d3SWeongyo Jeong 	MALO_DEBUG_INTR		= 0x00000040,	/* ISR */
893c7e78d3SWeongyo Jeong 	MALO_DEBUG_TX_PROC	= 0x00000080,	/* tx ISR proc */
903c7e78d3SWeongyo Jeong 	MALO_DEBUG_RX_PROC	= 0x00000100,	/* rx ISR proc */
913c7e78d3SWeongyo Jeong 	MALO_DEBUG_STATE	= 0x00000400,	/* 802.11 state transitions */
923c7e78d3SWeongyo Jeong 	MALO_DEBUG_NODE		= 0x00000800,	/* node management */
933c7e78d3SWeongyo Jeong 	MALO_DEBUG_RECV_ALL	= 0x00001000,	/* trace all frames (beacons) */
943c7e78d3SWeongyo Jeong 	MALO_DEBUG_FW		= 0x00008000,	/* firmware */
953c7e78d3SWeongyo Jeong 	MALO_DEBUG_ANY		= 0xffffffff
963c7e78d3SWeongyo Jeong };
973c7e78d3SWeongyo Jeong #define	IFF_DUMPPKTS_RECV(sc, wh)					\
983c7e78d3SWeongyo Jeong 	(((sc->malo_debug & MALO_DEBUG_RECV) &&				\
99*c249cc38SAdrian Chadd 	  ((sc->malo_debug & MALO_DEBUG_RECV_ALL) || !IEEE80211_IS_MGMT_BEACON(wh))))
1003c7e78d3SWeongyo Jeong #define	IFF_DUMPPKTS_XMIT(sc)						\
1017a79cebfSGleb Smirnoff 	(sc->malo_debug & MALO_DEBUG_XMIT)
1023c7e78d3SWeongyo Jeong #define	DPRINTF(sc, m, fmt, ...) do {				\
1033c7e78d3SWeongyo Jeong 	if (sc->malo_debug & (m))				\
1043c7e78d3SWeongyo Jeong 		printf(fmt, __VA_ARGS__);			\
1053c7e78d3SWeongyo Jeong } while (0)
1063c7e78d3SWeongyo Jeong #else
1073c7e78d3SWeongyo Jeong #define	DPRINTF(sc, m, fmt, ...) do {				\
1083c7e78d3SWeongyo Jeong 	(void) sc;						\
1093c7e78d3SWeongyo Jeong } while (0)
1103c7e78d3SWeongyo Jeong #endif
1113c7e78d3SWeongyo Jeong 
112d745c852SEd Schouten static MALLOC_DEFINE(M_MALODEV, "malodev", "malo driver dma buffers");
1133c7e78d3SWeongyo Jeong 
114fcd9500fSBernhard Schmidt static struct ieee80211vap *malo_vap_create(struct ieee80211com *,
115fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
116fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
117fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
118b032f27cSSam Leffler static  void	malo_vap_delete(struct ieee80211vap *);
1193c7e78d3SWeongyo Jeong static	int	malo_dma_setup(struct malo_softc *);
1203c7e78d3SWeongyo Jeong static	int	malo_setup_hwdma(struct malo_softc *);
1213c7e78d3SWeongyo Jeong static	void	malo_txq_init(struct malo_softc *, struct malo_txq *, int);
1223c7e78d3SWeongyo Jeong static	void	malo_tx_cleanupq(struct malo_softc *, struct malo_txq *);
1237a79cebfSGleb Smirnoff static	void	malo_parent(struct ieee80211com *);
1247a79cebfSGleb Smirnoff static	int	malo_transmit(struct ieee80211com *, struct mbuf *);
1257a79cebfSGleb Smirnoff static	void	malo_start(struct malo_softc *);
1267cf545d0SJohn Baldwin static	void	malo_watchdog(void *);
127272f6adeSGleb Smirnoff static	void	malo_updateslot(struct ieee80211com *);
128b032f27cSSam Leffler static	int	malo_newstate(struct ieee80211vap *, enum ieee80211_state, int);
1293c7e78d3SWeongyo Jeong static	void	malo_scan_start(struct ieee80211com *);
1303c7e78d3SWeongyo Jeong static	void	malo_scan_end(struct ieee80211com *);
1313c7e78d3SWeongyo Jeong static	void	malo_set_channel(struct ieee80211com *);
1323c7e78d3SWeongyo Jeong static	int	malo_raw_xmit(struct ieee80211_node *, struct mbuf *,
1333c7e78d3SWeongyo Jeong 		    const struct ieee80211_bpf_params *);
1343c7e78d3SWeongyo Jeong static	void	malo_sysctlattach(struct malo_softc *);
1353c7e78d3SWeongyo Jeong static	void	malo_announce(struct malo_softc *);
1363c7e78d3SWeongyo Jeong static	void	malo_dma_cleanup(struct malo_softc *);
1377a79cebfSGleb Smirnoff static	void	malo_stop(struct malo_softc *);
1383c7e78d3SWeongyo Jeong static	int	malo_chan_set(struct malo_softc *, struct ieee80211_channel *);
1393c7e78d3SWeongyo Jeong static	int	malo_mode_init(struct malo_softc *);
1403c7e78d3SWeongyo Jeong static	void	malo_tx_proc(void *, int);
1413c7e78d3SWeongyo Jeong static	void	malo_rx_proc(void *, int);
1423c7e78d3SWeongyo Jeong static	void	malo_init(void *);
1433c7e78d3SWeongyo Jeong 
1443c7e78d3SWeongyo Jeong /*
1453c7e78d3SWeongyo Jeong  * Read/Write shorthands for accesses to BAR 0.  Note that all BAR 1
1463c7e78d3SWeongyo Jeong  * operations are done in the "hal" except getting H/W MAC address at
1473c7e78d3SWeongyo Jeong  * malo_attach and there should be no reference to them here.
1483c7e78d3SWeongyo Jeong  */
1493c7e78d3SWeongyo Jeong static uint32_t
malo_bar0_read4(struct malo_softc * sc,bus_size_t off)1503c7e78d3SWeongyo Jeong malo_bar0_read4(struct malo_softc *sc, bus_size_t off)
1513c7e78d3SWeongyo Jeong {
1523c7e78d3SWeongyo Jeong 	return bus_space_read_4(sc->malo_io0t, sc->malo_io0h, off);
1533c7e78d3SWeongyo Jeong }
1543c7e78d3SWeongyo Jeong 
1553c7e78d3SWeongyo Jeong static void
malo_bar0_write4(struct malo_softc * sc,bus_size_t off,uint32_t val)1563c7e78d3SWeongyo Jeong malo_bar0_write4(struct malo_softc *sc, bus_size_t off, uint32_t val)
1573c7e78d3SWeongyo Jeong {
15868d9da91SWarner Losh 	DPRINTF(sc, MALO_DEBUG_FW, "%s: off 0x%jx val 0x%x\n",
1592706e872SMarius Strobl 	    __func__, (uintmax_t)off, val);
1603c7e78d3SWeongyo Jeong 
1613c7e78d3SWeongyo Jeong 	bus_space_write_4(sc->malo_io0t, sc->malo_io0h, off, val);
1623c7e78d3SWeongyo Jeong }
1633c7e78d3SWeongyo Jeong 
1643c7e78d3SWeongyo Jeong int
malo_attach(uint16_t devid,struct malo_softc * sc)1653c7e78d3SWeongyo Jeong malo_attach(uint16_t devid, struct malo_softc *sc)
1663c7e78d3SWeongyo Jeong {
1677a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
168ba2c1fbcSAdrian Chadd 	struct malo_hal *mh;
1697a79cebfSGleb Smirnoff 	int error;
170a061fea6SAndriy Voskoboinyk 	uint8_t bands[IEEE80211_MODE_BYTES];
1713c7e78d3SWeongyo Jeong 
1723c7e78d3SWeongyo Jeong 	MALO_LOCK_INIT(sc);
1737cf545d0SJohn Baldwin 	callout_init_mtx(&sc->malo_watchdog_timer, &sc->malo_mtx, 0);
1747a79cebfSGleb Smirnoff 	mbufq_init(&sc->malo_snd, ifqmaxlen);
1753c7e78d3SWeongyo Jeong 
1763c7e78d3SWeongyo Jeong 	mh = malo_hal_attach(sc->malo_dev, devid,
1773c7e78d3SWeongyo Jeong 	    sc->malo_io1h, sc->malo_io1t, sc->malo_dmat);
1783c7e78d3SWeongyo Jeong 	if (mh == NULL) {
1797a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "unable to attach HAL\n");
1803c7e78d3SWeongyo Jeong 		error = EIO;
1813c7e78d3SWeongyo Jeong 		goto bad;
1823c7e78d3SWeongyo Jeong 	}
1833c7e78d3SWeongyo Jeong 	sc->malo_mh = mh;
1843c7e78d3SWeongyo Jeong 
185b032f27cSSam Leffler 	/*
186b032f27cSSam Leffler 	 * Load firmware so we can get setup.  We arbitrarily pick station
187b032f27cSSam Leffler 	 * firmware; we'll re-load firmware as needed so setting up
188b032f27cSSam Leffler 	 * the wrong mode isn't a big deal.
189b032f27cSSam Leffler 	 */
190b032f27cSSam Leffler 	error = malo_hal_fwload(mh, "malo8335-h", "malo8335-m");
191b032f27cSSam Leffler 	if (error != 0) {
1927a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "unable to setup firmware\n");
193b032f27cSSam Leffler 		goto bad1;
194b032f27cSSam Leffler 	}
195b032f27cSSam Leffler 	/* XXX gethwspecs() extracts correct informations?  not maybe!  */
196b032f27cSSam Leffler 	error = malo_hal_gethwspecs(mh, &sc->malo_hwspecs);
197b032f27cSSam Leffler 	if (error != 0) {
1987a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "unable to fetch h/w specs\n");
199b032f27cSSam Leffler 		goto bad1;
200b032f27cSSam Leffler 	}
201b032f27cSSam Leffler 
202b032f27cSSam Leffler 	DPRINTF(sc, MALO_DEBUG_FW,
203b032f27cSSam Leffler 	    "malo_hal_gethwspecs: hwversion 0x%x hostif 0x%x"
204b032f27cSSam Leffler 	    "maxnum_wcb 0x%x maxnum_mcaddr 0x%x maxnum_tx_wcb 0x%x"
205b032f27cSSam Leffler 	    "regioncode 0x%x num_antenna 0x%x fw_releasenum 0x%x"
206b032f27cSSam Leffler 	    "wcbbase0 0x%x rxdesc_read 0x%x rxdesc_write 0x%x"
207b032f27cSSam Leffler 	    "ul_fw_awakecookie 0x%x w[4] = %x %x %x %x",
208b032f27cSSam Leffler 	    sc->malo_hwspecs.hwversion,
209b032f27cSSam Leffler 	    sc->malo_hwspecs.hostinterface, sc->malo_hwspecs.maxnum_wcb,
210b032f27cSSam Leffler 	    sc->malo_hwspecs.maxnum_mcaddr, sc->malo_hwspecs.maxnum_tx_wcb,
211b032f27cSSam Leffler 	    sc->malo_hwspecs.regioncode, sc->malo_hwspecs.num_antenna,
212b032f27cSSam Leffler 	    sc->malo_hwspecs.fw_releasenum, sc->malo_hwspecs.wcbbase0,
213b032f27cSSam Leffler 	    sc->malo_hwspecs.rxdesc_read, sc->malo_hwspecs.rxdesc_write,
214b032f27cSSam Leffler 	    sc->malo_hwspecs.ul_fw_awakecookie,
215b032f27cSSam Leffler 	    sc->malo_hwspecs.wcbbase[0], sc->malo_hwspecs.wcbbase[1],
216b032f27cSSam Leffler 	    sc->malo_hwspecs.wcbbase[2], sc->malo_hwspecs.wcbbase[3]);
217b032f27cSSam Leffler 
218b032f27cSSam Leffler 	/* NB: firmware looks that it does not export regdomain info API.  */
2190046e186SAndriy Voskoboinyk 	memset(bands, 0, sizeof(bands));
2200046e186SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11B);
2210046e186SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11G);
2220046e186SAndriy Voskoboinyk 	ieee80211_init_channels(ic, NULL, bands);
223b032f27cSSam Leffler 
2243c7e78d3SWeongyo Jeong 	sc->malo_txantenna = 0x2;	/* h/w default */
2253c7e78d3SWeongyo Jeong 	sc->malo_rxantenna = 0xffff;	/* h/w default */
2263c7e78d3SWeongyo Jeong 
2273c7e78d3SWeongyo Jeong 	/*
2283c7e78d3SWeongyo Jeong 	 * Allocate tx + rx descriptors and populate the lists.
2293c7e78d3SWeongyo Jeong 	 * We immediately push the information to the firmware
2303c7e78d3SWeongyo Jeong 	 * as otherwise it gets upset.
2313c7e78d3SWeongyo Jeong 	 */
2323c7e78d3SWeongyo Jeong 	error = malo_dma_setup(sc);
2333c7e78d3SWeongyo Jeong 	if (error != 0) {
2347a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
2357a79cebfSGleb Smirnoff 		    "failed to setup descriptors: %d\n", error);
2363c7e78d3SWeongyo Jeong 		goto bad1;
2373c7e78d3SWeongyo Jeong 	}
238b032f27cSSam Leffler 	error = malo_setup_hwdma(sc);	/* push to firmware */
239b032f27cSSam Leffler 	if (error != 0)			/* NB: malo_setupdma prints msg */
240136694a4SWeongyo Jeong 		goto bad2;
2413c7e78d3SWeongyo Jeong 
2423c7e78d3SWeongyo Jeong 	sc->malo_tq = taskqueue_create_fast("malo_taskq", M_NOWAIT,
2433c7e78d3SWeongyo Jeong 		taskqueue_thread_enqueue, &sc->malo_tq);
2443c7e78d3SWeongyo Jeong 	taskqueue_start_threads(&sc->malo_tq, 1, PI_NET,
2457a79cebfSGleb Smirnoff 		"%s taskq", device_get_nameunit(sc->malo_dev));
2463c7e78d3SWeongyo Jeong 
2476c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->malo_rxtask, 0, malo_rx_proc, sc);
2483c7e78d3SWeongyo Jeong 	TASK_INIT(&sc->malo_txtask, 0, malo_tx_proc, sc);
2493c7e78d3SWeongyo Jeong 
25059686fe9SGleb Smirnoff 	ic->ic_softc = sc;
251c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(sc->malo_dev);
2523c7e78d3SWeongyo Jeong 	/* XXX not right but it's not used anywhere important */
2533c7e78d3SWeongyo Jeong 	ic->ic_phytype = IEEE80211_T_OFDM;
2543c7e78d3SWeongyo Jeong 	ic->ic_opmode = IEEE80211_M_STA;
2553c7e78d3SWeongyo Jeong 	ic->ic_caps =
256c43feedeSSam Leffler 	      IEEE80211_C_STA			/* station mode supported */
257c43feedeSSam Leffler 	    | IEEE80211_C_BGSCAN		/* capable of bg scanning */
2583c7e78d3SWeongyo Jeong 	    | IEEE80211_C_MONITOR		/* monitor mode */
2593c7e78d3SWeongyo Jeong 	    | IEEE80211_C_SHPREAMBLE		/* short preamble supported */
2603c7e78d3SWeongyo Jeong 	    | IEEE80211_C_SHSLOT		/* short slot time supported */
2613c7e78d3SWeongyo Jeong 	    | IEEE80211_C_TXPMGT		/* capable of txpow mgt */
2623c7e78d3SWeongyo Jeong 	    | IEEE80211_C_WPA			/* capable of WPA1+WPA2 */
2633c7e78d3SWeongyo Jeong 	    ;
2647a79cebfSGleb Smirnoff 	IEEE80211_ADDR_COPY(ic->ic_macaddr, sc->malo_hwspecs.macaddr);
2653c7e78d3SWeongyo Jeong 
2663c7e78d3SWeongyo Jeong 	/*
2673c7e78d3SWeongyo Jeong 	 * Transmit requires space in the packet for a special format transmit
2683c7e78d3SWeongyo Jeong 	 * record and optional padding between this record and the payload.
2693c7e78d3SWeongyo Jeong 	 * Ask the net80211 layer to arrange this when encapsulating
2703c7e78d3SWeongyo Jeong 	 * packets so we can add it efficiently.
2713c7e78d3SWeongyo Jeong 	 */
2723c7e78d3SWeongyo Jeong 	ic->ic_headroom = sizeof(struct malo_txrec) -
2733c7e78d3SWeongyo Jeong 		sizeof(struct ieee80211_frame);
2743c7e78d3SWeongyo Jeong 
2753c7e78d3SWeongyo Jeong 	/* call MI attach routine. */
2767a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
2773c7e78d3SWeongyo Jeong 	/* override default methods */
278b032f27cSSam Leffler 	ic->ic_vap_create = malo_vap_create;
279b032f27cSSam Leffler 	ic->ic_vap_delete = malo_vap_delete;
2803c7e78d3SWeongyo Jeong 	ic->ic_raw_xmit = malo_raw_xmit;
281b032f27cSSam Leffler 	ic->ic_updateslot = malo_updateslot;
2823c7e78d3SWeongyo Jeong 	ic->ic_scan_start = malo_scan_start;
2833c7e78d3SWeongyo Jeong 	ic->ic_scan_end = malo_scan_end;
2843c7e78d3SWeongyo Jeong 	ic->ic_set_channel = malo_set_channel;
2857a79cebfSGleb Smirnoff 	ic->ic_parent = malo_parent;
2867a79cebfSGleb Smirnoff 	ic->ic_transmit = malo_transmit;
2873c7e78d3SWeongyo Jeong 
2883c7e78d3SWeongyo Jeong 	sc->malo_invalid = 0;		/* ready to go, enable int handling */
2893c7e78d3SWeongyo Jeong 
2905463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
2915463c4a4SSam Leffler 	    &sc->malo_tx_th.wt_ihdr, sizeof(sc->malo_tx_th),
2925463c4a4SSam Leffler 		MALO_TX_RADIOTAP_PRESENT,
2935463c4a4SSam Leffler 	    &sc->malo_rx_th.wr_ihdr, sizeof(sc->malo_rx_th),
2945463c4a4SSam Leffler 		MALO_RX_RADIOTAP_PRESENT);
2953c7e78d3SWeongyo Jeong 
2963c7e78d3SWeongyo Jeong 	/*
2973c7e78d3SWeongyo Jeong 	 * Setup dynamic sysctl's.
2983c7e78d3SWeongyo Jeong 	 */
2993c7e78d3SWeongyo Jeong 	malo_sysctlattach(sc);
3003c7e78d3SWeongyo Jeong 
3013c7e78d3SWeongyo Jeong 	if (bootverbose)
3023c7e78d3SWeongyo Jeong 		ieee80211_announce(ic);
303b032f27cSSam Leffler 	malo_announce(sc);
3043c7e78d3SWeongyo Jeong 
3053c7e78d3SWeongyo Jeong 	return 0;
306136694a4SWeongyo Jeong bad2:
307136694a4SWeongyo Jeong 	malo_dma_cleanup(sc);
3083c7e78d3SWeongyo Jeong bad1:
3093c7e78d3SWeongyo Jeong 	malo_hal_detach(mh);
3103c7e78d3SWeongyo Jeong bad:
3113c7e78d3SWeongyo Jeong 	sc->malo_invalid = 1;
3123c7e78d3SWeongyo Jeong 
3133c7e78d3SWeongyo Jeong 	return error;
3143c7e78d3SWeongyo Jeong }
3153c7e78d3SWeongyo Jeong 
316b032f27cSSam Leffler static struct ieee80211vap *
malo_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])317fcd9500fSBernhard Schmidt malo_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
318fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
319b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
320b032f27cSSam Leffler     const uint8_t mac[IEEE80211_ADDR_LEN])
321b032f27cSSam Leffler {
3227a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
323b032f27cSSam Leffler 	struct malo_vap *mvp;
324b032f27cSSam Leffler 	struct ieee80211vap *vap;
325b032f27cSSam Leffler 
326b032f27cSSam Leffler 	if (!TAILQ_EMPTY(&ic->ic_vaps)) {
3277a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "multiple vaps not supported\n");
328b032f27cSSam Leffler 		return NULL;
329b032f27cSSam Leffler 	}
330b032f27cSSam Leffler 	switch (opmode) {
331b032f27cSSam Leffler 	case IEEE80211_M_STA:
332b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
333b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
334b032f27cSSam Leffler 		/* fall thru... */
335b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
336b032f27cSSam Leffler 		break;
337b032f27cSSam Leffler 	default:
3387a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "%s mode not supported\n",
339b032f27cSSam Leffler 		    ieee80211_opmode_name[opmode]);
340b032f27cSSam Leffler 		return NULL;		/* unsupported */
341b032f27cSSam Leffler 	}
3427a79cebfSGleb Smirnoff 	mvp = malloc(sizeof(struct malo_vap), M_80211_VAP, M_WAITOK | M_ZERO);
343b032f27cSSam Leffler 	vap = &mvp->malo_vap;
3447a79cebfSGleb Smirnoff 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
345b032f27cSSam Leffler 
346b032f27cSSam Leffler 	/* override state transition machine */
347b032f27cSSam Leffler 	mvp->malo_newstate = vap->iv_newstate;
348b032f27cSSam Leffler 	vap->iv_newstate = malo_newstate;
349b032f27cSSam Leffler 
350b032f27cSSam Leffler 	/* complete setup */
351b032f27cSSam Leffler 	ieee80211_vap_attach(vap,
3527a79cebfSGleb Smirnoff 	    ieee80211_media_change, ieee80211_media_status, mac);
353b032f27cSSam Leffler 	ic->ic_opmode = opmode;
354b032f27cSSam Leffler 	return vap;
355b032f27cSSam Leffler }
356b032f27cSSam Leffler 
357b032f27cSSam Leffler static void
malo_vap_delete(struct ieee80211vap * vap)358b032f27cSSam Leffler malo_vap_delete(struct ieee80211vap *vap)
359b032f27cSSam Leffler {
360b032f27cSSam Leffler 	struct malo_vap *mvp = MALO_VAP(vap);
361b032f27cSSam Leffler 
362b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
363b032f27cSSam Leffler 	free(mvp, M_80211_VAP);
364b032f27cSSam Leffler }
365b032f27cSSam Leffler 
3663c7e78d3SWeongyo Jeong int
malo_intr(void * arg)3673c7e78d3SWeongyo Jeong malo_intr(void *arg)
3683c7e78d3SWeongyo Jeong {
3693c7e78d3SWeongyo Jeong 	struct malo_softc *sc = arg;
3703c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
3713c7e78d3SWeongyo Jeong 	uint32_t status;
3723c7e78d3SWeongyo Jeong 
3733c7e78d3SWeongyo Jeong 	if (sc->malo_invalid) {
3743c7e78d3SWeongyo Jeong 		/*
3753c7e78d3SWeongyo Jeong 		 * The hardware is not ready/present, don't touch anything.
3763c7e78d3SWeongyo Jeong 		 * Note this can happen early on if the IRQ is shared.
3773c7e78d3SWeongyo Jeong 		 */
3783c7e78d3SWeongyo Jeong 		DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
3793c7e78d3SWeongyo Jeong 		return (FILTER_STRAY);
3803c7e78d3SWeongyo Jeong 	}
3813c7e78d3SWeongyo Jeong 
3823c7e78d3SWeongyo Jeong 	/*
3833c7e78d3SWeongyo Jeong 	 * Figure out the reason(s) for the interrupt.
3843c7e78d3SWeongyo Jeong 	 */
3853c7e78d3SWeongyo Jeong 	malo_hal_getisr(mh, &status);		/* NB: clears ISR too */
3863c7e78d3SWeongyo Jeong 	if (status == 0)			/* must be a shared irq */
3873c7e78d3SWeongyo Jeong 		return (FILTER_STRAY);
3883c7e78d3SWeongyo Jeong 
3893c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_INTR, "%s: status 0x%x imask 0x%x\n",
3903c7e78d3SWeongyo Jeong 	    __func__, status, sc->malo_imask);
3913c7e78d3SWeongyo Jeong 
3923c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_RX_RDY)
393cbc4d2dbSJohn Baldwin 		taskqueue_enqueue(sc->malo_tq, &sc->malo_rxtask);
3943c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_TX_DONE)
395cbc4d2dbSJohn Baldwin 		taskqueue_enqueue(sc->malo_tq, &sc->malo_txtask);
3963c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_OPC_DONE)
3973c7e78d3SWeongyo Jeong 		malo_hal_cmddone(mh);
3983c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_MAC_EVENT)
3993c7e78d3SWeongyo Jeong 		;
4003c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_RX_PROBLEM)
4013c7e78d3SWeongyo Jeong 		;
4023c7e78d3SWeongyo Jeong 	if (status & MALO_A2HRIC_BIT_ICV_ERROR) {
4033c7e78d3SWeongyo Jeong 		/* TKIP ICV error */
4043c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_rx_badtkipicv++;
4053c7e78d3SWeongyo Jeong 	}
4063c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
4073c7e78d3SWeongyo Jeong 	if (((status | sc->malo_imask) ^ sc->malo_imask) != 0)
4083c7e78d3SWeongyo Jeong 		DPRINTF(sc, MALO_DEBUG_INTR,
4093c7e78d3SWeongyo Jeong 		    "%s: can't handle interrupt status 0x%x\n",
4103c7e78d3SWeongyo Jeong 		    __func__, status);
4113c7e78d3SWeongyo Jeong #endif
4123c7e78d3SWeongyo Jeong 	return (FILTER_HANDLED);
4133c7e78d3SWeongyo Jeong }
4143c7e78d3SWeongyo Jeong 
4153c7e78d3SWeongyo Jeong static void
malo_load_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)4163c7e78d3SWeongyo Jeong malo_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
4173c7e78d3SWeongyo Jeong {
4183c7e78d3SWeongyo Jeong 	bus_addr_t *paddr = (bus_addr_t*) arg;
4193c7e78d3SWeongyo Jeong 
4203c7e78d3SWeongyo Jeong 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
4213c7e78d3SWeongyo Jeong 
4223c7e78d3SWeongyo Jeong 	*paddr = segs->ds_addr;
4233c7e78d3SWeongyo Jeong }
4243c7e78d3SWeongyo Jeong 
4253c7e78d3SWeongyo Jeong static int
malo_desc_setup(struct malo_softc * sc,const char * name,struct malo_descdma * dd,int nbuf,size_t bufsize,int ndesc,size_t descsize)4263c7e78d3SWeongyo Jeong malo_desc_setup(struct malo_softc *sc, const char *name,
4273c7e78d3SWeongyo Jeong     struct malo_descdma *dd,
4283c7e78d3SWeongyo Jeong     int nbuf, size_t bufsize, int ndesc, size_t descsize)
4293c7e78d3SWeongyo Jeong {
4303c7e78d3SWeongyo Jeong 	int error;
4313c7e78d3SWeongyo Jeong 	uint8_t *ds;
4323c7e78d3SWeongyo Jeong 
4333c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RESET,
4343c7e78d3SWeongyo Jeong 	    "%s: %s DMA: %u bufs (%ju) %u desc/buf (%ju)\n",
4353c7e78d3SWeongyo Jeong 	    __func__, name, nbuf, (uintmax_t) bufsize,
4363c7e78d3SWeongyo Jeong 	    ndesc, (uintmax_t) descsize);
4373c7e78d3SWeongyo Jeong 
4383c7e78d3SWeongyo Jeong 	dd->dd_name = name;
4393c7e78d3SWeongyo Jeong 	dd->dd_desc_len = nbuf * ndesc * descsize;
4403c7e78d3SWeongyo Jeong 
4413c7e78d3SWeongyo Jeong 	/*
4423c7e78d3SWeongyo Jeong 	 * Setup DMA descriptor area.
4433c7e78d3SWeongyo Jeong 	 */
4443c7e78d3SWeongyo Jeong 	error = bus_dma_tag_create(bus_get_dma_tag(sc->malo_dev),/* parent */
4453c7e78d3SWeongyo Jeong 		       PAGE_SIZE, 0,		/* alignment, bounds */
4463c7e78d3SWeongyo Jeong 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
4473c7e78d3SWeongyo Jeong 		       BUS_SPACE_MAXADDR,	/* highaddr */
4483c7e78d3SWeongyo Jeong 		       NULL, NULL,		/* filter, filterarg */
4493c7e78d3SWeongyo Jeong 		       dd->dd_desc_len,		/* maxsize */
4503c7e78d3SWeongyo Jeong 		       1,			/* nsegments */
4513c7e78d3SWeongyo Jeong 		       dd->dd_desc_len,		/* maxsegsize */
4523c7e78d3SWeongyo Jeong 		       BUS_DMA_ALLOCNOW,	/* flags */
4533c7e78d3SWeongyo Jeong 		       NULL,			/* lockfunc */
4543c7e78d3SWeongyo Jeong 		       NULL,			/* lockarg */
4553c7e78d3SWeongyo Jeong 		       &dd->dd_dmat);
4563c7e78d3SWeongyo Jeong 	if (error != 0) {
4577a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "cannot allocate %s DMA tag\n",
4587a79cebfSGleb Smirnoff 		    dd->dd_name);
4593c7e78d3SWeongyo Jeong 		return error;
4603c7e78d3SWeongyo Jeong 	}
4613c7e78d3SWeongyo Jeong 
4623c7e78d3SWeongyo Jeong 	/* allocate descriptors */
4633c7e78d3SWeongyo Jeong 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
4643c7e78d3SWeongyo Jeong 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dd->dd_dmamap);
4653c7e78d3SWeongyo Jeong 	if (error != 0) {
4667a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
4677a79cebfSGleb Smirnoff 		    "unable to alloc memory for %u %s descriptors, "
4683c7e78d3SWeongyo Jeong 		    "error %u\n", nbuf * ndesc, dd->dd_name, error);
4693c7e78d3SWeongyo Jeong 		goto fail1;
4703c7e78d3SWeongyo Jeong 	}
4713c7e78d3SWeongyo Jeong 
4723c7e78d3SWeongyo Jeong 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
4733c7e78d3SWeongyo Jeong 	    dd->dd_desc, dd->dd_desc_len,
4743c7e78d3SWeongyo Jeong 	    malo_load_cb, &dd->dd_desc_paddr, BUS_DMA_NOWAIT);
4753c7e78d3SWeongyo Jeong 	if (error != 0) {
4767a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
4777a79cebfSGleb Smirnoff 		    "unable to map %s descriptors, error %u\n",
4783c7e78d3SWeongyo Jeong 		    dd->dd_name, error);
4793c7e78d3SWeongyo Jeong 		goto fail2;
4803c7e78d3SWeongyo Jeong 	}
4813c7e78d3SWeongyo Jeong 
4823c7e78d3SWeongyo Jeong 	ds = dd->dd_desc;
4833c7e78d3SWeongyo Jeong 	memset(ds, 0, dd->dd_desc_len);
4842706e872SMarius Strobl 	DPRINTF(sc, MALO_DEBUG_RESET,
4852706e872SMarius Strobl 	    "%s: %s DMA map: %p (%lu) -> 0x%jx (%lu)\n",
4863c7e78d3SWeongyo Jeong 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
4872706e872SMarius Strobl 	    (uintmax_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
4883c7e78d3SWeongyo Jeong 
4893c7e78d3SWeongyo Jeong 	return 0;
4903c7e78d3SWeongyo Jeong fail2:
4913c7e78d3SWeongyo Jeong 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
4923c7e78d3SWeongyo Jeong fail1:
4933c7e78d3SWeongyo Jeong 	bus_dma_tag_destroy(dd->dd_dmat);
4943c7e78d3SWeongyo Jeong 	memset(dd, 0, sizeof(*dd));
4953c7e78d3SWeongyo Jeong 	return error;
4963c7e78d3SWeongyo Jeong }
4973c7e78d3SWeongyo Jeong 
4983c7e78d3SWeongyo Jeong #define	DS2PHYS(_dd, _ds) \
4993c7e78d3SWeongyo Jeong 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
5003c7e78d3SWeongyo Jeong 
5013c7e78d3SWeongyo Jeong static int
malo_rxdma_setup(struct malo_softc * sc)5023c7e78d3SWeongyo Jeong malo_rxdma_setup(struct malo_softc *sc)
5033c7e78d3SWeongyo Jeong {
5043c7e78d3SWeongyo Jeong 	int error, bsize, i;
5053c7e78d3SWeongyo Jeong 	struct malo_rxbuf *bf;
5063c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
5073c7e78d3SWeongyo Jeong 
5083c7e78d3SWeongyo Jeong 	error = malo_desc_setup(sc, "rx", &sc->malo_rxdma,
5093c7e78d3SWeongyo Jeong 	    malo_rxbuf, sizeof(struct malo_rxbuf),
5103c7e78d3SWeongyo Jeong 	    1, sizeof(struct malo_rxdesc));
5113c7e78d3SWeongyo Jeong 	if (error != 0)
5123c7e78d3SWeongyo Jeong 		return error;
5133c7e78d3SWeongyo Jeong 
5143c7e78d3SWeongyo Jeong 	/*
5153c7e78d3SWeongyo Jeong 	 * Allocate rx buffers and set them up.
5163c7e78d3SWeongyo Jeong 	 */
5173c7e78d3SWeongyo Jeong 	bsize = malo_rxbuf * sizeof(struct malo_rxbuf);
5183c7e78d3SWeongyo Jeong 	bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
5193c7e78d3SWeongyo Jeong 	if (bf == NULL) {
5207a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
5217a79cebfSGleb Smirnoff 		    "malloc of %u rx buffers failed\n", bsize);
5223c7e78d3SWeongyo Jeong 		return error;
5233c7e78d3SWeongyo Jeong 	}
5243c7e78d3SWeongyo Jeong 	sc->malo_rxdma.dd_bufptr = bf;
5253c7e78d3SWeongyo Jeong 
5263c7e78d3SWeongyo Jeong 	STAILQ_INIT(&sc->malo_rxbuf);
5273c7e78d3SWeongyo Jeong 	ds = sc->malo_rxdma.dd_desc;
5283c7e78d3SWeongyo Jeong 	for (i = 0; i < malo_rxbuf; i++, bf++, ds++) {
5293c7e78d3SWeongyo Jeong 		bf->bf_desc = ds;
5303c7e78d3SWeongyo Jeong 		bf->bf_daddr = DS2PHYS(&sc->malo_rxdma, ds);
5313c7e78d3SWeongyo Jeong 		error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
5323c7e78d3SWeongyo Jeong 		    &bf->bf_dmamap);
5333c7e78d3SWeongyo Jeong 		if (error != 0) {
5347a79cebfSGleb Smirnoff 			device_printf(sc->malo_dev,
5357a79cebfSGleb Smirnoff 			    "%s: unable to dmamap for rx buffer, error %d\n",
5367a79cebfSGleb Smirnoff 			    __func__, error);
5373c7e78d3SWeongyo Jeong 			return error;
5383c7e78d3SWeongyo Jeong 		}
5393c7e78d3SWeongyo Jeong 		/* NB: tail is intentional to preserve descriptor order */
5403c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&sc->malo_rxbuf, bf, bf_list);
5413c7e78d3SWeongyo Jeong 	}
5423c7e78d3SWeongyo Jeong 	return 0;
5433c7e78d3SWeongyo Jeong }
5443c7e78d3SWeongyo Jeong 
5453c7e78d3SWeongyo Jeong static int
malo_txdma_setup(struct malo_softc * sc,struct malo_txq * txq)5463c7e78d3SWeongyo Jeong malo_txdma_setup(struct malo_softc *sc, struct malo_txq *txq)
5473c7e78d3SWeongyo Jeong {
5483c7e78d3SWeongyo Jeong 	int error, bsize, i;
5493c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
5503c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
5513c7e78d3SWeongyo Jeong 
5523c7e78d3SWeongyo Jeong 	error = malo_desc_setup(sc, "tx", &txq->dma,
5533c7e78d3SWeongyo Jeong 	    malo_txbuf, sizeof(struct malo_txbuf),
5543c7e78d3SWeongyo Jeong 	    MALO_TXDESC, sizeof(struct malo_txdesc));
5553c7e78d3SWeongyo Jeong 	if (error != 0)
5563c7e78d3SWeongyo Jeong 		return error;
5573c7e78d3SWeongyo Jeong 
5583c7e78d3SWeongyo Jeong 	/* allocate and setup tx buffers */
5593c7e78d3SWeongyo Jeong 	bsize = malo_txbuf * sizeof(struct malo_txbuf);
5603c7e78d3SWeongyo Jeong 	bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
5613c7e78d3SWeongyo Jeong 	if (bf == NULL) {
5627a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "malloc of %u tx buffers failed\n",
5633c7e78d3SWeongyo Jeong 		    malo_txbuf);
5643c7e78d3SWeongyo Jeong 		return ENOMEM;
5653c7e78d3SWeongyo Jeong 	}
5663c7e78d3SWeongyo Jeong 	txq->dma.dd_bufptr = bf;
5673c7e78d3SWeongyo Jeong 
5683c7e78d3SWeongyo Jeong 	STAILQ_INIT(&txq->free);
5693c7e78d3SWeongyo Jeong 	txq->nfree = 0;
5703c7e78d3SWeongyo Jeong 	ds = txq->dma.dd_desc;
5713c7e78d3SWeongyo Jeong 	for (i = 0; i < malo_txbuf; i++, bf++, ds += MALO_TXDESC) {
5723c7e78d3SWeongyo Jeong 		bf->bf_desc = ds;
5733c7e78d3SWeongyo Jeong 		bf->bf_daddr = DS2PHYS(&txq->dma, ds);
5743c7e78d3SWeongyo Jeong 		error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
5753c7e78d3SWeongyo Jeong 		    &bf->bf_dmamap);
5763c7e78d3SWeongyo Jeong 		if (error != 0) {
5777a79cebfSGleb Smirnoff 			device_printf(sc->malo_dev,
5787a79cebfSGleb Smirnoff 			    "unable to create dmamap for tx "
5793c7e78d3SWeongyo Jeong 			    "buffer %u, error %u\n", i, error);
5803c7e78d3SWeongyo Jeong 			return error;
5813c7e78d3SWeongyo Jeong 		}
5823c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
5833c7e78d3SWeongyo Jeong 		txq->nfree++;
5843c7e78d3SWeongyo Jeong 	}
5853c7e78d3SWeongyo Jeong 
5863c7e78d3SWeongyo Jeong 	return 0;
5873c7e78d3SWeongyo Jeong }
5883c7e78d3SWeongyo Jeong 
5893c7e78d3SWeongyo Jeong static void
malo_desc_cleanup(struct malo_softc * sc,struct malo_descdma * dd)5903c7e78d3SWeongyo Jeong malo_desc_cleanup(struct malo_softc *sc, struct malo_descdma *dd)
5913c7e78d3SWeongyo Jeong {
5923c7e78d3SWeongyo Jeong 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
5933c7e78d3SWeongyo Jeong 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
5943c7e78d3SWeongyo Jeong 	bus_dma_tag_destroy(dd->dd_dmat);
5953c7e78d3SWeongyo Jeong 
5963c7e78d3SWeongyo Jeong 	memset(dd, 0, sizeof(*dd));
5973c7e78d3SWeongyo Jeong }
5983c7e78d3SWeongyo Jeong 
5993c7e78d3SWeongyo Jeong static void
malo_rxdma_cleanup(struct malo_softc * sc)6003c7e78d3SWeongyo Jeong malo_rxdma_cleanup(struct malo_softc *sc)
6013c7e78d3SWeongyo Jeong {
6023c7e78d3SWeongyo Jeong 	struct malo_rxbuf *bf;
6033c7e78d3SWeongyo Jeong 
6043c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
6053c7e78d3SWeongyo Jeong 		if (bf->bf_m != NULL) {
6063c7e78d3SWeongyo Jeong 			m_freem(bf->bf_m);
6073c7e78d3SWeongyo Jeong 			bf->bf_m = NULL;
6083c7e78d3SWeongyo Jeong 		}
6093c7e78d3SWeongyo Jeong 		if (bf->bf_dmamap != NULL) {
6103c7e78d3SWeongyo Jeong 			bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
6113c7e78d3SWeongyo Jeong 			bf->bf_dmamap = NULL;
6123c7e78d3SWeongyo Jeong 		}
6133c7e78d3SWeongyo Jeong 	}
6143c7e78d3SWeongyo Jeong 	STAILQ_INIT(&sc->malo_rxbuf);
6153c7e78d3SWeongyo Jeong 	if (sc->malo_rxdma.dd_bufptr != NULL) {
6163c7e78d3SWeongyo Jeong 		free(sc->malo_rxdma.dd_bufptr, M_MALODEV);
6173c7e78d3SWeongyo Jeong 		sc->malo_rxdma.dd_bufptr = NULL;
6183c7e78d3SWeongyo Jeong 	}
6193c7e78d3SWeongyo Jeong 	if (sc->malo_rxdma.dd_desc_len != 0)
6203c7e78d3SWeongyo Jeong 		malo_desc_cleanup(sc, &sc->malo_rxdma);
6213c7e78d3SWeongyo Jeong }
6223c7e78d3SWeongyo Jeong 
6233c7e78d3SWeongyo Jeong static void
malo_txdma_cleanup(struct malo_softc * sc,struct malo_txq * txq)6243c7e78d3SWeongyo Jeong malo_txdma_cleanup(struct malo_softc *sc, struct malo_txq *txq)
6253c7e78d3SWeongyo Jeong {
6263c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
6273c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
6283c7e78d3SWeongyo Jeong 
6293c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &txq->free, bf_list) {
6303c7e78d3SWeongyo Jeong 		if (bf->bf_m != NULL) {
6313c7e78d3SWeongyo Jeong 			m_freem(bf->bf_m);
6323c7e78d3SWeongyo Jeong 			bf->bf_m = NULL;
6333c7e78d3SWeongyo Jeong 		}
6343c7e78d3SWeongyo Jeong 		ni = bf->bf_node;
6353c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
6363c7e78d3SWeongyo Jeong 		if (ni != NULL) {
6373c7e78d3SWeongyo Jeong 			/*
6383c7e78d3SWeongyo Jeong 			 * Reclaim node reference.
6393c7e78d3SWeongyo Jeong 			 */
6403c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
6413c7e78d3SWeongyo Jeong 		}
6423c7e78d3SWeongyo Jeong 		if (bf->bf_dmamap != NULL) {
6433c7e78d3SWeongyo Jeong 			bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
6443c7e78d3SWeongyo Jeong 			bf->bf_dmamap = NULL;
6453c7e78d3SWeongyo Jeong 		}
6463c7e78d3SWeongyo Jeong 	}
6473c7e78d3SWeongyo Jeong 	STAILQ_INIT(&txq->free);
6483c7e78d3SWeongyo Jeong 	txq->nfree = 0;
6493c7e78d3SWeongyo Jeong 	if (txq->dma.dd_bufptr != NULL) {
6503c7e78d3SWeongyo Jeong 		free(txq->dma.dd_bufptr, M_MALODEV);
6513c7e78d3SWeongyo Jeong 		txq->dma.dd_bufptr = NULL;
6523c7e78d3SWeongyo Jeong 	}
6533c7e78d3SWeongyo Jeong 	if (txq->dma.dd_desc_len != 0)
6543c7e78d3SWeongyo Jeong 		malo_desc_cleanup(sc, &txq->dma);
6553c7e78d3SWeongyo Jeong }
6563c7e78d3SWeongyo Jeong 
6573c7e78d3SWeongyo Jeong static void
malo_dma_cleanup(struct malo_softc * sc)6583c7e78d3SWeongyo Jeong malo_dma_cleanup(struct malo_softc *sc)
6593c7e78d3SWeongyo Jeong {
6603c7e78d3SWeongyo Jeong 	int i;
6613c7e78d3SWeongyo Jeong 
6623c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
6633c7e78d3SWeongyo Jeong 		malo_txdma_cleanup(sc, &sc->malo_txq[i]);
6643c7e78d3SWeongyo Jeong 
6653c7e78d3SWeongyo Jeong 	malo_rxdma_cleanup(sc);
6663c7e78d3SWeongyo Jeong }
6673c7e78d3SWeongyo Jeong 
6683c7e78d3SWeongyo Jeong static int
malo_dma_setup(struct malo_softc * sc)6693c7e78d3SWeongyo Jeong malo_dma_setup(struct malo_softc *sc)
6703c7e78d3SWeongyo Jeong {
6713c7e78d3SWeongyo Jeong 	int error, i;
6723c7e78d3SWeongyo Jeong 
6733c7e78d3SWeongyo Jeong 	/* rxdma initializing.  */
6743c7e78d3SWeongyo Jeong 	error = malo_rxdma_setup(sc);
6753c7e78d3SWeongyo Jeong 	if (error != 0)
6763c7e78d3SWeongyo Jeong 		return error;
6773c7e78d3SWeongyo Jeong 
6783c7e78d3SWeongyo Jeong 	/* NB: we just have 1 tx queue now.  */
6793c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
6803c7e78d3SWeongyo Jeong 		error = malo_txdma_setup(sc, &sc->malo_txq[i]);
6813c7e78d3SWeongyo Jeong 		if (error != 0) {
6823c7e78d3SWeongyo Jeong 			malo_dma_cleanup(sc);
6833c7e78d3SWeongyo Jeong 
6843c7e78d3SWeongyo Jeong 			return error;
6853c7e78d3SWeongyo Jeong 		}
6863c7e78d3SWeongyo Jeong 
6873c7e78d3SWeongyo Jeong 		malo_txq_init(sc, &sc->malo_txq[i], i);
6883c7e78d3SWeongyo Jeong 	}
6893c7e78d3SWeongyo Jeong 
6903c7e78d3SWeongyo Jeong 	return 0;
6913c7e78d3SWeongyo Jeong }
6923c7e78d3SWeongyo Jeong 
6933c7e78d3SWeongyo Jeong static void
malo_hal_set_rxtxdma(struct malo_softc * sc)6943c7e78d3SWeongyo Jeong malo_hal_set_rxtxdma(struct malo_softc *sc)
6953c7e78d3SWeongyo Jeong {
6963c7e78d3SWeongyo Jeong 	int i;
6973c7e78d3SWeongyo Jeong 
6983c7e78d3SWeongyo Jeong 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read,
6993c7e78d3SWeongyo Jeong 	    sc->malo_hwdma.rxdesc_read);
7003c7e78d3SWeongyo Jeong 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_write,
7013c7e78d3SWeongyo Jeong 	    sc->malo_hwdma.rxdesc_read);
7023c7e78d3SWeongyo Jeong 
7033c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
7043c7e78d3SWeongyo Jeong 		malo_bar0_write4(sc,
7053c7e78d3SWeongyo Jeong 		    sc->malo_hwspecs.wcbbase[i], sc->malo_hwdma.wcbbase[i]);
7063c7e78d3SWeongyo Jeong 	}
7073c7e78d3SWeongyo Jeong }
7083c7e78d3SWeongyo Jeong 
7093c7e78d3SWeongyo Jeong /*
7103c7e78d3SWeongyo Jeong  * Inform firmware of our tx/rx dma setup.  The BAR 0 writes below are
7113c7e78d3SWeongyo Jeong  * for compatibility with older firmware.  For current firmware we send
7123c7e78d3SWeongyo Jeong  * this information with a cmd block via malo_hal_sethwdma.
7133c7e78d3SWeongyo Jeong  */
7143c7e78d3SWeongyo Jeong static int
malo_setup_hwdma(struct malo_softc * sc)7153c7e78d3SWeongyo Jeong malo_setup_hwdma(struct malo_softc *sc)
7163c7e78d3SWeongyo Jeong {
7173c7e78d3SWeongyo Jeong 	int i;
7183c7e78d3SWeongyo Jeong 	struct malo_txq *txq;
7193c7e78d3SWeongyo Jeong 
7203c7e78d3SWeongyo Jeong 	sc->malo_hwdma.rxdesc_read = sc->malo_rxdma.dd_desc_paddr;
7213c7e78d3SWeongyo Jeong 
7223c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
7233c7e78d3SWeongyo Jeong 		txq = &sc->malo_txq[i];
7243c7e78d3SWeongyo Jeong 		sc->malo_hwdma.wcbbase[i] = txq->dma.dd_desc_paddr;
7253c7e78d3SWeongyo Jeong 	}
7263c7e78d3SWeongyo Jeong 	sc->malo_hwdma.maxnum_txwcb = malo_txbuf;
7273c7e78d3SWeongyo Jeong 	sc->malo_hwdma.maxnum_wcb = MALO_NUM_TX_QUEUES;
7283c7e78d3SWeongyo Jeong 
7293c7e78d3SWeongyo Jeong 	malo_hal_set_rxtxdma(sc);
7303c7e78d3SWeongyo Jeong 
7313c7e78d3SWeongyo Jeong 	return 0;
7323c7e78d3SWeongyo Jeong }
7333c7e78d3SWeongyo Jeong 
7343c7e78d3SWeongyo Jeong static void
malo_txq_init(struct malo_softc * sc,struct malo_txq * txq,int qnum)7353c7e78d3SWeongyo Jeong malo_txq_init(struct malo_softc *sc, struct malo_txq *txq, int qnum)
7363c7e78d3SWeongyo Jeong {
7373c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf, *bn;
7383c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
7393c7e78d3SWeongyo Jeong 
7403c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK_INIT(sc, txq);
7413c7e78d3SWeongyo Jeong 	txq->qnum = qnum;
7423c7e78d3SWeongyo Jeong 	txq->txpri = 0;	/* XXX */
7433c7e78d3SWeongyo Jeong 
7443c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &txq->free, bf_list) {
7453c7e78d3SWeongyo Jeong 		bf->bf_txq = txq;
7463c7e78d3SWeongyo Jeong 
7473c7e78d3SWeongyo Jeong 		ds = bf->bf_desc;
7483c7e78d3SWeongyo Jeong 		bn = STAILQ_NEXT(bf, bf_list);
7493c7e78d3SWeongyo Jeong 		if (bn == NULL)
7503c7e78d3SWeongyo Jeong 			bn = STAILQ_FIRST(&txq->free);
7513c7e78d3SWeongyo Jeong 		ds->physnext = htole32(bn->bf_daddr);
7523c7e78d3SWeongyo Jeong 	}
7533c7e78d3SWeongyo Jeong 	STAILQ_INIT(&txq->active);
7543c7e78d3SWeongyo Jeong }
7553c7e78d3SWeongyo Jeong 
7563c7e78d3SWeongyo Jeong /*
7573c7e78d3SWeongyo Jeong  * Reclaim resources for a setup queue.
7583c7e78d3SWeongyo Jeong  */
7593c7e78d3SWeongyo Jeong static void
malo_tx_cleanupq(struct malo_softc * sc,struct malo_txq * txq)7603c7e78d3SWeongyo Jeong malo_tx_cleanupq(struct malo_softc *sc, struct malo_txq *txq)
7613c7e78d3SWeongyo Jeong {
7623c7e78d3SWeongyo Jeong 	/* XXX hal work? */
7633c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK_DESTROY(txq);
7643c7e78d3SWeongyo Jeong }
7653c7e78d3SWeongyo Jeong 
7663c7e78d3SWeongyo Jeong /*
7673c7e78d3SWeongyo Jeong  * Allocate a tx buffer for sending a frame.
7683c7e78d3SWeongyo Jeong  */
7693c7e78d3SWeongyo Jeong static struct malo_txbuf *
malo_getbuf(struct malo_softc * sc,struct malo_txq * txq)7703c7e78d3SWeongyo Jeong malo_getbuf(struct malo_softc *sc, struct malo_txq *txq)
7713c7e78d3SWeongyo Jeong {
7723c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
7733c7e78d3SWeongyo Jeong 
7743c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK(txq);
7753c7e78d3SWeongyo Jeong 	bf = STAILQ_FIRST(&txq->free);
7763c7e78d3SWeongyo Jeong 	if (bf != NULL) {
7773c7e78d3SWeongyo Jeong 		STAILQ_REMOVE_HEAD(&txq->free, bf_list);
7783c7e78d3SWeongyo Jeong 		txq->nfree--;
7793c7e78d3SWeongyo Jeong 	}
7803c7e78d3SWeongyo Jeong 	MALO_TXQ_UNLOCK(txq);
7813c7e78d3SWeongyo Jeong 	if (bf == NULL) {
7823c7e78d3SWeongyo Jeong 		DPRINTF(sc, MALO_DEBUG_XMIT,
7833c7e78d3SWeongyo Jeong 		    "%s: out of xmit buffers on q %d\n", __func__, txq->qnum);
7843c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_qstop++;
7853c7e78d3SWeongyo Jeong 	}
7863c7e78d3SWeongyo Jeong 	return bf;
7873c7e78d3SWeongyo Jeong }
7883c7e78d3SWeongyo Jeong 
7893c7e78d3SWeongyo Jeong static int
malo_tx_dmasetup(struct malo_softc * sc,struct malo_txbuf * bf,struct mbuf * m0)7903c7e78d3SWeongyo Jeong malo_tx_dmasetup(struct malo_softc *sc, struct malo_txbuf *bf, struct mbuf *m0)
7913c7e78d3SWeongyo Jeong {
7923c7e78d3SWeongyo Jeong 	struct mbuf *m;
7933c7e78d3SWeongyo Jeong 	int error;
7943c7e78d3SWeongyo Jeong 
7953c7e78d3SWeongyo Jeong 	/*
7963c7e78d3SWeongyo Jeong 	 * Load the DMA map so any coalescing is done.  This also calculates
7973c7e78d3SWeongyo Jeong 	 * the number of descriptors we need.
7983c7e78d3SWeongyo Jeong 	 */
7993c7e78d3SWeongyo Jeong 	error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
8003c7e78d3SWeongyo Jeong 				     bf->bf_segs, &bf->bf_nseg,
8013c7e78d3SWeongyo Jeong 				     BUS_DMA_NOWAIT);
8023c7e78d3SWeongyo Jeong 	if (error == EFBIG) {
8033c7e78d3SWeongyo Jeong 		/* XXX packet requires too many descriptors */
8043c7e78d3SWeongyo Jeong 		bf->bf_nseg = MALO_TXDESC + 1;
8053c7e78d3SWeongyo Jeong 	} else if (error != 0) {
8063c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_busdma++;
8073c7e78d3SWeongyo Jeong 		m_freem(m0);
8083c7e78d3SWeongyo Jeong 		return error;
8093c7e78d3SWeongyo Jeong 	}
8103c7e78d3SWeongyo Jeong 	/*
8113c7e78d3SWeongyo Jeong 	 * Discard null packets and check for packets that require too many
8123c7e78d3SWeongyo Jeong 	 * TX descriptors.  We try to convert the latter to a cluster.
8133c7e78d3SWeongyo Jeong 	 */
8143c7e78d3SWeongyo Jeong 	if (error == EFBIG) {		/* too many desc's, linearize */
8153c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_linear++;
816c6499eccSGleb Smirnoff 		m = m_defrag(m0, M_NOWAIT);
8173c7e78d3SWeongyo Jeong 		if (m == NULL) {
8183c7e78d3SWeongyo Jeong 			m_freem(m0);
8193c7e78d3SWeongyo Jeong 			sc->malo_stats.mst_tx_nombuf++;
8203c7e78d3SWeongyo Jeong 			return ENOMEM;
8213c7e78d3SWeongyo Jeong 		}
8223c7e78d3SWeongyo Jeong 		m0 = m;
8233c7e78d3SWeongyo Jeong 		error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
8243c7e78d3SWeongyo Jeong 					     bf->bf_segs, &bf->bf_nseg,
8253c7e78d3SWeongyo Jeong 					     BUS_DMA_NOWAIT);
8263c7e78d3SWeongyo Jeong 		if (error != 0) {
8273c7e78d3SWeongyo Jeong 			sc->malo_stats.mst_tx_busdma++;
8283c7e78d3SWeongyo Jeong 			m_freem(m0);
8293c7e78d3SWeongyo Jeong 			return error;
8303c7e78d3SWeongyo Jeong 		}
8313c7e78d3SWeongyo Jeong 		KASSERT(bf->bf_nseg <= MALO_TXDESC,
8323c7e78d3SWeongyo Jeong 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
8333c7e78d3SWeongyo Jeong 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
8343c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_nodata++;
8353c7e78d3SWeongyo Jeong 		m_freem(m0);
8363c7e78d3SWeongyo Jeong 		return EIO;
8373c7e78d3SWeongyo Jeong 	}
8383c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_XMIT, "%s: m %p len %u\n",
8393c7e78d3SWeongyo Jeong 		__func__, m0, m0->m_pkthdr.len);
8403c7e78d3SWeongyo Jeong 	bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
8413c7e78d3SWeongyo Jeong 	bf->bf_m = m0;
8423c7e78d3SWeongyo Jeong 
8433c7e78d3SWeongyo Jeong 	return 0;
8443c7e78d3SWeongyo Jeong }
8453c7e78d3SWeongyo Jeong 
8463c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
8473c7e78d3SWeongyo Jeong static void
malo_printrxbuf(const struct malo_rxbuf * bf,u_int ix)8483c7e78d3SWeongyo Jeong malo_printrxbuf(const struct malo_rxbuf *bf, u_int ix)
8493c7e78d3SWeongyo Jeong {
8503c7e78d3SWeongyo Jeong 	const struct malo_rxdesc *ds = bf->bf_desc;
8513c7e78d3SWeongyo Jeong 	uint32_t status = le32toh(ds->status);
8523c7e78d3SWeongyo Jeong 
8532706e872SMarius Strobl 	printf("R[%2u] (DS.V:%p DS.P:0x%jx) NEXT:%08x DATA:%08x RC:%02x%s\n"
8543c7e78d3SWeongyo Jeong 	    "      STAT:%02x LEN:%04x SNR:%02x NF:%02x CHAN:%02x"
8552706e872SMarius Strobl 	    " RATE:%02x QOS:%04x\n", ix, ds, (uintmax_t)bf->bf_daddr,
8563c7e78d3SWeongyo Jeong 	    le32toh(ds->physnext), le32toh(ds->physbuffdata),
8573c7e78d3SWeongyo Jeong 	    ds->rxcontrol,
8583c7e78d3SWeongyo Jeong 	    ds->rxcontrol != MALO_RXD_CTRL_DRIVER_OWN ?
8593c7e78d3SWeongyo Jeong 	        "" : (status & MALO_RXD_STATUS_OK) ? " *" : " !",
8603c7e78d3SWeongyo Jeong 	    ds->status, le16toh(ds->pktlen), ds->snr, ds->nf, ds->channel,
8613c7e78d3SWeongyo Jeong 	    ds->rate, le16toh(ds->qosctrl));
8623c7e78d3SWeongyo Jeong }
8633c7e78d3SWeongyo Jeong 
8643c7e78d3SWeongyo Jeong static void
malo_printtxbuf(const struct malo_txbuf * bf,u_int qnum,u_int ix)8653c7e78d3SWeongyo Jeong malo_printtxbuf(const struct malo_txbuf *bf, u_int qnum, u_int ix)
8663c7e78d3SWeongyo Jeong {
8673c7e78d3SWeongyo Jeong 	const struct malo_txdesc *ds = bf->bf_desc;
8683c7e78d3SWeongyo Jeong 	uint32_t status = le32toh(ds->status);
8693c7e78d3SWeongyo Jeong 
8703c7e78d3SWeongyo Jeong 	printf("Q%u[%3u]", qnum, ix);
8712706e872SMarius Strobl 	printf(" (DS.V:%p DS.P:0x%jx)\n", ds, (uintmax_t)bf->bf_daddr);
8723c7e78d3SWeongyo Jeong 	printf("    NEXT:%08x DATA:%08x LEN:%04x STAT:%08x%s\n",
8733c7e78d3SWeongyo Jeong 	    le32toh(ds->physnext),
8743c7e78d3SWeongyo Jeong 	    le32toh(ds->pktptr), le16toh(ds->pktlen), status,
8753c7e78d3SWeongyo Jeong 	    status & MALO_TXD_STATUS_USED ?
8763c7e78d3SWeongyo Jeong 	    "" : (status & 3) != 0 ? " *" : " !");
8773c7e78d3SWeongyo Jeong 	printf("    RATE:%02x PRI:%x QOS:%04x SAP:%08x FORMAT:%04x\n",
8783c7e78d3SWeongyo Jeong 	    ds->datarate, ds->txpriority, le16toh(ds->qosctrl),
8793c7e78d3SWeongyo Jeong 	    le32toh(ds->sap_pktinfo), le16toh(ds->format));
8803c7e78d3SWeongyo Jeong #if 0
8813c7e78d3SWeongyo Jeong 	{
8823c7e78d3SWeongyo Jeong 		const uint8_t *cp = (const uint8_t *) ds;
8833c7e78d3SWeongyo Jeong 		int i;
8843c7e78d3SWeongyo Jeong 		for (i = 0; i < sizeof(struct malo_txdesc); i++) {
8853c7e78d3SWeongyo Jeong 			printf("%02x ", cp[i]);
8863c7e78d3SWeongyo Jeong 			if (((i+1) % 16) == 0)
8873c7e78d3SWeongyo Jeong 				printf("\n");
8883c7e78d3SWeongyo Jeong 		}
8893c7e78d3SWeongyo Jeong 		printf("\n");
8903c7e78d3SWeongyo Jeong 	}
8913c7e78d3SWeongyo Jeong #endif
8923c7e78d3SWeongyo Jeong }
8933c7e78d3SWeongyo Jeong #endif /* MALO_DEBUG */
8943c7e78d3SWeongyo Jeong 
8953c7e78d3SWeongyo Jeong static __inline void
malo_updatetxrate(struct ieee80211_node * ni,int rix)8963c7e78d3SWeongyo Jeong malo_updatetxrate(struct ieee80211_node *ni, int rix)
8973c7e78d3SWeongyo Jeong {
8983c7e78d3SWeongyo Jeong 	static const int ieeerates[] =
8993c7e78d3SWeongyo Jeong 	    { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 96, 108 };
900d6166defSAdrian Chadd 	if (rix < nitems(ieeerates))
9013c7e78d3SWeongyo Jeong 		ni->ni_txrate = ieeerates[rix];
9023c7e78d3SWeongyo Jeong }
9033c7e78d3SWeongyo Jeong 
9043c7e78d3SWeongyo Jeong static int
malo_fix2rate(int fix_rate)9053c7e78d3SWeongyo Jeong malo_fix2rate(int fix_rate)
9063c7e78d3SWeongyo Jeong {
9073c7e78d3SWeongyo Jeong 	static const int rates[] =
9083c7e78d3SWeongyo Jeong 	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 96, 108 };
909d6166defSAdrian Chadd 	return (fix_rate < nitems(rates) ? rates[fix_rate] : 0);
9103c7e78d3SWeongyo Jeong }
9113c7e78d3SWeongyo Jeong 
9123c7e78d3SWeongyo Jeong /*
9133c7e78d3SWeongyo Jeong  * Process completed xmit descriptors from the specified queue.
9143c7e78d3SWeongyo Jeong  */
9153c7e78d3SWeongyo Jeong static int
malo_tx_processq(struct malo_softc * sc,struct malo_txq * txq)9163c7e78d3SWeongyo Jeong malo_tx_processq(struct malo_softc *sc, struct malo_txq *txq)
9173c7e78d3SWeongyo Jeong {
9183c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
9193c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
9203c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
9213c7e78d3SWeongyo Jeong 	int nreaped;
9223c7e78d3SWeongyo Jeong 	uint32_t status;
9233c7e78d3SWeongyo Jeong 
9243c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_TX_PROC, "%s: tx queue %u\n",
9253c7e78d3SWeongyo Jeong 	    __func__, txq->qnum);
9263c7e78d3SWeongyo Jeong 	for (nreaped = 0;; nreaped++) {
9273c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
9283c7e78d3SWeongyo Jeong 		bf = STAILQ_FIRST(&txq->active);
9293c7e78d3SWeongyo Jeong 		if (bf == NULL) {
9303c7e78d3SWeongyo Jeong 			MALO_TXQ_UNLOCK(txq);
9313c7e78d3SWeongyo Jeong 			break;
9323c7e78d3SWeongyo Jeong 		}
9333c7e78d3SWeongyo Jeong 		ds = bf->bf_desc;
9343c7e78d3SWeongyo Jeong 		MALO_TXDESC_SYNC(txq, ds,
9353c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9363c7e78d3SWeongyo Jeong 		if (ds->status & htole32(MALO_TXD_STATUS_FW_OWNED)) {
9373c7e78d3SWeongyo Jeong 			MALO_TXQ_UNLOCK(txq);
9383c7e78d3SWeongyo Jeong 			break;
9393c7e78d3SWeongyo Jeong 		}
9403c7e78d3SWeongyo Jeong 		STAILQ_REMOVE_HEAD(&txq->active, bf_list);
9413c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
9423c7e78d3SWeongyo Jeong 
9433c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
9443c7e78d3SWeongyo Jeong 		if (sc->malo_debug & MALO_DEBUG_XMIT_DESC)
9453c7e78d3SWeongyo Jeong 			malo_printtxbuf(bf, txq->qnum, nreaped);
9463c7e78d3SWeongyo Jeong #endif
9473c7e78d3SWeongyo Jeong 		ni = bf->bf_node;
9483c7e78d3SWeongyo Jeong 		if (ni != NULL) {
9493c7e78d3SWeongyo Jeong 			status = le32toh(ds->status);
9503c7e78d3SWeongyo Jeong 			if (status & MALO_TXD_STATUS_OK) {
9513c7e78d3SWeongyo Jeong 				uint16_t format = le16toh(ds->format);
952fe5ebb23SBjoern A. Zeeb 				uint8_t txant =_IEEE80211_MASKSHIFT(
953fe5ebb23SBjoern A. Zeeb 				    format, MALO_TXD_ANTENNA);
9543c7e78d3SWeongyo Jeong 
9553c7e78d3SWeongyo Jeong 				sc->malo_stats.mst_ant_tx[txant]++;
9563c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_OK_RETRY)
9573c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_retries++;
9583c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_OK_MORE_RETRY)
9593c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_mretries++;
9603c7e78d3SWeongyo Jeong 				malo_updatetxrate(ni, ds->datarate);
9613c7e78d3SWeongyo Jeong 				sc->malo_stats.mst_tx_rate = ds->datarate;
9623c7e78d3SWeongyo Jeong 			} else {
9633c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_FAILED_LINK_ERROR)
9643c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_linkerror++;
9653c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_FAILED_XRETRY)
9663c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_xretries++;
9673c7e78d3SWeongyo Jeong 				if (status & MALO_TXD_STATUS_FAILED_AGING)
9683c7e78d3SWeongyo Jeong 					sc->malo_stats.mst_tx_aging++;
9693c7e78d3SWeongyo Jeong 			}
9703c7e78d3SWeongyo Jeong 			/* XXX strip fw len in case header inspected */
9713c7e78d3SWeongyo Jeong 			m_adj(bf->bf_m, sizeof(uint16_t));
9727a79cebfSGleb Smirnoff 			ieee80211_tx_complete(ni, bf->bf_m,
9733c7e78d3SWeongyo Jeong 			    (status & MALO_TXD_STATUS_OK) == 0);
9747a79cebfSGleb Smirnoff 		} else
9757a79cebfSGleb Smirnoff 			m_freem(bf->bf_m);
9767a79cebfSGleb Smirnoff 
9773c7e78d3SWeongyo Jeong 		ds->status = htole32(MALO_TXD_STATUS_IDLE);
9783c7e78d3SWeongyo Jeong 		ds->pktlen = htole32(0);
9793c7e78d3SWeongyo Jeong 
9803c7e78d3SWeongyo Jeong 		bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
9813c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTWRITE);
9823c7e78d3SWeongyo Jeong 		bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
9833c7e78d3SWeongyo Jeong 		bf->bf_m = NULL;
9843c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
9853c7e78d3SWeongyo Jeong 
9863c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
9873c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
9883c7e78d3SWeongyo Jeong 		txq->nfree++;
9893c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
9903c7e78d3SWeongyo Jeong 	}
9913c7e78d3SWeongyo Jeong 	return nreaped;
9923c7e78d3SWeongyo Jeong }
9933c7e78d3SWeongyo Jeong 
9943c7e78d3SWeongyo Jeong /*
9953c7e78d3SWeongyo Jeong  * Deferred processing of transmit interrupt.
9963c7e78d3SWeongyo Jeong  */
9973c7e78d3SWeongyo Jeong static void
malo_tx_proc(void * arg,int npending)9983c7e78d3SWeongyo Jeong malo_tx_proc(void *arg, int npending)
9993c7e78d3SWeongyo Jeong {
10003c7e78d3SWeongyo Jeong 	struct malo_softc *sc = arg;
10013c7e78d3SWeongyo Jeong 	int i, nreaped;
10023c7e78d3SWeongyo Jeong 
10033c7e78d3SWeongyo Jeong 	/*
10043c7e78d3SWeongyo Jeong 	 * Process each active queue.
10053c7e78d3SWeongyo Jeong 	 */
10063c7e78d3SWeongyo Jeong 	nreaped = 0;
10077a79cebfSGleb Smirnoff 	MALO_LOCK(sc);
10083c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
10093c7e78d3SWeongyo Jeong 		if (!STAILQ_EMPTY(&sc->malo_txq[i].active))
10103c7e78d3SWeongyo Jeong 			nreaped += malo_tx_processq(sc, &sc->malo_txq[i]);
10113c7e78d3SWeongyo Jeong 	}
10123c7e78d3SWeongyo Jeong 
10133c7e78d3SWeongyo Jeong 	if (nreaped != 0) {
10147cf545d0SJohn Baldwin 		sc->malo_timer = 0;
10157a79cebfSGleb Smirnoff 		malo_start(sc);
10163c7e78d3SWeongyo Jeong 	}
10177a79cebfSGleb Smirnoff 	MALO_UNLOCK(sc);
10183c7e78d3SWeongyo Jeong }
10193c7e78d3SWeongyo Jeong 
10203c7e78d3SWeongyo Jeong static int
malo_tx_start(struct malo_softc * sc,struct ieee80211_node * ni,struct malo_txbuf * bf,struct mbuf * m0)10213c7e78d3SWeongyo Jeong malo_tx_start(struct malo_softc *sc, struct ieee80211_node *ni,
10223c7e78d3SWeongyo Jeong     struct malo_txbuf *bf, struct mbuf *m0)
10233c7e78d3SWeongyo Jeong {
1024fba6f8efSWarner Losh 	int error, iswep;
1025fba6f8efSWarner Losh 	int hdrlen, pktlen;
10263c7e78d3SWeongyo Jeong 	struct ieee80211_frame *wh;
10277a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
10285463c4a4SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
10293c7e78d3SWeongyo Jeong 	struct malo_txdesc *ds;
10303c7e78d3SWeongyo Jeong 	struct malo_txrec *tr;
10313c7e78d3SWeongyo Jeong 	struct malo_txq *txq;
10323c7e78d3SWeongyo Jeong 	uint16_t qos;
10333c7e78d3SWeongyo Jeong 
10343c7e78d3SWeongyo Jeong 	wh = mtod(m0, struct ieee80211_frame *);
10355945b5f5SKevin Lo 	iswep = wh->i_fc[1] & IEEE80211_FC1_PROTECTED;
1036fba6f8efSWarner Losh 	hdrlen = ieee80211_anyhdrsize(wh);
10373c7e78d3SWeongyo Jeong 	pktlen = m0->m_pkthdr.len;
10383c7e78d3SWeongyo Jeong 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
1039f3f08e16SAndriy Voskoboinyk 		qos = *(uint16_t *)ieee80211_getqos(wh);
10403c7e78d3SWeongyo Jeong 	} else
10413c7e78d3SWeongyo Jeong 		qos = 0;
10423c7e78d3SWeongyo Jeong 
10433c7e78d3SWeongyo Jeong 	if (iswep) {
10443c7e78d3SWeongyo Jeong 		struct ieee80211_key *k;
10453c7e78d3SWeongyo Jeong 
10463c7e78d3SWeongyo Jeong 		/*
10473c7e78d3SWeongyo Jeong 		 * Construct the 802.11 header+trailer for an encrypted
10483c7e78d3SWeongyo Jeong 		 * frame. The only reason this can fail is because of an
10493c7e78d3SWeongyo Jeong 		 * unknown or unsupported cipher/key type.
10503c7e78d3SWeongyo Jeong 		 *
10513c7e78d3SWeongyo Jeong 		 * NB: we do this even though the firmware will ignore
10523c7e78d3SWeongyo Jeong 		 *     what we've done for WEP and TKIP as we need the
10533c7e78d3SWeongyo Jeong 		 *     ExtIV filled in for CCMP and this also adjusts
10543c7e78d3SWeongyo Jeong 		 *     the headers which simplifies our work below.
10553c7e78d3SWeongyo Jeong 		 */
1056b032f27cSSam Leffler 		k = ieee80211_crypto_encap(ni, m0);
10573c7e78d3SWeongyo Jeong 		if (k == NULL) {
10583c7e78d3SWeongyo Jeong 			/*
10593c7e78d3SWeongyo Jeong 			 * This can happen when the key is yanked after the
10603c7e78d3SWeongyo Jeong 			 * frame was queued.  Just discard the frame; the
10613c7e78d3SWeongyo Jeong 			 * 802.11 layer counts failures and provides
10623c7e78d3SWeongyo Jeong 			 * debugging/diagnostics.
10633c7e78d3SWeongyo Jeong 			 */
10643c7e78d3SWeongyo Jeong 			m_freem(m0);
10653c7e78d3SWeongyo Jeong 			return EIO;
10663c7e78d3SWeongyo Jeong 		}
10673c7e78d3SWeongyo Jeong 
10683c7e78d3SWeongyo Jeong 		/*
10693c7e78d3SWeongyo Jeong 		 * Adjust the packet length for the crypto additions
10703c7e78d3SWeongyo Jeong 		 * done during encap and any other bits that the f/w
10713c7e78d3SWeongyo Jeong 		 * will add later on.
10723c7e78d3SWeongyo Jeong 		 */
10733c7e78d3SWeongyo Jeong 		pktlen = m0->m_pkthdr.len;
10743c7e78d3SWeongyo Jeong 
10753c7e78d3SWeongyo Jeong 		/* packet header may have moved, reset our local pointer */
10763c7e78d3SWeongyo Jeong 		wh = mtod(m0, struct ieee80211_frame *);
10773c7e78d3SWeongyo Jeong 	}
10783c7e78d3SWeongyo Jeong 
10795463c4a4SSam Leffler 	if (ieee80211_radiotap_active_vap(vap)) {
10803c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_flags = 0;	/* XXX */
10813c7e78d3SWeongyo Jeong 		if (iswep)
10823c7e78d3SWeongyo Jeong 			sc->malo_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
10833c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_txpower = ni->ni_txpower;
10843c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_antenna = sc->malo_txantenna;
10853c7e78d3SWeongyo Jeong 
10865463c4a4SSam Leffler 		ieee80211_radiotap_tx(vap, m0);
10873c7e78d3SWeongyo Jeong 	}
10883c7e78d3SWeongyo Jeong 
10893c7e78d3SWeongyo Jeong 	/*
10903c7e78d3SWeongyo Jeong 	 * Copy up/down the 802.11 header; the firmware requires
10913c7e78d3SWeongyo Jeong 	 * we present a 2-byte payload length followed by a
10923c7e78d3SWeongyo Jeong 	 * 4-address header (w/o QoS), followed (optionally) by
10933c7e78d3SWeongyo Jeong 	 * any WEP/ExtIV header (but only filled in for CCMP).
10943c7e78d3SWeongyo Jeong 	 * We are assured the mbuf has sufficient headroom to
10953c7e78d3SWeongyo Jeong 	 * prepend in-place by the setup of ic_headroom in
10963c7e78d3SWeongyo Jeong 	 * malo_attach.
10973c7e78d3SWeongyo Jeong 	 */
10983c7e78d3SWeongyo Jeong 	if (hdrlen < sizeof(struct malo_txrec)) {
10993c7e78d3SWeongyo Jeong 		const int space = sizeof(struct malo_txrec) - hdrlen;
11003c7e78d3SWeongyo Jeong 		if (M_LEADINGSPACE(m0) < space) {
11013c7e78d3SWeongyo Jeong 			/* NB: should never happen */
11023c7e78d3SWeongyo Jeong 			device_printf(sc->malo_dev,
11033c7e78d3SWeongyo Jeong 			    "not enough headroom, need %d found %zd, "
11043c7e78d3SWeongyo Jeong 			    "m_flags 0x%x m_len %d\n",
11053c7e78d3SWeongyo Jeong 			    space, M_LEADINGSPACE(m0), m0->m_flags, m0->m_len);
11063c7e78d3SWeongyo Jeong 			ieee80211_dump_pkt(ic,
11073c7e78d3SWeongyo Jeong 			    mtod(m0, const uint8_t *), m0->m_len, 0, -1);
11083c7e78d3SWeongyo Jeong 			m_freem(m0);
11093c7e78d3SWeongyo Jeong 			/* XXX stat */
11103c7e78d3SWeongyo Jeong 			return EIO;
11113c7e78d3SWeongyo Jeong 		}
11123c7e78d3SWeongyo Jeong 		M_PREPEND(m0, space, M_NOWAIT);
11133c7e78d3SWeongyo Jeong 	}
11143c7e78d3SWeongyo Jeong 	tr = mtod(m0, struct malo_txrec *);
11153c7e78d3SWeongyo Jeong 	if (wh != (struct ieee80211_frame *) &tr->wh)
11163c7e78d3SWeongyo Jeong 		ovbcopy(wh, &tr->wh, hdrlen);
11173c7e78d3SWeongyo Jeong 	/*
11183c7e78d3SWeongyo Jeong 	 * Note: the "firmware length" is actually the length of the fully
11193c7e78d3SWeongyo Jeong 	 * formed "802.11 payload".  That is, it's everything except for
11203c7e78d3SWeongyo Jeong 	 * the 802.11 header.  In particular this includes all crypto
11213c7e78d3SWeongyo Jeong 	 * material including the MIC!
11223c7e78d3SWeongyo Jeong 	 */
11233c7e78d3SWeongyo Jeong 	tr->fwlen = htole16(pktlen - hdrlen);
11243c7e78d3SWeongyo Jeong 
11253c7e78d3SWeongyo Jeong 	/*
11263c7e78d3SWeongyo Jeong 	 * Load the DMA map so any coalescing is done.  This
11273c7e78d3SWeongyo Jeong 	 * also calculates the number of descriptors we need.
11283c7e78d3SWeongyo Jeong 	 */
11293c7e78d3SWeongyo Jeong 	error = malo_tx_dmasetup(sc, bf, m0);
11303c7e78d3SWeongyo Jeong 	if (error != 0)
11313c7e78d3SWeongyo Jeong 		return error;
11323c7e78d3SWeongyo Jeong 	bf->bf_node = ni;			/* NB: held reference */
11333c7e78d3SWeongyo Jeong 	m0 = bf->bf_m;				/* NB: may have changed */
11343c7e78d3SWeongyo Jeong 	tr = mtod(m0, struct malo_txrec *);
11353c7e78d3SWeongyo Jeong 	wh = (struct ieee80211_frame *)&tr->wh;
11363c7e78d3SWeongyo Jeong 
11373c7e78d3SWeongyo Jeong 	/*
11383c7e78d3SWeongyo Jeong 	 * Formulate tx descriptor.
11393c7e78d3SWeongyo Jeong 	 */
11403c7e78d3SWeongyo Jeong 	ds = bf->bf_desc;
11413c7e78d3SWeongyo Jeong 	txq = bf->bf_txq;
11423c7e78d3SWeongyo Jeong 
11433c7e78d3SWeongyo Jeong 	ds->qosctrl = qos;			/* NB: already little-endian */
11443c7e78d3SWeongyo Jeong 	ds->pktptr = htole32(bf->bf_segs[0].ds_addr);
11453c7e78d3SWeongyo Jeong 	ds->pktlen = htole16(bf->bf_segs[0].ds_len);
11463c7e78d3SWeongyo Jeong 	/* NB: pPhysNext setup once, don't touch */
1147*c249cc38SAdrian Chadd 	ds->datarate = IEEE80211_IS_DATA(wh) ? 1 : 0;
11483c7e78d3SWeongyo Jeong 	ds->sap_pktinfo = 0;
11493c7e78d3SWeongyo Jeong 	ds->format = 0;
11503c7e78d3SWeongyo Jeong 
11513c7e78d3SWeongyo Jeong 	/*
11523c7e78d3SWeongyo Jeong 	 * Select transmit rate.
11533c7e78d3SWeongyo Jeong 	 */
11543c7e78d3SWeongyo Jeong 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
11553c7e78d3SWeongyo Jeong 	case IEEE80211_FC0_TYPE_MGT:
11563c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_tx_mgmt++;
11573c7e78d3SWeongyo Jeong 		/* fall thru... */
11583c7e78d3SWeongyo Jeong 	case IEEE80211_FC0_TYPE_CTL:
11593c7e78d3SWeongyo Jeong 		ds->txpriority = 1;
11603c7e78d3SWeongyo Jeong 		break;
11613c7e78d3SWeongyo Jeong 	case IEEE80211_FC0_TYPE_DATA:
11623c7e78d3SWeongyo Jeong 		ds->txpriority = txq->qnum;
11633c7e78d3SWeongyo Jeong 		break;
11643c7e78d3SWeongyo Jeong 	default:
11657a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "bogus frame type 0x%x (%s)\n",
11663c7e78d3SWeongyo Jeong 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
11673c7e78d3SWeongyo Jeong 		/* XXX statistic */
11683c7e78d3SWeongyo Jeong 		m_freem(m0);
11693c7e78d3SWeongyo Jeong 		return EIO;
11703c7e78d3SWeongyo Jeong 	}
11713c7e78d3SWeongyo Jeong 
11723c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
11733c7e78d3SWeongyo Jeong 	if (IFF_DUMPPKTS_XMIT(sc))
11743c7e78d3SWeongyo Jeong 		ieee80211_dump_pkt(ic,
11753c7e78d3SWeongyo Jeong 		    mtod(m0, const uint8_t *)+sizeof(uint16_t),
11763c7e78d3SWeongyo Jeong 		    m0->m_len - sizeof(uint16_t), ds->datarate, -1);
11773c7e78d3SWeongyo Jeong #endif
11783c7e78d3SWeongyo Jeong 
11793c7e78d3SWeongyo Jeong 	MALO_TXQ_LOCK(txq);
1180*c249cc38SAdrian Chadd 	if (!IEEE80211_IS_DATA(wh))
11813c7e78d3SWeongyo Jeong 		ds->status |= htole32(1);
11823c7e78d3SWeongyo Jeong 	ds->status |= htole32(MALO_TXD_STATUS_FW_OWNED);
11833c7e78d3SWeongyo Jeong 	STAILQ_INSERT_TAIL(&txq->active, bf, bf_list);
11843c7e78d3SWeongyo Jeong 	MALO_TXDESC_SYNC(txq, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
11853c7e78d3SWeongyo Jeong 
11867cf545d0SJohn Baldwin 	sc->malo_timer = 5;
11873c7e78d3SWeongyo Jeong 	MALO_TXQ_UNLOCK(txq);
11883c7e78d3SWeongyo Jeong 	return 0;
11893c7e78d3SWeongyo Jeong }
11903c7e78d3SWeongyo Jeong 
11917a79cebfSGleb Smirnoff static int
malo_transmit(struct ieee80211com * ic,struct mbuf * m)11927a79cebfSGleb Smirnoff malo_transmit(struct ieee80211com *ic, struct mbuf *m)
119379d2c5e8SGleb Smirnoff {
11947a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
11957a79cebfSGleb Smirnoff 	int error;
11967a79cebfSGleb Smirnoff 
11977a79cebfSGleb Smirnoff 	MALO_LOCK(sc);
11987a79cebfSGleb Smirnoff 	if (!sc->malo_running) {
11997a79cebfSGleb Smirnoff 		MALO_UNLOCK(sc);
12007a79cebfSGleb Smirnoff 		return (ENXIO);
12017a79cebfSGleb Smirnoff 	}
12027a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->malo_snd, m);
12037a79cebfSGleb Smirnoff 	if (error) {
12047a79cebfSGleb Smirnoff 		MALO_UNLOCK(sc);
12057a79cebfSGleb Smirnoff 		return (error);
12067a79cebfSGleb Smirnoff 	}
12077a79cebfSGleb Smirnoff 	malo_start(sc);
12087a79cebfSGleb Smirnoff 	MALO_UNLOCK(sc);
12097a79cebfSGleb Smirnoff 	return (0);
12107a79cebfSGleb Smirnoff }
12117a79cebfSGleb Smirnoff 
12127a79cebfSGleb Smirnoff static void
malo_start(struct malo_softc * sc)12137a79cebfSGleb Smirnoff malo_start(struct malo_softc *sc)
12147a79cebfSGleb Smirnoff {
12153c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
1216b032f27cSSam Leffler 	struct malo_txq *txq = &sc->malo_txq[0];
12173c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf = NULL;
12183c7e78d3SWeongyo Jeong 	struct mbuf *m;
1219b032f27cSSam Leffler 	int nqueued = 0;
12203c7e78d3SWeongyo Jeong 
12217a79cebfSGleb Smirnoff 	MALO_LOCK_ASSERT(sc);
12227a79cebfSGleb Smirnoff 
12237a79cebfSGleb Smirnoff 	if (!sc->malo_running || sc->malo_invalid)
12243c7e78d3SWeongyo Jeong 		return;
12253c7e78d3SWeongyo Jeong 
12267a79cebfSGleb Smirnoff 	while ((m = mbufq_dequeue(&sc->malo_snd)) != NULL) {
1227b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
12283c7e78d3SWeongyo Jeong 		bf = malo_getbuf(sc, txq);
12293c7e78d3SWeongyo Jeong 		if (bf == NULL) {
12307a79cebfSGleb Smirnoff 			mbufq_prepend(&sc->malo_snd, m);
12313c7e78d3SWeongyo Jeong 			sc->malo_stats.mst_tx_qstop++;
12323c7e78d3SWeongyo Jeong 			break;
12333c7e78d3SWeongyo Jeong 		}
12343c7e78d3SWeongyo Jeong 		/*
12353c7e78d3SWeongyo Jeong 		 * Pass the frame to the h/w for transmission.
12363c7e78d3SWeongyo Jeong 		 */
12373c7e78d3SWeongyo Jeong 		if (malo_tx_start(sc, ni, bf, m)) {
12387a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
12397a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
12403c7e78d3SWeongyo Jeong 			if (bf != NULL) {
12413c7e78d3SWeongyo Jeong 				bf->bf_m = NULL;
12423c7e78d3SWeongyo Jeong 				bf->bf_node = NULL;
12433c7e78d3SWeongyo Jeong 				MALO_TXQ_LOCK(txq);
12443c7e78d3SWeongyo Jeong 				STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
12453c7e78d3SWeongyo Jeong 				MALO_TXQ_UNLOCK(txq);
12463c7e78d3SWeongyo Jeong 			}
12473c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
12483c7e78d3SWeongyo Jeong 			continue;
12493c7e78d3SWeongyo Jeong 		}
12503c7e78d3SWeongyo Jeong 		nqueued++;
12513c7e78d3SWeongyo Jeong 
12523c7e78d3SWeongyo Jeong 		if (nqueued >= malo_txcoalesce) {
12533c7e78d3SWeongyo Jeong 			/*
12543c7e78d3SWeongyo Jeong 			 * Poke the firmware to process queued frames;
12553c7e78d3SWeongyo Jeong 			 * see below about (lack of) locking.
12563c7e78d3SWeongyo Jeong 			 */
12573c7e78d3SWeongyo Jeong 			nqueued = 0;
12583c7e78d3SWeongyo Jeong 			malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
12593c7e78d3SWeongyo Jeong 		}
12603c7e78d3SWeongyo Jeong 	}
12613c7e78d3SWeongyo Jeong 
12623c7e78d3SWeongyo Jeong 	if (nqueued) {
12633c7e78d3SWeongyo Jeong 		/*
12643c7e78d3SWeongyo Jeong 		 * NB: We don't need to lock against tx done because
12653c7e78d3SWeongyo Jeong 		 * this just prods the firmware to check the transmit
12663c7e78d3SWeongyo Jeong 		 * descriptors.  The firmware will also start fetching
12673c7e78d3SWeongyo Jeong 		 * descriptors by itself if it notices new ones are
12683c7e78d3SWeongyo Jeong 		 * present when it goes to deliver a tx done interrupt
12693c7e78d3SWeongyo Jeong 		 * to the host. So if we race with tx done processing
12703c7e78d3SWeongyo Jeong 		 * it's ok.  Delivering the kick here rather than in
12713c7e78d3SWeongyo Jeong 		 * malo_tx_start is an optimization to avoid poking the
12723c7e78d3SWeongyo Jeong 		 * firmware for each packet.
12733c7e78d3SWeongyo Jeong 		 *
12743c7e78d3SWeongyo Jeong 		 * NB: the queue id isn't used so 0 is ok.
12753c7e78d3SWeongyo Jeong 		 */
12763c7e78d3SWeongyo Jeong 		malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
12773c7e78d3SWeongyo Jeong 	}
12783c7e78d3SWeongyo Jeong }
12793c7e78d3SWeongyo Jeong 
12803c7e78d3SWeongyo Jeong static void
malo_watchdog(void * arg)12817cf545d0SJohn Baldwin malo_watchdog(void *arg)
12823c7e78d3SWeongyo Jeong {
12837a79cebfSGleb Smirnoff 	struct malo_softc *sc = arg;
12843c7e78d3SWeongyo Jeong 
12857cf545d0SJohn Baldwin 	callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
12867cf545d0SJohn Baldwin 	if (sc->malo_timer == 0 || --sc->malo_timer > 0)
12877cf545d0SJohn Baldwin 		return;
12887cf545d0SJohn Baldwin 
12897a79cebfSGleb Smirnoff 	if (sc->malo_running && !sc->malo_invalid) {
12907a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev, "watchdog timeout\n");
12913c7e78d3SWeongyo Jeong 
12923c7e78d3SWeongyo Jeong 		/* XXX no way to reset h/w. now  */
12933c7e78d3SWeongyo Jeong 
12947a79cebfSGleb Smirnoff 		counter_u64_add(sc->malo_ic.ic_oerrors, 1);
12953c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_watchdog++;
12963c7e78d3SWeongyo Jeong 	}
12973c7e78d3SWeongyo Jeong }
12983c7e78d3SWeongyo Jeong 
12993c7e78d3SWeongyo Jeong static int
malo_hal_reset(struct malo_softc * sc)13003c7e78d3SWeongyo Jeong malo_hal_reset(struct malo_softc *sc)
13013c7e78d3SWeongyo Jeong {
13023c7e78d3SWeongyo Jeong 	static int first = 0;
13037a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
13043c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
13053c7e78d3SWeongyo Jeong 
13063c7e78d3SWeongyo Jeong 	if (first == 0) {
13073c7e78d3SWeongyo Jeong 		/*
13083c7e78d3SWeongyo Jeong 		 * NB: when the device firstly is initialized, sometimes
13093c7e78d3SWeongyo Jeong 		 * firmware could override rx/tx dma registers so we re-set
13103c7e78d3SWeongyo Jeong 		 * these values once.
13113c7e78d3SWeongyo Jeong 		 */
13123c7e78d3SWeongyo Jeong 		malo_hal_set_rxtxdma(sc);
13133c7e78d3SWeongyo Jeong 		first = 1;
13143c7e78d3SWeongyo Jeong 	}
13153c7e78d3SWeongyo Jeong 
13163c7e78d3SWeongyo Jeong 	malo_hal_setantenna(mh, MHA_ANTENNATYPE_RX, sc->malo_rxantenna);
13173c7e78d3SWeongyo Jeong 	malo_hal_setantenna(mh, MHA_ANTENNATYPE_TX, sc->malo_txantenna);
13183c7e78d3SWeongyo Jeong 	malo_hal_setradio(mh, 1, MHP_AUTO_PREAMBLE);
13193c7e78d3SWeongyo Jeong 	malo_chan_set(sc, ic->ic_curchan);
13203c7e78d3SWeongyo Jeong 
13213c7e78d3SWeongyo Jeong 	/* XXX needs other stuffs?  */
13223c7e78d3SWeongyo Jeong 
13233c7e78d3SWeongyo Jeong 	return 1;
13243c7e78d3SWeongyo Jeong }
13253c7e78d3SWeongyo Jeong 
13263c7e78d3SWeongyo Jeong static __inline struct mbuf *
malo_getrxmbuf(struct malo_softc * sc,struct malo_rxbuf * bf)13273c7e78d3SWeongyo Jeong malo_getrxmbuf(struct malo_softc *sc, struct malo_rxbuf *bf)
13283c7e78d3SWeongyo Jeong {
13293c7e78d3SWeongyo Jeong 	struct mbuf *m;
13303c7e78d3SWeongyo Jeong 	bus_addr_t paddr;
13313c7e78d3SWeongyo Jeong 	int error;
13323c7e78d3SWeongyo Jeong 
13333c7e78d3SWeongyo Jeong 	/* XXX don't need mbuf, just dma buffer */
1334c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
13353c7e78d3SWeongyo Jeong 	if (m == NULL) {
13363c7e78d3SWeongyo Jeong 		sc->malo_stats.mst_rx_nombuf++;	/* XXX */
13373c7e78d3SWeongyo Jeong 		return NULL;
13383c7e78d3SWeongyo Jeong 	}
13393c7e78d3SWeongyo Jeong 	error = bus_dmamap_load(sc->malo_dmat, bf->bf_dmamap,
13403c7e78d3SWeongyo Jeong 	    mtod(m, caddr_t), MJUMPAGESIZE,
13413c7e78d3SWeongyo Jeong 	    malo_load_cb, &paddr, BUS_DMA_NOWAIT);
13423c7e78d3SWeongyo Jeong 	if (error != 0) {
13437a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
13443c7e78d3SWeongyo Jeong 		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
13453c7e78d3SWeongyo Jeong 		m_freem(m);
13463c7e78d3SWeongyo Jeong 		return NULL;
13473c7e78d3SWeongyo Jeong 	}
13483c7e78d3SWeongyo Jeong 	bf->bf_data = paddr;
13493c7e78d3SWeongyo Jeong 	bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
13503c7e78d3SWeongyo Jeong 
13513c7e78d3SWeongyo Jeong 	return m;
13523c7e78d3SWeongyo Jeong }
13533c7e78d3SWeongyo Jeong 
13543c7e78d3SWeongyo Jeong static int
malo_rxbuf_init(struct malo_softc * sc,struct malo_rxbuf * bf)13553c7e78d3SWeongyo Jeong malo_rxbuf_init(struct malo_softc *sc, struct malo_rxbuf *bf)
13563c7e78d3SWeongyo Jeong {
13573c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
13583c7e78d3SWeongyo Jeong 
13593c7e78d3SWeongyo Jeong 	ds = bf->bf_desc;
13603c7e78d3SWeongyo Jeong 	if (bf->bf_m == NULL) {
13613c7e78d3SWeongyo Jeong 		bf->bf_m = malo_getrxmbuf(sc, bf);
13623c7e78d3SWeongyo Jeong 		if (bf->bf_m == NULL) {
13633c7e78d3SWeongyo Jeong 			/* mark descriptor to be skipped */
13643c7e78d3SWeongyo Jeong 			ds->rxcontrol = MALO_RXD_CTRL_OS_OWN;
13653c7e78d3SWeongyo Jeong 			/* NB: don't need PREREAD */
13663c7e78d3SWeongyo Jeong 			MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREWRITE);
13673c7e78d3SWeongyo Jeong 			return ENOMEM;
13683c7e78d3SWeongyo Jeong 		}
13693c7e78d3SWeongyo Jeong 	}
13703c7e78d3SWeongyo Jeong 
13713c7e78d3SWeongyo Jeong 	/*
13723c7e78d3SWeongyo Jeong 	 * Setup descriptor.
13733c7e78d3SWeongyo Jeong 	 */
13743c7e78d3SWeongyo Jeong 	ds->qosctrl = 0;
13753c7e78d3SWeongyo Jeong 	ds->snr = 0;
13763c7e78d3SWeongyo Jeong 	ds->status = MALO_RXD_STATUS_IDLE;
13773c7e78d3SWeongyo Jeong 	ds->channel = 0;
13783c7e78d3SWeongyo Jeong 	ds->pktlen = htole16(MALO_RXSIZE);
13793c7e78d3SWeongyo Jeong 	ds->nf = 0;
13803c7e78d3SWeongyo Jeong 	ds->physbuffdata = htole32(bf->bf_data);
13813c7e78d3SWeongyo Jeong 	/* NB: don't touch pPhysNext, set once */
13823c7e78d3SWeongyo Jeong 	ds->rxcontrol = MALO_RXD_CTRL_DRIVER_OWN;
13833c7e78d3SWeongyo Jeong 	MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
13843c7e78d3SWeongyo Jeong 
13853c7e78d3SWeongyo Jeong 	return 0;
13863c7e78d3SWeongyo Jeong }
13873c7e78d3SWeongyo Jeong 
13883c7e78d3SWeongyo Jeong /*
13893c7e78d3SWeongyo Jeong  * Setup the rx data structures.  This should only be done once or we may get
13903c7e78d3SWeongyo Jeong  * out of sync with the firmware.
13913c7e78d3SWeongyo Jeong  */
13923c7e78d3SWeongyo Jeong static int
malo_startrecv(struct malo_softc * sc)13933c7e78d3SWeongyo Jeong malo_startrecv(struct malo_softc *sc)
13943c7e78d3SWeongyo Jeong {
13953c7e78d3SWeongyo Jeong 	struct malo_rxbuf *bf, *prev;
13963c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
13973c7e78d3SWeongyo Jeong 
13983c7e78d3SWeongyo Jeong 	if (sc->malo_recvsetup == 1) {
13993c7e78d3SWeongyo Jeong 		malo_mode_init(sc);		/* set filters, etc. */
14003c7e78d3SWeongyo Jeong 		return 0;
14013c7e78d3SWeongyo Jeong 	}
14023c7e78d3SWeongyo Jeong 
14033c7e78d3SWeongyo Jeong 	prev = NULL;
14043c7e78d3SWeongyo Jeong 	STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
14053c7e78d3SWeongyo Jeong 		int error = malo_rxbuf_init(sc, bf);
14063c7e78d3SWeongyo Jeong 		if (error != 0) {
14073c7e78d3SWeongyo Jeong 			DPRINTF(sc, MALO_DEBUG_RECV,
14083c7e78d3SWeongyo Jeong 			    "%s: malo_rxbuf_init failed %d\n",
14093c7e78d3SWeongyo Jeong 			    __func__, error);
14103c7e78d3SWeongyo Jeong 			return error;
14113c7e78d3SWeongyo Jeong 		}
14123c7e78d3SWeongyo Jeong 		if (prev != NULL) {
14133c7e78d3SWeongyo Jeong 			ds = prev->bf_desc;
14143c7e78d3SWeongyo Jeong 			ds->physnext = htole32(bf->bf_daddr);
14153c7e78d3SWeongyo Jeong 		}
14163c7e78d3SWeongyo Jeong 		prev = bf;
14173c7e78d3SWeongyo Jeong 	}
14183c7e78d3SWeongyo Jeong 	if (prev != NULL) {
14193c7e78d3SWeongyo Jeong 		ds = prev->bf_desc;
14203c7e78d3SWeongyo Jeong 		ds->physnext =
14213c7e78d3SWeongyo Jeong 		    htole32(STAILQ_FIRST(&sc->malo_rxbuf)->bf_daddr);
14223c7e78d3SWeongyo Jeong 	}
14233c7e78d3SWeongyo Jeong 
14243c7e78d3SWeongyo Jeong 	sc->malo_recvsetup = 1;
14253c7e78d3SWeongyo Jeong 
14263c7e78d3SWeongyo Jeong 	malo_mode_init(sc);		/* set filters, etc. */
14273c7e78d3SWeongyo Jeong 
14283c7e78d3SWeongyo Jeong 	return 0;
14293c7e78d3SWeongyo Jeong }
14303c7e78d3SWeongyo Jeong 
14313c7e78d3SWeongyo Jeong static void
malo_init_locked(struct malo_softc * sc)1432b032f27cSSam Leffler malo_init_locked(struct malo_softc *sc)
14333c7e78d3SWeongyo Jeong {
14343c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
14353c7e78d3SWeongyo Jeong 	int error;
14363c7e78d3SWeongyo Jeong 
1437b032f27cSSam Leffler 	MALO_LOCK_ASSERT(sc);
14383c7e78d3SWeongyo Jeong 
14393c7e78d3SWeongyo Jeong 	/*
14403c7e78d3SWeongyo Jeong 	 * Stop anything previously setup.  This is safe whether this is
14413c7e78d3SWeongyo Jeong 	 * the first time through or not.
14423c7e78d3SWeongyo Jeong 	 */
14437a79cebfSGleb Smirnoff 	malo_stop(sc);
14443c7e78d3SWeongyo Jeong 
14453c7e78d3SWeongyo Jeong 	/*
14463c7e78d3SWeongyo Jeong 	 * Push state to the firmware.
14473c7e78d3SWeongyo Jeong 	 */
14483c7e78d3SWeongyo Jeong 	if (!malo_hal_reset(sc)) {
14497a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
14507a79cebfSGleb Smirnoff 		    "%s: unable to reset hardware\n", __func__);
1451b032f27cSSam Leffler 		return;
14523c7e78d3SWeongyo Jeong 	}
14533c7e78d3SWeongyo Jeong 
14543c7e78d3SWeongyo Jeong 	/*
14553c7e78d3SWeongyo Jeong 	 * Setup recv (once); transmit is already good to go.
14563c7e78d3SWeongyo Jeong 	 */
14573c7e78d3SWeongyo Jeong 	error = malo_startrecv(sc);
14583c7e78d3SWeongyo Jeong 	if (error != 0) {
14597a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
14607a79cebfSGleb Smirnoff 		    "%s: unable to start recv logic, error %d\n",
14613c7e78d3SWeongyo Jeong 		    __func__, error);
1462b032f27cSSam Leffler 		return;
14633c7e78d3SWeongyo Jeong 	}
14643c7e78d3SWeongyo Jeong 
14653c7e78d3SWeongyo Jeong 	/*
14663c7e78d3SWeongyo Jeong 	 * Enable interrupts.
14673c7e78d3SWeongyo Jeong 	 */
14683c7e78d3SWeongyo Jeong 	sc->malo_imask = MALO_A2HRIC_BIT_RX_RDY
14693c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_TX_DONE
14703c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_OPC_DONE
14713c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_MAC_EVENT
14723c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_RX_PROBLEM
14733c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_ICV_ERROR
14743c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_RADAR_DETECT
14753c7e78d3SWeongyo Jeong 	    | MALO_A2HRIC_BIT_CHAN_SWITCH;
14763c7e78d3SWeongyo Jeong 
14777a79cebfSGleb Smirnoff 	sc->malo_running = 1;
14783c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, sc->malo_imask);
14797cf545d0SJohn Baldwin 	callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
1480b032f27cSSam Leffler }
14813c7e78d3SWeongyo Jeong 
1482b032f27cSSam Leffler static void
malo_init(void * arg)1483b032f27cSSam Leffler malo_init(void *arg)
1484b032f27cSSam Leffler {
1485b032f27cSSam Leffler 	struct malo_softc *sc = (struct malo_softc *) arg;
14867a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
1487b032f27cSSam Leffler 
1488b032f27cSSam Leffler 	MALO_LOCK(sc);
1489b032f27cSSam Leffler 	malo_init_locked(sc);
14903c7e78d3SWeongyo Jeong 	MALO_UNLOCK(sc);
14913c7e78d3SWeongyo Jeong 
14927a79cebfSGleb Smirnoff 	if (sc->malo_running)
1493b032f27cSSam Leffler 		ieee80211_start_all(ic);	/* start all vap's */
14943c7e78d3SWeongyo Jeong }
14953c7e78d3SWeongyo Jeong 
14969602d600SGleb Smirnoff struct malo_copy_maddr_ctx {
14979602d600SGleb Smirnoff 	uint8_t macs[IEEE80211_ADDR_LEN * MALO_HAL_MCAST_MAX];
14989602d600SGleb Smirnoff 	int nmc;
14999602d600SGleb Smirnoff };
15009602d600SGleb Smirnoff 
15019602d600SGleb Smirnoff static u_int
malo_copy_maddr(void * arg,struct sockaddr_dl * sdl,u_int nmc)15029602d600SGleb Smirnoff malo_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int nmc)
15039602d600SGleb Smirnoff {
15049602d600SGleb Smirnoff 	struct malo_copy_maddr_ctx *ctx = arg;
15059602d600SGleb Smirnoff 
15069602d600SGleb Smirnoff 	if (ctx->nmc == MALO_HAL_MCAST_MAX)
15079602d600SGleb Smirnoff 		return (0);
15089602d600SGleb Smirnoff 
15099602d600SGleb Smirnoff 	IEEE80211_ADDR_COPY(ctx->macs + (ctx->nmc * IEEE80211_ADDR_LEN),
15109602d600SGleb Smirnoff 	    LLADDR(sdl));
15119602d600SGleb Smirnoff 	ctx->nmc++;
15129602d600SGleb Smirnoff 
15139602d600SGleb Smirnoff 	return (1);
15149602d600SGleb Smirnoff }
15159602d600SGleb Smirnoff 
15163c7e78d3SWeongyo Jeong /*
15173c7e78d3SWeongyo Jeong  * Set the multicast filter contents into the hardware.
15183c7e78d3SWeongyo Jeong  */
15193c7e78d3SWeongyo Jeong static void
malo_setmcastfilter(struct malo_softc * sc)15203c7e78d3SWeongyo Jeong malo_setmcastfilter(struct malo_softc *sc)
15213c7e78d3SWeongyo Jeong {
15229602d600SGleb Smirnoff 	struct malo_copy_maddr_ctx ctx;
15237a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
15247a79cebfSGleb Smirnoff 	struct ieee80211vap *vap;
15253c7e78d3SWeongyo Jeong 
15263c7e78d3SWeongyo Jeong 
15277a79cebfSGleb Smirnoff 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
15287a79cebfSGleb Smirnoff 	    ic->ic_promisc > 0)
15293c7e78d3SWeongyo Jeong 		goto all;
15303c7e78d3SWeongyo Jeong 
15319602d600SGleb Smirnoff 	ctx.nmc = 0;
15329602d600SGleb Smirnoff 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
15339602d600SGleb Smirnoff 		if_foreach_llmaddr(vap->iv_ifp, malo_copy_maddr, &ctx);
15347a79cebfSGleb Smirnoff 
15359602d600SGleb Smirnoff 	malo_hal_setmcast(sc->malo_mh, ctx.nmc, ctx.macs);
15363c7e78d3SWeongyo Jeong 
15373c7e78d3SWeongyo Jeong all:
15383c7e78d3SWeongyo Jeong 	/*
15393c7e78d3SWeongyo Jeong 	 * XXX we don't know how to set the f/w for supporting
15403c7e78d3SWeongyo Jeong 	 * IFF_ALLMULTI | IFF_PROMISC cases
15413c7e78d3SWeongyo Jeong 	 */
15423c7e78d3SWeongyo Jeong 	return;
15433c7e78d3SWeongyo Jeong }
15443c7e78d3SWeongyo Jeong 
15453c7e78d3SWeongyo Jeong static int
malo_mode_init(struct malo_softc * sc)15463c7e78d3SWeongyo Jeong malo_mode_init(struct malo_softc *sc)
15473c7e78d3SWeongyo Jeong {
15487a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
15493c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
15503c7e78d3SWeongyo Jeong 
15516459bd28SAndriy Voskoboinyk 	malo_hal_setpromisc(mh, ic->ic_promisc > 0);
15523c7e78d3SWeongyo Jeong 	malo_setmcastfilter(sc);
15533c7e78d3SWeongyo Jeong 
15543c7e78d3SWeongyo Jeong 	return ENXIO;
15553c7e78d3SWeongyo Jeong }
15563c7e78d3SWeongyo Jeong 
15573c7e78d3SWeongyo Jeong static void
malo_tx_draintxq(struct malo_softc * sc,struct malo_txq * txq)15583c7e78d3SWeongyo Jeong malo_tx_draintxq(struct malo_softc *sc, struct malo_txq *txq)
15593c7e78d3SWeongyo Jeong {
15603c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
15613c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
156221863401SDimitry Andric 	u_int ix __unused;
15633c7e78d3SWeongyo Jeong 
15643c7e78d3SWeongyo Jeong 	/*
15653c7e78d3SWeongyo Jeong 	 * NB: this assumes output has been stopped and
15663c7e78d3SWeongyo Jeong 	 *     we do not need to block malo_tx_tasklet
15673c7e78d3SWeongyo Jeong 	 */
15683c7e78d3SWeongyo Jeong 	for (ix = 0;; ix++) {
15693c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
15703c7e78d3SWeongyo Jeong 		bf = STAILQ_FIRST(&txq->active);
15713c7e78d3SWeongyo Jeong 		if (bf == NULL) {
15723c7e78d3SWeongyo Jeong 			MALO_TXQ_UNLOCK(txq);
15733c7e78d3SWeongyo Jeong 			break;
15743c7e78d3SWeongyo Jeong 		}
15753c7e78d3SWeongyo Jeong 		STAILQ_REMOVE_HEAD(&txq->active, bf_list);
15763c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
15773c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
15783c7e78d3SWeongyo Jeong 		if (sc->malo_debug & MALO_DEBUG_RESET) {
15797a79cebfSGleb Smirnoff 			struct ieee80211com *ic = &sc->malo_ic;
15803c7e78d3SWeongyo Jeong 			const struct malo_txrec *tr =
15813c7e78d3SWeongyo Jeong 			    mtod(bf->bf_m, const struct malo_txrec *);
15823c7e78d3SWeongyo Jeong 			malo_printtxbuf(bf, txq->qnum, ix);
1583b032f27cSSam Leffler 			ieee80211_dump_pkt(ic, (const uint8_t *)&tr->wh,
15843c7e78d3SWeongyo Jeong 			    bf->bf_m->m_len - sizeof(tr->fwlen), 0, -1);
15853c7e78d3SWeongyo Jeong 		}
15863c7e78d3SWeongyo Jeong #endif /* MALO_DEBUG */
15873c7e78d3SWeongyo Jeong 		bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
15883c7e78d3SWeongyo Jeong 		ni = bf->bf_node;
15893c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
15903c7e78d3SWeongyo Jeong 		if (ni != NULL) {
15913c7e78d3SWeongyo Jeong 			/*
15923c7e78d3SWeongyo Jeong 			 * Reclaim node reference.
15933c7e78d3SWeongyo Jeong 			 */
15943c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
15953c7e78d3SWeongyo Jeong 		}
15963c7e78d3SWeongyo Jeong 		m_freem(bf->bf_m);
15973c7e78d3SWeongyo Jeong 		bf->bf_m = NULL;
15983c7e78d3SWeongyo Jeong 
15993c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
16003c7e78d3SWeongyo Jeong 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
16013c7e78d3SWeongyo Jeong 		txq->nfree++;
16023c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
16033c7e78d3SWeongyo Jeong 	}
16043c7e78d3SWeongyo Jeong }
16053c7e78d3SWeongyo Jeong 
16063c7e78d3SWeongyo Jeong static void
malo_stop(struct malo_softc * sc)16077a79cebfSGleb Smirnoff malo_stop(struct malo_softc *sc)
16083c7e78d3SWeongyo Jeong {
16093c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
1610b032f27cSSam Leffler 	int i;
16113c7e78d3SWeongyo Jeong 
16127a79cebfSGleb Smirnoff 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid %u running %u\n",
16137a79cebfSGleb Smirnoff 	    __func__, sc->malo_invalid, sc->malo_running);
16143c7e78d3SWeongyo Jeong 
16153c7e78d3SWeongyo Jeong 	MALO_LOCK_ASSERT(sc);
16163c7e78d3SWeongyo Jeong 
16177a79cebfSGleb Smirnoff 	if (!sc->malo_running)
16183c7e78d3SWeongyo Jeong 		return;
16193c7e78d3SWeongyo Jeong 
16203c7e78d3SWeongyo Jeong 	/*
16213c7e78d3SWeongyo Jeong 	 * Shutdown the hardware and driver:
16223c7e78d3SWeongyo Jeong 	 *    disable interrupts
16233c7e78d3SWeongyo Jeong 	 *    turn off the radio
16243c7e78d3SWeongyo Jeong 	 *    drain and release tx queues
16253c7e78d3SWeongyo Jeong 	 *
16263c7e78d3SWeongyo Jeong 	 * Note that some of this work is not possible if the hardware
16273c7e78d3SWeongyo Jeong 	 * is gone (invalid).
16283c7e78d3SWeongyo Jeong 	 */
16297a79cebfSGleb Smirnoff 	sc->malo_running = 0;
16307cf545d0SJohn Baldwin 	callout_stop(&sc->malo_watchdog_timer);
16317cf545d0SJohn Baldwin 	sc->malo_timer = 0;
16327a79cebfSGleb Smirnoff 	/* disable interrupt.  */
16333c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, 0);
16343c7e78d3SWeongyo Jeong 	/* turn off the radio.  */
16353c7e78d3SWeongyo Jeong 	malo_hal_setradio(mh, 0, MHP_AUTO_PREAMBLE);
16363c7e78d3SWeongyo Jeong 
16373c7e78d3SWeongyo Jeong 	/* drain and release tx queues.  */
16383c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
16393c7e78d3SWeongyo Jeong 		malo_tx_draintxq(sc, &sc->malo_txq[i]);
16403c7e78d3SWeongyo Jeong }
16413c7e78d3SWeongyo Jeong 
16427a79cebfSGleb Smirnoff static void
malo_parent(struct ieee80211com * ic)16437a79cebfSGleb Smirnoff malo_parent(struct ieee80211com *ic)
16443c7e78d3SWeongyo Jeong {
16457a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
16467a79cebfSGleb Smirnoff 	int startall = 0;
16473c7e78d3SWeongyo Jeong 
16483c7e78d3SWeongyo Jeong 	MALO_LOCK(sc);
16497a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
16503c7e78d3SWeongyo Jeong 		/*
16513c7e78d3SWeongyo Jeong 		 * Beware of being called during attach/detach
16523c7e78d3SWeongyo Jeong 		 * to reset promiscuous mode.  In that case we
16533c7e78d3SWeongyo Jeong 		 * will still be marked UP but not RUNNING.
16543c7e78d3SWeongyo Jeong 		 * However trying to re-init the interface
16553c7e78d3SWeongyo Jeong 		 * is the wrong thing to do as we've already
16563c7e78d3SWeongyo Jeong 		 * torn down much of our state.  There's
16573c7e78d3SWeongyo Jeong 		 * probably a better way to deal with this.
16583c7e78d3SWeongyo Jeong 		 */
16597a79cebfSGleb Smirnoff 		if (!sc->malo_running && !sc->malo_invalid) {
16607a79cebfSGleb Smirnoff 			malo_init(sc);
1661b032f27cSSam Leffler 			startall = 1;
1662b032f27cSSam Leffler 		}
16637a79cebfSGleb Smirnoff 		/*
16647a79cebfSGleb Smirnoff 		 * To avoid rescanning another access point,
16657a79cebfSGleb Smirnoff 		 * do not call malo_init() here.  Instead,
16667a79cebfSGleb Smirnoff 		 * only reflect promisc mode settings.
16677a79cebfSGleb Smirnoff 		 */
16687a79cebfSGleb Smirnoff 		malo_mode_init(sc);
16697a79cebfSGleb Smirnoff 	} else if (sc->malo_running)
16707a79cebfSGleb Smirnoff 		malo_stop(sc);
16713c7e78d3SWeongyo Jeong 	MALO_UNLOCK(sc);
1672b032f27cSSam Leffler 	if (startall)
1673b032f27cSSam Leffler 		ieee80211_start_all(ic);
16743c7e78d3SWeongyo Jeong }
16753c7e78d3SWeongyo Jeong 
16763c7e78d3SWeongyo Jeong /*
16773c7e78d3SWeongyo Jeong  * Callback from the 802.11 layer to update the slot time
16783c7e78d3SWeongyo Jeong  * based on the current setting.  We use it to notify the
16793c7e78d3SWeongyo Jeong  * firmware of ERP changes and the f/w takes care of things
16803c7e78d3SWeongyo Jeong  * like slot time and preamble.
16813c7e78d3SWeongyo Jeong  */
16823c7e78d3SWeongyo Jeong static void
malo_updateslot(struct ieee80211com * ic)1683272f6adeSGleb Smirnoff malo_updateslot(struct ieee80211com *ic)
16843c7e78d3SWeongyo Jeong {
1685272f6adeSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
16863c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
16873c7e78d3SWeongyo Jeong 	int error;
16883c7e78d3SWeongyo Jeong 
16893c7e78d3SWeongyo Jeong 	/* NB: can be called early; suppress needless cmds */
16907a79cebfSGleb Smirnoff 	if (!sc->malo_running)
16913c7e78d3SWeongyo Jeong 		return;
16923c7e78d3SWeongyo Jeong 
16933c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RESET,
16943c7e78d3SWeongyo Jeong 	    "%s: chan %u MHz/flags 0x%x %s slot, (ic_flags 0x%x)\n",
16953c7e78d3SWeongyo Jeong 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
16963c7e78d3SWeongyo Jeong 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", ic->ic_flags);
16973c7e78d3SWeongyo Jeong 
16983c7e78d3SWeongyo Jeong 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
16993c7e78d3SWeongyo Jeong 		error = malo_hal_set_slot(mh, 1);
17003c7e78d3SWeongyo Jeong 	else
17013c7e78d3SWeongyo Jeong 		error = malo_hal_set_slot(mh, 0);
17023c7e78d3SWeongyo Jeong 
17033c7e78d3SWeongyo Jeong 	if (error != 0)
17043c7e78d3SWeongyo Jeong 		device_printf(sc->malo_dev, "setting %s slot failed\n",
17053c7e78d3SWeongyo Jeong 			ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long");
17063c7e78d3SWeongyo Jeong }
17073c7e78d3SWeongyo Jeong 
17083c7e78d3SWeongyo Jeong static int
malo_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1709b032f27cSSam Leffler malo_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17103c7e78d3SWeongyo Jeong {
1711b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17127a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
17133c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
17143c7e78d3SWeongyo Jeong 	int error;
17153c7e78d3SWeongyo Jeong 
17163c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1717b032f27cSSam Leffler 	    ieee80211_state_name[vap->iv_state],
17183c7e78d3SWeongyo Jeong 	    ieee80211_state_name[nstate]);
17193c7e78d3SWeongyo Jeong 
17203c7e78d3SWeongyo Jeong 	/*
1721b032f27cSSam Leffler 	 * Invoke the net80211 layer first so iv_bss is setup.
17223c7e78d3SWeongyo Jeong 	 */
1723b032f27cSSam Leffler 	error = MALO_VAP(vap)->malo_newstate(vap, nstate, arg);
1724b032f27cSSam Leffler 	if (error != 0)
1725b032f27cSSam Leffler 		return error;
1726b032f27cSSam Leffler 
1727b032f27cSSam Leffler 	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1728b032f27cSSam Leffler 		struct ieee80211_node *ni = vap->iv_bss;
1729b032f27cSSam Leffler 		enum ieee80211_phymode mode = ieee80211_chan2mode(ni->ni_chan);
1730b032f27cSSam Leffler 		const struct ieee80211_txparam *tp = &vap->iv_txparms[mode];
1731b032f27cSSam Leffler 
1732b032f27cSSam Leffler 		DPRINTF(sc, MALO_DEBUG_STATE,
1733b032f27cSSam Leffler 		    "%s: %s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
1734b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d associd 0x%x mode %d rate %d\n",
1735ec22a3a2SJustin Hibbits 		    if_name(vap->iv_ifp), __func__, vap->iv_flags,
1736b032f27cSSam Leffler 		    ni->ni_intval, ether_sprintf(ni->ni_bssid), ni->ni_capinfo,
1737b032f27cSSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan),
1738b032f27cSSam Leffler 		    ni->ni_associd, mode, tp->ucastrate);
1739b032f27cSSam Leffler 
17403c7e78d3SWeongyo Jeong 		malo_hal_setradio(mh, 1,
17413c7e78d3SWeongyo Jeong 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
17423c7e78d3SWeongyo Jeong 			MHP_SHORT_PREAMBLE : MHP_LONG_PREAMBLE);
1743b032f27cSSam Leffler 		malo_hal_setassocid(sc->malo_mh, ni->ni_bssid, ni->ni_associd);
1744b032f27cSSam Leffler 		malo_hal_set_rate(mh, mode,
1745b032f27cSSam Leffler 		   tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1746b032f27cSSam Leffler 		       0 : malo_fix2rate(tp->ucastrate));
17473c7e78d3SWeongyo Jeong 	}
1748b032f27cSSam Leffler 	return 0;
17493c7e78d3SWeongyo Jeong }
17503c7e78d3SWeongyo Jeong 
17513c7e78d3SWeongyo Jeong static int
malo_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)17523c7e78d3SWeongyo Jeong malo_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
17533c7e78d3SWeongyo Jeong 	const struct ieee80211_bpf_params *params)
17543c7e78d3SWeongyo Jeong {
17553c7e78d3SWeongyo Jeong 	struct ieee80211com *ic = ni->ni_ic;
17567a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
17573c7e78d3SWeongyo Jeong 	struct malo_txbuf *bf;
17583c7e78d3SWeongyo Jeong 	struct malo_txq *txq;
17593c7e78d3SWeongyo Jeong 
17607a79cebfSGleb Smirnoff 	if (!sc->malo_running || sc->malo_invalid) {
17613c7e78d3SWeongyo Jeong 		m_freem(m);
17623c7e78d3SWeongyo Jeong 		return ENETDOWN;
17633c7e78d3SWeongyo Jeong 	}
17643c7e78d3SWeongyo Jeong 
17653c7e78d3SWeongyo Jeong 	/*
17663c7e78d3SWeongyo Jeong 	 * Grab a TX buffer and associated resources.  Note that we depend
17673c7e78d3SWeongyo Jeong 	 * on the classification by the 802.11 layer to get to the right h/w
17683c7e78d3SWeongyo Jeong 	 * queue.  Management frames must ALWAYS go on queue 1 but we
17693c7e78d3SWeongyo Jeong 	 * cannot just force that here because we may receive non-mgt frames.
17703c7e78d3SWeongyo Jeong 	 */
17713c7e78d3SWeongyo Jeong 	txq = &sc->malo_txq[0];
17723c7e78d3SWeongyo Jeong 	bf = malo_getbuf(sc, txq);
17733c7e78d3SWeongyo Jeong 	if (bf == NULL) {
17743c7e78d3SWeongyo Jeong 		m_freem(m);
17753c7e78d3SWeongyo Jeong 		return ENOBUFS;
17763c7e78d3SWeongyo Jeong 	}
17773c7e78d3SWeongyo Jeong 
17783c7e78d3SWeongyo Jeong 	/*
17793c7e78d3SWeongyo Jeong 	 * Pass the frame to the h/w for transmission.
17803c7e78d3SWeongyo Jeong 	 */
17813c7e78d3SWeongyo Jeong 	if (malo_tx_start(sc, ni, bf, m) != 0) {
17823c7e78d3SWeongyo Jeong 		bf->bf_m = NULL;
17833c7e78d3SWeongyo Jeong 		bf->bf_node = NULL;
17843c7e78d3SWeongyo Jeong 		MALO_TXQ_LOCK(txq);
17853c7e78d3SWeongyo Jeong 		STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
17863c7e78d3SWeongyo Jeong 		txq->nfree++;
17873c7e78d3SWeongyo Jeong 		MALO_TXQ_UNLOCK(txq);
17883c7e78d3SWeongyo Jeong 
17893c7e78d3SWeongyo Jeong 		return EIO;		/* XXX */
17903c7e78d3SWeongyo Jeong 	}
17913c7e78d3SWeongyo Jeong 
17923c7e78d3SWeongyo Jeong 	/*
17933c7e78d3SWeongyo Jeong 	 * NB: We don't need to lock against tx done because this just
17943c7e78d3SWeongyo Jeong 	 * prods the firmware to check the transmit descriptors.  The firmware
17953c7e78d3SWeongyo Jeong 	 * will also start fetching descriptors by itself if it notices
17963c7e78d3SWeongyo Jeong 	 * new ones are present when it goes to deliver a tx done interrupt
17973c7e78d3SWeongyo Jeong 	 * to the host. So if we race with tx done processing it's ok.
17983c7e78d3SWeongyo Jeong 	 * Delivering the kick here rather than in malo_tx_start is
17993c7e78d3SWeongyo Jeong 	 * an optimization to avoid poking the firmware for each packet.
18003c7e78d3SWeongyo Jeong 	 *
18013c7e78d3SWeongyo Jeong 	 * NB: the queue id isn't used so 0 is ok.
18023c7e78d3SWeongyo Jeong 	 */
18033c7e78d3SWeongyo Jeong 	malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
18043c7e78d3SWeongyo Jeong 
18053c7e78d3SWeongyo Jeong 	return 0;
18063c7e78d3SWeongyo Jeong }
18073c7e78d3SWeongyo Jeong 
18083c7e78d3SWeongyo Jeong static void
malo_sysctlattach(struct malo_softc * sc)18093c7e78d3SWeongyo Jeong malo_sysctlattach(struct malo_softc *sc)
18103c7e78d3SWeongyo Jeong {
18113c7e78d3SWeongyo Jeong #ifdef	MALO_DEBUG
18123c7e78d3SWeongyo Jeong 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->malo_dev);
18133c7e78d3SWeongyo Jeong 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->malo_dev);
18143c7e78d3SWeongyo Jeong 
18153c7e78d3SWeongyo Jeong 	sc->malo_debug = malo_debug;
18163c7e78d3SWeongyo Jeong 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
18173c7e78d3SWeongyo Jeong 		"debug", CTLFLAG_RW, &sc->malo_debug, 0,
18183c7e78d3SWeongyo Jeong 		"control debugging printfs");
18193c7e78d3SWeongyo Jeong #endif
18203c7e78d3SWeongyo Jeong }
18213c7e78d3SWeongyo Jeong 
18223c7e78d3SWeongyo Jeong static void
malo_announce(struct malo_softc * sc)18233c7e78d3SWeongyo Jeong malo_announce(struct malo_softc *sc)
18243c7e78d3SWeongyo Jeong {
18253c7e78d3SWeongyo Jeong 
18267a79cebfSGleb Smirnoff 	device_printf(sc->malo_dev,
18277a79cebfSGleb Smirnoff 		"versions [hw %d fw %d.%d.%d.%d] (regioncode %d)\n",
18283c7e78d3SWeongyo Jeong 		sc->malo_hwspecs.hwversion,
18293c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 24) & 0xff,
18303c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 16) & 0xff,
18313c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 8) & 0xff,
18323c7e78d3SWeongyo Jeong 		(sc->malo_hwspecs.fw_releasenum >> 0) & 0xff,
18333c7e78d3SWeongyo Jeong 		sc->malo_hwspecs.regioncode);
18343c7e78d3SWeongyo Jeong 
18353c7e78d3SWeongyo Jeong 	if (bootverbose || malo_rxbuf != MALO_RXBUF)
18367a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
18377a79cebfSGleb Smirnoff 		    "using %u rx buffers\n", malo_rxbuf);
18383c7e78d3SWeongyo Jeong 	if (bootverbose || malo_txbuf != MALO_TXBUF)
18397a79cebfSGleb Smirnoff 		device_printf(sc->malo_dev,
18407a79cebfSGleb Smirnoff 		    "using %u tx buffers\n", malo_txbuf);
18413c7e78d3SWeongyo Jeong }
18423c7e78d3SWeongyo Jeong 
18433c7e78d3SWeongyo Jeong /*
18443c7e78d3SWeongyo Jeong  * Convert net80211 channel to a HAL channel.
18453c7e78d3SWeongyo Jeong  */
18463c7e78d3SWeongyo Jeong static void
malo_mapchan(struct malo_hal_channel * hc,const struct ieee80211_channel * chan)18473c7e78d3SWeongyo Jeong malo_mapchan(struct malo_hal_channel *hc, const struct ieee80211_channel *chan)
18483c7e78d3SWeongyo Jeong {
18493c7e78d3SWeongyo Jeong 	hc->channel = chan->ic_ieee;
18503c7e78d3SWeongyo Jeong 
18513c7e78d3SWeongyo Jeong 	*(uint32_t *)&hc->flags = 0;
18523c7e78d3SWeongyo Jeong 	if (IEEE80211_IS_CHAN_2GHZ(chan))
18533c7e78d3SWeongyo Jeong 		hc->flags.freqband = MALO_FREQ_BAND_2DOT4GHZ;
18543c7e78d3SWeongyo Jeong }
18553c7e78d3SWeongyo Jeong 
18563c7e78d3SWeongyo Jeong /*
18573c7e78d3SWeongyo Jeong  * Set/change channels.  If the channel is really being changed,
18583c7e78d3SWeongyo Jeong  * it's done by reseting the chip.  To accomplish this we must
18593c7e78d3SWeongyo Jeong  * first cleanup any pending DMA, then restart stuff after a la
18603c7e78d3SWeongyo Jeong  * malo_init.
18613c7e78d3SWeongyo Jeong  */
18623c7e78d3SWeongyo Jeong static int
malo_chan_set(struct malo_softc * sc,struct ieee80211_channel * chan)18633c7e78d3SWeongyo Jeong malo_chan_set(struct malo_softc *sc, struct ieee80211_channel *chan)
18643c7e78d3SWeongyo Jeong {
18653c7e78d3SWeongyo Jeong 	struct malo_hal *mh = sc->malo_mh;
18663c7e78d3SWeongyo Jeong 	struct malo_hal_channel hchan;
18673c7e78d3SWeongyo Jeong 
18683c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RESET, "%s: chan %u MHz/flags 0x%x\n",
18693c7e78d3SWeongyo Jeong 	    __func__, chan->ic_freq, chan->ic_flags);
18703c7e78d3SWeongyo Jeong 
18713c7e78d3SWeongyo Jeong 	/*
18723c7e78d3SWeongyo Jeong 	 * Convert to a HAL channel description with the flags constrained
18733c7e78d3SWeongyo Jeong 	 * to reflect the current operating mode.
18743c7e78d3SWeongyo Jeong 	 */
18753c7e78d3SWeongyo Jeong 	malo_mapchan(&hchan, chan);
18763c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, 0);		/* disable interrupts */
18773c7e78d3SWeongyo Jeong 	malo_hal_setchannel(mh, &hchan);
18783c7e78d3SWeongyo Jeong 	malo_hal_settxpower(mh, &hchan);
18793c7e78d3SWeongyo Jeong 
18803c7e78d3SWeongyo Jeong 	/*
18813c7e78d3SWeongyo Jeong 	 * Update internal state.
18823c7e78d3SWeongyo Jeong 	 */
18833c7e78d3SWeongyo Jeong 	sc->malo_tx_th.wt_chan_freq = htole16(chan->ic_freq);
18843c7e78d3SWeongyo Jeong 	sc->malo_rx_th.wr_chan_freq = htole16(chan->ic_freq);
18853c7e78d3SWeongyo Jeong 	if (IEEE80211_IS_CHAN_ANYG(chan)) {
18863c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_G);
18873c7e78d3SWeongyo Jeong 		sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_G);
18883c7e78d3SWeongyo Jeong 	} else {
18893c7e78d3SWeongyo Jeong 		sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_B);
18903c7e78d3SWeongyo Jeong 		sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_B);
18913c7e78d3SWeongyo Jeong 	}
18923c7e78d3SWeongyo Jeong 	sc->malo_curchan = hchan;
18933c7e78d3SWeongyo Jeong 	malo_hal_intrset(mh, sc->malo_imask);
18943c7e78d3SWeongyo Jeong 
18953c7e78d3SWeongyo Jeong 	return 0;
18963c7e78d3SWeongyo Jeong }
18973c7e78d3SWeongyo Jeong 
18983c7e78d3SWeongyo Jeong static void
malo_scan_start(struct ieee80211com * ic)18993c7e78d3SWeongyo Jeong malo_scan_start(struct ieee80211com *ic)
19003c7e78d3SWeongyo Jeong {
19017a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
19023c7e78d3SWeongyo Jeong 
19033c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
19043c7e78d3SWeongyo Jeong }
19053c7e78d3SWeongyo Jeong 
19063c7e78d3SWeongyo Jeong static void
malo_scan_end(struct ieee80211com * ic)19073c7e78d3SWeongyo Jeong malo_scan_end(struct ieee80211com *ic)
19083c7e78d3SWeongyo Jeong {
19097a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
19103c7e78d3SWeongyo Jeong 
19113c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
19123c7e78d3SWeongyo Jeong }
19133c7e78d3SWeongyo Jeong 
19143c7e78d3SWeongyo Jeong static void
malo_set_channel(struct ieee80211com * ic)19153c7e78d3SWeongyo Jeong malo_set_channel(struct ieee80211com *ic)
19163c7e78d3SWeongyo Jeong {
19177a79cebfSGleb Smirnoff 	struct malo_softc *sc = ic->ic_softc;
19183c7e78d3SWeongyo Jeong 
19193c7e78d3SWeongyo Jeong 	(void) malo_chan_set(sc, ic->ic_curchan);
19203c7e78d3SWeongyo Jeong }
19213c7e78d3SWeongyo Jeong 
19223c7e78d3SWeongyo Jeong static void
malo_rx_proc(void * arg,int npending)19233c7e78d3SWeongyo Jeong malo_rx_proc(void *arg, int npending)
19243c7e78d3SWeongyo Jeong {
19253c7e78d3SWeongyo Jeong 	struct malo_softc *sc = arg;
19267a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
1927b032f27cSSam Leffler 	struct malo_rxbuf *bf;
19283c7e78d3SWeongyo Jeong 	struct malo_rxdesc *ds;
19293c7e78d3SWeongyo Jeong 	struct mbuf *m, *mnew;
19303c7e78d3SWeongyo Jeong 	struct ieee80211_qosframe *wh;
19313c7e78d3SWeongyo Jeong 	struct ieee80211_node *ni;
19323c7e78d3SWeongyo Jeong 	int off, len, hdrlen, pktlen, rssi, ntodo;
19333c7e78d3SWeongyo Jeong 	uint8_t *data, status;
19343c7e78d3SWeongyo Jeong 	uint32_t readptr, writeptr;
19353c7e78d3SWeongyo Jeong 
19363c7e78d3SWeongyo Jeong 	DPRINTF(sc, MALO_DEBUG_RX_PROC,
19373c7e78d3SWeongyo Jeong 	    "%s: pending %u rdptr(0x%x) 0x%x wrptr(0x%x) 0x%x\n",
19383c7e78d3SWeongyo Jeong 	    __func__, npending,
19393c7e78d3SWeongyo Jeong 	    sc->malo_hwspecs.rxdesc_read,
19403c7e78d3SWeongyo Jeong 	    malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read),
19413c7e78d3SWeongyo Jeong 	    sc->malo_hwspecs.rxdesc_write,
19423c7e78d3SWeongyo Jeong 	    malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write));
19433c7e78d3SWeongyo Jeong 
19443c7e78d3SWeongyo Jeong 	readptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read);
19453c7e78d3SWeongyo Jeong 	writeptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write);
19463c7e78d3SWeongyo Jeong 	if (readptr == writeptr)
19473c7e78d3SWeongyo Jeong 		return;
19483c7e78d3SWeongyo Jeong 
19493c7e78d3SWeongyo Jeong 	bf = sc->malo_rxnext;
1950b032f27cSSam Leffler 	for (ntodo = malo_rxquota; ntodo > 0 && readptr != writeptr; ntodo--) {
19513c7e78d3SWeongyo Jeong 		if (bf == NULL) {
19523c7e78d3SWeongyo Jeong 			bf = STAILQ_FIRST(&sc->malo_rxbuf);
19533c7e78d3SWeongyo Jeong 			break;
19543c7e78d3SWeongyo Jeong 		}
19553c7e78d3SWeongyo Jeong 		ds = bf->bf_desc;
19563c7e78d3SWeongyo Jeong 		if (bf->bf_m == NULL) {
19573c7e78d3SWeongyo Jeong 			/*
19583c7e78d3SWeongyo Jeong 			 * If data allocation failed previously there
19593c7e78d3SWeongyo Jeong 			 * will be no buffer; try again to re-populate it.
19603c7e78d3SWeongyo Jeong 			 * Note the firmware will not advance to the next
19613c7e78d3SWeongyo Jeong 			 * descriptor with a dma buffer so we must mimic
19623c7e78d3SWeongyo Jeong 			 * this or we'll get out of sync.
19633c7e78d3SWeongyo Jeong 			 */
19643c7e78d3SWeongyo Jeong 			DPRINTF(sc, MALO_DEBUG_ANY,
19653c7e78d3SWeongyo Jeong 			    "%s: rx buf w/o dma memory\n", __func__);
19663c7e78d3SWeongyo Jeong 			(void)malo_rxbuf_init(sc, bf);
19673c7e78d3SWeongyo Jeong 			break;
19683c7e78d3SWeongyo Jeong 		}
19693c7e78d3SWeongyo Jeong 		MALO_RXDESC_SYNC(sc, ds,
19703c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
19713c7e78d3SWeongyo Jeong 		if (ds->rxcontrol != MALO_RXD_CTRL_DMA_OWN)
19723c7e78d3SWeongyo Jeong 			break;
19733c7e78d3SWeongyo Jeong 
19743c7e78d3SWeongyo Jeong 		readptr = le32toh(ds->physnext);
19753c7e78d3SWeongyo Jeong 
19763c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
19773c7e78d3SWeongyo Jeong 		if (sc->malo_debug & MALO_DEBUG_RECV_DESC)
19783c7e78d3SWeongyo Jeong 			malo_printrxbuf(bf, 0);
19793c7e78d3SWeongyo Jeong #endif
19803c7e78d3SWeongyo Jeong 		status = ds->status;
19813c7e78d3SWeongyo Jeong 		if (status & MALO_RXD_STATUS_DECRYPT_ERR_MASK) {
19827a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
19833c7e78d3SWeongyo Jeong 			goto rx_next;
19843c7e78d3SWeongyo Jeong 		}
19853c7e78d3SWeongyo Jeong 		/*
19863c7e78d3SWeongyo Jeong 		 * Sync the data buffer.
19873c7e78d3SWeongyo Jeong 		 */
19883c7e78d3SWeongyo Jeong 		len = le16toh(ds->pktlen);
19893c7e78d3SWeongyo Jeong 		bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
19903c7e78d3SWeongyo Jeong 		    BUS_DMASYNC_POSTREAD);
19913c7e78d3SWeongyo Jeong 		/*
19923c7e78d3SWeongyo Jeong 		 * The 802.11 header is provided all or in part at the front;
19933c7e78d3SWeongyo Jeong 		 * use it to calculate the true size of the header that we'll
19943c7e78d3SWeongyo Jeong 		 * construct below.  We use this to figure out where to copy
19953c7e78d3SWeongyo Jeong 		 * payload prior to constructing the header.
19963c7e78d3SWeongyo Jeong 		 */
19973c7e78d3SWeongyo Jeong 		m = bf->bf_m;
1998c2ede4b3SMartin Blapp 		data = mtod(m, uint8_t *);
19993c7e78d3SWeongyo Jeong 		hdrlen = ieee80211_anyhdrsize(data + sizeof(uint16_t));
20003c7e78d3SWeongyo Jeong 		off = sizeof(uint16_t) + sizeof(struct ieee80211_frame_addr4);
20013c7e78d3SWeongyo Jeong 
20023c7e78d3SWeongyo Jeong 		/*
20033c7e78d3SWeongyo Jeong 		 * Calculate RSSI. XXX wrong
20043c7e78d3SWeongyo Jeong 		 */
20053c7e78d3SWeongyo Jeong 		rssi = 2 * ((int) ds->snr - ds->nf);	/* NB: .5 dBm  */
20063c7e78d3SWeongyo Jeong 		if (rssi > 100)
20073c7e78d3SWeongyo Jeong 			rssi = 100;
20083c7e78d3SWeongyo Jeong 
20093c7e78d3SWeongyo Jeong 		pktlen = hdrlen + (len - off);
20103c7e78d3SWeongyo Jeong 		/*
20113c7e78d3SWeongyo Jeong 		 * NB: we know our frame is at least as large as
20123c7e78d3SWeongyo Jeong 		 * IEEE80211_MIN_LEN because there is a 4-address frame at
20133c7e78d3SWeongyo Jeong 		 * the front.  Hence there's no need to vet the packet length.
20143c7e78d3SWeongyo Jeong 		 * If the frame in fact is too small it should be discarded
20153c7e78d3SWeongyo Jeong 		 * at the net80211 layer.
20163c7e78d3SWeongyo Jeong 		 */
20173c7e78d3SWeongyo Jeong 
20183c7e78d3SWeongyo Jeong 		/* XXX don't need mbuf, just dma buffer */
20193c7e78d3SWeongyo Jeong 		mnew = malo_getrxmbuf(sc, bf);
20203c7e78d3SWeongyo Jeong 		if (mnew == NULL) {
20217a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
20223c7e78d3SWeongyo Jeong 			goto rx_next;
20233c7e78d3SWeongyo Jeong 		}
20243c7e78d3SWeongyo Jeong 		/*
20253c7e78d3SWeongyo Jeong 		 * Attach the dma buffer to the mbuf; malo_rxbuf_init will
20263c7e78d3SWeongyo Jeong 		 * re-setup the rx descriptor using the replacement dma
20273c7e78d3SWeongyo Jeong 		 * buffer we just installed above.
20283c7e78d3SWeongyo Jeong 		 */
20293c7e78d3SWeongyo Jeong 		bf->bf_m = mnew;
20303c7e78d3SWeongyo Jeong 		m->m_data += off - hdrlen;
20313c7e78d3SWeongyo Jeong 		m->m_pkthdr.len = m->m_len = pktlen;
20323c7e78d3SWeongyo Jeong 
20333c7e78d3SWeongyo Jeong 		/*
20343c7e78d3SWeongyo Jeong 		 * Piece 802.11 header together.
20353c7e78d3SWeongyo Jeong 		 */
20363c7e78d3SWeongyo Jeong 		wh = mtod(m, struct ieee80211_qosframe *);
20373c7e78d3SWeongyo Jeong 		/* NB: don't need to do this sometimes but ... */
20383c7e78d3SWeongyo Jeong 		/* XXX special case so we can memcpy after m_devget? */
20393c7e78d3SWeongyo Jeong 		ovbcopy(data + sizeof(uint16_t), wh, hdrlen);
2040f3f08e16SAndriy Voskoboinyk 		if (IEEE80211_QOS_HAS_SEQ(wh))
2041f3f08e16SAndriy Voskoboinyk 			*(uint16_t *)ieee80211_getqos(wh) = ds->qosctrl;
20425463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic)) {
20433c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_flags = 0;
20443c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_rate = ds->rate;
20453c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_antsignal = rssi;
20463c7e78d3SWeongyo Jeong 			sc->malo_rx_th.wr_antnoise = ds->nf;
20473c7e78d3SWeongyo Jeong 		}
20483c7e78d3SWeongyo Jeong #ifdef MALO_DEBUG
20493c7e78d3SWeongyo Jeong 		if (IFF_DUMPPKTS_RECV(sc, wh)) {
20503c7e78d3SWeongyo Jeong 			ieee80211_dump_pkt(ic, mtod(m, caddr_t),
20513c7e78d3SWeongyo Jeong 			    len, ds->rate, rssi);
20523c7e78d3SWeongyo Jeong 		}
20533c7e78d3SWeongyo Jeong #endif
20543c7e78d3SWeongyo Jeong 		/* dispatch */
20553c7e78d3SWeongyo Jeong 		ni = ieee80211_find_rxnode(ic,
2056b032f27cSSam Leffler 		    (struct ieee80211_frame_min *)wh);
2057b032f27cSSam Leffler 		if (ni != NULL) {
20585463c4a4SSam Leffler 			(void) ieee80211_input(ni, m, rssi, ds->nf);
20593c7e78d3SWeongyo Jeong 			ieee80211_free_node(ni);
2060b032f27cSSam Leffler 		} else
20615463c4a4SSam Leffler 			(void) ieee80211_input_all(ic, m, rssi, ds->nf);
20623c7e78d3SWeongyo Jeong rx_next:
20633c7e78d3SWeongyo Jeong 		/* NB: ignore ENOMEM so we process more descriptors */
20643c7e78d3SWeongyo Jeong 		(void) malo_rxbuf_init(sc, bf);
20653c7e78d3SWeongyo Jeong 		bf = STAILQ_NEXT(bf, bf_list);
20663c7e78d3SWeongyo Jeong 	}
20673c7e78d3SWeongyo Jeong 
20683c7e78d3SWeongyo Jeong 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read, readptr);
20693c7e78d3SWeongyo Jeong 	sc->malo_rxnext = bf;
20703c7e78d3SWeongyo Jeong 
20717a79cebfSGleb Smirnoff 	if (mbufq_first(&sc->malo_snd) != NULL)
20727a79cebfSGleb Smirnoff 		malo_start(sc);
20733c7e78d3SWeongyo Jeong }
20743c7e78d3SWeongyo Jeong 
20753c7e78d3SWeongyo Jeong /*
20763c7e78d3SWeongyo Jeong  * Reclaim all tx queue resources.
20773c7e78d3SWeongyo Jeong  */
20783c7e78d3SWeongyo Jeong static void
malo_tx_cleanup(struct malo_softc * sc)20793c7e78d3SWeongyo Jeong malo_tx_cleanup(struct malo_softc *sc)
20803c7e78d3SWeongyo Jeong {
20813c7e78d3SWeongyo Jeong 	int i;
20823c7e78d3SWeongyo Jeong 
20833c7e78d3SWeongyo Jeong 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
20843c7e78d3SWeongyo Jeong 		malo_tx_cleanupq(sc, &sc->malo_txq[i]);
20853c7e78d3SWeongyo Jeong }
20863c7e78d3SWeongyo Jeong 
20873c7e78d3SWeongyo Jeong int
malo_detach(struct malo_softc * sc)20883c7e78d3SWeongyo Jeong malo_detach(struct malo_softc *sc)
20893c7e78d3SWeongyo Jeong {
20907a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->malo_ic;
20913c7e78d3SWeongyo Jeong 
20927a79cebfSGleb Smirnoff 	malo_stop(sc);
20933c7e78d3SWeongyo Jeong 
20943c7e78d3SWeongyo Jeong 	if (sc->malo_tq != NULL) {
20953c7e78d3SWeongyo Jeong 		taskqueue_drain(sc->malo_tq, &sc->malo_rxtask);
20963c7e78d3SWeongyo Jeong 		taskqueue_drain(sc->malo_tq, &sc->malo_txtask);
20973c7e78d3SWeongyo Jeong 		taskqueue_free(sc->malo_tq);
20983c7e78d3SWeongyo Jeong 		sc->malo_tq = NULL;
20993c7e78d3SWeongyo Jeong 	}
21003c7e78d3SWeongyo Jeong 
21013c7e78d3SWeongyo Jeong 	/*
21023c7e78d3SWeongyo Jeong 	 * NB: the order of these is important:
21033c7e78d3SWeongyo Jeong 	 * o call the 802.11 layer before detaching the hal to
21043c7e78d3SWeongyo Jeong 	 *   insure callbacks into the driver to delete global
21053c7e78d3SWeongyo Jeong 	 *   key cache entries can be handled
21063c7e78d3SWeongyo Jeong 	 * o reclaim the tx queue data structures after calling
21073c7e78d3SWeongyo Jeong 	 *   the 802.11 layer as we'll get called back to reclaim
21083c7e78d3SWeongyo Jeong 	 *   node state and potentially want to use them
21093c7e78d3SWeongyo Jeong 	 * o to cleanup the tx queues the hal is called, so detach
21103c7e78d3SWeongyo Jeong 	 *   it last
21113c7e78d3SWeongyo Jeong 	 * Other than that, it's straightforward...
21123c7e78d3SWeongyo Jeong 	 */
2113b032f27cSSam Leffler 	ieee80211_ifdetach(ic);
21147cf545d0SJohn Baldwin 	callout_drain(&sc->malo_watchdog_timer);
21153c7e78d3SWeongyo Jeong 	malo_dma_cleanup(sc);
21163c7e78d3SWeongyo Jeong 	malo_tx_cleanup(sc);
21173c7e78d3SWeongyo Jeong 	malo_hal_detach(sc->malo_mh);
21187a79cebfSGleb Smirnoff 	mbufq_drain(&sc->malo_snd);
21193c7e78d3SWeongyo Jeong 	MALO_LOCK_DESTROY(sc);
21203c7e78d3SWeongyo Jeong 
21213c7e78d3SWeongyo Jeong 	return 0;
21223c7e78d3SWeongyo Jeong }
21233c7e78d3SWeongyo Jeong 
21243c7e78d3SWeongyo Jeong void
malo_shutdown(struct malo_softc * sc)21253c7e78d3SWeongyo Jeong malo_shutdown(struct malo_softc *sc)
21263c7e78d3SWeongyo Jeong {
21277a79cebfSGleb Smirnoff 
21287a79cebfSGleb Smirnoff 	malo_stop(sc);
21293c7e78d3SWeongyo Jeong }
21303c7e78d3SWeongyo Jeong 
21313c7e78d3SWeongyo Jeong void
malo_suspend(struct malo_softc * sc)21323c7e78d3SWeongyo Jeong malo_suspend(struct malo_softc *sc)
21333c7e78d3SWeongyo Jeong {
21343c7e78d3SWeongyo Jeong 
21357a79cebfSGleb Smirnoff 	malo_stop(sc);
21363c7e78d3SWeongyo Jeong }
21373c7e78d3SWeongyo Jeong 
21383c7e78d3SWeongyo Jeong void
malo_resume(struct malo_softc * sc)21393c7e78d3SWeongyo Jeong malo_resume(struct malo_softc *sc)
21403c7e78d3SWeongyo Jeong {
21413c7e78d3SWeongyo Jeong 
21427a79cebfSGleb Smirnoff 	if (sc->malo_ic.ic_nrunning > 0)
21433c7e78d3SWeongyo Jeong 		malo_init(sc);
21443c7e78d3SWeongyo Jeong }
2145